s-select-groupon-sku.vue 16 KB

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