s-select-sku.vue 16 KB

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