s-select-seckill-sku.vue 13 KB

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