s-select-sku.vue 11 KB

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