s-select-sku.vue 13 KB

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