s-select-sku.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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("输入的积分", points)
  160. const userCanUsePoints = parseFloat(points2point(userWallet.value.integralDO.currentQuota));
  161. calcNums()
  162. // console.log("1", state.selectedSku.use_points, state.selectedSku.price)
  163. // console.log("下单数目", state.selectedSku.goods_num)
  164. nextTick(()=>{
  165. state.selectedSku.use_points = points.replace(/^(-)*(\d+).(\d\d).*$/, "$1$2.$3");
  166. calcNums()
  167. })
  168. if (points <= 0) {
  169. sheep.$helper.toast('最低使用1积分');
  170. nextTick(() => {
  171. state.selectedSku.use_points = 1;
  172. calcNums()
  173. })
  174. }
  175. if (points > userCanUsePoints) {
  176. sheep.$helper.toast('可用积分不足');
  177. console.log('输入的积分大于可用积分了', points, userCanUsePoints);
  178. nextTick(() => {
  179. state.selectedSku.use_points = userCanUsePoints;
  180. // 重新计算 对应的商品数量
  181. calcNums()
  182. });
  183. }
  184. }
  185. watch(
  186. () => state.selectedSku,
  187. (newVal) => {
  188. emits('change', newVal);
  189. }, {
  190. immediate: true, // 立即执行
  191. deep: true, // 深度监听
  192. },
  193. );
  194. // 输入框改变数量
  195. function onNumberChange(e) {
  196. if (e === 0) return;
  197. if (state.selectedSku.goods_num === e) return;
  198. state.selectedSku.goods_num = e;
  199. }
  200. // 加入购物车
  201. function onAddCart() {
  202. if (state.selectedSku.id <= 0) {
  203. sheep.$helper.toast('请选择规格');
  204. return;
  205. }
  206. if (state.selectedSku.stock <= 0) {
  207. sheep.$helper.toast('库存不足');
  208. return;
  209. }
  210. emits('addCart', state.selectedSku);
  211. }
  212. // 立即购买
  213. function onBuy() {
  214. if (props.goodsInfo.spuPayType == 2) {
  215. if (state.selectedSku.goods_num < 1 || !state.selectedSku.use_points) {
  216. sheep.$helper.toast('输入金额少于可购买数量');
  217. return;
  218. }
  219. // 下单时检测如果
  220. if (state.selectedSku.goods_num > state.selectedSku.stock) {
  221. state.selectedSku.use_points = (state.selectedSku.stock * fen2yuan(state.selectedSku.price)).toFixed(2)
  222. state.selectedSku.goods_num = state.selectedSku.stock
  223. }
  224. }
  225. if (state.selectedSku.id <= 0) {
  226. sheep.$helper.toast('请选择规格');
  227. return;
  228. }
  229. if (state.selectedSku.stock <= 0) {
  230. sheep.$helper.toast('库存不足');
  231. return;
  232. }
  233. emits('buy', state.selectedSku);
  234. }
  235. // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
  236. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  237. let newSkus = []; // 所有可以选择的 sku 数组
  238. if (isChecked) {
  239. // 情况一:选中 property
  240. // 获得当前点击选中 property 的、所有可用 SKU
  241. for (let price of skuList.value) {
  242. if (price.stock <= 0) {
  243. continue;
  244. }
  245. if (price.value_id_array.indexOf(valueId) >= 0) {
  246. newSkus.push(price);
  247. }
  248. }
  249. } else {
  250. // 情况二:取消选中 property
  251. // 当前所选 property 下,所有可以选择的 SKU
  252. newSkus = getCanUseSkuList();
  253. }
  254. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  255. let noChooseValueIds = [];
  256. for (let price of newSkus) {
  257. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  258. }
  259. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  260. if (isChecked) {
  261. // 去除当前选中的 value 属性值 id
  262. let index = noChooseValueIds.indexOf(valueId);
  263. noChooseValueIds.splice(index, 1);
  264. } else {
  265. // 循环去除当前已选择的 value 属性值 id
  266. state.currentPropertyArray.forEach((currentPropertyId) => {
  267. if (currentPropertyId.toString() !== '') {
  268. return;
  269. }
  270. // currentPropertyId 为空是反选 填充的
  271. let index = noChooseValueIds.indexOf(currentPropertyId);
  272. if (index >= 0) {
  273. // currentPropertyId 存在于 noChooseValueIds
  274. noChooseValueIds.splice(index, 1);
  275. }
  276. });
  277. }
  278. // 当前已选择的 property 数组
  279. let choosePropertyIds = [];
  280. if (!isChecked) {
  281. // 当前已选择的 property
  282. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  283. if (currentPropertyId !== '') {
  284. // currentPropertyId 为空是反选 填充的
  285. choosePropertyIds.push(currentValueId);
  286. }
  287. });
  288. } else {
  289. // 当前点击选择的 property
  290. choosePropertyIds = [propertyId];
  291. }
  292. for (let propertyIndex in propertyList) {
  293. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  294. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  295. continue;
  296. }
  297. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  298. for (let valueIndex in propertyList[propertyIndex]['values']) {
  299. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  300. noChooseValueIds.indexOf(
  301. propertyList[propertyIndex]['values'][valueIndex]['id'],
  302. ) < 0; // true 禁用 or false 不禁用
  303. }
  304. }
  305. }
  306. // 当前所选属性下,获取所有有库存的 SKU 们
  307. function getCanUseSkuList() {
  308. let newSkus = [];
  309. for (let sku of skuList.value) {
  310. if (sku.stock <= 0) {
  311. continue;
  312. }
  313. let isOk = true;
  314. state.currentPropertyArray.forEach((propertyId) => {
  315. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  316. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  317. isOk = false;
  318. }
  319. });
  320. if (isOk) {
  321. newSkus.push(sku);
  322. }
  323. }
  324. return newSkus;
  325. }
  326. // 选择规格
  327. function onSelectSku(propertyId, valueId) {
  328. // console.log(propertyId,valueId)
  329. // 清空已输入金额
  330. state.selectedSku.use_points = 0;
  331. // 清空已选择
  332. let isChecked = true; // 选中 or 取消选中
  333. if (
  334. state.currentPropertyArray[propertyId] !== undefined &&
  335. state.currentPropertyArray[propertyId] === valueId
  336. ) {
  337. // 点击已被选中的,删除并填充 ''
  338. isChecked = false;
  339. state.currentPropertyArray.splice(propertyId, 1, '');
  340. } else {
  341. // 选中
  342. state.currentPropertyArray[propertyId] = valueId;
  343. }
  344. // 选中的 property 大类
  345. let choosePropertyId = [];
  346. state.currentPropertyArray.forEach((currentPropertyId) => {
  347. if (currentPropertyId !== '') {
  348. // currentPropertyId 为空是反选 填充的
  349. choosePropertyId.push(currentPropertyId);
  350. }
  351. });
  352. // 当前所选 property 下,所有可以选择的 SKU 们
  353. let newSkuList = getCanUseSkuList();
  354. // 判断所有 property 大类是否选择完成
  355. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  356. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  357. state.selectedSku = newSkuList[0];
  358. } else {
  359. state.selectedSku = {};
  360. }
  361. // 改变 property 禁用状态
  362. changeDisabled(isChecked, propertyId, valueId);
  363. }
  364. changeDisabled(false);
  365. onMounted(() => {
  366. // 如果商品的属性只有一条,则默认选中
  367. if (propertyList.length == 1 && propertyList[0].values.length == 1) {
  368. onSelectSku(0, 0);
  369. }
  370. });
  371. // TODO 非繁人:待讨论的优化点:1)单规格,要不要默认选中;2)默认要不要选中第一个规格
  372. </script>
  373. <style lang="scss" scoped>
  374. .origin-price-text {
  375. font-size: 22rpx;
  376. font-weight: 400;
  377. color: $gray-c;
  378. font-family: OPPOSANS;
  379. background: #ffca3e;
  380. padding: 2px 8px;
  381. border-radius: 4px;
  382. display: inline-block;
  383. color: #597533;
  384. }
  385. // 购买
  386. .buy-box {
  387. padding: 10rpx 0;
  388. .add-btn {
  389. width: 356rpx;
  390. height: 80rpx;
  391. border-radius: 40rpx 0 0 40rpx;
  392. background-color: var(--ui-BG-Main-light);
  393. color: var(--ui-BG-Main);
  394. }
  395. .buy-btn {
  396. width: 356rpx;
  397. height: 80rpx;
  398. border-radius: 0 40rpx 40rpx 0;
  399. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  400. color: #fff;
  401. }
  402. .one-buy-btn {
  403. width: 98%;
  404. height: 80rpx;
  405. border-radius: 40rpx;
  406. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  407. color: #fff;
  408. }
  409. .score-btn {
  410. width: 100%;
  411. margin: 0 20rpx;
  412. height: 80rpx;
  413. border-radius: 40rpx;
  414. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  415. color: #fff;
  416. }
  417. }
  418. .ss-modal-box {
  419. border-radius: 30rpx 30rpx 0 0;
  420. max-height: 1000rpx;
  421. .modal-header {
  422. position: relative;
  423. padding: 80rpx 20rpx 40rpx;
  424. .sku-image {
  425. width: 160rpx;
  426. height: 160rpx;
  427. border-radius: 10rpx;
  428. }
  429. .header-right {
  430. height: 160rpx;
  431. }
  432. .close-icon {
  433. position: absolute;
  434. top: 10rpx;
  435. right: 20rpx;
  436. font-size: 46rpx;
  437. opacity: 0.2;
  438. }
  439. .goods-title {
  440. font-size: 28rpx;
  441. font-weight: 500;
  442. line-height: 42rpx;
  443. }
  444. .score-img {
  445. width: 36rpx;
  446. height: 36rpx;
  447. margin: 0 4rpx;
  448. }
  449. .score-text {
  450. font-size: 30rpx;
  451. font-weight: 500;
  452. color: $red;
  453. font-family: OPPOSANS;
  454. }
  455. .price-text {
  456. font-size: 30rpx;
  457. font-weight: 500;
  458. color: $red;
  459. font-family: OPPOSANS;
  460. &::before {
  461. // content: '¥';
  462. // font-size: 30rpx;
  463. // font-weight: 500;
  464. // color: $red;
  465. }
  466. }
  467. .stock-text {
  468. font-size: 26rpx;
  469. color: #999999;
  470. }
  471. }
  472. .modal-content {
  473. padding: 0 20rpx;
  474. .modal-content-scroll {
  475. max-height: 600rpx;
  476. .label-text {
  477. font-size: 26rpx;
  478. font-weight: 500;
  479. }
  480. .buy-num-box {
  481. height: 100rpx;
  482. }
  483. .spec-btn {
  484. height: 60rpx;
  485. min-width: 100rpx;
  486. padding: 0 30rpx;
  487. background: #f4f4f4;
  488. border-radius: 30rpx;
  489. color: #434343;
  490. font-size: 26rpx;
  491. margin-right: 10rpx;
  492. margin-bottom: 10rpx;
  493. }
  494. .disabled-btn {
  495. font-weight: 400;
  496. color: #c6c6c6;
  497. background: #f8f8f8;
  498. }
  499. }
  500. }
  501. }
  502. </style>