s-select-groupon-sku.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <!-- 拼团商品规格弹窗 -->
  3. <su-popup :show="show" round="10" @close="emits('close')">
  4. <!-- SKU 信息 -->
  5. <view class="ss-modal-box bg-white ss-flex-col">
  6. <view class="modal-header ss-flex ss-col-center">
  7. <view class="header-left ss-m-r-30">
  8. <image
  9. class="sku-image"
  10. :src="sheep.$url.cdn(state.selectedSku.picUrl || goodsInfo.picUrl)"
  11. mode="aspectFill"
  12. />
  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('/static/img/shop/goods/groupon-tag-white.png')" />
  20. </view>
  21. </view>
  22. <view class="tig-title">拼团价</view>
  23. </view>
  24. <view class="info-title">
  25. {{ goodsInfo.name }}
  26. </view>
  27. </view>
  28. <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
  29. <view class="price-text">
  30. {{
  31. fen2yuan(
  32. state.selectedSku.price || goodsInfo.price || state.selectedSku.marketPrice,
  33. )
  34. }}</view
  35. >
  36. <view class="stock-text ss-m-l-20">
  37. 库存{{ state.selectedSku.stock || goodsInfo.stock }}件
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="modal-content ss-flex-1">
  43. <scroll-view scroll-y="true" class="modal-content-scroll">
  44. <view class="sku-item ss-m-b-20" v-for="property in propertyList" :key="property.id">
  45. <view class="label-text ss-m-b-20">{{ property.name }}</view>
  46. <view class="ss-flex ss-col-center ss-flex-wrap">
  47. <button
  48. class="ss-reset-button spec-btn"
  49. v-for="value in property.values"
  50. :class="[
  51. {
  52. 'checked-btn': state.currentPropertyArray[property.id] === value.id,
  53. },
  54. {
  55. 'disabled-btn': value.disabled === true,
  56. },
  57. ]"
  58. :key="value.id"
  59. :disabled="value.disabled === true"
  60. @tap="onSelectSku(property.id, value.id)"
  61. >
  62. {{ value.name }}
  63. </button>
  64. </view>
  65. </view>
  66. <view class="buy-num-box ss-flex ss-col-center ss-row-between">
  67. <view class="label-text">购买数量</view>
  68. <su-number-box
  69. :min="1"
  70. :max="state.selectedSku.stock"
  71. :step="1"
  72. v-model="state.selectedSku.count"
  73. @change="onNumberChange($event)"
  74. activity="groupon"
  75. />
  76. </view>
  77. </scroll-view>
  78. </view>
  79. <!-- 操作区 -->
  80. <view class="modal-footer ss-p-y-20">
  81. <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
  82. <view class="ss-flex">
  83. <button class="ss-reset-button origin-price-btn ss-flex-col">
  84. <view class="btn-title">{{ grouponNum + '人团' }}</view>
  85. </button>
  86. <button class="ss-reset-button btn-tox ss-flex-col" @tap="onBuy">
  87. <view class="btn-price">
  88. {{
  89. fen2yuan(
  90. state.selectedSku.price * state.selectedSku.count ||
  91. goodsInfo.price * state.selectedSku.count ||
  92. state.selectedSku.marketPrice * state.selectedSku.count ||
  93. goodsInfo.price,
  94. )
  95. }}
  96. </view>
  97. <view v-if="grouponAction === 'create'">立即开团</view>
  98. <view v-else-if="grouponAction === 'join'">参与拼团</view>
  99. </button>
  100. </view>
  101. </view>
  102. </view>
  103. </view>
  104. </su-popup>
  105. </template>
  106. <script setup>
  107. import { computed, reactive, watch } from 'vue';
  108. import sheep from '@/sheep';
  109. import { convertProductPropertyList, fen2yuan } from '@/sheep/hooks/useGoods';
  110. const headerBg = sheep.$url.css('/static/img/shop/goods/groupon-btn-long.png');
  111. const emits = defineEmits(['change', 'addCart', 'buy', 'close', 'ladder']);
  112. const props = defineProps({
  113. show: {
  114. type: Boolean,
  115. default: false,
  116. },
  117. goodsInfo: {
  118. type: Object,
  119. default() {},
  120. },
  121. grouponAction: {
  122. type: String,
  123. default: 'create',
  124. },
  125. grouponNum: {
  126. type: [Number, String],
  127. default: 0,
  128. },
  129. });
  130. const state = reactive({
  131. selectedSku: {}, // 选中的 SKU
  132. currentPropertyArray: [], // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  133. grouponNum: props.grouponNum,
  134. });
  135. const propertyList = convertProductPropertyList(props.goodsInfo.skus);
  136. // SKU 列表
  137. const skuList = computed(() => {
  138. let skuPrices = props.goodsInfo.skus;
  139. for (let price of skuPrices) {
  140. price.value_id_array = price.properties.map((item) => item.valueId);
  141. }
  142. return skuPrices;
  143. });
  144. watch(
  145. () => state.selectedSku,
  146. (newVal) => {
  147. emits('change', newVal);
  148. },
  149. {
  150. immediate: true, // 立即执行
  151. deep: true, // 深度监听
  152. },
  153. );
  154. // 输入框改变数量
  155. function onNumberChange(e) {
  156. if (e === 0) return;
  157. if (state.selectedSku.count === e) return;
  158. state.selectedSku.count = e;
  159. }
  160. // 点击购买
  161. function onBuy() {
  162. if (!state.selectedSku.id || state.selectedSku.id <= 0) {
  163. sheep.$helper.toast('请选择规格');
  164. return;
  165. }
  166. if (state.selectedSku.stock <= 0) {
  167. sheep.$helper.toast('库存不足');
  168. return;
  169. }
  170. emits('buy', state.selectedSku);
  171. }
  172. // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
  173. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  174. let newSkus = []; // 所有可以选择的 sku 数组
  175. if (isChecked) {
  176. // 情况一:选中 property
  177. // 获得当前点击选中 property 的、所有可用 SKU
  178. for (let price of skuList.value) {
  179. if (price.stock <= 0) {
  180. continue;
  181. }
  182. if (price.value_id_array.indexOf(valueId) >= 0) {
  183. newSkus.push(price);
  184. }
  185. }
  186. } else {
  187. // 情况二:取消选中 property
  188. // 当前所选 property 下,所有可以选择的 SKU
  189. newSkus = getCanUseSkuList();
  190. }
  191. // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
  192. let noChooseValueIds = [];
  193. for (let price of newSkus) {
  194. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  195. }
  196. noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
  197. if (isChecked) {
  198. // 去除当前选中的 value 属性值 id
  199. let index = noChooseValueIds.indexOf(valueId);
  200. noChooseValueIds.splice(index, 1);
  201. } else {
  202. // 循环去除当前已选择的 value 属性值 id
  203. state.currentPropertyArray.forEach((currentPropertyId) => {
  204. if (currentPropertyId.toString() !== '') {
  205. return;
  206. }
  207. // currentPropertyId 为空是反选 填充的
  208. let index = noChooseValueIds.indexOf(currentPropertyId);
  209. if (index >= 0) {
  210. // currentPropertyId 存在于 noChooseValueIds
  211. noChooseValueIds.splice(index, 1);
  212. }
  213. });
  214. }
  215. // 当前已选择的 property 数组
  216. let choosePropertyIds = [];
  217. if (!isChecked) {
  218. // 当前已选择的 property
  219. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  220. if (currentPropertyId !== '') {
  221. // currentPropertyId 为空是反选 填充的
  222. choosePropertyIds.push(currentValueId);
  223. }
  224. });
  225. } else {
  226. // 当前点击选择的 property
  227. choosePropertyIds = [propertyId];
  228. }
  229. for (let propertyIndex in propertyList) {
  230. // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
  231. if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
  232. continue;
  233. }
  234. // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
  235. for (let valueIndex in propertyList[propertyIndex]['values']) {
  236. propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
  237. noChooseValueIds.indexOf(propertyList[propertyIndex]['values'][valueIndex]['id']) < 0; // true 禁用 or false 不禁用
  238. }
  239. }
  240. }
  241. // 当前所选属性下,获取所有有库存的 SKU 们
  242. function getCanUseSkuList() {
  243. let newSkus = [];
  244. for (let sku of skuList.value) {
  245. if (sku.stock <= 0) {
  246. continue;
  247. }
  248. let isOk = true;
  249. state.currentPropertyArray.forEach((propertyId) => {
  250. // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
  251. if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
  252. isOk = false;
  253. }
  254. });
  255. if (isOk) {
  256. newSkus.push(sku);
  257. }
  258. }
  259. return newSkus;
  260. }
  261. // 选择规格
  262. function onSelectSku(propertyId, valueId) {
  263. // 清空已选择
  264. let isChecked = true; // 选中 or 取消选中
  265. if (
  266. state.currentPropertyArray[propertyId] !== undefined &&
  267. state.currentPropertyArray[propertyId] === valueId
  268. ) {
  269. // 点击已被选中的,删除并填充 ''
  270. isChecked = false;
  271. state.currentPropertyArray.splice(propertyId, 1, '');
  272. } else {
  273. // 选中
  274. state.currentPropertyArray[propertyId] = valueId;
  275. }
  276. // 选中的 property 大类
  277. let choosePropertyId = [];
  278. state.currentPropertyArray.forEach((currentPropertyId) => {
  279. if (currentPropertyId !== '') {
  280. // currentPropertyId 为空是反选 填充的
  281. choosePropertyId.push(currentPropertyId);
  282. }
  283. });
  284. // 当前所选 property 下,所有可以选择的 SKU 们
  285. let newSkuList = getCanUseSkuList();
  286. // 判断所有 property 大类是否选择完成
  287. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  288. newSkuList[0].count = state.selectedSku.count || 1;
  289. state.selectedSku = newSkuList[0];
  290. } else {
  291. state.selectedSku = {};
  292. }
  293. // 改变 property 禁用状态
  294. changeDisabled(isChecked, propertyId, valueId);
  295. }
  296. changeDisabled(false);
  297. // TODO 芋艿:待讨论的优化点:1)单规格,要不要默认选中;2)默认要不要选中第一个规格
  298. </script>
  299. <style lang="scss" scoped>
  300. // 购买
  301. .buy-btn {
  302. margin: 0 20rpx;
  303. width: 100%;
  304. height: 80rpx;
  305. border-radius: 40rpx;
  306. background: linear-gradient(90deg, #ff6000, #fe832a);
  307. color: #fff;
  308. }
  309. .btn-tox {
  310. width: 382rpx;
  311. height: 80rpx;
  312. font-size: 24rpx;
  313. font-weight: 600;
  314. margin-left: -50rpx;
  315. background-image: v-bind(headerBg);
  316. background-repeat: no-repeat;
  317. background-size: 100% 100%;
  318. color: #ffffff;
  319. line-height: normal;
  320. border-radius: 0px 40rpx 40rpx 0px;
  321. .btn-price {
  322. font-family: OPPOSANS;
  323. &::before {
  324. content: '¥';
  325. }
  326. }
  327. }
  328. .origin-price-btn {
  329. width: 370rpx;
  330. height: 80rpx;
  331. background: rgba(#ff5651, 0.1);
  332. color: #ff6000;
  333. border-radius: 40rpx 0px 0px 40rpx;
  334. line-height: normal;
  335. font-size: 24rpx;
  336. font-weight: 500;
  337. .btn-price {
  338. font-family: OPPOSANS;
  339. &::before {
  340. content: '¥';
  341. }
  342. }
  343. .btn-title {
  344. font-size: 28rpx;
  345. }
  346. }
  347. .ss-modal-box {
  348. border-radius: 30rpx 30rpx 0 0;
  349. max-height: 1000rpx;
  350. .modal-header {
  351. position: relative;
  352. padding: 80rpx 20rpx 40rpx;
  353. .sku-image {
  354. width: 160rpx;
  355. height: 160rpx;
  356. border-radius: 10rpx;
  357. }
  358. .header-right {
  359. height: 160rpx;
  360. }
  361. .close-icon {
  362. position: absolute;
  363. top: 10rpx;
  364. right: 20rpx;
  365. font-size: 46rpx;
  366. opacity: 0.2;
  367. }
  368. .goods-title {
  369. font-size: 28rpx;
  370. font-weight: 500;
  371. line-height: 42rpx;
  372. position: relative;
  373. .tig {
  374. border: 2rpx solid #ff6000;
  375. border-radius: 4rpx;
  376. width: 126rpx;
  377. height: 38rpx;
  378. position: absolute;
  379. left: 0;
  380. top: 0;
  381. .tig-icon {
  382. width: 40rpx;
  383. height: 40rpx;
  384. background: #ff6000;
  385. margin-left: -2rpx;
  386. border-radius: 4rpx 0 0 4rpx;
  387. .groupon-tag {
  388. width: 32rpx;
  389. height: 32rpx;
  390. }
  391. }
  392. .tig-title {
  393. font-size: 24rpx;
  394. font-weight: 500;
  395. line-height: normal;
  396. color: #ff6000;
  397. width: 86rpx;
  398. display: flex;
  399. justify-content: center;
  400. align-items: center;
  401. }
  402. }
  403. .info-title {
  404. text-indent: 132rpx;
  405. }
  406. }
  407. .price-text {
  408. font-size: 30rpx;
  409. font-weight: 500;
  410. color: $red;
  411. font-family: OPPOSANS;
  412. &::before {
  413. content: '¥';
  414. font-size: 24rpx;
  415. }
  416. }
  417. .stock-text {
  418. font-size: 26rpx;
  419. color: #999999;
  420. }
  421. }
  422. .modal-content {
  423. padding: 0 20rpx;
  424. .modal-content-scroll {
  425. max-height: 600rpx;
  426. .label-text {
  427. font-size: 26rpx;
  428. font-weight: 500;
  429. }
  430. .buy-num-box {
  431. height: 100rpx;
  432. }
  433. .spec-btn {
  434. height: 60rpx;
  435. min-width: 100rpx;
  436. padding: 0 30rpx;
  437. background: #f4f4f4;
  438. border-radius: 30rpx;
  439. color: #434343;
  440. font-size: 26rpx;
  441. margin-right: 10rpx;
  442. margin-bottom: 10rpx;
  443. }
  444. .checked-btn {
  445. background: linear-gradient(90deg, #ff6000, #fe832a);
  446. font-weight: 500;
  447. color: #ffffff;
  448. }
  449. .disabled-btn {
  450. font-weight: 400;
  451. color: #c6c6c6;
  452. background: #f8f8f8;
  453. }
  454. }
  455. }
  456. }
  457. image {
  458. width: 100%;
  459. height: 100%;
  460. }
  461. </style>