specPopup.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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" :min="1" @change="changeValue()" />
  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. isData: null,
  53. preSele: [],
  54. itemsL: [],
  55. toList: [],
  56. total: 0
  57. }
  58. },
  59. methods: {
  60. changeValue(value) {},
  61. change(e) {
  62. if (!e.show) { // 当弹出层关闭时
  63. // 重置规格选择
  64. if (this.specs.length > 0 && this.preSele.length < 0) {
  65. this.specs.forEach(e => {
  66. e.values.forEach(s => {
  67. s.selected = false
  68. })
  69. })
  70. //this.vModelValue = 1
  71. }
  72. this.speL = []
  73. this.seleBack()
  74. }
  75. },
  76. async selectSpec1(item, spec) {
  77. // 首先,取消选择所有其他选项
  78. item.values.forEach(value => {
  79. value.selected = false
  80. })
  81. spec.selected = !spec.selected
  82. const newIds = spec.ids
  83. if (this.preSele.length > 0) {
  84. this.speL = this.preSele
  85. }
  86. const filSpeL = this.speL.filter(value => value.ids !== newIds);
  87. filSpeL.push(spec)
  88. this.speL = filSpeL
  89. const seleList = this.speL.filter(item => item.selected == true)
  90. this.itemsL = seleList.map(item => item.label)
  91. // 计算选中规格项的skuIds数组的交集
  92. if (seleList.length > 0) {
  93. let intersection = seleList[0].skuIds
  94. for (let i = 1; i < seleList.length; i++) {
  95. // 使用filter和includes方法找到交集
  96. intersection = intersection.filter(skuId => {
  97. return seleList[i].skuIds.includes(skuId);
  98. })
  99. }
  100. // 如果交集不为空,取第一个元素作为唯一字符串
  101. const uniqueString = intersection.length > 0 ? intersection[0] : '';
  102. // 更新组件中的intSkuId
  103. this.intSkuId = uniqueString;
  104. } else {
  105. // 如果没有选中的规格项,返回空字符串
  106. this.intSkuId = '';
  107. }
  108. this.isSelected = this.specs.every(spec => spec.values.some(value => value.selected))
  109. if (this.isSelected) {
  110. // 如果所有规格都至少有一个选项被选中
  111. const res = await this.$request('get', `/item/sku/queryItem/${this.intSkuId}`)
  112. if (res) {
  113. this.caMsg = res.data
  114. this.vModelValue = 1
  115. console.log('this.caMsg', this.caMsg)
  116. }
  117. //console.log('所有规格都已选择')
  118. if (this.isData == true) {
  119. this.preSele = seleList
  120. }
  121. } else {}
  122. },
  123. // 返回选中的数据
  124. seleBack() {
  125. const itemDetal = {
  126. id: this.intSkuId,
  127. item: Array.from(this.itemsL).join(','),
  128. count: this.vModelValue
  129. }
  130. this.$emit('seChanged', itemDetal);
  131. },
  132. async open(e) {
  133. if (e || e != null) {
  134. this.isData = e
  135. }
  136. this.$refs.popup.open('bottom')
  137. const spuId = this.cardMsg.spuId
  138. const res = await this.$request('get', `/item/sku/queryPropMapping?spuId=${spuId}`)
  139. this.specs = res.data.filter(item => item.values && item.values.length > 0)
  140. this.specs.forEach(item => {
  141. Object.assign(item, {
  142. values: item.values.map(value => ({
  143. ...value,
  144. ids: item.id
  145. // selected: false
  146. }))
  147. })
  148. if (this.preSele.length <= 0) {
  149. Object.assign(item, {
  150. values: item.values.map(value => ({
  151. ...value,
  152. selected: false
  153. }))
  154. })
  155. } else {
  156. Object.assign(item, {
  157. values: item.values.map(value => ({
  158. ...value,
  159. selected: this.preSele.some(prevSpe =>
  160. prevSpe.label === value.label && prevSpe.ids ===
  161. value.ids)
  162. }))
  163. })
  164. }
  165. })
  166. },
  167. toShop() {
  168. if (this.isSelected == false) {
  169. uni.showToast({
  170. title: '还有规格未选择',
  171. icon: 'error',
  172. duration: 1000
  173. });
  174. } else {
  175. const e = uni.getStorageSync('carl')
  176. const userId = uni.getStorageSync('appUserId')
  177. const count = this.vModelValue
  178. const params = {
  179. storeId: e.id,
  180. storeName: e.name,
  181. skuId: this.caMsg.skuId,
  182. basePrice: this.caMsg.retailPrice,
  183. price: this.caMsg.retailPrice,
  184. quantity: count,
  185. extendProps: userId
  186. }
  187. this.$request('post', `/front/shoppingCart`, params, true).then(response => {
  188. // 请求成功
  189. if (response.code == 200) {
  190. // 弹出消息
  191. uni.showToast({
  192. title: '加入购物车成功',
  193. icon: 'success',
  194. duration: 2000
  195. })
  196. this.$emit('addShop');
  197. } else if (response.code == 500) {
  198. uni.showToast({
  199. title: response.msg,
  200. icon: 'error',
  201. duration: 2000
  202. });
  203. }
  204. }).catch(error => {
  205. // 请求失败
  206. console.error('请求失败:', error);
  207. })
  208. // 关闭弹出层
  209. this.$refs.popup.close()
  210. }
  211. },
  212. totalPrice() {
  213. const e = uni.getStorageSync('carl')
  214. const userId = uni.getStorageSync('appUserId')
  215. const toList = {
  216. storeId: e.id,
  217. storeName: e.name,
  218. skuId: this.caMsg.skuId,
  219. basePrice: this.caMsg.retailPrice,
  220. price: this.caMsg.retailPrice,
  221. quantity: this.vModelValue,
  222. extendProps: userId,
  223. imgUrl: this.caMsg.img,
  224. spuId: this.caMsg.spuId,
  225. skuName: this.caMsg.skuName,
  226. skuProperties: this.caMsg.skuProperties,
  227. skuTitle: this.caMsg.title,
  228. }
  229. let total = 0;
  230. // 如果 selected 属性不存在,直接计算所有商品
  231. let priceInCents = Math.round(toList.price * 100);
  232. let quant = toList.quantity;
  233. total += priceInCents * quant;
  234. this.toList.push(toList)
  235. // 将总价格转换回浮点数(以元为单位)
  236. this.total = (total / 100).toFixed(2);
  237. },
  238. toBuy() {
  239. this.totalPrice()
  240. const carlist = {
  241. carlist: this.toList,
  242. total: this.total
  243. }
  244. uni.navigateTo({
  245. url: `/pages/order/submitOrder/submitOrder?data=${encodeURIComponent(JSON.stringify(carlist))}`
  246. })
  247. this.toList = []
  248. console.log('carlist', carlist)
  249. this.$refs.popup.close()
  250. // if (carlist.carlist.length <= 0) {
  251. // // this.$refs.uNotify.show({'请选择'})
  252. // uni.showToast({
  253. // title: '请选择',
  254. // icon: 'error',
  255. // duration: 2000
  256. // });
  257. // } else {
  258. // uni.navigateTo({
  259. // url: `/pages/order/submitOrder/submitOrder?data=${encodeURIComponent(JSON.stringify(carlist))}`
  260. // })
  261. // this.$refs.popup.close()
  262. // }
  263. // uni.navigateTo({
  264. // url: `/pages/order/submitOrder/submitOrder`
  265. // })
  266. }
  267. }
  268. }
  269. </script>
  270. <style lang="scss" scoped>
  271. .btn-box {
  272. width: 100%;
  273. display: flex;
  274. justify-content: space-around;
  275. align-items: center;
  276. padding-top: 80upx;
  277. .btn {
  278. width: 45%;
  279. height: 80upx;
  280. text-align: center;
  281. line-height: 80upx;
  282. border-radius: 40upx;
  283. }
  284. .reset {
  285. border: solid 1px #EEE;
  286. box-sizing: border-box;
  287. box-sizing: border-box;
  288. border: solid 1px #FF0000;
  289. font-size: 14px;
  290. color: #FF0000;
  291. }
  292. .confirm {
  293. background-image: $base-bg-gradient-color;
  294. color: #FFF;
  295. }
  296. }
  297. .spec-box {
  298. width: 100%;
  299. display: flex;
  300. flex-wrap: wrap;
  301. .spec-item {
  302. height: 60upx;
  303. line-height: 60upx;
  304. border: solid 1px #EEE;
  305. box-sizing: border-box;
  306. text-align: center;
  307. margin-left: 20upx;
  308. border-radius: 3px;
  309. margin-bottom: 20upx;
  310. font-size: 12px;
  311. }
  312. .spec-item:first-child {
  313. margin-left: 0;
  314. }
  315. .on-spec {
  316. background-image: $base-bg-gradient-color;
  317. color: #FFF;
  318. }
  319. }
  320. .s-box {
  321. width: 100%;
  322. display: flex;
  323. border-bottom: solid 1px #EEE;
  324. .s-img {
  325. width: 140upx;
  326. height: 140upx;
  327. background-color: #EEE;
  328. border-radius: 4px;
  329. }
  330. .g-info {
  331. padding-left: 20upx;
  332. .s-name {
  333. padding-bottom: 4px;
  334. }
  335. .s-price {
  336. color: #F00;
  337. font-size: 14px;
  338. }
  339. }
  340. }
  341. .purchase-num {
  342. width: 100%;
  343. display: flex;
  344. justify-content: space-between;
  345. align-items: center;
  346. border-bottom: solid 1px #EEE;
  347. padding-bottom: 20upx;
  348. }
  349. </style>