detail.vue 13 KB

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