s-select-sku.vue 13 KB

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