detail.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <view>
  3. <u-navbar title="兑换详情" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
  4. <view class="detail">
  5. <view class="detail-top">
  6. <u-swiper :list="picUrlArr" height="348" radius="0" :indicator="true" :circular="true"></u-swiper>
  7. </view>
  8. <view class="detail-title">
  9. <view class="txt">{{ info.title }}</view>
  10. <view class="coin">
  11. <image src="../../static/icon/bean.png" mode=""></image>
  12. <view>x {{ info.exchangePrice }}</view>
  13. </view>
  14. </view>
  15. <view class="detail-goods">商品详情</view>
  16. <view class="detail-description">
  17. <view class="" v-html="description"></view>
  18. </view>
  19. </view>
  20. <view class="footer-fixed">
  21. <view class="flex btn">
  22. <button type="default" @click="exChange">立即兑换</button>
  23. </view>
  24. </view>
  25. <!-- 兑换选择 -->
  26. <u-popup :show="choiceShow" mode="bottom" @close="close" :closeable="true">
  27. <view class="choiceShow-wrap">
  28. <view class="flex goods">
  29. <view class="flex image-wrap">
  30. <image :src="picUrlArr[0]" mode="aspectFill"></image>
  31. </view>
  32. <view class="info">
  33. <view class="info-title">{{ info.title }}</view>
  34. <view class="info-coin">
  35. <image src="../../static/icon/bean.png" mode=""></image>
  36. <view>x {{ payInfo.exValue }}</view>
  37. </view>
  38. <view class="info-stock">库存:{{ payInfo.quantity }}</view>
  39. </view>
  40. </view>
  41. <view class="sku" v-for="(item, index) in skuList" :key="index">
  42. <view class="sku-title">{{ item.name }}</view>
  43. <view class="flex sku-list">
  44. <view
  45. :class="{'action': item.actionIndex == indexs, 'sku-list-item': item.actionIndex != indexs}"
  46. v-for="(ele, indexs) in item.value" :key="indexs" @click="getSku(ele, item, indexs)">
  47. {{ ele }}
  48. </view>
  49. </view>
  50. </view>
  51. <view class="flex quantity">
  52. <view class="quantity-title">兑换数量</view>
  53. <view class="quantity-title">
  54. <u-number-box v-model="orderNum" :min="1" :disabledInput="true"
  55. @change="valChange($event, payInfo)"></u-number-box>
  56. </view>
  57. </view>
  58. <view class="flex btn">
  59. <view class="flex btn-left">
  60. <view class="title">应付:</view>
  61. <view class="flex coin">
  62. <image src="../../static/icon/bean.png" mode=""></image>
  63. <view>x {{ payInfo.value }}</view>
  64. </view>
  65. </view>
  66. <view class="btn-right">
  67. <view class="confirm" @click="confirmPrize">立即兑换</view>
  68. </view>
  69. </view>
  70. </view>
  71. </u-popup>
  72. <u-popup :show="tipShow" mode="center">
  73. <view class="tip-show">
  74. <view class="flex tip-show-title">
  75. <u-icon name="checkmark-circle" color="#EB7009" size="24"></u-icon>
  76. <text>兑换成功,已放入奖品库</text>
  77. </view>
  78. <view class="flex tip-show-btn">
  79. <view class="close" @click="tipShow = false">关闭</view>
  80. <navigator class="prize" :url="`/pages/prize/index`" hover-class="navigator-hover" @click="tipShow = false">前往查看</navigator>
  81. </view>
  82. </view>
  83. </u-popup>
  84. </view>
  85. </template>
  86. <script>
  87. import env from '../../config/env.js'
  88. import $http from '@/utils/request.js'
  89. export default {
  90. data() {
  91. return {
  92. goodsId: '',
  93. boxId: '',
  94. picUrlArr: [],
  95. info: {},
  96. prizeList: [],
  97. description: '',
  98. choiceShow: false,
  99. initData: {},
  100. orderNum: 1,
  101. skuList: [],
  102. skuListInit: [],
  103. payInfo: {},
  104. tipShow: false
  105. };
  106. },
  107. onLoad(opthios) {
  108. this.goodsId = opthios.id
  109. },
  110. onShow(){
  111. this.getDetail()
  112. this.getBean()
  113. },
  114. methods: {
  115. getDetail() {
  116. uni.showLoading({
  117. title: '加载中'
  118. });
  119. $http.post('/api/v1/mp/user/exchange/goods/detail', {
  120. goodsId: this.goodsId,
  121. noToken: true
  122. }).then(res => {
  123. uni.hideLoading();
  124. if (res.code == 0) {
  125. this.info = res.data
  126. let picUrlArr = res.data.picUrl.split(',')
  127. picUrlArr.forEach(item => {
  128. this.picUrlArr.push(env.filePublic + item)
  129. })
  130. // 处理富文本
  131. this.description = this.formatRichText(res.data.description);
  132. this.skuListInit = res.data.skuList
  133. if (res.data.skuList.length) {
  134. let skuProp = JSON.parse(res.data.skuProp)
  135. skuProp.forEach(item => {
  136. item.actionIndex = 0,
  137. item.txt = `${item.name}:${item.value[0]}`
  138. })
  139. let actionSku = skuProp.map(item => {
  140. return item.txt
  141. }).join(';')
  142. let sku = res.data.skuList.find(item => {
  143. return item.properties == actionSku
  144. })
  145. this.payInfo = {
  146. ...sku,
  147. exValue: sku.value,
  148. picUrl: env.filePublic + sku.picUrl
  149. }
  150. this.skuList = skuProp
  151. } else {
  152. this.payInfo = {
  153. ...res.data,
  154. exValue: this.info.value,
  155. picUrl: this.picUrlArr[0]
  156. }
  157. }
  158. }
  159. }).catch(() => {
  160. uni.hideLoading();
  161. })
  162. },
  163. getBean() {
  164. uni.showLoading({
  165. title: '加载中'
  166. });
  167. $http.post('/api/v1/mp/user/mine/init', {}).then(res => {
  168. uni.hideLoading();
  169. if (res.code == 0) {
  170. this.initData = res.data
  171. }
  172. }).catch(() => {
  173. uni.hideLoading();
  174. })
  175. },
  176. exChange() {
  177. this.choiceShow = true
  178. },
  179. close() {
  180. this.choiceShow = false
  181. },
  182. valChange(e, item) {
  183. let value = e.value
  184. this.$set(item, 'value', value * item.exValue)
  185. console.log(value, item);
  186. },
  187. getSku(e, item, indexs) {
  188. this.$set(item, 'txt', `${item.name}:${e}`)
  189. this.$set(item, 'actionIndex', indexs)
  190. this.orderNum = 1
  191. let actionSku = this.skuList.map(item => {
  192. return item.txt
  193. }).join(';')
  194. let sku = this.skuListInit.find(item => {
  195. return item.properties == actionSku
  196. })
  197. this.payInfo = {
  198. ...sku,
  199. exValue: sku.value,
  200. picUrl: env.filePublic + sku.picUrl
  201. }
  202. },
  203. confirmPrize() {
  204. uni.showLoading({
  205. title: '兑换中'
  206. });
  207. let data = {
  208. goodsId: this.payInfo.goodsId,
  209. skuId: this.payInfo.skuId,
  210. orderNum: this.orderNum,
  211. }
  212. console.log(data);
  213. if (this.initData.coinNum < this.payInfo.value) {
  214. uni.$u.toast('盲豆余额不足');
  215. return
  216. }
  217. $http.post('/api/v1/mp/user/exchange/submit', data).then(res=>{
  218. uni.hideLoading();
  219. if(res.code == 0){
  220. this.choiceShow = false
  221. this.tipShow = true
  222. }
  223. }).catch(() => {
  224. uni.hideLoading();
  225. })
  226. },
  227. /**
  228. * 处理富文本里的图片宽度自适应
  229. * 1.去掉img标签里的style、width、height属性
  230. * 2.img标签添加style属性:max-width:100%;height:auto
  231. * 3.修改所有style里的width属性为max-width:100%
  232. * 4.去掉<br/>标签
  233. * @param html
  234. * @returns {void|string|*}
  235. */
  236. formatRichText(html) { //控制小程序中图片大小
  237. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  238. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  239. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  240. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  241. return match;
  242. });
  243. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  244. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  245. 'max-width:100%;');
  246. return match;
  247. });
  248. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  249. newContent = newContent.replace(/\<img/gi,
  250. '<img style="max-width:100%;height:auto;margin:10rpx auto;"');
  251. return newContent;
  252. },
  253. }
  254. }
  255. </script>
  256. <style lang="scss" scoped>
  257. </style>
  258. <style lang="scss" scoped>
  259. .detail {
  260. padding-bottom: 100rpx;
  261. &-top {
  262. height: 696rpx;
  263. background-color: #FFFFFF;
  264. }
  265. &-title {
  266. padding: 24rpx;
  267. background-color: #FFFFFF;
  268. margin-bottom: 10rpx;
  269. .txt {
  270. font-size: 32rpx;
  271. line-height: 44rpx;
  272. overflow: hidden;
  273. text-overflow: ellipsis;
  274. display: -webkit-box;
  275. -webkit-box-orient: vertical;
  276. -webkit-line-clamp: 2;
  277. margin-bottom: 24rpx;
  278. font-weight: bold;
  279. }
  280. .coin {
  281. display: flex;
  282. align-items: center;
  283. font-size: 32rpx;
  284. line-height: 44rpx;
  285. color: rgba(235, 112, 9, 100);
  286. }
  287. image {
  288. width: 42rpx;
  289. height: 42rpx;
  290. margin-right: 20rpx;
  291. }
  292. }
  293. &-goods {
  294. height: 88rpx;
  295. text-align: center;
  296. line-height: 88rpx;
  297. font-weight: bold;
  298. background-color: #FFFFFF;
  299. }
  300. &-description {
  301. image {
  302. width: 100%;
  303. }
  304. }
  305. }
  306. .choiceShow-wrap {
  307. min-height: 400rpx;
  308. padding: 80rpx 20rpx 60rpx;
  309. .goods {
  310. justify-content: space-between;
  311. margin-bottom: 20rpx;
  312. .image-wrap {
  313. width: 220rpx;
  314. height: 220rpx;
  315. border: 1px solid rgba(236, 236, 236, 100);
  316. border-radius: 10rpx;
  317. image {
  318. width: 174rpx;
  319. height: 174rpx;
  320. }
  321. }
  322. .info {
  323. flex: 1;
  324. display: flex;
  325. flex-direction: column;
  326. justify-content: space-between;
  327. padding-left: 26rpx;
  328. height: 220rpx;
  329. &-title {
  330. font-size: 32rpx;
  331. line-height: 44rpx;
  332. font-weight: bold;
  333. }
  334. &-coin {
  335. display: flex;
  336. align-items: center;
  337. font-size: 32rpx;
  338. line-height: 44rpx;
  339. color: rgba(235, 112, 9, 100);
  340. font-weight: bold;
  341. image {
  342. width: 42rpx;
  343. height: 42rpx;
  344. margin-right: 20rpx;
  345. }
  346. }
  347. &-stock {
  348. line-height: 44rpx;
  349. }
  350. }
  351. }
  352. .sku {
  353. margin-bottom: 30rpx;
  354. &-title {
  355. line-height: 42rpx;
  356. }
  357. &-list {
  358. justify-content: flex-start;
  359. &-item {
  360. line-height: 44rpx;
  361. padding: 0 20rpx;
  362. border: 1px solid rgba(187, 187, 187, 100);
  363. margin-left: 36rpx;
  364. }
  365. .action {
  366. line-height: 44rpx;
  367. padding: 0 20rpx;
  368. margin-left: 36rpx;
  369. border: 1px solid $uni-bg-color;
  370. }
  371. // &-item:first-child {
  372. // margin-left: 0;
  373. // }
  374. }
  375. }
  376. .quantity {
  377. justify-content: space-between;
  378. margin-bottom: 40rpx;
  379. }
  380. .btn {
  381. justify-content: space-between;
  382. &-left {
  383. .coin {
  384. display: flex;
  385. align-items: center;
  386. font-size: 32rpx;
  387. line-height: 44rpx;
  388. color: rgba(235, 112, 9, 100);
  389. font-weight: bold;
  390. margin-left: 20rpx;
  391. image {
  392. width: 42rpx;
  393. height: 42rpx;
  394. margin-right: 20rpx;
  395. }
  396. }
  397. }
  398. &-right {
  399. .confirm {
  400. width: 250rpx;
  401. height: 60rpx;
  402. line-height: 60rpx;
  403. border-radius: 8px;
  404. background-color: rgba(235, 112, 9, 100);
  405. color: rgba(255, 255, 255, 100);
  406. font-size: 28rpx;
  407. text-align: center;
  408. }
  409. }
  410. }
  411. }
  412. .tip-show{
  413. width: 80vw;
  414. background-color: #FFFFFF;
  415. border-radius: 10rpx;
  416. padding: 60rpx 40rpx;
  417. &-title{
  418. margin-bottom: 64rpx;
  419. text{
  420. margin-left: 20rpx;
  421. }
  422. }
  423. &-btn{
  424. justify-content: space-around;
  425. .close{
  426. width: 160rpx;
  427. height: 60rpx;
  428. line-height: 60rpx;
  429. border-radius: 8rpx;
  430. color: rgba(235, 112, 9, 100);
  431. font-size: 28rpx;
  432. text-align: center;
  433. border: 1px solid rgba(235, 112, 9, 100);
  434. }
  435. .prize{
  436. width: 160rpx;
  437. height: 60rpx;
  438. line-height: 60rpx;
  439. border-radius: 8rpx;
  440. color: rgba(235, 112, 9, 100);
  441. font-size: 28rpx;
  442. text-align: center;
  443. background-color: rgba(235, 112, 9, 100);
  444. color: rgba(255, 255, 255, 100);
  445. font-size: 14px;
  446. }
  447. }
  448. }
  449. .footer-fixed {
  450. position: fixed;
  451. bottom: var(--window-bottom);
  452. left: 0;
  453. right: 0;
  454. z-index: 11;
  455. box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
  456. background: #fff;
  457. // 设置ios刘海屏底部横线安全区域
  458. padding-bottom: constant(safe-area-inset-bottom);
  459. padding-bottom: env(safe-area-inset-bottom);
  460. .btn {
  461. padding: 20rpx 40rpx;
  462. /deep/ button {
  463. width: 100%;
  464. line-height: 60rpx;
  465. font-size: 28rpx;
  466. height: 60rpx;
  467. color: #fff;
  468. background-color: $uni-bg-color;
  469. border: none;
  470. border-radius: 100rpx;
  471. }
  472. }
  473. }
  474. </style>