s-select-seckill-sku.vue 12 KB

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