specPopup.vue 9.3 KB

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