AccessoryItem.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!-- with.vue -->
  2. <template>
  3. <view class="div ui-recommend-wrapper" v-if="bundling_array">
  4. <view class="div wrapper-swipe">
  5. <swiper
  6. v-bind:style="getWrapperStyle"
  7. :autoplay="false"
  8. @change="handleChange"
  9. :current="currentIndex"
  10. :indicator-dots="false"
  11. >
  12. <swiper-item v-for="(item, index) in bundling_array" :key="index">
  13. <view class="div image-swipe-wrapper">
  14. <view class="div"
  15. v-for="(image, j) in b_goods_array[index]"
  16. :key="j"
  17. :style="index%3==0?'margin-right:0':''"
  18. @click="goDetail(image.id)"
  19. >
  20. <image mode="aspectFit" class="img"
  21. :src="image.image"
  22. v-bind:style="getItemStyle"
  23. />
  24. <text class="span">¥{{ image.price }}</text>
  25. </view>
  26. </view>
  27. <view class="div ui-recommend-all" v-on:click="goRecommend(index)">购买组合</view>
  28. </swiper-item>
  29. </swiper>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import { urlencode } from '@/util/common'
  35. import { productAccessoryList } from '../../../../api/homegoodsdetail'
  36. import { cartAdd } from '../../../../api/homecart'
  37. import { mapState, mapMutations } from 'vuex'
  38. export default {
  39. data () {
  40. return {
  41. bundling_array: null,
  42. b_goods_array: null,
  43. indicatorArray: [],
  44. currentIndex: 0
  45. }
  46. },
  47. created () {
  48. this.getRecommendList()
  49. },
  50. computed: {
  51. ...mapState({
  52. currentProductId: state => state.goodsdetail.currentProductId
  53. }),
  54. getWrapperStyle: function () {
  55. const res = uni.getSystemInfoSync()
  56. var width = res.windowWidth
  57. var height = res.windowHeight
  58. let itemWidth = width - 24
  59. let itemHeight = (width - 48) / 3 +80
  60. return "width:"+itemWidth +"px;height:"+itemHeight +"px"
  61. },
  62. getItemStyle: function () {
  63. const res = uni.getSystemInfoSync()
  64. var width = res.windowWidth
  65. var height = res.windowHeight
  66. let itemWidth = (width - 48) / 3
  67. let itemHeight = itemWidth
  68. return "width:"+itemWidth +"px;height:"+itemHeight +"px"
  69. }
  70. // CODE REVIEW: 代码格式
  71. },
  72. methods: {
  73. /*
  74. getRecommendList: 获取推荐商品
  75. */
  76. getRecommendList () {
  77. let params = {
  78. product: this.currentProductId ? this.currentProductId : '',
  79. page: 1,
  80. per_page: 10
  81. }
  82. productAccessoryList(params.product).then(
  83. res => {
  84. if (res) {
  85. this.bundling_array = res.result.bundling_array
  86. this.b_goods_array = res.result.b_goods_array
  87. }
  88. }
  89. )
  90. },
  91. /*
  92. buildList:构建促销展示商品的数据
  93. @params: res 接口数据返回的促销商品
  94. */
  95. buildList (res) {
  96. let index = Math.ceil(res.length / 3)
  97. let newArray = []
  98. if (index) {
  99. for (let i = 0; i <= index - 1; i++) {
  100. let subArray = []
  101. subArray.push(res.slice(i * 3, i * 3 + 3))
  102. newArray.push(subArray)
  103. }
  104. }
  105. return newArray
  106. },
  107. /*
  108. buildSwipeIndicators: 根据轮播图的长度计算位于底部的按钮的个数
  109. */
  110. buildSwipeIndicators () {
  111. let photos = this.list
  112. for (let i = 0, len = photos.length - 1; i <= len; i++) {
  113. photos[i].index = i
  114. this.indicatorArray.push(photos[i])
  115. }
  116. },
  117. /*
  118. handleChange: 查看大图的时候滑动大图设置位于底部的按钮的选中状态同时隐藏查看大图的头部信息
  119. @params: index 当前滑动的图片的index
  120. */
  121. handleChange (e) {
  122. this.currentIndex = e.detail.current
  123. },
  124. /*
  125. goRecommend: 跳转到相关商品页面
  126. */
  127. goRecommend (id) {
  128. cartAdd(0, 0, id).then(
  129. res => {
  130. uni.navigateTo({ url: '/pages/member/cart/Cart' })
  131. },
  132. error => {
  133. uni.showToast({icon:'none',title: error.message})
  134. }
  135. )
  136. },
  137. goDetail (id) {
  138. let data = Object.assign({}, { id: id })
  139. uni.navigateTo({ url: '/pages/home/goodsdetail/Goodsdetail'+'?'+urlencode( { goods_id: id } )})
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. .ui-recommend-wrapper {
  146. background: #ffffff;
  147. margin-bottom:$pageSpace;
  148. .wrapper-swipe {
  149. padding:0.75rem $pageSpace;
  150. padding-right:0;
  151. .mint-swipe {
  152. height:5rem;
  153. .mint-swipe-items-wrap {
  154. .mint-swipe-item {
  155. }
  156. }
  157. }
  158. }
  159. .swiper-indicators {
  160. position: relative;
  161. margin-top:1rem;
  162. bottom: 0;
  163. }
  164. .ui-recommend-all {
  165. height:1.5rem;
  166. line-height:1.5rem;
  167. text-align: center;
  168. background: #ffffff;
  169. font-size:$subFontSize;
  170. color:$primaryColor;
  171. border:1px solid $primaryColor;
  172. border-radius:1.5rem;
  173. width: 6rem;
  174. margin:0 auto;
  175. }
  176. .div.image-swipe-wrapper {
  177. overflow:hidden;
  178. justify-content: space-between;
  179. align-content: center;
  180. align-items: center;
  181. white-space: nowrap;
  182. .div {
  183. display:inline-block;
  184. position: relative;
  185. margin-right:$pageSpace;
  186. box-shadow: 0px 4px 4px #f7f7f7;
  187. margin-bottom:.5rem;
  188. .img {
  189. padding: 0;
  190. margin: 0;
  191. }
  192. .span {
  193. left:0;
  194. bottom: 0;
  195. width: 100%;
  196. display: block;
  197. text-align: center;
  198. border-radius:0.4rem;
  199. height:1.2rem;
  200. line-height:1.2rem;
  201. font-size:$subFontSize;
  202. color: $primaryColor;
  203. }
  204. }
  205. }
  206. }
  207. </style>