s-select-sku.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 ss-p-b-20" @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 v-if="props.goodsInfo.type === '0'" class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-20">
  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. // type: 0实体商品, 99文件
  139. if (props.goodsInfo.type !== '0') {
  140. uni.showToast({ title: '虚拟商品不能加入购物车', icon: 'none', duration: 2000 })
  141. return
  142. }
  143. if (state.selectedSku.id <= 0) {
  144. sheep.$helper.toast('请选择规格');
  145. return;
  146. }
  147. if (state.selectedSku.stock <= 0) {
  148. sheep.$helper.toast('库存不足');
  149. return;
  150. }
  151. emits('addCart', state.selectedSku);
  152. }
  153. // 立即购买
  154. function onBuy() {
  155. if (state.selectedSku.id <= 0) {
  156. sheep.$helper.toast('请选择规格');
  157. return;
  158. }
  159. if (state.selectedSku.stock <= 0) {
  160. sheep.$helper.toast('库存不足');
  161. return;
  162. }
  163. emits('buy', state.selectedSku);
  164. }
  165. // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
  166. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  167. let newSkus = []; // 所有可以选择的 sku 数组
  168. if (isChecked) {
  169. // 情况一:选中 property
  170. // 获得当前点击选中 property 的、所有可用 SKU
  171. for (let price of skuList.value) {
  172. if (price.stock <= 0) {
  173. continue;
  174. }
  175. if (price.value_id_array.indexOf(valueId) >= 0) {
  176. newSkus.push(price);
  177. }
  178. }
  179. } else {
  180. // 情况二:取消选中 property
  181. // 当前所选 property 下,所有可以选择的 SKU
  182. newSkus = getCanUseSkuList();
  183. }
  184. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  185. let noChooseValueIds = [];
  186. for (let price of newSkus) {
  187. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  188. }
  189. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  190. if (isChecked) {
  191. // 去除当前选中的 value 属性值 id
  192. let index = noChooseValueIds.indexOf(valueId);
  193. noChooseValueIds.splice(index, 1);
  194. } else {
  195. // 循环去除当前已选择的 value 属性值 id
  196. state.currentPropertyArray.forEach((currentPropertyId) => {
  197. if (currentPropertyId.toString() !== '') {
  198. return;
  199. }
  200. // currentPropertyId 为空是反选 填充的
  201. let index = noChooseValueIds.indexOf(currentPropertyId);
  202. if (index >= 0) {
  203. // currentPropertyId 存在于 noChooseValueIds
  204. noChooseValueIds.splice(index, 1);
  205. }
  206. });
  207. }
  208. // 当前已选择的 property 数组
  209. let choosePropertyIds = [];
  210. if (!isChecked) {
  211. // 当前已选择的 property
  212. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  213. if (currentPropertyId !== '') {
  214. // currentPropertyId 为空是反选 填充的
  215. choosePropertyIds.push(currentValueId);
  216. }
  217. });
  218. } else {
  219. // 当前点击选择的 property
  220. choosePropertyIds = [propertyId];
  221. }
  222. for (let propertyIndex in propertyList) {
  223. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  224. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  225. continue;
  226. }
  227. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  228. for (let valueIndex in propertyList[propertyIndex]['values']) {
  229. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  230. noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true 禁用 or false 不禁用
  231. }
  232. }
  233. }
  234. // 当前所选属性下,获取所有有库存的 SKU 们
  235. function getCanUseSkuList() {
  236. let newSkus = [];
  237. for (let sku of skuList.value) {
  238. if (sku.stock <= 0) {
  239. continue;
  240. }
  241. let isOk = true;
  242. state.currentPropertyArray.forEach((propertyId) => {
  243. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  244. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  245. isOk = false;
  246. }
  247. });
  248. if (isOk) {
  249. newSkus.push(sku);
  250. }
  251. }
  252. return newSkus;
  253. }
  254. // 选择规格
  255. function onSelectSku(propertyId, valueId) {
  256. // 清空已选择
  257. let isChecked = true; // 选中 or 取消选中
  258. if (
  259. state.currentPropertyArray[propertyId] !== undefined &&
  260. state.currentPropertyArray[propertyId] === valueId
  261. ) {
  262. // 点击已被选中的,删除并填充 ''
  263. isChecked = false;
  264. state.currentPropertyArray.splice(propertyId, 1, '');
  265. } else {
  266. // 选中
  267. state.currentPropertyArray[propertyId] = valueId;
  268. }
  269. // 选中的 property 大类
  270. let choosePropertyId = [];
  271. state.currentPropertyArray.forEach((currentPropertyId) => {
  272. if (currentPropertyId !== '') {
  273. // currentPropertyId 为空是反选 填充的
  274. choosePropertyId.push(currentPropertyId);
  275. }
  276. });
  277. // 当前所选 property 下,所有可以选择的 SKU 们
  278. let newSkuList = getCanUseSkuList();
  279. // 判断所有 property 大类是否选择完成
  280. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  281. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  282. state.selectedSku = newSkuList[0];
  283. } else {
  284. state.selectedSku = {};
  285. }
  286. // 改变 property 禁用状态
  287. changeDisabled(isChecked, propertyId, valueId);
  288. }
  289. changeDisabled(false);
  290. // 默认选中第一个规格
  291. if (propertyList && propertyList.length) {
  292. propertyList.forEach((e) => {
  293. if (e.values && e.values.length) onSelectSku(e.id, e.values[0].id)
  294. })
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. // 购买
  299. .buy-box {
  300. padding: 10rpx 0;
  301. .add-btn {
  302. width: 356rpx;
  303. height: 80rpx;
  304. border-radius: 40rpx 0 0 40rpx;
  305. background-color: var(--ui-BG-Main-light);
  306. color: var(--ui-BG-Main);
  307. }
  308. .buy-btn {
  309. width: 356rpx;
  310. height: 80rpx;
  311. border-radius: 0 40rpx 40rpx 0;
  312. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  313. color: #fff;
  314. }
  315. .score-btn {
  316. width: 100%;
  317. margin: 0 20rpx;
  318. height: 80rpx;
  319. border-radius: 40rpx;
  320. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  321. color: #fff;
  322. }
  323. }
  324. .ss-modal-box {
  325. border-radius: 30rpx 30rpx 0 0;
  326. max-height: 1000rpx;
  327. .modal-header {
  328. position: relative;
  329. padding: 80rpx 20rpx 40rpx;
  330. .sku-image {
  331. width: 160rpx;
  332. height: 160rpx;
  333. border-radius: 10rpx;
  334. }
  335. .header-right {
  336. height: 160rpx;
  337. }
  338. .close-icon {
  339. position: absolute;
  340. top: 10rpx;
  341. right: 20rpx;
  342. font-size: 46rpx;
  343. opacity: 0.2;
  344. }
  345. .goods-title {
  346. font-size: 28rpx;
  347. font-weight: 500;
  348. line-height: 42rpx;
  349. }
  350. .score-img {
  351. width: 36rpx;
  352. height: 36rpx;
  353. margin: 0 4rpx;
  354. }
  355. .score-text {
  356. font-size: 30rpx;
  357. font-weight: 500;
  358. color: $red;
  359. font-family: OPPOSANS;
  360. }
  361. .price-text {
  362. font-size: 30rpx;
  363. font-weight: 500;
  364. color: $red;
  365. font-family: OPPOSANS;
  366. &::before {
  367. content: '¥';
  368. font-size: 30rpx;
  369. font-weight: 500;
  370. color: $red;
  371. }
  372. }
  373. .stock-text {
  374. font-size: 26rpx;
  375. color: #999999;
  376. }
  377. }
  378. .modal-content {
  379. padding: 0 20rpx;
  380. .modal-content-scroll {
  381. max-height: 600rpx;
  382. .label-text {
  383. font-size: 26rpx;
  384. font-weight: 500;
  385. }
  386. .buy-num-box {
  387. height: 100rpx;
  388. }
  389. .spec-btn {
  390. height: 60rpx;
  391. min-width: 100rpx;
  392. padding: 0 30rpx;
  393. background: #f4f4f4;
  394. border-radius: 30rpx;
  395. color: #434343;
  396. font-size: 26rpx;
  397. margin-right: 10rpx;
  398. margin-bottom: 10rpx;
  399. }
  400. .disabled-btn {
  401. font-weight: 400;
  402. color: #c6c6c6;
  403. background: #f8f8f8;
  404. }
  405. }
  406. }
  407. }
  408. .iconBox {
  409. width: fit-content;
  410. height: fit-content;
  411. padding: 2rpx 10rpx;
  412. background-color: rgb(255, 242, 241);
  413. color: #ff2621;
  414. font-size: 24rpx;
  415. margin-left: 5rpx;
  416. }
  417. .origin-price-text {
  418. font-size: 26rpx;
  419. font-weight: 400;
  420. text-decoration: line-through;
  421. color: $gray-c;
  422. font-family: OPPOSANS;
  423. &::before {
  424. content: '¥';
  425. }
  426. }
  427. </style>