s-select-seckill-sku.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <template>
  2. <!-- 规格弹窗 -->
  3. <su-popup :show="show" round="10" @close="emits('close')">
  4. <!-- SKU 信息 -->
  5. <view class="ss-modal-box bg-white ss-flex-col">
  6. <view class="modal-header ss-flex ss-col-center">
  7. <!-- 规格图 -->
  8. <view class="header-left ss-m-r-30">
  9. <image
  10. class="sku-image"
  11. :src="sheep.$url.cdn(state.selectedSku.picUrl || state.goodsInfo.picUrl)"
  12. mode="aspectFill"
  13. >
  14. </image>
  15. </view>
  16. <view class="header-right ss-flex-col ss-row-between ss-flex-1">
  17. <!-- 名称 -->
  18. <view class="goods-title ss-line-2">{{ state.goodsInfo.name }}</view>
  19. <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
  20. <!-- 价格 -->
  21. <view class="price-text">
  22. {{ fen2yuan(state.selectedSku.price || state.goodsInfo.price) }}
  23. </view>
  24. <!-- 秒杀价格标签 -->
  25. <view class="tig ss-flex ss-col-center">
  26. <view class="tig-icon ss-flex ss-col-center ss-row-center">
  27. <text class="cicon-alarm"></text>
  28. </view>
  29. <view class="tig-title">秒杀价</view>
  30. </view>
  31. <!-- 库存 -->
  32. <view class="stock-text ss-m-l-20">
  33. 库存{{ state.selectedSku.stock || state.goodsInfo.stock }}件
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="modal-content ss-flex-1">
  39. <scroll-view scroll-y="true" class="modal-content-scroll">
  40. <view class="sku-item ss-m-b-20" v-for="property in propertyList" :key="property.id">
  41. <view class="label-text ss-m-b-20">{{ property.name }}</view>
  42. <view class="ss-flex ss-col-center ss-flex-wrap">
  43. <button
  44. class="ss-reset-button spec-btn"
  45. v-for="value in property.values"
  46. :class="[
  47. {
  48. 'checked-btn': state.currentPropertyArray[property.id] === value.id,
  49. },
  50. {
  51. 'disabled-btn': value.disabled === true,
  52. },
  53. ]"
  54. :key="value.id"
  55. :disabled="value.disabled === true"
  56. @tap="onSelectSku(property.id, value.id)"
  57. >
  58. {{ value.name }}
  59. </button>
  60. </view>
  61. </view>
  62. <view class="buy-num-box ss-flex ss-col-center ss-row-between">
  63. <view class="label-text">购买数量</view>
  64. <su-number-box
  65. :min="1"
  66. :max="min([singleLimitCount, state.selectedSku.stock])"
  67. :step="1"
  68. v-model="state.selectedSku.count"
  69. @change="onBuyCountChange($event)"
  70. activity="seckill"
  71. ></su-number-box>
  72. </view>
  73. </scroll-view>
  74. </view>
  75. <view class="modal-footer">
  76. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
  77. <button class="ss-reset-button buy-btn" @tap="onBuy">确认</button>
  78. </view>
  79. </view>
  80. </view>
  81. </su-popup>
  82. </template>
  83. <script setup>
  84. /**
  85. * 秒杀活动SKU选择,
  86. * 与s-select-sku的区别:多一个秒杀价的标签、没有加入购物车按钮、立即购买按钮叫确认、秒杀有最大购买数量限制
  87. * 差别不大,可以考虑合并 todo @芋艿
  88. */
  89. // 按钮状态: active,nostock
  90. import { computed, reactive, watch } from 'vue';
  91. import sheep from '@/sheep';
  92. import {convertProductPropertyList, fen2yuan} from "@/sheep/hooks/useGoods";
  93. import {min} from "lodash";
  94. const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
  95. const props = defineProps({
  96. modelValue: {
  97. type: Object,
  98. default() {},
  99. },
  100. show: {
  101. type: Boolean,
  102. default: false,
  103. },
  104. // 单次限购数量
  105. singleLimitCount: {
  106. type: Number,
  107. default: 1,
  108. }
  109. });
  110. const state = reactive({
  111. goodsInfo: computed(() => props.modelValue),
  112. selectedSku: {},
  113. currentPropertyArray: [],
  114. });
  115. const propertyList = convertProductPropertyList(state.goodsInfo.skus);
  116. // SKU 列表
  117. const skuList = computed(() => {
  118. let skuPrices = state.goodsInfo.skus;
  119. for (let price of skuPrices) {
  120. price.value_id_array = price.properties.map((item) => item.valueId)
  121. }
  122. return skuPrices;
  123. });
  124. if (!state.goodsInfo.is_sku) {
  125. state.selectedSku = state.goodsInfo.skus[0];
  126. }
  127. watch(
  128. () => state.selectedSku,
  129. (newVal) => {
  130. emits('change', newVal);
  131. },
  132. {
  133. immediate: true, // 立即执行
  134. deep: true, // 深度监听
  135. },
  136. );
  137. const onBuy = () => {
  138. if (state.selectedSku.id) {
  139. if (state.selectedSku.stock <= 0) {
  140. sheep.$helper.toast('库存不足');
  141. } else {
  142. emits('buy', state.selectedSku);
  143. }
  144. } else {
  145. sheep.$helper.toast('请选择规格');
  146. }
  147. };
  148. // 购买数量改变
  149. function onBuyCountChange(buyCount) {
  150. if (buyCount > 0 && state.selectedSku.count !== buyCount) {
  151. state.selectedSku.count = buyCount;
  152. }
  153. }
  154. // 改变禁用状态
  155. const changeDisabled = (isChecked = false, propertyId = 0, valueId = 0) => {
  156. let newSkus = []; // 所有可以选择的 sku 数组
  157. if (isChecked) {
  158. // 情况一:选中 property
  159. // 获得当前点击选中 property 的、所有可用 SKU
  160. for (let price of skuList.value) {
  161. if (price.stock <= 0) {
  162. continue;
  163. }
  164. if (price.value_id_array.indexOf(valueId) >= 0) {
  165. newSkus.push(price);
  166. }
  167. }
  168. } else {
  169. // 情况二:取消选中 property
  170. // 当前所选 property 下,所有可以选择的 SKU
  171. newSkus = getCanUseSkuList();
  172. }
  173. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  174. let noChooseValueIds = [];
  175. for (let price of newSkus) {
  176. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  177. }
  178. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  179. if (isChecked) {
  180. // 去除当前选中的 value 属性值 id
  181. let index = noChooseValueIds.indexOf(valueId);
  182. noChooseValueIds.splice(index, 1);
  183. } else {
  184. // 循环去除当前已选择的 value 属性值 id
  185. state.currentPropertyArray.forEach((currentPropertyId) => {
  186. if (currentPropertyId.toString() !== '') {
  187. return;
  188. }
  189. // currentPropertyId 为空是反选 填充的
  190. let index = noChooseValueIds.indexOf(currentPropertyId);
  191. if (index >= 0) {
  192. // currentPropertyId 存在于 noChooseValueIds
  193. noChooseValueIds.splice(index, 1);
  194. }
  195. });
  196. }
  197. // 当前已选择的 property 数组
  198. let choosePropertyIds = [];
  199. if (!isChecked) {
  200. // 当前已选择的 property
  201. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  202. if (currentPropertyId !== '') {
  203. // currentPropertyId 为空是反选 填充的
  204. choosePropertyIds.push(currentValueId);
  205. }
  206. });
  207. } else {
  208. // 当前点击选择的 property
  209. choosePropertyIds = [propertyId];
  210. }
  211. for (let propertyIndex in propertyList) {
  212. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  213. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  214. continue;
  215. }
  216. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  217. for (let valueIndex in propertyList[propertyIndex]['values']) {
  218. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  219. noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true 禁用 or false 不禁用
  220. }
  221. }
  222. };
  223. // 获取可用的(有库存的)SKU 列表
  224. const getCanUseSkuList = () => {
  225. let newSkus = [];
  226. for (let sku of skuList.value) {
  227. if (sku.stock <= 0) {
  228. continue;
  229. }
  230. let isOk = true;
  231. state.currentPropertyArray.forEach((propertyId) => {
  232. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  233. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  234. isOk = false;
  235. }
  236. });
  237. if (isOk) {
  238. newSkus.push(sku);
  239. }
  240. }
  241. return newSkus;
  242. };
  243. // 选择规格
  244. const onSelectSku = (propertyId, valueId) => {
  245. // 清空已选择
  246. let isChecked = true; // 选中 or 取消选中
  247. if (state.currentPropertyArray[propertyId] !== undefined && state.currentPropertyArray[propertyId] === valueId) {
  248. // 点击已被选中的,删除并填充 ''
  249. isChecked = false;
  250. state.currentPropertyArray.splice(propertyId, 1, '');
  251. } else {
  252. // 选中
  253. state.currentPropertyArray[propertyId] = valueId;
  254. }
  255. // 选中的 property 大类
  256. let choosePropertyId = [];
  257. state.currentPropertyArray.forEach((currentPropertyId) => {
  258. if (currentPropertyId !== '') {
  259. // currentPropertyId 为空是反选 填充的
  260. choosePropertyId.push(currentPropertyId);
  261. }
  262. });
  263. // 当前所选 property 下,所有可以选择的 SKU 们
  264. let newSkuList = getCanUseSkuList();
  265. // 判断所有 property 大类是否选择完成
  266. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  267. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  268. state.selectedSku = newSkuList[0];
  269. } else {
  270. state.selectedSku = {};
  271. }
  272. // 改变 property 禁用状态
  273. changeDisabled(isChecked, propertyId, valueId);
  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>