detail.vue 12 KB

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