s-select-sku.vue 13 KB

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