shopro-sku.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <template>
  2. <!-- 规格 -->
  3. <view class="cu-modal bottom-modal" @touchmove.stop.prevent="" :class="{ show: showModal }"
  4. @tap.stop="showModal = false" v-if="goodsInfo.sku_price">
  5. <view class="cu-dialog" @tap.stop style="border-radius: 30rpx 30rpx 0 0;">
  6. <view class="shop-modal page_box" :style="goodsInfo.is_sku == 0 ? 'height:700rpx' : ''">
  7. <text class="u-iconfont uicon-close-circle-fill close-icon" @tap.stop="showModal = false"></text>
  8. <!-- 商品卡片-->
  9. <view class="top u-flex modal-head__box">
  10. <image class="shop-img" :src="currentSkuPrice.image ? currentSkuPrice.image : goodsInfo.image"
  11. mode="aspectFill"></image>
  12. <view class=" goods-box u-flex-col u-row-between">
  13. <view class="goods-title u-ellipsis-2">{{ goodsInfo.title }}</view>
  14. <view class="u-flex u-row-between goods-bottom">
  15. <view class="price-box u-flex">
  16. <view v-if="goodsType === 'score'">{{ currentSkuPrice.price_text || goodsInfo.price }}
  17. </view>
  18. <view v-else-if="grouponBuyType === 'groupon'">
  19. ¥{{ currentSkuPrice.groupon_price || (goodsInfo.activity_type === 'groupon' ? goodsInfo.groupon_price : goodsInfo.price) }}
  20. </view>
  21. <view v-else>¥{{ currentSkuPrice.price || goodsInfo.price }}</view>
  22. </view>
  23. <text class="stock">库存{{ currentSkuPrice.stock || goodsInfo.stock }}件</text>
  24. </view>
  25. </view>
  26. </view>
  27. <!-- 规格选项 -->
  28. <scroll-view scroll-y class="content_box">
  29. <view class="select-box u-flex-col u-row-left" v-for="(s, x) in skuList" :key="s.id">
  30. <view class="type-title u-flex">{{ s.name }}</view>
  31. <view class="tag-box u-flex u-flex-wrap">
  32. <button class="tag u-reset-button" v-for="(sc, y) in s.content" :key="sc.id"
  33. :class="{ 'tag-active': currentSkuArray[sc.pid] == sc.id, 'tag-disabled': sc.disabled == true }"
  34. :disabled="sc.disabled == true" @tap="chooseSku(sc.pid, sc.id)">
  35. {{ sc.name }}
  36. </button>
  37. </view>
  38. </view>
  39. <!-- 计步器 -->
  40. <view class="buy-num-box u-flex u-row-between">
  41. <view class="num-title">购买数量</view>
  42. <u-number-box v-model="goodsNum" :min="1" :step="1" :max="maxStep" @plus="plus"
  43. :long-press="false" @change="changeNum"></u-number-box>
  44. </view>
  45. </scroll-view>
  46. <!-- 功能按钮 -->
  47. <view class="btn-box foot_box u-flex u-row-between" v-if="buyType === 'cart' || buyType === 'buy'">
  48. <button class="u-reset-button cu-btn save-btn"
  49. v-if="(activityRules && activityRules.status === 'ing') || !goodsInfo.activity_type"
  50. @tap="confirm">确认</button>
  51. <button class="u-reset-button cu-btn cancel-btn"
  52. v-if="activityRules && activityRules.status !== 'ing' && goodsInfo.activity_type"
  53. @tap="confirm">确定</button>
  54. </view>
  55. <view class="btn-box foot_box u-flex u-row-between" v-else>
  56. <button class="u-reset-button cu-btn cart-btn" @tap="confirmCart">加入购物车</button>
  57. <button class="u-reset-button cu-btn buy-btn" @tap="confirmBuy">立即购买</button>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. /**
  65. * 多规格组件
  66. * @property {Object} goodsInfo - 商品数据
  67. * @property {Boolean} value = showModal - 显隐
  68. * @property {String} buyType - 购买方式
  69. * @property {String} goodsType - 商品类别
  70. * @property {String} grouponBuyType -拼团商品购买方式
  71. * @property {Number} grouponId - 拼团ID,分享进入
  72. * @property {Object} activityRules - 活动状态。
  73. */
  74. import { mapMutations, mapActions, mapState } from 'vuex';
  75. export default {
  76. components: {},
  77. data() {
  78. return {
  79. maxStep: 999,
  80. skuList: [],
  81. currentSkuPrice: {},
  82. currentSkuArray: [],
  83. goodsNum: 1,
  84. confirmGoodsInfo: {},
  85. type: this.buyType
  86. };
  87. },
  88. props: {
  89. goodsInfo: {},
  90. activityRules: {},
  91. value: {},
  92. buyType: {
  93. type: String,
  94. default: 'sku'
  95. },
  96. goodsType: {
  97. type: String,
  98. default: 'goods'
  99. },
  100. grouponBuyType: {
  101. type: String,
  102. default: 'alone'
  103. },
  104. grouponId: {
  105. //参加拼团的时候,传入当前团id;
  106. type: Number,
  107. default: 0
  108. }
  109. },
  110. created() {
  111. this.skuList = this.goodsInfo.sku;
  112. this.changeDisabled(false);
  113. },
  114. mounted() {
  115. // 单规格选项
  116. if (!this.goodsInfo.is_sku) {
  117. this.currentSkuPrice = this.skuPrice[0];
  118. this.maxStep = this.skuPrice[0].stock > 999 ? 999 : this.skuPrice[0].stock;
  119. }
  120. },
  121. watch: {
  122. type(nweVal, oldVal) {
  123. return newVal;
  124. }
  125. },
  126. computed: {
  127. skuPrice() {
  128. return this.goodsInfo.sku_price;
  129. },
  130. showModal: {
  131. get() {
  132. return this.value;
  133. },
  134. set(val) {
  135. val ? uni.hideTabBar() : uni.showTabBar();
  136. this.$emit('input', val);
  137. return val;
  138. }
  139. },
  140. currentSkuText() {
  141. let that = this;
  142. let str = '';
  143. let currentSkuArray = this.currentSkuArray;
  144. currentSkuArray.forEach(v => {
  145. that.skuList.forEach(s => {
  146. s.content.forEach(u => {
  147. if (u.id === v) {
  148. str += ' ' + u.name;
  149. }
  150. });
  151. });
  152. });
  153. that.$emit('getSkuText', str);
  154. return str;
  155. }
  156. },
  157. methods: {
  158. ...mapActions(['addCartGoods', 'getCartList']),
  159. jump(path, parmas) {
  160. this.$Router.push({
  161. path: path,
  162. query: parmas
  163. });
  164. },
  165. // 选择规格
  166. chooseSku(pid, skuId) {
  167. let that = this;
  168. let isChecked = true; // 选中 or 取消选中
  169. this.goodsNum = 1; //选择规格时,数量重置为1.
  170. this.maxStep = 999; //选择其他规格时,取消上个规格库存限制
  171. if (that.currentSkuArray[pid] != undefined && that.currentSkuArray[pid] == skuId) {
  172. // 点击已被选中的,删除并填充 ''
  173. isChecked = false;
  174. that.currentSkuArray.splice(pid, 1, '');
  175. } else {
  176. // 选中
  177. that.$set(that.currentSkuArray, pid, skuId);
  178. }
  179. let chooseSkuId = []; // 选中的规格大类
  180. that.currentSkuArray.forEach(sku => {
  181. if (sku != '') {
  182. // sku 为空是反选 填充的
  183. chooseSkuId.push(sku);
  184. }
  185. });
  186. // 当前所选规格下,所有可以选择的 skuPric
  187. let newPrice = this.getCanUseSkuPrice();
  188. // 判断所有规格大类是否选择完成
  189. if (chooseSkuId.length == that.skuList.length && newPrice.length) {
  190. that.currentSkuPrice = newPrice[0];
  191. } else {
  192. that.currentSkuPrice = {};
  193. }
  194. // 改变规格项禁用状态
  195. this.changeDisabled(isChecked, pid, skuId);
  196. },
  197. // 改变禁用状态
  198. changeDisabled(isChecked = false, pid = 0, skuId = 0) {
  199. let newPrice = []; // 所有可以选择的 skuPrice
  200. if (isChecked) {
  201. // 选中规格
  202. // 当前点击选中规格下的 所有可用 skuPrice
  203. for (let price of this.skuPrice) {
  204. if (price.stock <= 0) {
  205. // this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
  206. continue;
  207. }
  208. if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) {
  209. newPrice.push(price);
  210. }
  211. }
  212. } else {
  213. // 取消选择规格
  214. // 当前所选规格下,所有可以选择的 skuPric
  215. newPrice = this.getCanUseSkuPrice();
  216. }
  217. // 所有存在并且有库存未选择的规格项 的 子项 id
  218. let noChooseSkuIds = [];
  219. for (let price of newPrice) {
  220. noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr);
  221. }
  222. // 去重
  223. noChooseSkuIds = Array.from(new Set(noChooseSkuIds));
  224. if (isChecked) {
  225. // 去除当前选中的规格项
  226. let index = noChooseSkuIds.indexOf(skuId.toString());
  227. noChooseSkuIds.splice(index, 1);
  228. } else {
  229. // 循环去除当前已选择的规格项
  230. this.currentSkuArray.forEach(sku => {
  231. if (sku.toString() != '') {
  232. // sku 为空是反选 填充的
  233. let index = noChooseSkuIds.indexOf(sku.toString());
  234. if (index >= 0) {
  235. // sku 存在于 noChooseSkuIds
  236. noChooseSkuIds.splice(index, 1);
  237. }
  238. }
  239. });
  240. }
  241. // 当前已选择的规格大类
  242. let chooseSkuKey = [];
  243. if (!isChecked) {
  244. // 当前已选择的规格大类
  245. this.currentSkuArray.forEach((sku, key) => {
  246. if (sku != '') {
  247. // sku 为空是反选 填充的
  248. chooseSkuKey.push(key);
  249. }
  250. });
  251. } else {
  252. // 当前点击选择的规格大类
  253. chooseSkuKey = [pid];
  254. }
  255. for (let i in this.skuList) {
  256. // 当前点击的规格,或者取消选择时候 已选中的规格 不进行处理
  257. if (chooseSkuKey.indexOf(this.skuList[i]['id']) >= 0) {
  258. continue;
  259. }
  260. for (let j in this.skuList[i]['content']) {
  261. // 如果当前规格项 id 不存在于有库存的规格项中,则禁用
  262. if (noChooseSkuIds.indexOf(this.skuList[i]['content'][j]['id'].toString()) >= 0) {
  263. this.skuList[i]['content'][j]['disabled'] = false;
  264. } else {
  265. this.skuList[i]['content'][j]['disabled'] = true;
  266. }
  267. }
  268. }
  269. },
  270. // 当前所选规格下,获取所有有库存的 skuPrice
  271. getCanUseSkuPrice() {
  272. let newPrice = [];
  273. for (let price of this.skuPrice) {
  274. if (price.stock <= 0) {
  275. // || price.stock < this.goodsNum 不判断是否大于当前选择数量,在 uni-number-box 判断,并且 取 stock 和 goods_num 的小值
  276. continue;
  277. }
  278. var isOk = true;
  279. this.currentSkuArray.forEach(sku => {
  280. // sku 不为空,并且,这个 条 skuPrice 没有被选中,则排除
  281. if (sku.toString() != '' && price.goods_sku_id_arr.indexOf(sku.toString()) < 0) {
  282. isOk = false;
  283. }
  284. });
  285. if (isOk) {
  286. newPrice.push(price);
  287. }
  288. }
  289. return newPrice;
  290. },
  291. // 数量
  292. changeNum(e) {
  293. this.changeDisabled(false);
  294. },
  295. // 增加
  296. plus(e) {
  297. if (e.value >= this.currentSkuPrice.stock) {
  298. this.maxStep = this.currentSkuPrice.stock;
  299. this.$u.toast('库存不足');
  300. return;
  301. }
  302. if (this.goodsInfo.activity_type === 'seckill' || this.goodsInfo.activity_type === 'groupon') {
  303. let rules = this.goodsInfo.activity.rules;
  304. if (rules.limit_buy != 0 && e.value >= rules.limit_buy) {
  305. this.maxStep = rules.limit_buy;
  306. this.$u.toast('本次活动最多购买' + rules.limit_buy + '件');
  307. return;
  308. }
  309. }
  310. },
  311. // 加入购物车确定
  312. confirmCart() {
  313. let that = this;
  314. if (this.confirmSku()) {
  315. let confirmGoodsList = {
  316. list: [that.confirmGoodsInfo],
  317. from: 'goods'
  318. };
  319. that.addCartGoods(confirmGoodsList).then(res => {
  320. if (res.code === 1) {
  321. that.showModal = false;
  322. that.$u.toast(res.msg);
  323. }
  324. });
  325. }
  326. },
  327. // 立即购买
  328. confirmBuy() {
  329. let that = this;
  330. that.showModal = false;
  331. if (this.confirmSku()) {
  332. let confirmGoodsList = [];
  333. confirmGoodsList.push(that.confirmGoodsInfo);
  334. that.jump('/pages/order/confirm', {
  335. goodsList: confirmGoodsList,
  336. from: 'goods',
  337. orderType: that.goodsType,
  338. grouponBuyType: that.grouponBuyType,
  339. grouponId: that.grouponId
  340. });
  341. }
  342. },
  343. // 确定
  344. confirm() {
  345. if (this.confirmSku()) {
  346. switch (this.buyType) {
  347. case 'cart':
  348. this.confirmCart();
  349. break;
  350. case 'buy':
  351. this.confirmBuy();
  352. break;
  353. default:
  354. }
  355. }
  356. },
  357. // 确定规格
  358. confirmSku() {
  359. let that = this;
  360. if (that.currentSkuPrice.stock == 0 || that.currentSkuPrice.stock < that.goodsNum) {
  361. that.$u.toast('库存不足');
  362. that.showModal = false;
  363. return false;
  364. } else {
  365. that.currentSkuPrice.goods_num = that.goodsNum;
  366. that.confirmGoodsInfo = {
  367. goods_id: that.currentSkuPrice.goods_id,
  368. goods_num: that.currentSkuPrice.goods_num,
  369. sku_price_id: that.currentSkuPrice.id,
  370. goods_price: that.currentSkuPrice.price
  371. };
  372. if (!that.confirmGoodsInfo.sku_price_id) {
  373. that.$u.toast('请选择规格');
  374. return false;
  375. } else {
  376. that.showModal = false;
  377. return true;
  378. }
  379. }
  380. }
  381. }
  382. };
  383. </script>
  384. <style lang="scss" scoped>
  385. .size-box {
  386. line-height: 82rpx;
  387. background: #fff;
  388. padding: 0 20rpx;
  389. margin: 20rpx 0;
  390. font-size: 28rpx;
  391. .title {
  392. color: #999;
  393. margin-right: 20rpx;
  394. }
  395. }
  396. // 规格
  397. .shop-modal {
  398. width: 750rpx;
  399. height: 950rpx;
  400. background: rgba(255, 255, 255, 1);
  401. padding: 20rpx 20rpx 30rpx;
  402. position: relative;
  403. .close-icon {
  404. font-size: 34rpx;
  405. color: #e0e0e0;
  406. position: absolute;
  407. top: 20rpx;
  408. right: 20rpx;
  409. }
  410. // 商品卡片
  411. .top {
  412. margin: 30rpx 0;
  413. border-radius: 20rpx;
  414. padding: 20rpx;
  415. .shop-img {
  416. width: 160upx;
  417. height: 160upx;
  418. border-radius: 6upx;
  419. margin-right: 30upx;
  420. background: #ccc;
  421. }
  422. .goods-box {
  423. height: 160upx;
  424. width: 490rpx;
  425. align-items: flex-start;
  426. .goods-title {
  427. font-size: 28rpx;
  428. font-weight: 500;
  429. color: rgba(51, 51, 51, 1);
  430. line-height: 42rpx;
  431. text-align: left;
  432. }
  433. .goods-bottom {
  434. width: 100%;
  435. }
  436. .price-box {
  437. font-size: 36upx;
  438. font-weight: bold;
  439. color: #e1212b;
  440. .unit {
  441. font-size: 24upx;
  442. font-weight: bold;
  443. color: #e1212b;
  444. }
  445. }
  446. .stock {
  447. font-size: 26rpx;
  448. color: #999;
  449. }
  450. }
  451. }
  452. // 规格选项
  453. .select-box {
  454. margin-bottom: 30rpx;
  455. .type-title {
  456. font-size: 26upx;
  457. font-weight: 400;
  458. margin-bottom: 20upx;
  459. }
  460. .tag-box {
  461. flex-wrap: wrap;
  462. }
  463. .tag {
  464. line-height: 62rpx;
  465. background: #f4f4f4;
  466. border-radius: 31rpx;
  467. font-size: 28upx;
  468. font-weight: 400;
  469. color: #666666;
  470. padding: 0 30upx;
  471. margin-bottom: 20rpx;
  472. margin-right: 10rpx;
  473. }
  474. .tag-active {
  475. background: linear-gradient(90deg, #e9b461, #eecc89);
  476. font-size: 28rpx;
  477. font-weight: 400;
  478. color: #ffffff;
  479. }
  480. .tag-disabled {
  481. background: #f8f8f8;
  482. color: #cacaca;
  483. }
  484. }
  485. .buy-num-box {
  486. .num-title {
  487. font-size: 26upx;
  488. font-weight: 400;
  489. margin-bottom: 20upx;
  490. }
  491. }
  492. }
  493. .btn-box {
  494. height: 100rpx;
  495. .cu-btn {
  496. width: 340rpx;
  497. line-height: 70rpx;
  498. border-radius: 35rpx;
  499. font-size: 28rpx;
  500. font-weight: 500;
  501. color: rgba(255, 255, 255, 0.9);
  502. padding: 0;
  503. }
  504. .cart-btn {
  505. background: linear-gradient(90deg, rgba(103, 104, 105, 1), rgba(82, 82, 82, 1));
  506. box-shadow: 0px 2rpx 5rpx 0px rgba(102, 103, 104, 0.46);
  507. }
  508. .buy-btn {
  509. background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
  510. }
  511. .save-btn {
  512. width: 710rpx;
  513. height: 70rpx;
  514. background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
  515. font-size: 28rpx;
  516. font-weight: 500;
  517. color: rgba(255, 255, 255, 1);
  518. border-radius: 35rpx;
  519. padding: 0;
  520. }
  521. .cancel-btn {
  522. width: 710rpx;
  523. height: 70rpx;
  524. background: rgba(221, 221, 221, 1);
  525. font-size: 28rpx;
  526. font-weight: 500;
  527. color: #999999;
  528. border-radius: 35rpx;
  529. padding: 0;
  530. }
  531. }
  532. </style>