s-select-sku.vue 12 KB

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