s-select-groupon-sku.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <template>
  2. <!-- 拼团商品规格弹窗 -->
  3. <su-popup :show="show" round="10" @close="emits('close')">
  4. <view class="ss-modal-box bg-white ss-flex-col">
  5. <view class="modal-header ss-flex ss-col-center">
  6. <view class="header-left ss-m-r-30">
  7. <image
  8. class="sku-image"
  9. :src="sheep.$url.cdn(state.selectedSkuPrice.image || goodsInfo.image)"
  10. mode="aspectFill"
  11. >
  12. </image>
  13. </view>
  14. <view class="header-right ss-flex-col ss-row-between ss-flex-1">
  15. <view class="goods-title ss-line-2">
  16. <view class="tig ss-flex ss-col-center">
  17. <view class="tig-icon ss-flex ss-col-center ss-row-center">
  18. <view class="groupon-tag">
  19. <image :src="sheep.$url.static('/static/img/shop/goods/groupon-tag-white.png')">
  20. </image>
  21. </view>
  22. </view>
  23. <view class="tig-title">拼团价</view>
  24. </view>
  25. <view class="info-title">
  26. {{ goodsInfo.title }}
  27. </view>
  28. </view>
  29. <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
  30. <view class="price-text"> {{ goodsPrice }}</view>
  31. <view class="stock-text ss-m-l-20">
  32. 库存{{ state.selectedSkuPrice.stock || goodsInfo.stock }}件
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <view class="modal-content ss-flex-1">
  38. <scroll-view scroll-y="true" class="modal-content-scroll">
  39. <view
  40. v-if="grouponAction === 'create' && activityType === 'groupon_ladder'"
  41. class="sku-item ss-m-b-20"
  42. >
  43. <view class="label-text ss-m-b-20">拼团人数</view>
  44. <view class="ss-flex ss-col-center ss-flex-wrap">
  45. <button
  46. v-for="(ladder, key) in goodsInfo.activity.rules.ladders"
  47. :key="key"
  48. class="ss-reset-button spec-btn"
  49. :class="[
  50. {
  51. 'checked-btn': grouponNum == ladder,
  52. },
  53. ]"
  54. @tap="onSelectLadder(ladder)"
  55. >
  56. {{ ladder }}人团
  57. </button>
  58. </view>
  59. </view>
  60. <view class="sku-item ss-m-b-20" v-for="sku1 in goodsInfo.skus" :key="sku1.id">
  61. <view class="label-text ss-m-b-20">{{ sku1.name }}</view>
  62. <view class="ss-flex ss-col-center ss-flex-wrap">
  63. <button
  64. class="ss-reset-button spec-btn"
  65. v-for="sku2 in sku1.children"
  66. :class="[
  67. {
  68. 'checked-btn': state.currentSkuArray[sku2.parent_id] == sku2.id,
  69. },
  70. {
  71. 'disabled-btn': sku2.disabled == true,
  72. },
  73. ]"
  74. :key="sku2.id"
  75. :disabled="sku2.disabled == true"
  76. @tap="onSelectSku(sku2.parent_id, sku2.id)"
  77. >
  78. {{ sku2.name }}
  79. </button>
  80. </view>
  81. </view>
  82. <view class="buy-num-box ss-flex ss-col-center ss-row-between">
  83. <view class="label-text">购买数量</view>
  84. <su-number-box
  85. :min="1"
  86. :max="state.selectedSkuPrice.stock"
  87. :step="1"
  88. v-model="state.selectedSkuPrice.goods_num"
  89. @change="onNumberChange($event)"
  90. activity="groupon"
  91. ></su-number-box>
  92. </view>
  93. </scroll-view>
  94. </view>
  95. <view class="modal-footer ss-p-y-20">
  96. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
  97. <view class="ss-flex">
  98. <button class="ss-reset-button origin-price-btn ss-flex-col">
  99. <view class="btn-title">{{ grouponNum === 0 ? '阶梯团' : grouponNum + '人团' }}</view>
  100. </button>
  101. <button class="ss-reset-button btn-tox ss-flex-col" @tap="onBuy">
  102. <view class="btn-price">
  103. {{
  104. grouponAction === 'create' && goodsInfo.activity.rules.is_leader_discount == 1
  105. ? leaderPrice
  106. : goodsPrice
  107. }}
  108. </view>
  109. <view
  110. v-if="
  111. grouponAction === 'create' && goodsInfo.activity.rules.is_leader_discount == 0
  112. "
  113. >立即开团</view
  114. >
  115. <view
  116. v-else-if="
  117. grouponAction === 'create' && goodsInfo.activity.rules.is_leader_discount == 1
  118. "
  119. >团长立减价</view
  120. >
  121. <view v-else-if="grouponAction === 'join'">参与拼团</view>
  122. </button>
  123. </view>
  124. </view>
  125. </view>
  126. </view>
  127. </su-popup>
  128. </template>
  129. <script setup>
  130. import { computed, reactive, watch } from 'vue';
  131. import sheep from '@/sheep';
  132. import { formatPrice } from '@/sheep/hooks/useGoods';
  133. import { isEmpty } from 'lodash';
  134. const headerBg = sheep.$url.css('/static/img/shop/goods/groupon-btn-long.png');
  135. const emits = defineEmits(['change', 'addCart', 'buy', 'close', 'ladder']);
  136. const props = defineProps({
  137. show: {
  138. type: Boolean,
  139. default: false,
  140. },
  141. goodsInfo: {
  142. type: Object,
  143. default() {},
  144. },
  145. grouponAction: {
  146. type: String,
  147. default: 'create',
  148. },
  149. grouponNum: {
  150. type: [Number, String],
  151. default: 0,
  152. },
  153. });
  154. const state = reactive({
  155. selectedSkuPrice: {},
  156. currentSkuArray: [],
  157. grouponNum: props.grouponNum,
  158. });
  159. //输入框改变数量
  160. function onNumberChange(e) {
  161. if(e === 0) return;
  162. if (state.selectedSkuPrice.goods_num === e) return;
  163. state.selectedSkuPrice.goods_num = e;
  164. }
  165. // 默认单规格
  166. if (!props.goodsInfo.is_sku) {
  167. state.selectedSkuPrice = props.goodsInfo.sku_prices[0];
  168. }
  169. // 活动类型
  170. const activityType = props.goodsInfo.activity_type;
  171. // 可选规格
  172. const skuPrices = computed(() => {
  173. let skuPrices = props.goodsInfo.sku_prices;
  174. if (props.goodsInfo.is_sku) {
  175. skuPrices.forEach((item) => {
  176. item.goods_sku_id_arr = item.goods_sku_ids.split(',');
  177. });
  178. }
  179. return skuPrices;
  180. });
  181. const skuList = props.goodsInfo.skus;
  182. // 规格价格
  183. const goodsPrice = computed(() => {
  184. if (isEmpty(state.selectedSkuPrice)) {
  185. return formatPrice(props.goodsInfo.price);
  186. }
  187. if (props.grouponNum === 0 && activityType === 'groupon_ladder') {
  188. return formatPrice(props.goodsInfo.price);
  189. }
  190. if (activityType === 'groupon') {
  191. return state.selectedSkuPrice.groupon_price;
  192. }
  193. if (activityType === 'groupon_ladder') {
  194. const ladder = getSkuPriceByLadder();
  195. state.selectedSkuPrice.ladder_price = ladder.ladder_price;
  196. return ladder.ladder_price;
  197. }
  198. });
  199. // 团长优惠
  200. const leaderPrice = computed(() => {
  201. if (isEmpty(state.selectedSkuPrice)) {
  202. return formatPrice(props.goodsInfo.price);
  203. }
  204. if (props.grouponNum === 0 && activityType === 'groupon_ladder') {
  205. return formatPrice(props.goodsInfo.price);
  206. }
  207. if (activityType === 'groupon') {
  208. return state.selectedSkuPrice.leader_price;
  209. }
  210. if (activityType === 'groupon_ladder') {
  211. const ladder = getSkuPriceByLadder();
  212. return ladder.leader_ladder_price;
  213. }
  214. });
  215. // 获取阶梯价
  216. function getSkuPriceByLadder() {
  217. return state.selectedSkuPrice.ladders.find((item) => item.ladder == props.grouponNum);
  218. }
  219. watch(
  220. () => state.selectedSkuPrice,
  221. (newVal) => {
  222. emits('change', newVal);
  223. },
  224. {
  225. immediate: true, // 立即执行
  226. deep: true, // 深度监听
  227. },
  228. );
  229. // 点击购买
  230. function onBuy() {
  231. if (!state.selectedSkuPrice.goods_id) {
  232. sheep.$helper.toast('请选择规格');
  233. return;
  234. }
  235. if (state.selectedSkuPrice.stock <= 0) {
  236. sheep.$helper.toast('库存不足');
  237. return;
  238. }
  239. emits('buy', state.selectedSkuPrice);
  240. }
  241. // 改变禁用状态
  242. function changeDisabled(isChecked = false, pid = 0, skuId = 0) {
  243. let newPrice = []; // 所有可以选择的 skuPrice
  244. if (isChecked) {
  245. // 选中规格
  246. // 当前点击选中规格下的 所有可用 skuPrice
  247. for (let price of skuPrices.value) {
  248. if (price.stock <= 0) {
  249. // this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
  250. continue;
  251. }
  252. if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) {
  253. newPrice.push(price);
  254. }
  255. }
  256. } else {
  257. // 取消选中
  258. // 当前所选规格下,所有可以选择的 skuPrice
  259. newPrice = getCanUseSkuPrice();
  260. }
  261. // 所有存在并且有库存未选择的规格项 的 子项 id
  262. let noChooseSkuIds = [];
  263. for (let price of newPrice) {
  264. noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr);
  265. }
  266. // 去重
  267. noChooseSkuIds = Array.from(new Set(noChooseSkuIds));
  268. if (isChecked) {
  269. // 去除当前选中的规格项
  270. let index = noChooseSkuIds.indexOf(skuId.toString());
  271. noChooseSkuIds.splice(index, 1);
  272. } else {
  273. // 循环去除当前已选择的规格项
  274. state.currentSkuArray.forEach((sku) => {
  275. if (sku.toString() != '') {
  276. // sku 为空是反选 填充的
  277. let index = noChooseSkuIds.indexOf(sku.toString());
  278. if (index >= 0) {
  279. // sku 存在于 noChooseSkuIds
  280. noChooseSkuIds.splice(index, 1);
  281. }
  282. }
  283. });
  284. }
  285. // 当前已选择的规格大类
  286. let chooseSkuKey = [];
  287. if (!isChecked) {
  288. // 当前已选择的规格大类
  289. state.currentSkuArray.forEach((sku, key) => {
  290. if (sku != '') {
  291. // sku 为空是反选 填充的
  292. chooseSkuKey.push(key);
  293. }
  294. });
  295. } else {
  296. // 当前点击选择的规格大类
  297. chooseSkuKey = [pid];
  298. }
  299. for (let i in skuList) {
  300. // 当前点击的规格,或者取消选择时候 已选中的规格 不进行处理
  301. if (chooseSkuKey.indexOf(skuList[i]['id']) >= 0) {
  302. continue;
  303. }
  304. for (let j in skuList[i]['children']) {
  305. // 如果当前规格项 id 不存在于有库存的规格项中,则禁用
  306. if (noChooseSkuIds.indexOf(skuList[i]['children'][j]['id'].toString()) >= 0) {
  307. skuList[i]['children'][j]['disabled'] = false;
  308. } else {
  309. skuList[i]['children'][j]['disabled'] = true;
  310. }
  311. }
  312. }
  313. }
  314. // 当前所选规格下,获取所有有库存的 skuPrice
  315. function getCanUseSkuPrice() {
  316. let newPrice = [];
  317. for (let price of skuPrices.value) {
  318. if (price.stock <= 0) {
  319. // || price.stock < this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
  320. continue;
  321. }
  322. var isOk = true;
  323. state.currentSkuArray.forEach((sku) => {
  324. // sku 不为空,并且,这个 条 skuPrice 没有被选中,则排除
  325. if (sku.toString() != '' && price.goods_sku_id_arr.indexOf(sku.toString()) < 0) {
  326. isOk = false;
  327. }
  328. });
  329. if (isOk) {
  330. newPrice.push(price);
  331. }
  332. }
  333. return newPrice;
  334. }
  335. // 选择阶梯拼团人数
  336. function onSelectLadder(ladder) {
  337. emits('ladder', ladder);
  338. }
  339. // 选择规格
  340. function onSelectSku(pid, skuId) {
  341. // 清空已选择
  342. if (activityType === 'groupon_ladder' && props.grouponNum == 0) {
  343. sheep.$helper.toast('请选择拼团人数');
  344. return;
  345. }
  346. let isChecked = true; // 选中 or 取消选中
  347. if (state.currentSkuArray[pid] != undefined && state.currentSkuArray[pid] == skuId) {
  348. // 点击已被选中的,删除并填充 ''
  349. isChecked = false;
  350. state.currentSkuArray.splice(pid, 1, '');
  351. } else {
  352. // 选中
  353. state.currentSkuArray[pid] = skuId;
  354. }
  355. let chooseSkuId = []; // 选中的规格大类
  356. state.currentSkuArray.forEach((sku) => {
  357. if (sku != '') {
  358. // sku 为空是反选 填充的
  359. chooseSkuId.push(sku);
  360. }
  361. });
  362. // 当前所选规格下,所有可以选择的 skuPric
  363. let newPrice = getCanUseSkuPrice();
  364. // 判断所有规格大类是否选择完成
  365. if (chooseSkuId.length == skuList.length && newPrice.length) {
  366. newPrice[0].goods_num = state.selectedSkuPrice.goods_num || 1;
  367. state.selectedSkuPrice = newPrice[0];
  368. } else {
  369. state.selectedSkuPrice = {};
  370. }
  371. // 改变规格项禁用状态
  372. changeDisabled(isChecked, pid, skuId);
  373. }
  374. changeDisabled(false);
  375. </script>
  376. <style lang="scss" scoped>
  377. // 购买
  378. .buy-btn {
  379. margin: 0 20rpx;
  380. width: 100%;
  381. height: 80rpx;
  382. border-radius: 40rpx;
  383. background: linear-gradient(90deg, #ff6000, #fe832a);
  384. color: #fff;
  385. }
  386. .btn-tox {
  387. width: 382rpx;
  388. height: 80rpx;
  389. font-size: 24rpx;
  390. font-weight: 600;
  391. margin-left: -50rpx;
  392. background-image: v-bind(headerBg);
  393. background-repeat: no-repeat;
  394. background-size: 100% 100%;
  395. color: #ffffff;
  396. line-height: normal;
  397. border-radius: 0px 40rpx 40rpx 0px;
  398. .btn-price {
  399. font-family: OPPOSANS;
  400. &::before {
  401. content: '¥';
  402. }
  403. }
  404. }
  405. .origin-price-btn {
  406. width: 370rpx;
  407. height: 80rpx;
  408. background: rgba(#ff5651, 0.1);
  409. color: #ff6000;
  410. border-radius: 40rpx 0px 0px 40rpx;
  411. line-height: normal;
  412. font-size: 24rpx;
  413. font-weight: 500;
  414. .btn-price {
  415. font-family: OPPOSANS;
  416. &::before {
  417. content: '¥';
  418. }
  419. }
  420. .btn-title {
  421. font-size: 28rpx;
  422. }
  423. }
  424. .ss-modal-box {
  425. border-radius: 30rpx 30rpx 0 0;
  426. max-height: 1000rpx;
  427. .modal-header {
  428. position: relative;
  429. padding: 80rpx 20rpx 40rpx;
  430. .sku-image {
  431. width: 160rpx;
  432. height: 160rpx;
  433. border-radius: 10rpx;
  434. }
  435. .header-right {
  436. height: 160rpx;
  437. }
  438. .close-icon {
  439. position: absolute;
  440. top: 10rpx;
  441. right: 20rpx;
  442. font-size: 46rpx;
  443. opacity: 0.2;
  444. }
  445. .goods-title {
  446. font-size: 28rpx;
  447. font-weight: 500;
  448. line-height: 42rpx;
  449. position: relative;
  450. .tig {
  451. border: 2rpx solid #ff6000;
  452. border-radius: 4rpx;
  453. width: 126rpx;
  454. height: 38rpx;
  455. position: absolute;
  456. left: 0;
  457. top: 0;
  458. .tig-icon {
  459. width: 40rpx;
  460. height: 40rpx;
  461. background: #ff6000;
  462. margin-left: -2rpx;
  463. border-radius: 4rpx 0 0 4rpx;
  464. .groupon-tag {
  465. width: 32rpx;
  466. height: 32rpx;
  467. }
  468. }
  469. .tig-title {
  470. font-size: 24rpx;
  471. font-weight: 500;
  472. line-height: normal;
  473. color: #ff6000;
  474. width: 86rpx;
  475. display: flex;
  476. justify-content: center;
  477. align-items: center;
  478. }
  479. }
  480. .info-title {
  481. text-indent: 132rpx;
  482. }
  483. }
  484. .price-text {
  485. font-size: 30rpx;
  486. font-weight: 500;
  487. color: $red;
  488. font-family: OPPOSANS;
  489. &::before {
  490. content: '¥';
  491. font-size: 24rpx;
  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. .checked-btn {
  522. background: linear-gradient(90deg, #ff6000, #fe832a);
  523. font-weight: 500;
  524. color: #ffffff;
  525. }
  526. .disabled-btn {
  527. font-weight: 400;
  528. color: #c6c6c6;
  529. background: #f8f8f8;
  530. }
  531. }
  532. }
  533. }
  534. image {
  535. width: 100%;
  536. height: 100%;
  537. }
  538. </style>