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