s-select-sku.vue 15 KB

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