detail.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view>
  3. <u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="奖品详情"></u-navbar>
  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-info">
  9. <view class="content" v-text="info.title"></view>
  10. </view>
  11. <view class="detail-title">商品详情</view>
  12. <view class="detail-description">
  13. <view class="" v-html="description"></view>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import env from '../../config/env.js'
  20. import $http from '@/utils/request.js'
  21. export default {
  22. data() {
  23. return {
  24. boxId: '',
  25. picUrlArr: [],
  26. info: {},
  27. prizeList: [],
  28. description:'',
  29. payShow: false,
  30. payInfo: {}
  31. };
  32. },
  33. onLoad(opthios) {
  34. this.getDetail(opthios.id)
  35. },
  36. methods: {
  37. getDetail(id) {
  38. uni.showLoading({
  39. title: '加载中'
  40. });
  41. $http.post('/api/v1/mp/user/exchange/goods/detail', {
  42. noToken: true,
  43. goodsId: id
  44. }).then(res => {
  45. uni.hideLoading();
  46. if (res.code == 0) {
  47. this.info = res.data
  48. let picUrlArr = res.data.picUrl.split(',')
  49. picUrlArr.forEach(item => {
  50. this.picUrlArr.push(env.filePublic + item + '_s')
  51. })
  52. // 处理富文本
  53. this.description = this.formatRichText(res.data.description);
  54. }
  55. }).catch(() => {
  56. uni.hideLoading();
  57. })
  58. },
  59. close() {
  60. this.payShow = false
  61. },
  62. exchange() {
  63. let data = {
  64. couponIds: [],
  65. autoCoupon: 1,
  66. boxId: this.info.boxId,
  67. ticketId: this.info.ticketId,
  68. orderNum: 1
  69. }
  70. $http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
  71. if (res.code == 0) {
  72. let info = {
  73. ...res.data,
  74. ...this.info,
  75. picUrl: env.filePublic + res.data.picUrl,
  76. }
  77. this.payInfo = info
  78. this.payShow = true
  79. }
  80. })
  81. },
  82. /**
  83. * 处理富文本里的图片宽度自适应
  84. * 1.去掉img标签里的style、width、height属性
  85. * 2.img标签添加style属性:max-width:100%;height:auto
  86. * 3.修改所有style里的width属性为max-width:100%
  87. * 4.去掉<br/>标签
  88. * @param html
  89. * @returns {void|string|*}
  90. */
  91. formatRichText(html) { //控制小程序中图片大小
  92. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  93. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  94. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  95. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  96. return match;
  97. });
  98. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  99. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  100. 'max-width:100%;');
  101. return match;
  102. });
  103. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  104. newContent = newContent.replace(/\<img/gi,
  105. '<img style="max-width:100%;height:auto;margin:10rpx auto;"');
  106. return newContent;
  107. },
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .detail {
  113. &-info{
  114. padding: 50rpx 20rpx 50rpx;
  115. margin-bottom: 10rpx;
  116. background-color: #fff;
  117. .content{
  118. font-size: 32rpx;
  119. font-weight: bold;
  120. }
  121. }
  122. &-title{
  123. height: 88rpx;
  124. text-align: center;
  125. line-height: 88rpx;
  126. font-weight: bold;
  127. background-color: #FFFFFF;
  128. }
  129. &-description {
  130. image {
  131. width: 100%;
  132. }
  133. }
  134. }
  135. </style>