s-select-sku.vue 15 KB

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