detail.vue 12 KB

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