s-select-sku.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. <!-- 我要房券 -->
  79. <view v-else class="ss-m-b-20">
  80. <checkbox-group class="ss-m-b-10" @change="isGetPrizeChange">
  81. <label>
  82. <checkbox value="getPrize" :checked="isGetPrize" />
  83. 我要房券
  84. </label>
  85. </checkbox-group>
  86. <div v-if="noCity" style="color: gray;" class="ss-m-20">房券已被抢完啦</div>
  87. <div v-show="isGetPrize" class="ss-m-20">
  88. <div class="getPrizeTip ss-m-20">请勾选一到三个地点作为期望获赠房券的所在地,系统将随机抽取一张</div>
  89. <view v-if="prizeAreaList?.length" class="ss-flex ss-col-center ss-flex-wrap">
  90. <button
  91. class="ss-reset-button spec-btn"
  92. v-for="(item, index) of prizeAreaList"
  93. :key="item+index"
  94. :class="[
  95. {
  96. 'ui-BG-Main-Gradient': item.active,
  97. },
  98. {
  99. 'disabled-btn': item.total <= 0,
  100. },
  101. ]"
  102. :disabled="item.total <= 0"
  103. @tap="cityChange(index, item.active)"
  104. >
  105. {{ item.label }}
  106. </button>
  107. </view>
  108. </div>
  109. </view>
  110. </scroll-view>
  111. </view>
  112. <!-- 操作区 -->
  113. <view class="modal-footer border-top">
  114. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
  115. <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="onAddCart"
  116. >加入购物车</button
  117. >
  118. <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">立即购买</button>
  119. </view>
  120. </view>
  121. </view>
  122. </su-popup>
  123. </template>
  124. <script setup>
  125. import { computed, reactive, watch, ref } from 'vue';
  126. import sheep from '@/sheep';
  127. import { formatStock, convertProductPropertyList, fen2yuan } from '@/sheep/hooks/useGoods';
  128. import PrizeApi from '@/sheep/api/prizeDraw'
  129. const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
  130. const props = defineProps({
  131. goodsInfo: {
  132. type: Object,
  133. default() {},
  134. },
  135. show: {
  136. type: Boolean,
  137. default: false,
  138. },
  139. });
  140. const state = reactive({
  141. selectedSku: {}, // 选中的 SKU
  142. currentPropertyArray: [], // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  143. });
  144. const propertyList = convertProductPropertyList(props.goodsInfo.skus);
  145. // SKU 列表
  146. const skuList = computed(() => {
  147. let skuPrices = props.goodsInfo.skus;
  148. for (let price of skuPrices) {
  149. price.value_id_array = price.properties.map((item) => item.valueId);
  150. }
  151. return skuPrices;
  152. });
  153. watch(
  154. () => state.selectedSku,
  155. (newVal) => {
  156. emits('change', newVal);
  157. },
  158. {
  159. immediate: true, // 立即执行
  160. deep: true, // 深度监听
  161. },
  162. );
  163. // 输入框改变数量
  164. function onNumberChange(e) {
  165. if (e === 0) return;
  166. if (state.selectedSku.goods_num === e) return;
  167. state.selectedSku.goods_num = e;
  168. }
  169. // 加入购物车
  170. function onAddCart() {
  171. // type: 0实体商品, 99文件
  172. if (props.goodsInfo.type !== '0') {
  173. uni.showToast({ title: '虚拟商品不能加入购物车', icon: 'none', duration: 2000 })
  174. return
  175. }
  176. if (state.selectedSku.id <= 0) {
  177. sheep.$helper.toast('请选择规格');
  178. return;
  179. }
  180. if (state.selectedSku.stock <= 0) {
  181. sheep.$helper.toast('库存不足');
  182. return;
  183. }
  184. emits('addCart', state.selectedSku);
  185. }
  186. // 立即购买
  187. function onBuy() {
  188. if (state.selectedSku.id <= 0) {
  189. sheep.$helper.toast('请选择规格');
  190. return;
  191. }
  192. if (state.selectedSku.stock <= 0) {
  193. sheep.$helper.toast('库存不足');
  194. return;
  195. }
  196. if (isGetPrize.value && !checkList?.length) {
  197. sheep.$helper.toast('请勾选期望获赠房券的所在地!');
  198. return;
  199. }
  200. let city = []
  201. if (isGetPrize.value && checkList?.length) {
  202. checkList.forEach(index => city.push(prizeAreaList.value[index].label))
  203. }
  204. emits('buy', { ...state.selectedSku, city })
  205. }
  206. // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
  207. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  208. let newSkus = []; // 所有可以选择的 sku 数组
  209. if (isChecked) {
  210. // 情况一:选中 property
  211. // 获得当前点击选中 property 的、所有可用 SKU
  212. for (let price of skuList.value) {
  213. if (price.stock <= 0) {
  214. continue;
  215. }
  216. if (price.value_id_array.indexOf(valueId) >= 0) {
  217. newSkus.push(price);
  218. }
  219. }
  220. } else {
  221. // 情况二:取消选中 property
  222. // 当前所选 property 下,所有可以选择的 SKU
  223. newSkus = getCanUseSkuList();
  224. }
  225. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  226. let noChooseValueIds = [];
  227. for (let price of newSkus) {
  228. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  229. }
  230. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  231. if (isChecked) {
  232. // 去除当前选中的 value 属性值 id
  233. let index = noChooseValueIds.indexOf(valueId);
  234. noChooseValueIds.splice(index, 1);
  235. } else {
  236. // 循环去除当前已选择的 value 属性值 id
  237. state.currentPropertyArray.forEach((currentPropertyId) => {
  238. if (currentPropertyId.toString() !== '') {
  239. return;
  240. }
  241. // currentPropertyId 为空是反选 填充的
  242. let index = noChooseValueIds.indexOf(currentPropertyId);
  243. if (index >= 0) {
  244. // currentPropertyId 存在于 noChooseValueIds
  245. noChooseValueIds.splice(index, 1);
  246. }
  247. });
  248. }
  249. // 当前已选择的 property 数组
  250. let choosePropertyIds = [];
  251. if (!isChecked) {
  252. // 当前已选择的 property
  253. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  254. if (currentPropertyId !== '') {
  255. // currentPropertyId 为空是反选 填充的
  256. choosePropertyIds.push(currentValueId);
  257. }
  258. });
  259. } else {
  260. // 当前点击选择的 property
  261. choosePropertyIds = [propertyId];
  262. }
  263. for (let propertyIndex in propertyList) {
  264. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  265. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  266. continue;
  267. }
  268. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  269. for (let valueIndex in propertyList[propertyIndex]['values']) {
  270. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  271. noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true 禁用 or false 不禁用
  272. }
  273. }
  274. }
  275. // 当前所选属性下,获取所有有库存的 SKU 们
  276. function getCanUseSkuList() {
  277. let newSkus = [];
  278. for (let sku of skuList.value) {
  279. if (sku.stock <= 0) {
  280. continue;
  281. }
  282. let isOk = true;
  283. state.currentPropertyArray.forEach((propertyId) => {
  284. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  285. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  286. isOk = false;
  287. }
  288. });
  289. if (isOk) {
  290. newSkus.push(sku);
  291. }
  292. }
  293. return newSkus;
  294. }
  295. // 选择规格
  296. function onSelectSku(propertyId, valueId) {
  297. // 清空已选择
  298. let isChecked = true; // 选中 or 取消选中
  299. if (
  300. state.currentPropertyArray[propertyId] !== undefined &&
  301. state.currentPropertyArray[propertyId] === valueId
  302. ) {
  303. // 点击已被选中的,删除并填充 ''
  304. isChecked = false;
  305. state.currentPropertyArray.splice(propertyId, 1, '');
  306. } else {
  307. // 选中
  308. state.currentPropertyArray[propertyId] = valueId;
  309. }
  310. // 选中的 property 大类
  311. let choosePropertyId = [];
  312. state.currentPropertyArray.forEach((currentPropertyId) => {
  313. if (currentPropertyId !== '') {
  314. // currentPropertyId 为空是反选 填充的
  315. choosePropertyId.push(currentPropertyId);
  316. }
  317. });
  318. // 当前所选 property 下,所有可以选择的 SKU 们
  319. let newSkuList = getCanUseSkuList();
  320. // 判断所有 property 大类是否选择完成
  321. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  322. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  323. state.selectedSku = newSkuList[0];
  324. } else {
  325. state.selectedSku = {};
  326. }
  327. // 改变 property 禁用状态
  328. changeDisabled(isChecked, propertyId, valueId);
  329. }
  330. changeDisabled(false);
  331. // 默认选中第一个规格
  332. if (propertyList && propertyList.length) {
  333. propertyList.forEach((e) => {
  334. if (e.values && e.values.length) onSelectSku(e.id, e.values[0].id)
  335. })
  336. }
  337. function checkAllTotalsAreZero(obj) {
  338. for (let city in obj) {
  339. if (obj.hasOwnProperty(city) && obj[city].total !== 0) {
  340. return false
  341. }
  342. }
  343. return true
  344. }
  345. const isGetPrize = ref(false)
  346. const noCity = ref(false)
  347. const prizeAreaList = ref([])
  348. // 根据商品id活动对应的奖品区域信息
  349. const getAreaData = async () => {
  350. const id = props.goodsInfo?.id
  351. if (!id) return
  352. const params = {
  353. spuId: id,
  354. type: 'city',
  355. }
  356. const res = await PrizeApi.getPrizeAreaByGoodsId(params)
  357. const data = res?.data
  358. if (!data || !Object.keys(data).length || checkAllTotalsAreZero(data)) {
  359. noCity.value = true
  360. isGetPrize.value = false
  361. return
  362. }
  363. let list = [] // 只显示还有库存的城市
  364. Object.keys(data).forEach(cityName => {
  365. list.push({ label: cityName, total: data[cityName].total, active: false })
  366. })
  367. prizeAreaList.value = [...new Set(list)]
  368. // getPrizeLoading.value = false
  369. }
  370. let checkList = []
  371. const cityChange = (index, active) => {
  372. if (active) { // 取消
  373. checkList = checkList.filter(item => item !== index)
  374. } else { // 选中
  375. if (checkList.length > 2) return sheep.$helper.toast('最多只可选择三个')
  376. checkList.push(index)
  377. }
  378. prizeAreaList.value[index].active = !active
  379. }
  380. const isGetPrizeChange = (e) => {
  381. isGetPrize.value = Boolean(e?.detail?.value?.length)
  382. if (!isGetPrize.value) {
  383. // checkList = []
  384. // prizeAreaList.value.forEach(e => e.active = false)
  385. return
  386. }
  387. getAreaData()
  388. }
  389. </script>
  390. <style lang="scss" scoped>
  391. // 购买
  392. .getPrizeTip {
  393. font-weight: bold;
  394. text-decoration: underline;
  395. }
  396. .buy-box {
  397. padding: 10rpx 0;
  398. .add-btn {
  399. width: 356rpx;
  400. height: 80rpx;
  401. border-radius: 40rpx 0 0 40rpx;
  402. background-color: var(--ui-BG-Main-light);
  403. color: var(--ui-BG-Main);
  404. }
  405. .buy-btn {
  406. width: 356rpx;
  407. height: 80rpx;
  408. border-radius: 0 40rpx 40rpx 0;
  409. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  410. color: #fff;
  411. }
  412. .score-btn {
  413. width: 100%;
  414. margin: 0 20rpx;
  415. height: 80rpx;
  416. border-radius: 40rpx;
  417. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  418. color: #fff;
  419. }
  420. }
  421. .ss-modal-box {
  422. border-radius: 30rpx 30rpx 0 0;
  423. max-height: 1000rpx;
  424. .modal-header {
  425. position: relative;
  426. padding: 80rpx 20rpx 40rpx;
  427. .sku-image {
  428. width: 160rpx;
  429. height: 160rpx;
  430. border-radius: 10rpx;
  431. }
  432. .header-right {
  433. height: 160rpx;
  434. }
  435. .close-icon {
  436. position: absolute;
  437. top: 10rpx;
  438. right: 20rpx;
  439. font-size: 46rpx;
  440. opacity: 0.2;
  441. }
  442. .goods-title {
  443. font-size: 28rpx;
  444. font-weight: 500;
  445. line-height: 42rpx;
  446. }
  447. .score-img {
  448. width: 36rpx;
  449. height: 36rpx;
  450. margin: 0 4rpx;
  451. }
  452. .score-text {
  453. font-size: 30rpx;
  454. font-weight: 500;
  455. color: $red;
  456. font-family: OPPOSANS;
  457. }
  458. .price-text {
  459. font-size: 30rpx;
  460. font-weight: 500;
  461. color: $red;
  462. font-family: OPPOSANS;
  463. &::before {
  464. content: '¥';
  465. font-size: 30rpx;
  466. font-weight: 500;
  467. color: $red;
  468. }
  469. }
  470. .stock-text {
  471. font-size: 26rpx;
  472. color: #999999;
  473. }
  474. }
  475. .modal-content {
  476. padding: 0 20rpx;
  477. .modal-content-scroll {
  478. max-height: 600rpx;
  479. .label-text {
  480. font-size: 26rpx;
  481. font-weight: 500;
  482. }
  483. .buy-num-box {
  484. height: 100rpx;
  485. }
  486. .spec-btn {
  487. height: 60rpx;
  488. min-width: 100rpx;
  489. padding: 0 30rpx;
  490. background: #f4f4f4;
  491. border-radius: 30rpx;
  492. color: #434343;
  493. font-size: 26rpx;
  494. margin-right: 10rpx;
  495. margin-bottom: 10rpx;
  496. }
  497. .disabled-btn {
  498. font-weight: 400;
  499. color: #c6c6c6;
  500. background: #f8f8f8;
  501. }
  502. }
  503. }
  504. }
  505. .iconBox {
  506. width: fit-content;
  507. height: fit-content;
  508. padding: 2rpx 10rpx;
  509. background-color: rgb(255, 242, 241);
  510. color: #ff2621;
  511. font-size: 24rpx;
  512. margin-left: 5rpx;
  513. }
  514. .origin-price-text {
  515. font-size: 26rpx;
  516. font-weight: 400;
  517. text-decoration: line-through;
  518. color: $gray-c;
  519. font-family: OPPOSANS;
  520. &::before {
  521. content: '¥';
  522. }
  523. }
  524. </style>