detail.vue 12 KB

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