goods_detail.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view v-if="goods_info.goods_name" class="goods-detail-container">
  3. <!-- 轮播图区域 -->
  4. <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
  5. <swiper-item v-for="(item, i) in goods_info.pics" :key="i">
  6. <!-- 把当前点击的图片的索引,传递到 preview() 处理函数中 -->
  7. <image :src="item.pics_big" @click="preview(i)"></image>
  8. </swiper-item>
  9. </swiper>
  10. <!-- 商品信息区域 -->
  11. <view class="goods-info-box">
  12. <!-- 商品价格 -->
  13. <view class="price">¥{{goods_info.goods_price}}</view>
  14. <!-- 信息主体区域 -->
  15. <view class="goods-info-body">
  16. <!-- 商品名称 -->
  17. <view class="goods-name">{{goods_info.goods_name}}</view>
  18. <!-- 收藏 -->
  19. <view class="favi">
  20. <uni-icons type="star" size="18" color="gray"></uni-icons>
  21. <text>收藏</text>
  22. </view>
  23. </view>
  24. <!-- 运费 -->
  25. <view class="yf">快递:免运费</view>
  26. </view>
  27. <!-- 商品详情信息 -->
  28. <rich-text :nodes="goods_info.goods_introduce"></rich-text>
  29. <!-- 商品导航组件 -->
  30. <view class="goods_nav">
  31. <!-- fill 控制右侧按钮的样式 -->
  32. <!-- options 左侧按钮的配置项 -->
  33. <!-- buttonGroup 右侧按钮的配置项 -->
  34. <!-- click 左侧按钮的点击事件处理函数 -->
  35. <!-- buttonClick 右侧按钮的点击事件处理函数 -->
  36. <uni-goods-nav :fill="true" :options="options"
  37. :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. data() {
  44. return {
  45. // 商品详情对象
  46. goods_info: {},
  47. // 左侧按钮组的配置对象
  48. options: [{
  49. icon: 'shop',
  50. text: '店铺'
  51. }, {
  52. icon: 'cart',
  53. text: '购物车',
  54. info: 2
  55. }],
  56. // 右侧按钮组的配置对象
  57. buttonGroup: [{
  58. text: '加入购物车',
  59. backgroundColor: '#ff0000',
  60. color: '#fff'
  61. },
  62. {
  63. text: '立即购买',
  64. backgroundColor: '#ffa200',
  65. color: '#fff'
  66. }]
  67. };
  68. },
  69. onLoad(options) {
  70. // 获取商品 Id
  71. const goods_id = options.goods_id
  72. // 调用请求商品详情数据的方法
  73. this.getGoodsDetail(goods_id)
  74. },
  75. methods: {
  76. // 定义请求商品详情数据的方法
  77. async getGoodsDetail(goods_id) {
  78. const { data: res } = await
  79. uni.$http.get('/api/public/v1/goods/detail', { goods_id })
  80. if (res.meta.status !== 200) return uni.$showMsg()
  81. // 使用字符串的 replace() 方法,为 img 标签添加行内的 style 样式,从而解决图片底部
  82. 空白间隙的问题
  83. res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
  84. // 为 data 中的数据赋值
  85. this.goods_info = res.message
  86. },
  87. // 实现轮播图的预览效果
  88. preview(i) {
  89. // 调用 uni.previewImage() 方法预览图片
  90. uni.previewImage({
  91. // 预览时,默认显示图片的索引
  92. current: i,
  93. // 所有图片 url 地址的数组
  94. urls: this.goods_info.pics.map(x => x.pics_big)
  95. })
  96. },
  97. // 左侧按钮的点击事件处理函数
  98. onClick(e) {
  99. if (e.content.text === '购物车') {
  100. // 切换到购物车页面
  101. uni.switchTab({
  102. url: '/pages/cart/cart'
  103. })
  104. }
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss">
  110. swiper {
  111. height: 750rpx;
  112. image {
  113. width: 100%;
  114. height: 100%;
  115. }
  116. }
  117. // 商品信息区域的样式
  118. .goods-info-box {
  119. padding: 10px;
  120. padding-right: 0;
  121. .price {
  122. color: #c00000;
  123. font-size: 18px;
  124. margin: 10px 0;
  125. }
  126. .goods-info-body {
  127. display: flex;
  128. justify-content: space-between;
  129. .goods-name {
  130. font-size: 13px;
  131. padding-right: 10px;
  132. }
  133. // 收藏区域
  134. .favi {
  135. width: 120px;
  136. font-size: 12px;
  137. display: flex;
  138. flex-direction: column;
  139. justify-content: center;
  140. align-items: center;
  141. border-left: 1px solid #efefef;
  142. color: gray;
  143. }
  144. }
  145. // 运费
  146. .yf {
  147. margin: 10px 0;
  148. font-size: 12px;
  149. color: gray;
  150. }
  151. }
  152. .goods-detail-container {
  153. // 给页面外层的容器,添加 50px 的内padding,
  154. // 防止页面内容被底部的商品导航组件遮盖
  155. padding-bottom: 50px;
  156. }
  157. .goods_nav {
  158. // 为商品导航组件添加固定定位
  159. position: fixed;
  160. bottom: 0;
  161. left: 0;
  162. width: 100%;
  163. }
  164. </style>