s-select-seckill-sku.vue 12 KB

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