s-select-sku.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <!-- SKU 信息 -->
  3. <div>
  4. <!-- 属性选择 -->
  5. <div class="d-flex mb-5" v-for="property in propertyList" :key="property.id">
  6. <span class="parameterColor mb-1">{{ property.name }}</span>:
  7. <span style="flex: 1;">
  8. <v-chip
  9. class="spec-btn mb-1 mr-3"
  10. :class="[
  11. state.currentPropertyArray[property.id] === value.id ? 'ui-BG-Main-Gradient' : 'ui-BG-Main-Normal',
  12. { 'disabled-btn': value.disabled === true, },
  13. ]"
  14. v-for="value in property.values"
  15. :key="value.id"
  16. label
  17. density="comfortable"
  18. color="#777"
  19. variant="outlined"
  20. :disabled="value.disabled === true"
  21. @click="onSelectSku(property.id, value.id)"
  22. >
  23. {{ value.name }}
  24. </v-chip>
  25. </span>
  26. </div>
  27. <!-- 购买数量- 库存 -->
  28. <div class="modal-content">
  29. <!-- 只展示库存 -->
  30. <div v-if="props.goodsType === 99" class="mb-7">
  31. <div class="parameterColor mb-3"><span class="l-s-10">库存</span>:{{ totalStock }}</div>
  32. <!-- 我要房券 -->
  33. <div v-if="showPrize">
  34. <v-checkbox-btn
  35. v-model="isGetPrize"
  36. color="primary"
  37. label="我要房券"
  38. class="ml-n3"
  39. :disabled="false"
  40. hide-details
  41. @update:modelValue="isGetPrizeChange"
  42. ></v-checkbox-btn>
  43. <div v-if="showPrize && noCity" style="color: gray;" class="ma-3">房券已被抢完啦</div>
  44. <div v-show="isGetPrize" class="ml-7">
  45. <div class="getPrizeTip">请勾选一到三个地点作为期望获赠房券的所在地,系统将随机抽取一张</div>
  46. <v-card :loading="getPrizeLoading" elevation="0" class="prizeArea">
  47. <v-chip-group v-model="choose" column multiple :max="3" mandatory>
  48. <v-chip
  49. v-for="(item, index) of prizeAreaList"
  50. :key="item+index"
  51. color="primary"
  52. :text="item.label"
  53. :value="item.label"
  54. class="mr-3 mb-3"
  55. label
  56. :disabled="item.total <= 0"
  57. filter
  58. ></v-chip>
  59. </v-chip-group>
  60. </v-card>
  61. </div>
  62. </div>
  63. </div>
  64. <div v-else class="d-flex align-center mb-10">
  65. <div class="buyCount mr-3">
  66. <span class="parameterColor"><span class="l-s-10">数量</span>:</span>
  67. <su-number-box
  68. :min="1"
  69. :max="state.selectedSku.stock"
  70. :step="1"
  71. ref="selectSkuRef"
  72. v-model="state.selectedSku.goods_num"
  73. @change="onNumberChange($event)"
  74. />
  75. </div>
  76. <!-- 数量可加减 库存 -->
  77. <span style="color: #b7b7b7; font-size: 14px;">库存:{{ totalStock }}</span>
  78. </div>
  79. </div>
  80. <!-- 操作区 -->
  81. <div>
  82. <v-btn class="mr-3" color="primary" width="200" @click="onBuy">立即购买</v-btn>
  83. <v-btn v-if="props.goodsType !== 99" color="warning" width="200" @click="onAddCart">加入购物车</v-btn>
  84. </div>
  85. </div>
  86. </template>
  87. <script setup>
  88. defineOptions({name: 'wares-s-select-sku'})
  89. import Snackbar from '@/plugins/snackbar'
  90. import suNumberBox from '@/components/FormUI/su-number-box/su-number-box.vue'
  91. import { computed, reactive, watch, ref } from 'vue'
  92. import { convertProductPropertyList } from '@/views/mall/utils'
  93. import { getPrizeAreaByGoodsId } from '@/api/mall/prize'
  94. // import { toNumber } from 'lodash-es'
  95. const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
  96. const props = defineProps({
  97. goodsInfo: {
  98. type: Object,
  99. default() {},
  100. },
  101. goodsType: {
  102. type: Number,
  103. default: 0,
  104. },
  105. showPrize: { // 是否可以抽取房券
  106. type: Boolean,
  107. default: false,
  108. },
  109. });
  110. const isGetPrize = ref(false)
  111. const totalStock = ref(props.goodsInfo?.stock-0 || 0)
  112. const state = reactive({
  113. selectedSku: {}, // 选中的 SKU
  114. currentPropertyArray: [], // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  115. });
  116. const propertyList = convertProductPropertyList(props.goodsInfo.skus);
  117. // SKU 列表
  118. const skuList = computed(() => {
  119. let skuPrices = props.goodsInfo.skus;
  120. for (let price of skuPrices) {
  121. price.value_id_array = price.properties.map((item) => item.valueId);
  122. }
  123. return skuPrices;
  124. });
  125. watch(
  126. () => state.selectedSku,
  127. (newVal) => {
  128. if (newVal?.stock) totalStock.value = newVal.stock
  129. emits('change', newVal);
  130. },
  131. {
  132. immediate: true, // 立即执行
  133. deep: true, // 深度监听
  134. },
  135. );
  136. // 输入框改变数量
  137. function onNumberChange(e) {
  138. if (e === 0) return;
  139. if (state.selectedSku.goods_num === e) return;
  140. state.selectedSku.goods_num = e;
  141. }
  142. // 加入购物车
  143. function onAddCart() {
  144. if (props.goodsInfo.type !== '0') return Snackbar.warning('虚拟商品不能加入购物车')
  145. if (state.selectedSku.id <= 0) {
  146. Snackbar.warning('请选择商品规格')
  147. return;
  148. }
  149. if (state.selectedSku.stock <= 0) {
  150. Snackbar.warning('库存不足')
  151. return;
  152. }
  153. if (isGetPrize.value && !choose.value?.length) {
  154. Snackbar.warning('请勾选期望获赠房券的所在地!')
  155. return;
  156. }
  157. emits('addCart', state.selectedSku);
  158. }
  159. // 立即购买
  160. function onBuy() {
  161. if (!state?.selectedSku?.id || state.selectedSku.id <= 0) {
  162. Snackbar.warning('请选择商品规格')
  163. return;
  164. }
  165. if (!state?.selectedSku?.stock || state.selectedSku.stock <= 0) {
  166. Snackbar.warning('库存不足')
  167. return;
  168. }
  169. if (isGetPrize.value && !choose.value?.length) return Snackbar.warning('请勾选期望获赠房券的所在地!')
  170. emits('buy', { ...state.selectedSku, city: isGetPrize.value ? choose.value : [] });
  171. }
  172. // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
  173. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  174. let newSkus = []; // 所有可以选择的 sku 数组
  175. if (isChecked) {
  176. // 情况一:选中 property
  177. // 获得当前点击选中 property 的、所有可用 SKU
  178. for (let price of skuList.value) {
  179. if (price.stock <= 0) {
  180. continue;
  181. }
  182. if (price.value_id_array.indexOf(valueId) >= 0) {
  183. newSkus.push(price);
  184. }
  185. }
  186. } else {
  187. // 情况二:取消选中 property
  188. // 当前所选 property 下,所有可以选择的 SKU
  189. newSkus = getCanUseSkuList();
  190. }
  191. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  192. let noChooseValueIds = [];
  193. for (let price of newSkus) {
  194. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  195. }
  196. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  197. if (isChecked) {
  198. // 去除当前选中的 value 属性值 id
  199. let index = noChooseValueIds.indexOf(valueId);
  200. noChooseValueIds.splice(index, 1);
  201. } else {
  202. // 循环去除当前已选择的 value 属性值 id
  203. state.currentPropertyArray.forEach((currentPropertyId) => {
  204. if (currentPropertyId.toString() !== '') {
  205. return;
  206. }
  207. // currentPropertyId 为空是反选 填充的
  208. let index = noChooseValueIds.indexOf(currentPropertyId);
  209. if (index >= 0) {
  210. // currentPropertyId 存在于 noChooseValueIds
  211. noChooseValueIds.splice(index, 1);
  212. }
  213. });
  214. }
  215. // 当前已选择的 property 数组
  216. let choosePropertyIds = [];
  217. if (!isChecked) {
  218. // 当前已选择的 property
  219. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  220. if (currentPropertyId !== '') {
  221. // currentPropertyId 为空是反选 填充的
  222. choosePropertyIds.push(currentValueId);
  223. }
  224. });
  225. } else {
  226. // 当前点击选择的 property
  227. choosePropertyIds = [propertyId];
  228. }
  229. for (let propertyIndex in propertyList) {
  230. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  231. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  232. continue;
  233. }
  234. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  235. for (let valueIndex in propertyList[propertyIndex]['values']) {
  236. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  237. noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true 禁用 or false 不禁用
  238. }
  239. }
  240. }
  241. // 当前所选属性下,获取所有有库存的 SKU 们
  242. function getCanUseSkuList() {
  243. let newSkus = [];
  244. for (let sku of skuList.value) {
  245. if (sku.stock <= 0) {
  246. continue;
  247. }
  248. let isOk = true;
  249. state.currentPropertyArray.forEach((propertyId) => {
  250. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  251. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  252. isOk = false;
  253. }
  254. });
  255. if (isOk) {
  256. newSkus.push(sku);
  257. }
  258. }
  259. return newSkus;
  260. }
  261. // 选择规格
  262. function onSelectSku(propertyId, valueId) {
  263. // 清空已选择
  264. let isChecked = true; // 选中 or 取消选中
  265. if (
  266. state.currentPropertyArray[propertyId] !== undefined &&
  267. state.currentPropertyArray[propertyId] === valueId
  268. ) {
  269. // 点击已被选中的,删除并填充 ''
  270. isChecked = false;
  271. state.currentPropertyArray.splice(propertyId, 1, '');
  272. } else {
  273. // 选中
  274. state.currentPropertyArray[propertyId] = valueId;
  275. }
  276. // 选中的 property 大类
  277. let choosePropertyId = [];
  278. state.currentPropertyArray.forEach((currentPropertyId) => {
  279. if (currentPropertyId !== '') {
  280. // currentPropertyId 为空是反选 填充的
  281. choosePropertyId.push(currentPropertyId);
  282. }
  283. });
  284. // 当前所选 property 下,所有可以选择的 SKU 们
  285. let newSkuList = getCanUseSkuList();
  286. // 判断所有 property 大类是否选择完成
  287. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  288. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  289. state.selectedSku = newSkuList[0];
  290. } else {
  291. state.selectedSku = {};
  292. }
  293. // 改变 property 禁用状态
  294. changeDisabled(isChecked, propertyId, valueId);
  295. }
  296. changeDisabled(false);
  297. // 默认选中第一个规格
  298. if (propertyList && propertyList.length) {
  299. propertyList.forEach((e) => {
  300. if (e.values && e.values.length) onSelectSku(e.id, e.values[0].id)
  301. })
  302. }
  303. const choose = ref([])
  304. const noCity = ref(false)
  305. const getPrizeLoading = ref(false)
  306. const prizeAreaList = ref([])
  307. function checkAllTotalsAreZero(obj) {
  308. for (let city in obj) {
  309. if (obj.hasOwnProperty(city) && obj[city].total !== 0) {
  310. return false
  311. }
  312. }
  313. return true
  314. }
  315. // 根据商品id活动对应的奖品区域信息
  316. const getAreaData = async () => {
  317. const id = props.goodsInfo?.id
  318. if (!id) return
  319. const params = {
  320. spuId: id,
  321. type: 'city',
  322. }
  323. getPrizeLoading.value = true
  324. const data = await getPrizeAreaByGoodsId(params)
  325. if (!data || !Object.keys(data).length || checkAllTotalsAreZero(data)) {
  326. noCity.value = true
  327. isGetPrize.value = false
  328. return
  329. }
  330. let list = [] // 只显示还有库存的城市
  331. Object.keys(data).forEach(cityName => {
  332. // if (data[cityName].list?.length) {
  333. // let total = 0
  334. // data[cityName].list.forEach(i => { if (i.total) total+= toNumber(i.total) })
  335. // if (total) list.push(cityName)
  336. // }
  337. list.push({ label: cityName, total: data[cityName].total })
  338. })
  339. prizeAreaList.value = [...new Set(list)]
  340. getPrizeLoading.value = false
  341. }
  342. const isGetPrizeChange = (bool) => {
  343. if (!bool) {
  344. return
  345. }
  346. getAreaData()
  347. }
  348. </script>
  349. <style lang="scss" scoped>
  350. // 主题色渐变,横向
  351. .ui-BG-Main-Gradient {
  352. // background: linear-gradient(90deg, #ff3000, #ff300099);
  353. // background: var(--v-primary-base);
  354. border: 1px solid #ff5000;
  355. color: #ff5000 !important;
  356. }
  357. .ui-BG-Main-Normal {
  358. border: 1px solid #dadde0;
  359. color: #11192d !important;
  360. }
  361. .disabled-btn {
  362. color: #c6c6c6;
  363. background: #f8f8f8;
  364. }
  365. .parameterColor {
  366. color: #7a7a7a;
  367. }
  368. input::-webkit-outer-spin-button, input::-webkit-inner-spin-button{
  369. -webkit-appearance: none !important;
  370. margin: 0;
  371. }
  372. .inputItem {
  373. width: 70px; border: 1px solid #eee; padding: 0 5px; text-align: center;
  374. }
  375. .buyCount {
  376. display: flex;
  377. align-items: center;
  378. }
  379. .ss-modal-box {
  380. border-radius: 30px 30px 0 0;
  381. max-height: 1000px;
  382. .modal-header {
  383. position: relative;
  384. padding: 80px 20px 40px;
  385. .sku-image {
  386. width: 160px;
  387. height: 160px;
  388. border-radius: 10px;
  389. }
  390. .header-right {
  391. height: 160px;
  392. }
  393. .close-icon {
  394. position: absolute;
  395. top: 10px;
  396. right: 20px;
  397. font-size: 46px;
  398. opacity: 0.2;
  399. }
  400. .goods-title {
  401. font-size: 28px;
  402. font-weight: 500;
  403. line-height: 42px;
  404. }
  405. .score-img {
  406. width: 36px;
  407. height: 36px;
  408. margin: 0 4px;
  409. }
  410. .stock-text {
  411. font-size: 26px;
  412. color: #999999;
  413. }
  414. }
  415. .modal-content {
  416. padding: 0 20px;
  417. .modal-content-scroll {
  418. max-height: 600px;
  419. .label-text {
  420. font-size: 26px;
  421. font-weight: 500;
  422. }
  423. .buy-num-box {
  424. height: 100px;
  425. }
  426. .spec-btn {
  427. height: 60px;
  428. min-width: 100px;
  429. padding: 0 30px;
  430. background: #f4f4f4;
  431. border-radius: 30px;
  432. color: #434343;
  433. font-size: 26px;
  434. margin-right: 10px;
  435. margin-bottom: 10px;
  436. }
  437. .disabled-btn {
  438. // font-weight: 400;
  439. color: #c6c6c6;
  440. background: #f8f8f8;
  441. }
  442. }
  443. }
  444. }
  445. .iconBox {
  446. width: fit-content;
  447. height: fit-content;
  448. padding: 2px 10px;
  449. background-color: rgb(255, 242, 241);
  450. color: #ff2621;
  451. font-size: 24px;
  452. margin-left: 5px;
  453. }
  454. .origin-price-text {
  455. font-size: 26px;
  456. font-weight: 400;
  457. text-decoration: line-through;
  458. color: gray;
  459. font-family: OPPOSANS;
  460. &::before {
  461. content: '¥';
  462. }
  463. }
  464. .getPrizeTip {
  465. font-size: 13px;
  466. font-weight: bold;
  467. text-decoration: underline;
  468. }
  469. .prizeArea {
  470. display: flex;
  471. flex-wrap: wrap;
  472. }
  473. </style>