s-select-groupon-sku.vue 16 KB

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