s-select-groupon-sku.vue 15 KB

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