s-select-groupon-sku.vue 13 KB

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