s-select-seckill-sku.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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";
  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 (state.currentPropertyArray[propertyId] !== undefined && state.currentPropertyArray[propertyId] === valueId) {
  249. // 点击已被选中的,删除并填充 ''
  250. isChecked = false;
  251. state.currentPropertyArray.splice(propertyId, 1, '');
  252. } else {
  253. // 选中
  254. state.currentPropertyArray[propertyId] = valueId;
  255. }
  256. // 选中的 property 大类
  257. let choosePropertyId = [];
  258. state.currentPropertyArray.forEach((currentPropertyId) => {
  259. if (currentPropertyId !== '') {
  260. // currentPropertyId 为空是反选 填充的
  261. choosePropertyId.push(currentPropertyId);
  262. }
  263. });
  264. // 当前所选 property 下,所有可以选择的 SKU 们
  265. let newSkuList = getCanUseSkuList();
  266. // 判断所有 property 大类是否选择完成
  267. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  268. newSkuList[0].count = state.selectedSku.count || 1;
  269. state.selectedSku = newSkuList[0];
  270. } else {
  271. state.selectedSku = {};
  272. }
  273. // 改变 property 禁用状态
  274. changeDisabled(isChecked, propertyId, valueId);
  275. };
  276. changeDisabled(false);
  277. </script>
  278. <style lang="scss" scoped>
  279. // 购买
  280. .buy-box {
  281. padding: 10rpx 20rpx;
  282. .buy-btn {
  283. width: 100%;
  284. height: 80rpx;
  285. border-radius: 40rpx;
  286. background: linear-gradient(90deg, #ff5854, #ff2621);
  287. color: #fff;
  288. }
  289. }
  290. .ss-modal-box {
  291. border-radius: 30rpx 30rpx 0 0;
  292. max-height: 1000rpx;
  293. .modal-header {
  294. position: relative;
  295. padding: 80rpx 20rpx 40rpx;
  296. .sku-image {
  297. width: 160rpx;
  298. height: 160rpx;
  299. border-radius: 10rpx;
  300. }
  301. .header-right {
  302. height: 160rpx;
  303. }
  304. .close-icon {
  305. position: absolute;
  306. top: 10rpx;
  307. right: 20rpx;
  308. font-size: 46rpx;
  309. opacity: 0.2;
  310. }
  311. .goods-title {
  312. font-size: 28rpx;
  313. font-weight: 500;
  314. line-height: 42rpx;
  315. }
  316. .price-text {
  317. font-size: 30rpx;
  318. font-weight: 500;
  319. color: $red;
  320. font-family: OPPOSANS;
  321. &::before {
  322. content: '¥';
  323. font-size: 24rpx;
  324. }
  325. }
  326. .stock-text {
  327. font-size: 26rpx;
  328. color: #999999;
  329. }
  330. }
  331. .modal-content {
  332. padding: 0 20rpx;
  333. .modal-content-scroll {
  334. max-height: 600rpx;
  335. .label-text {
  336. font-size: 26rpx;
  337. font-weight: 500;
  338. }
  339. .buy-num-box {
  340. height: 100rpx;
  341. }
  342. .spec-btn {
  343. height: 60rpx;
  344. min-width: 100rpx;
  345. padding: 0 30rpx;
  346. background: #f4f4f4;
  347. border-radius: 30rpx;
  348. color: #434343;
  349. font-size: 26rpx;
  350. margin-right: 10rpx;
  351. margin-bottom: 10rpx;
  352. }
  353. .checked-btn {
  354. background: linear-gradient(90deg, #ff5854, #ff2621);
  355. font-weight: 500;
  356. color: #ffffff;
  357. }
  358. .disabled-btn {
  359. font-weight: 400;
  360. color: #c6c6c6;
  361. background: #f8f8f8;
  362. }
  363. }
  364. }
  365. }
  366. .tig {
  367. border: 2rpx solid #ff5854;
  368. border-radius: 4rpx;
  369. width: 126rpx;
  370. height: 38rpx;
  371. .tig-icon {
  372. width: 40rpx;
  373. height: 40rpx;
  374. background: #ff5854;
  375. border-radius: 4rpx 0 0 4rpx;
  376. .cicon-alarm {
  377. font-size: 32rpx;
  378. color: #fff;
  379. }
  380. }
  381. .tig-title {
  382. font-size: 24rpx;
  383. font-weight: 500;
  384. line-height: normal;
  385. color: #ff6000;
  386. width: 86rpx;
  387. display: flex;
  388. justify-content: center;
  389. align-items: center;
  390. }
  391. }
  392. </style>