specPopup.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <uni-popup ref="popup" background-color="#fff" mode="bottom" @change="change">
  3. <view class="s-box padding-sm">
  4. <view class="s-img">
  5. <image mode="aspectFit" class="wh-full"
  6. :src="caMsg.img[0].url ? caMsg.img[0].url : cardMsg.imgList[0].url"></image>
  7. </view>
  8. <view class="g-info">
  9. <view class="s-name">{{caMsg.skuName ? caMsg.skuName : cardMsg.spuName}}</view>
  10. <view class="s-price">{{caMsg.retailPrice ? caMsg.retailPrice : cardMsg.minPrice}}</view>
  11. </view>
  12. </view>
  13. <scroll-view style="height: 270px;" scroll-y>
  14. <view v-for="(item, index) in specs" :key="index">
  15. <uni-section :title="item.name">
  16. <view class="spec-box padding-lr-sm">
  17. <view class="spec-item padding-lr-sm" v-for="(spe, cindex) in item.values"
  18. :class="{'on-spec': spe.selected}" :key="cindex" @click="selectSpec1(item, spe)">
  19. {{ spe.label }}
  20. </view>
  21. </view>
  22. </uni-section>
  23. </view>
  24. </scroll-view>
  25. <view class="purchase-num padding-lr-sm">
  26. <uni-section title="购买数量"></uni-section>
  27. <uni-number-box v-model="vModelValue" @change="changeValue(value)" />
  28. </view>
  29. <view class="btn-box">
  30. <view class="btn reset" @click="toShop">加入购物车</view>
  31. <view class="btn confirm" @click="toBuy">立即购买</view>
  32. </view>
  33. </uni-popup>
  34. </template>
  35. <script>
  36. export default {
  37. name: "specPopup",
  38. props: {
  39. cardMsg: {
  40. type: Object,
  41. required: true
  42. }
  43. },
  44. data() {
  45. return {
  46. vModelValue: 1,
  47. specs: [], // 总查询数据
  48. speL: [],
  49. intSkuId: [], // 选择唯一的id
  50. caMsg: [], // 根据id查出来的数据
  51. }
  52. },
  53. methods: {
  54. changeValue(e) {},
  55. change(e) {
  56. if (!e.show) { // 当弹出层关闭时
  57. // 重置规格选择
  58. if (this.specs.length > 0) {
  59. this.specs.forEach(e => {
  60. e.values.forEach(s => {
  61. s.selected = false
  62. })
  63. })
  64. }
  65. this.speL = []
  66. // 重置购买数量
  67. this.vModelValue = 1
  68. }
  69. },
  70. async selectSpec1(item, spec) {
  71. // 首先,取消选择所有其他选项
  72. item.values.forEach(value => {
  73. value.selected = false
  74. })
  75. // 然后,选择当前点击的选项
  76. spec.selected = true
  77. if (spec.selected) {
  78. this.speL.push(spec)
  79. }
  80. const seleList = this.speL.filter(item => item.selected == true)
  81. const items = seleList.map(item => item.label)
  82. // 计算选中规格项的skuIds数组的交集
  83. if (seleList.length > 0) {
  84. let intersection = seleList[0].skuIds
  85. // 遍历数组中的每个对象
  86. for (let i = 1; i < seleList.length; i++) {
  87. // 使用filter和includes方法找到交集
  88. intersection = intersection.filter(skuId => {
  89. return seleList[i].skuIds.includes(skuId);
  90. })
  91. }
  92. // 如果交集不为空,取第一个元素作为唯一字符串
  93. const uniqueString = intersection.length > 0 ? intersection[0] : '';
  94. // 更新组件中的intSkuId
  95. this.intSkuId = uniqueString;
  96. } else {
  97. // 如果没有选中的规格项,返回空字符串
  98. this.intSkuId = '';
  99. }
  100. console.log('speL', this.speL)
  101. // 确定每一个都有选择后发起请求
  102. const isSelected = this.specs.every(spec => spec.values.some(value => value.selected))
  103. if (isSelected) {
  104. // 如果所有规格都至少有一个选项被选中
  105. const res = await this.$request('get', `/item/sku/${this.intSkuId}`)
  106. this.caMsg = res.data
  107. // 返回选中规格项的label值
  108. console.log('intSkuId', this.intSkuId)
  109. console.log('items', items)
  110. console.log('所有规格都已选择')
  111. const itemDetal = {
  112. id: this.intSkuId,
  113. item: Array.from(items).join(','),
  114. count: this.vModelValue
  115. }
  116. this.$emit('seChanged', itemDetal);
  117. } else {}
  118. },
  119. async open() {
  120. this.$refs.popup.open('bottom')
  121. console.log("this.cardMsg")
  122. const spuId = this.cardMsg.spuId
  123. const res = await this.$request('get', `/item/sku/queryPropMapping?spuId=${spuId}`)
  124. this.specs = res.data.filter(item => item.values && item.values.length > 0);
  125. this.specs.forEach(item => {
  126. Object.assign(item, {
  127. values: item.values.map(value => ({
  128. ...value,
  129. ids: item.id,
  130. selected: false
  131. }))
  132. })
  133. })
  134. },
  135. toShop() {
  136. const e = uni.getStorageSync('carl')
  137. const userId = uni.getStorageSync('appUserId')
  138. const count = this.vModelValue
  139. const params = {
  140. storeId: e.id,
  141. storeName: e.name,
  142. skuId: this.caMsg.skuId,
  143. basePrice: this.caMsg.retailPrice,
  144. price: this.caMsg.retailPrice,
  145. quantity: count,
  146. extendProps: userId
  147. }
  148. console.log('params', params)
  149. this.$request('post', `/front/shoppingCart`, params, true).then(response => {
  150. // 请求成功
  151. if (response.code = 200) {
  152. // 弹出消息
  153. uni.showToast({
  154. title: '加入购物车成功',
  155. icon: 'success',
  156. duration: 2000
  157. })
  158. this.$emit('addShop');
  159. }
  160. }).catch(error => {
  161. // 请求失败
  162. console.error('请求失败:', error);
  163. })
  164. // this.$emit('update-shopmsg', this.shopMsg);
  165. // 关闭弹出层
  166. this.$refs.popup.close()
  167. },
  168. toBuy() {
  169. uni.navigateTo({
  170. url: `/pages/order/submitOrder/submitOrder`
  171. })
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .btn-box {
  178. width: 100%;
  179. display: flex;
  180. justify-content: space-around;
  181. align-items: center;
  182. padding-top: 80upx;
  183. .btn {
  184. width: 45%;
  185. height: 80upx;
  186. text-align: center;
  187. line-height: 80upx;
  188. border-radius: 40upx;
  189. }
  190. .reset {
  191. border: solid 1px #EEE;
  192. box-sizing: border-box;
  193. box-sizing: border-box;
  194. border: solid 1px #FF0000;
  195. font-size: 14px;
  196. color: #FF0000;
  197. }
  198. .confirm {
  199. background-image: $base-bg-gradient-color;
  200. color: #FFF;
  201. }
  202. }
  203. .spec-box {
  204. width: 100%;
  205. display: flex;
  206. flex-wrap: wrap;
  207. .spec-item {
  208. height: 60upx;
  209. line-height: 60upx;
  210. border: solid 1px #EEE;
  211. box-sizing: border-box;
  212. text-align: center;
  213. margin-left: 20upx;
  214. border-radius: 3px;
  215. margin-bottom: 20upx;
  216. font-size: 12px;
  217. }
  218. .spec-item:first-child {
  219. margin-left: 0;
  220. }
  221. .on-spec {
  222. background-image: $base-bg-gradient-color;
  223. color: #FFF;
  224. }
  225. }
  226. .s-box {
  227. width: 100%;
  228. display: flex;
  229. border-bottom: solid 1px #EEE;
  230. .s-img {
  231. width: 140upx;
  232. height: 140upx;
  233. background-color: #EEE;
  234. border-radius: 4px;
  235. }
  236. .g-info {
  237. padding-left: 20upx;
  238. .s-name {
  239. padding-bottom: 4px;
  240. }
  241. .s-price {
  242. color: #F00;
  243. font-size: 14px;
  244. }
  245. }
  246. }
  247. .purchase-num {
  248. width: 100%;
  249. display: flex;
  250. justify-content: space-between;
  251. align-items: center;
  252. border-bottom: solid 1px #EEE;
  253. padding-bottom: 20upx;
  254. }
  255. </style>