s-select-sku.vue 13 KB

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