s-select-sku.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <!-- 规格弹窗 -->
  3. <su-popup :show="show" round="10" @close="emits('close')">
  4. <view class="ss-modal-box bg-white ss-flex-col">
  5. <view class="modal-header ss-flex ss-col-center">
  6. <view class="header-left ss-m-r-30">
  7. <image class="sku-image" :src="state.selectedSkuPrice.image || goodsInfo.image"
  8. mode="aspectFill"></image>
  9. </view>
  10. <view class="header-right ss-flex-col ss-row-between ss-flex-1">
  11. <view class="goods-title ss-line-2">{{ goodsInfo.title }}</view>
  12. <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
  13. <view class="ss-flex">
  14. <view v-if="goodsPrice.price > 0" class="price-text">
  15. {{ goodsPrice.price }}
  16. </view>
  17. <view class="ss-flex">
  18. <view v-if="goodsPrice.price > 0 && goodsPrice.score > 0" class="score-text ss-m-l-4">+
  19. </view>
  20. <image v-if="goodsPrice.score > 0"
  21. :src="sheep.$url.static('/static/img/shop/goods/score1.svg')" class="score-img">
  22. </image>
  23. <view v-if="goodsPrice.score > 0" class="score-text">
  24. {{ goodsPrice.score }}
  25. </view>
  26. </view>
  27. </view>
  28. <view class="stock-text ss-m-l-20">
  29. {{
  30. state.selectedSkuPrice.stock
  31. ? formatStock(goodsInfo.stock_show_type, state.selectedSkuPrice.stock)
  32. : formatStock(goodsInfo.stock_show_type, goodsInfo.stock)
  33. }}
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="modal-content ss-flex-1">
  39. <scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
  40. <view class="sku-item ss-m-b-20" v-for="sku1 in skuList" :key="sku1.id">
  41. <view class="label-text ss-m-b-20">{{ sku1.name }}</view>
  42. <view class="ss-flex ss-col-center ss-flex-wrap">
  43. <button class="ss-reset-button spec-btn" v-for="sku2 in sku1.values" :class="[
  44. {
  45. 'ui-BG-Main-Gradient': state.currentSkuArray[sku1.id] == sku2.id,
  46. },
  47. {
  48. 'disabled-btn': sku2.disabled === true,
  49. },
  50. ]" :key="sku2.id" :disabled="sku2.disabled === true" @tap="onSelectSku(sku1.id, sku2.id)">
  51. {{ sku2.name }}
  52. </button>
  53. </view>
  54. </view>
  55. <view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40">
  56. <view class="label-text">购买数量</view>
  57. <su-number-box :min="1" :max="state.selectedSkuPrice.stock" :step="1"
  58. v-model="state.selectedSkuPrice.goods_num" @change="onNumberChange($event)" />
  59. </view>
  60. </scroll-view>
  61. </view>
  62. <view class="modal-footer border-top">
  63. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-if="isScore">
  64. <button class="ss-reset-button score-btn ui-Shadow-Main" @tap="onBuy">立即兑换</button>
  65. </view>
  66. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-else>
  67. <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="onAddCart">加入购物车</button>
  68. <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">立即购买</button>
  69. </view>
  70. </view>
  71. </view>
  72. </su-popup>
  73. </template>
  74. <script setup>
  75. import {
  76. computed,
  77. reactive,
  78. watch
  79. } from 'vue';
  80. import sheep from '@/sheep';
  81. import {
  82. formatStock,
  83. formatPrice,
  84. convertProductPropertyList
  85. } from '@/sheep/hooks/useGoods';
  86. import {
  87. isEmpty
  88. } from 'lodash';
  89. const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
  90. const props = defineProps({
  91. goodsInfo: {
  92. type: Object,
  93. default () {},
  94. },
  95. show: {
  96. type: Boolean,
  97. default: false,
  98. },
  99. isScore: {
  100. type: Boolean,
  101. default: false,
  102. },
  103. });
  104. const state = reactive({
  105. selectedSkuPrice: {},
  106. currentSkuArray: [],
  107. });
  108. // 输入框改变数量
  109. function onNumberChange(e) {
  110. if (e === 0) return;
  111. if (state.selectedSkuPrice.goods_num === e) return;
  112. state.selectedSkuPrice.goods_num = e;
  113. }
  114. // TODO 芋艿:去除单规格
  115. // 默认单规格
  116. // if (!props.goodsInfo.is_sku) {
  117. // state.selectedSkuPrice = props.goodsInfo.sku_prices[0];
  118. // }
  119. const skuList = convertProductPropertyList(props.goodsInfo.skus);
  120. // 可选规格
  121. const skuPrices = computed(() => {
  122. let skuPrices = props.goodsInfo.skus;
  123. // TODO 芋艿:去除单规格
  124. // if (props.goodsInfo.is_sku) {
  125. // skuPrices.forEach((item) => {
  126. // item.goods_sku_id_arr = item.goods_sku_ids.split(',');
  127. // });
  128. // }
  129. for (let price of skuPrices) {
  130. price.goods_sku_id_arr = price.properties.map((item) => item.valueId).join(',')
  131. }
  132. return skuPrices;
  133. });
  134. watch(
  135. () => state.selectedSkuPrice,
  136. (newVal) => {
  137. emits('change', newVal);
  138. }, {
  139. immediate: true, // 立即执行
  140. deep: true, // 深度监听
  141. },
  142. );
  143. const goodsPrice = computed(() => {
  144. let price, score;
  145. if (isEmpty(state.selectedSkuPrice)) {
  146. price = props.goodsInfo.price[0];
  147. score = props.goodsInfo.score || 0;
  148. } else {
  149. price = state.selectedSkuPrice.price;
  150. score = state.selectedSkuPrice.score || 0;
  151. }
  152. return {
  153. price,
  154. score,
  155. };
  156. });
  157. function onAddCart() {
  158. if (state.selectedSkuPrice.goods_id) {
  159. if (state.selectedSkuPrice.stock <= 0) {
  160. sheep.$helper.toast('库存不足');
  161. } else {
  162. emits('addCart', state.selectedSkuPrice);
  163. }
  164. } else {
  165. sheep.$helper.toast('请选择规格');
  166. }
  167. }
  168. function onBuy() {
  169. if (state.selectedSkuPrice.goods_id) {
  170. if (state.selectedSkuPrice.stock <= 0) {
  171. sheep.$helper.toast('库存不足');
  172. } else {
  173. emits('buy', state.selectedSkuPrice);
  174. }
  175. } else {
  176. sheep.$helper.toast('请选择规格');
  177. }
  178. }
  179. // 改变禁用状态
  180. function changeDisabled(isChecked = false, pid = 0, skuId = 0) {
  181. let newPrice = []; // 所有可以选择的 skuPrice
  182. if (isChecked) {
  183. // 选中规格
  184. // 当前点击选中规格下的 所有可用 skuPrice
  185. for (let price of skuPrices.value) {
  186. if (price.stock <= 0) {
  187. // this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
  188. continue;
  189. }
  190. console.log(price.goods_sku_id_arr, '=======')
  191. if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) {
  192. newPrice.push(price);
  193. }
  194. }
  195. } else {
  196. // 取消选中
  197. // 当前所选规格下,所有可以选择的 skuPrice
  198. newPrice = getCanUseSkuPrice();
  199. }
  200. // 所有存在并且有库存未选择的规格项 的 子项 id
  201. let noChooseSkuIds = [];
  202. for (let price of newPrice) {
  203. noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr);
  204. }
  205. // 去重
  206. noChooseSkuIds = Array.from(new Set(noChooseSkuIds));
  207. if (isChecked) {
  208. // 去除当前选中的规格项
  209. let index = noChooseSkuIds.indexOf(skuId.toString());
  210. noChooseSkuIds.splice(index, 1);
  211. } else {
  212. // 循环去除当前已选择的规格项
  213. state.currentSkuArray.forEach((sku) => {
  214. if (sku.toString() != '') {
  215. // sku 为空是反选 填充的
  216. let index = noChooseSkuIds.indexOf(sku.toString());
  217. if (index >= 0) {
  218. // sku 存在于 noChooseSkuIds
  219. noChooseSkuIds.splice(index, 1);
  220. }
  221. }
  222. });
  223. }
  224. // 当前已选择的规格大类
  225. let chooseSkuKey = [];
  226. if (!isChecked) {
  227. // 当前已选择的规格大类
  228. state.currentSkuArray.forEach((sku, key) => {
  229. if (sku != '') {
  230. // sku 为空是反选 填充的
  231. chooseSkuKey.push(key);
  232. }
  233. });
  234. } else {
  235. // 当前点击选择的规格大类
  236. chooseSkuKey = [pid];
  237. }
  238. for (let i in skuList) {
  239. // 当前点击的规格,或者取消选择时候 已选中的规格 不进行处理
  240. if (chooseSkuKey.indexOf(skuList[i]['id']) >= 0) {
  241. continue;
  242. }
  243. for (let j in skuList[i]['children']) {
  244. // 如果当前规格项 id 不存在于有库存的规格项中,则禁用
  245. if (noChooseSkuIds.indexOf(skuList[i]['children'][j]['id'].toString()) >= 0) {
  246. skuList[i]['children'][j]['disabled'] = false;
  247. } else {
  248. skuList[i]['children'][j]['disabled'] = true;
  249. }
  250. }
  251. }
  252. }
  253. // 当前所选规格下,获取所有有库存的 skuPrice
  254. function getCanUseSkuPrice() {
  255. // debugger
  256. let newPrice = [];
  257. for (let price of skuPrices.value) {
  258. if (price.stock <= 0) {
  259. // || price.stock < this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
  260. continue;
  261. }
  262. var isOk = true;
  263. state.currentSkuArray.forEach((sku) => {
  264. // sku 不为空,并且,这个 条 skuPrice 没有被选中,则排除
  265. if (sku.toString() != '' && price.goods_sku_id_arr.indexOf(sku.toString()) < 0) {
  266. isOk = false;
  267. }
  268. });
  269. if (isOk) {
  270. newPrice.push(price);
  271. }
  272. }
  273. return newPrice;
  274. }
  275. // 选择规格
  276. function onSelectSku(pid, skuId) {
  277. // debugger
  278. // 清空已选择
  279. let isChecked = true; // 选中 or 取消选中
  280. if (state.currentSkuArray[pid] != undefined && state.currentSkuArray[pid] == skuId) {
  281. // 点击已被选中的,删除并填充 ''
  282. isChecked = false;
  283. state.currentSkuArray.splice(pid, 1, '');
  284. } else {
  285. // 选中
  286. state.currentSkuArray[pid] = skuId;
  287. }
  288. let chooseSkuId = []; // 选中的规格大类
  289. state.currentSkuArray.forEach((sku) => {
  290. if (sku != '') {
  291. // sku 为空是反选 填充的
  292. chooseSkuId.push(sku);
  293. }
  294. });
  295. // 当前所选规格下,所有可以选择的 skuPrice
  296. let newPrice = getCanUseSkuPrice();
  297. // 判断所有规格大类是否选择完成
  298. if (chooseSkuId.length == skuList.length && newPrice.length) {
  299. newPrice[0].goods_num = state.selectedSkuPrice.goods_num || 1;
  300. state.selectedSkuPrice = newPrice[0];
  301. } else {
  302. state.selectedSkuPrice = {};
  303. }
  304. // 改变规格项禁用状态
  305. changeDisabled(isChecked, pid, skuId);
  306. }
  307. changeDisabled(false);
  308. </script>
  309. <style lang="scss" scoped>
  310. // 购买
  311. .buy-box {
  312. padding: 10rpx 0;
  313. .add-btn {
  314. width: 356rpx;
  315. height: 80rpx;
  316. border-radius: 40rpx 0 0 40rpx;
  317. background-color: var(--ui-BG-Main-light);
  318. color: var(--ui-BG-Main);
  319. }
  320. .buy-btn {
  321. width: 356rpx;
  322. height: 80rpx;
  323. border-radius: 0 40rpx 40rpx 0;
  324. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  325. color: #fff;
  326. }
  327. .score-btn {
  328. width: 100%;
  329. margin: 0 20rpx;
  330. height: 80rpx;
  331. border-radius: 40rpx;
  332. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  333. color: #fff;
  334. }
  335. }
  336. .ss-modal-box {
  337. border-radius: 30rpx 30rpx 0 0;
  338. max-height: 1000rpx;
  339. .modal-header {
  340. position: relative;
  341. padding: 80rpx 20rpx 40rpx;
  342. .sku-image {
  343. width: 160rpx;
  344. height: 160rpx;
  345. border-radius: 10rpx;
  346. }
  347. .header-right {
  348. height: 160rpx;
  349. }
  350. .close-icon {
  351. position: absolute;
  352. top: 10rpx;
  353. right: 20rpx;
  354. font-size: 46rpx;
  355. opacity: 0.2;
  356. }
  357. .goods-title {
  358. font-size: 28rpx;
  359. font-weight: 500;
  360. line-height: 42rpx;
  361. }
  362. .score-img {
  363. width: 36rpx;
  364. height: 36rpx;
  365. margin: 0 4rpx;
  366. }
  367. .score-text {
  368. font-size: 30rpx;
  369. font-weight: 500;
  370. color: $red;
  371. font-family: OPPOSANS;
  372. }
  373. .price-text {
  374. font-size: 30rpx;
  375. font-weight: 500;
  376. color: $red;
  377. font-family: OPPOSANS;
  378. &::before {
  379. content: '¥';
  380. font-size: 30rpx;
  381. font-weight: 500;
  382. color: $red;
  383. }
  384. }
  385. .stock-text {
  386. font-size: 26rpx;
  387. color: #999999;
  388. }
  389. }
  390. .modal-content {
  391. padding: 0 20rpx;
  392. .modal-content-scroll {
  393. max-height: 600rpx;
  394. .label-text {
  395. font-size: 26rpx;
  396. font-weight: 500;
  397. }
  398. .buy-num-box {
  399. height: 100rpx;
  400. }
  401. .spec-btn {
  402. height: 60rpx;
  403. min-width: 100rpx;
  404. padding: 0 30rpx;
  405. background: #f4f4f4;
  406. border-radius: 30rpx;
  407. color: #434343;
  408. font-size: 26rpx;
  409. margin-right: 10rpx;
  410. margin-bottom: 10rpx;
  411. }
  412. .disabled-btn {
  413. font-weight: 400;
  414. color: #c6c6c6;
  415. background: #f8f8f8;
  416. }
  417. }
  418. }
  419. }
  420. </style>