s-select-groupon-sku.vue 16 KB

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