s-select-sku.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <template>
  2. <!-- 规格弹窗 -->
  3. <su-popup :show="show" round="10" @close="emits('close')">
  4. <!-- SKU 信息 -->
  5. <!-- {{state.selectedSku}} -->
  6. <!-- {{goodsInfo}} -->
  7. <view class="ss-modal-box bg-white ss-flex-col">
  8. <view class="modal-header ss-flex ss-col-center">
  9. <view class="header-left ss-m-r-30">
  10. <image class="sku-image" :src="state.selectedSku.picUrl || goodsInfo.picUrl" mode="aspectFill" />
  11. </view>
  12. <view class="header-right ss-flex-col ss-row-between ss-flex-1">
  13. <view class="goods-title ss-line-2">{{ goodsInfo.name }}</view>
  14. <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
  15. <view class="ss-flex">
  16. <view class="price-text">
  17. <image src="@/static/icon/points.png" v-if="goodsInfo.spuPayType == 2"
  18. style="width: 30rpx; height: 30rpx"></image>
  19. <text v-if="goodsInfo.highPrecision">
  20. {{ fen2yuan6(state.selectedSku.highPrecisionPrice || goodsInfo.highPrecisionPrice) }}
  21. </text>
  22. <text v-else>
  23. {{ fen2yuan(state.selectedSku.price || goodsInfo.price) }}
  24. </text>
  25. <!-- {{ fen2yuan(state.selectedSku.price || goodsInfo.price) }} -->
  26. </view>
  27. <view class="origin-price-text ss-m-l-10" v-if="
  28. state.selectedSku.promotionFee >= 0 ||
  29. goodsInfo.promotionFee >= 0
  30. ">
  31. 积分:{{
  32. fen2yuan(
  33. state.selectedSku.promotionFee || goodsInfo.promotionFee,
  34. )
  35. }}
  36. </view>
  37. </view>
  38. <view class="stock-text ss-m-l-20">
  39. {{ formatStock('exact', state.selectedSku.stock || goodsInfo.stock) }}
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. <!-- 属性选择 -->
  45. <view class="modal-content ss-flex-1">
  46. <scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
  47. <view class="sku-item ss-m-b-20" v-for="property in propertyList" :key="property.id">
  48. <view class="label-text ss-m-b-20">{{ property.name }}</view>
  49. <view class="ss-flex ss-col-center ss-flex-wrap">
  50. <button class="ss-reset-button spec-btn" v-for="value in property.values" :class="[
  51. {
  52. 'ui-BG-Main-Gradient':
  53. state.currentPropertyArray[property.id] === value.id,
  54. },
  55. {
  56. 'disabled-btn': value.disabled === true,
  57. },
  58. ]" :key="value.id" :disabled="value.disabled === true" @tap="onSelectSku(property.id, value.id)">
  59. {{ value.name }}
  60. </button>
  61. </view>
  62. </view>
  63. <view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40"
  64. v-if="goodsInfo.spuPayType == 2">
  65. <view class="label-text">购买金额 (当前可用积分:{{
  66. points2point(userWallet.integralDO.currentQuota)
  67. }})</view>
  68. <view class="ss-flex ss-col-center">
  69. <image src="@/static/icon/points.png" v-if="goodsInfo.spuPayType == 2"
  70. style="width: 30rpx; height: 30rpx"></image>
  71. <input v-model="state.selectedSku.use_points" class="uni-input input-points"
  72. style="width: 100rpx; text-align: center" type="number" placeholder="0"
  73. @input="inputPoints" :disabled="!state.selectedSku.id" />
  74. </view>
  75. </view>
  76. <view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40" v-else>
  77. <view class="label-text">购买数量</view>
  78. <su-number-box :min="1" :max="state.selectedSku.stock" :step="1"
  79. v-model="state.selectedSku.goods_num" @change="onNumberChange($event)" />
  80. </view>
  81. </scroll-view>
  82. </view>
  83. <!-- 操作区 -->
  84. <view class="modal-footer border-top">
  85. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center"
  86. v-if="goodsInfo.spuType && goodsInfo.spuPayType == 1">
  87. <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="onAddCart">加入购物车</button>
  88. <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">立即购买</button>
  89. </view>
  90. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-else>
  91. <button class="ss-reset-button disabled-btn one-buy-btn ui-Shadow-Main " @tap="onBuy">
  92. <template v-if="goodsInfo.spuPayType != 1 && !goodsInfo.spuType">
  93. 立即兑换
  94. </template>
  95. <template v-else-if="!goodsInfo.spuType">
  96. 立即购买
  97. </template>
  98. <template v-else>
  99. 立即兑换
  100. </template>
  101. </button>
  102. </view>
  103. </view>
  104. </view>
  105. </su-popup>
  106. </template>
  107. <script setup>
  108. import {
  109. computed,
  110. reactive,
  111. watch,
  112. onMounted,
  113. nextTick
  114. } from 'vue';
  115. import sheep from '@/sheep';
  116. import {
  117. formatStock,
  118. convertProductPropertyList,
  119. fen2yuan,
  120. points2point,
  121. fen2yuan6
  122. } from '@/sheep/hooks/useGoods';
  123. const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
  124. const props = defineProps({
  125. goodsInfo: {
  126. type: Object,
  127. default () {},
  128. },
  129. show: {
  130. type: Boolean,
  131. default: false,
  132. },
  133. });
  134. const userWallet = computed(() => sheep.$store('user').userWallet);
  135. const state = reactive({
  136. selectedSku: {}, // 选中的 SKU
  137. currentPropertyArray: [], // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  138. });
  139. const propertyList = convertProductPropertyList(props.goodsInfo.skus);
  140. // SKU 列表
  141. const skuList = computed(() => {
  142. let skuPrices = props.goodsInfo.skus;
  143. for (let price of skuPrices) {
  144. price.value_id_array = price.properties.map((item) => item.valueId);
  145. }
  146. return skuPrices;
  147. });
  148. function calcNums(){
  149. if(props.goodsInfo.highPrecision){
  150. // console.log(state.selectedSku.use_points +"/" + (state.selectedSku.highPrecisionPrice / 100))
  151. state.selectedSku.goods_num = parseInt(state.selectedSku.use_points / (state.selectedSku.highPrecisionPrice / 100) );
  152. }else{
  153. state.selectedSku.goods_num = parseInt(state.selectedSku.use_points / (fen2yuan(state.selectedSku.price)) );
  154. }
  155. // console.log("计算后的数量是",state.selectedSku.goods_num)
  156. }
  157. function inputPoints(e) {
  158. const points = e.detail.value;
  159. console.log(typeof points);
  160. if(points == ''){
  161. return false
  162. }
  163. // 用户可用的积分
  164. const userCanUsePoints = parseFloat(points2point(userWallet.value.integralDO.currentQuota));
  165. calcNums()
  166. nextTick(()=>{
  167. state.selectedSku.use_points = parseInt(points);
  168. calcNums()
  169. })
  170. if (points <= 0) {
  171. sheep.$helper.toast('最低使用1积分');
  172. if (userCanUsePoints>=1) {
  173. nextTick(() => {
  174. state.selectedSku.use_points = 1;
  175. calcNums()
  176. })
  177. }else{
  178. nextTick(() => {
  179. state.selectedSku.use_points = 0;
  180. calcNums()
  181. })
  182. }
  183. }
  184. if (points > userCanUsePoints) {
  185. sheep.$helper.toast('可用积分不足');
  186. // console.log('输入的积分大于可用积分了', points, userCanUsePoints);
  187. nextTick(() => {
  188. state.selectedSku.use_points = parseInt(userCanUsePoints);
  189. // 重新计算 对应的商品数量
  190. calcNums()
  191. });
  192. }
  193. }
  194. watch(
  195. () => state.selectedSku,
  196. (newVal) => {
  197. emits('change', newVal);
  198. }, {
  199. immediate: true, // 立即执行
  200. deep: true, // 深度监听
  201. },
  202. );
  203. // 输入框改变数量
  204. function onNumberChange(e) {
  205. if (e === 0) return;
  206. if (state.selectedSku.goods_num === e) return;
  207. state.selectedSku.goods_num = e;
  208. }
  209. // 加入购物车
  210. function onAddCart() {
  211. if (state.selectedSku.id <= 0) {
  212. sheep.$helper.toast('请选择规格');
  213. return;
  214. }
  215. if (state.selectedSku.stock <= 0) {
  216. sheep.$helper.toast('库存不足');
  217. return;
  218. }
  219. emits('addCart', state.selectedSku);
  220. }
  221. // 立即购买
  222. function onBuy() {
  223. if (props.goodsInfo.spuPayType == 2) {
  224. if (state.selectedSku.goods_num < 1 || !state.selectedSku.use_points) {
  225. sheep.$helper.toast('输入金额少于可购买数量');
  226. return;
  227. }
  228. // 下单时检测如果库存不足
  229. if (state.selectedSku.goods_num > state.selectedSku.stock) {
  230. state.selectedSku.use_points = (state.selectedSku.stock * fen2yuan(state.selectedSku.price)).toFixed(2)
  231. state.selectedSku.goods_num = state.selectedSku.stock
  232. }
  233. }
  234. if (state.selectedSku.id <= 0) {
  235. sheep.$helper.toast('请选择规格');
  236. return;
  237. }
  238. if (state.selectedSku.stock <= 0) {
  239. sheep.$helper.toast('库存不足');
  240. return;
  241. }
  242. emits('buy', state.selectedSku);
  243. }
  244. // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
  245. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  246. let newSkus = []; // 所有可以选择的 sku 数组
  247. if (isChecked) {
  248. // 情况一:选中 property
  249. // 获得当前点击选中 property 的、所有可用 SKU
  250. for (let price of skuList.value) {
  251. if (price.stock <= 0) {
  252. continue;
  253. }
  254. if (price.value_id_array.indexOf(valueId) >= 0) {
  255. newSkus.push(price);
  256. }
  257. }
  258. } else {
  259. // 情况二:取消选中 property
  260. // 当前所选 property 下,所有可以选择的 SKU
  261. newSkus = getCanUseSkuList();
  262. }
  263. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  264. let noChooseValueIds = [];
  265. for (let price of newSkus) {
  266. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  267. }
  268. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  269. if (isChecked) {
  270. // 去除当前选中的 value 属性值 id
  271. let index = noChooseValueIds.indexOf(valueId);
  272. noChooseValueIds.splice(index, 1);
  273. } else {
  274. // 循环去除当前已选择的 value 属性值 id
  275. state.currentPropertyArray.forEach((currentPropertyId) => {
  276. if (currentPropertyId.toString() !== '') {
  277. return;
  278. }
  279. // currentPropertyId 为空是反选 填充的
  280. let index = noChooseValueIds.indexOf(currentPropertyId);
  281. if (index >= 0) {
  282. // currentPropertyId 存在于 noChooseValueIds
  283. noChooseValueIds.splice(index, 1);
  284. }
  285. });
  286. }
  287. // 当前已选择的 property 数组
  288. let choosePropertyIds = [];
  289. if (!isChecked) {
  290. // 当前已选择的 property
  291. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  292. if (currentPropertyId !== '') {
  293. // currentPropertyId 为空是反选 填充的
  294. choosePropertyIds.push(currentValueId);
  295. }
  296. });
  297. } else {
  298. // 当前点击选择的 property
  299. choosePropertyIds = [propertyId];
  300. }
  301. for (let propertyIndex in propertyList) {
  302. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  303. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  304. continue;
  305. }
  306. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  307. for (let valueIndex in propertyList[propertyIndex]['values']) {
  308. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  309. noChooseValueIds.indexOf(
  310. propertyList[propertyIndex]['values'][valueIndex]['id'],
  311. ) < 0; // true 禁用 or false 不禁用
  312. }
  313. }
  314. }
  315. // 当前所选属性下,获取所有有库存的 SKU 们
  316. function getCanUseSkuList() {
  317. let newSkus = [];
  318. for (let sku of skuList.value) {
  319. if (sku.stock <= 0) {
  320. continue;
  321. }
  322. let isOk = true;
  323. state.currentPropertyArray.forEach((propertyId) => {
  324. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  325. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  326. isOk = false;
  327. }
  328. });
  329. if (isOk) {
  330. newSkus.push(sku);
  331. }
  332. }
  333. return newSkus;
  334. }
  335. // 选择规格
  336. function onSelectSku(propertyId, valueId) {
  337. // console.log(propertyId,valueId)
  338. // 清空已输入金额
  339. state.selectedSku.use_points = 0;
  340. // 清空已选择
  341. let isChecked = true; // 选中 or 取消选中
  342. if (
  343. state.currentPropertyArray[propertyId] !== undefined &&
  344. state.currentPropertyArray[propertyId] === valueId
  345. ) {
  346. // 点击已被选中的,删除并填充 ''
  347. isChecked = false;
  348. state.currentPropertyArray.splice(propertyId, 1, '');
  349. } else {
  350. // 选中
  351. state.currentPropertyArray[propertyId] = valueId;
  352. }
  353. // 选中的 property 大类
  354. let choosePropertyId = [];
  355. state.currentPropertyArray.forEach((currentPropertyId) => {
  356. if (currentPropertyId !== '') {
  357. // currentPropertyId 为空是反选 填充的
  358. choosePropertyId.push(currentPropertyId);
  359. }
  360. });
  361. // 当前所选 property 下,所有可以选择的 SKU 们
  362. let newSkuList = getCanUseSkuList();
  363. // 判断所有 property 大类是否选择完成
  364. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  365. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  366. state.selectedSku = newSkuList[0];
  367. } else {
  368. state.selectedSku = {};
  369. }
  370. // 改变 property 禁用状态
  371. changeDisabled(isChecked, propertyId, valueId);
  372. }
  373. changeDisabled(false);
  374. onMounted(() => {
  375. // 如果商品的属性只有一条,则默认选中
  376. if (propertyList.length == 1 && propertyList[0].values.length == 1) {
  377. onSelectSku(0, 0);
  378. }
  379. });
  380. // TODO 非繁人:待讨论的优化点:1)单规格,要不要默认选中;2)默认要不要选中第一个规格
  381. </script>
  382. <style lang="scss" scoped>
  383. .origin-price-text {
  384. font-size: 22rpx;
  385. font-weight: 400;
  386. color: $gray-c;
  387. font-family: OPPOSANS;
  388. background: #ffca3e;
  389. padding: 2px 8px;
  390. border-radius: 4px;
  391. display: inline-block;
  392. color: #597533;
  393. }
  394. // 购买
  395. .buy-box {
  396. padding: 10rpx 0;
  397. .add-btn {
  398. width: 356rpx;
  399. height: 80rpx;
  400. border-radius: 40rpx 0 0 40rpx;
  401. background-color: var(--ui-BG-Main-light);
  402. color: var(--ui-BG-Main);
  403. }
  404. .buy-btn {
  405. width: 356rpx;
  406. height: 80rpx;
  407. border-radius: 0 40rpx 40rpx 0;
  408. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  409. color: #fff;
  410. }
  411. .one-buy-btn {
  412. width: 98%;
  413. height: 80rpx;
  414. border-radius: 40rpx;
  415. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  416. color: #fff;
  417. }
  418. .score-btn {
  419. width: 100%;
  420. margin: 0 20rpx;
  421. height: 80rpx;
  422. border-radius: 40rpx;
  423. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  424. color: #fff;
  425. }
  426. }
  427. .ss-modal-box {
  428. border-radius: 30rpx 30rpx 0 0;
  429. max-height: 1000rpx;
  430. .modal-header {
  431. position: relative;
  432. padding: 80rpx 20rpx 40rpx;
  433. .sku-image {
  434. width: 160rpx;
  435. height: 160rpx;
  436. border-radius: 10rpx;
  437. }
  438. .header-right {
  439. height: 160rpx;
  440. }
  441. .close-icon {
  442. position: absolute;
  443. top: 10rpx;
  444. right: 20rpx;
  445. font-size: 46rpx;
  446. opacity: 0.2;
  447. }
  448. .goods-title {
  449. font-size: 28rpx;
  450. font-weight: 500;
  451. line-height: 42rpx;
  452. }
  453. .score-img {
  454. width: 36rpx;
  455. height: 36rpx;
  456. margin: 0 4rpx;
  457. }
  458. .score-text {
  459. font-size: 30rpx;
  460. font-weight: 500;
  461. color: $red;
  462. font-family: OPPOSANS;
  463. }
  464. .price-text {
  465. font-size: 30rpx;
  466. font-weight: 500;
  467. color: $red;
  468. font-family: OPPOSANS;
  469. &::before {
  470. // content: '¥';
  471. // font-size: 30rpx;
  472. // font-weight: 500;
  473. // color: $red;
  474. }
  475. }
  476. .stock-text {
  477. font-size: 26rpx;
  478. color: #999999;
  479. }
  480. }
  481. .modal-content {
  482. padding: 0 20rpx;
  483. .modal-content-scroll {
  484. max-height: 600rpx;
  485. .label-text {
  486. font-size: 26rpx;
  487. font-weight: 500;
  488. }
  489. .buy-num-box {
  490. height: 100rpx;
  491. }
  492. .spec-btn {
  493. height: 60rpx;
  494. min-width: 100rpx;
  495. padding: 0 30rpx;
  496. background: #f4f4f4;
  497. border-radius: 30rpx;
  498. color: #434343;
  499. font-size: 26rpx;
  500. margin-right: 10rpx;
  501. margin-bottom: 10rpx;
  502. }
  503. .disabled-btn {
  504. font-weight: 400;
  505. color: #c6c6c6;
  506. background: #f8f8f8;
  507. }
  508. }
  509. }
  510. }
  511. </style>