s-select-sku.vue 13 KB

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