detail.vue 12 KB

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