s-select-groupon-sku.vue 15 KB

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