s-select-sku.vue 17 KB

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