s-select-sku.vue 11 KB

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