s-select-groupon-sku.vue 15 KB

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