s-select-sku.vue 11 KB

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