specPopup.vue 7.0 KB

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