order_addcart.vue 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. <template>
  2. <view>
  3. <view class='shoppingCart copy-data'>
  4. <view class='labelNav acea-row row-around'>
  5. <view class='item'><text class='iconfont icon-xuanzhong'></text>100%正品保证</view>
  6. <view class='item'><text class='iconfont icon-xuanzhong'></text>所有商品精挑细选</view>
  7. <view class='item'><text class='iconfont icon-xuanzhong'></text>售后无忧</view>
  8. </view>
  9. <view class="borRadius14 cartBox">
  10. <view
  11. v-if="(cartList.valid.length === 0 && cartList.invalid.length === 0) || (cartList.valid.length > 0)"
  12. class='nav acea-row row-between-wrapper'>
  13. <view>购物数量 <text class='num font-color'>{{cartCount}}</text></view>
  14. <view v-if="cartList.valid.length > 0 || cartList.invalid.length > 0"
  15. class='administrate acea-row row-center-wrapper' @click='manage'>{{ footerswitch ? '管理' : '取消'}}
  16. </view>
  17. </view>
  18. <view v-if="cartList.valid.length > 0 || cartList.invalid.length > 0" class="pad30">
  19. <!-- 有效的购物车 -->
  20. <view class='list'>
  21. <checkbox-group @change="checkboxChange">
  22. <block v-for="(item,index) in cartList.valid" :key="index">
  23. <view class='item acea-row row-between-wrapper'>
  24. <!-- #ifndef MP -->
  25. <checkbox :value="item.id.toString()" :checked="item.selected"
  26. :disabled="!item.canChecked && footerswitch" style="margin-right: 10rpx;" />
  27. <!-- #endif -->
  28. <!-- #ifdef MP -->
  29. <checkbox :value="item.id.toString()" :checked="item.selected"
  30. :disabled="!item.canChecked && footerswitch" />
  31. <!-- #endif -->
  32. <navigator :url='"/pages/goods_details/index?id=" + item.spu.id' hover-class='none'
  33. class='picTxt acea-row row-between-wrapper'>
  34. <view class='pictrue'>
  35. <image v-if="item.sku" :src='item.sku.picUrl' />
  36. <image v-else :src='item.spu.picUrl' />
  37. </view>
  38. <view class='text'>
  39. <view class='line1' :class="item.canChecked?'':'reColor'">
  40. {{ item.spu.name }}
  41. </view>
  42. <view class='infor line1' v-if="item.sku">属性:
  43. <text v-for="property in item.sku.properties" style="padding-left: 2px">{{property.valueName}}</text>
  44. </view>
  45. <view class='money' v-if="item.canChecked">¥{{ fen2yuan(item.sku.price) }}</view>
  46. <view class="reElection acea-row row-between-wrapper" v-else>
  47. <view class="title">请重新选择商品规格</view>
  48. <view class="reBnt cart-color acea-row row-center-wrapper" @click.stop="reElection(item)">重选</view>
  49. </view>
  50. </view>
  51. <view class='carnum acea-row row-center-wrapper' v-if="item.canChecked">
  52. <view class="reduce" :class="item.numSub ? 'on' : ''" @click.stop='subCart(index)'>-</view>
  53. <view class='num'>{{ item.count }}</view>
  54. <view class="plus" :class="item.numAdd ? 'on' : ''" @click.stop='addCart(index)'>+</view>
  55. </view>
  56. </navigator>
  57. </view>
  58. </block>
  59. </checkbox-group>
  60. </view>
  61. <!-- 无效的购物车 -->
  62. <view v-if="cartList.invalid.length > 0" class='invalidGoods borRadius14'
  63. :style="cartList.valid.length === 0 && cartList.invalid.length > 0
  64. ? 'position: relative;z-index: 111;top: -120rpx;'
  65. :'position: static;'">
  66. <view class='goodsNav acea-row row-between-wrapper'>
  67. <view v-if="cartList.invalid.length > 1 || cartList.valid.length > 0" @click='goodsOpen'>
  68. <text class='iconfont' :class='goodsHidden ? "icon-xiangxia":"icon-xiangshang"' /> 失效商品
  69. </view>
  70. <view v-else>失效商品</view>
  71. <view class='del' @click='unsetCart'>
  72. <text class='iconfont icon-shanchu1' />清空
  73. </view>
  74. </view>
  75. <view class='goodsList' :hidden='goodsHidden'>
  76. <block v-for="(item,index) in cartList.invalid" :key='index'>
  77. <view class='item acea-row row-between-wrapper'>
  78. <view class='invalid'>失效</view>
  79. <view class='picTxt acea-row row-between-wrapper'>
  80. <view class='pictrue'>
  81. <image v-if="item.sku" :src='item.sku.picUrl' />
  82. <image v-else :src='item.spu.picUrl' />
  83. </view>
  84. <view class='text acea-row row-column-between'>
  85. <view class='line1 name'>{{ item.spu.name }}</view>
  86. <view class='infor line1' v-if="item.sku.properties">属性:
  87. <text v-for="property in item.sku.properties" style="padding-left: 2px">{{property.valueName}}</text>
  88. </view>
  89. <view class='acea-row row-between-wrapper'>
  90. <view class='end'>该商品已失效</view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </block>
  96. </view>
  97. </view>
  98. </view>
  99. <!-- 热门推荐 -->
  100. <view class='noCart' v-if="cartList.valid.length === 0 && cartList.invalid.length === 0 && canShow">
  101. <view class='pictrue'>
  102. <image src='../../static/images/noCart.png' />
  103. </view>
  104. <recommend :hostProduct='hostProduct'></recommend>
  105. </view>
  106. </view>
  107. </view>
  108. <!-- 管理操作 -->
  109. <view class='footer acea-row row-between-wrapper' v-if="cartList.valid.length > 0">
  110. <view>
  111. <checkbox-group @change="checkboxAllChange">
  112. <checkbox value="all" :checked="!!isAllSelect" />
  113. <text class='checkAll'>全选({{selectValue.length}})</text>
  114. </checkbox-group>
  115. </view>
  116. <view class='money acea-row row-middle' v-if="footerswitch">
  117. <text class='font-color'>¥{{ fen2yuan(selectCountPrice) }}</text>
  118. <form @submit="subOrder" report-submit='true'>
  119. <button class='placeOrder bg-color' formType="submit">立即下单</button>
  120. </form>
  121. </view>
  122. <view class='button acea-row row-middle' v-else>
  123. <form @submit="subCollect" report-submit='true'>
  124. <button class='bnt cart-color' formType="submit">收藏</button>
  125. </form>
  126. <form @submit="subDel" report-submit='true'>
  127. <button class='bnt' formType="submit">删除</button>
  128. </form>
  129. </view>
  130. </view>
  131. <!-- SKU 不可用的商品,重新选择 SKU -->
  132. <productWindow
  133. :attr="attr"
  134. :isShow='1'
  135. :iSplus='1'
  136. :iScart='1'
  137. @ChangeAttr="ChangeAttr"
  138. @ChangeCartNum="ChangeCartNum"
  139. @iptCartNum="iptCartNum"
  140. @close="closeAttr"
  141. @goCat="reGoCat"
  142. />
  143. <view class="uni-p-b-96" />
  144. <view class="uni-p-b-98" />
  145. </view>
  146. </template>
  147. <script>
  148. let sysHeight = 0
  149. import { toLogin } from '@/libs/login.js';
  150. import { mapGetters } from "vuex";
  151. import recommend from '@/components/recommend';
  152. import productWindow from '@/components/productWindow';
  153. import * as TradeCartApi from '@/api/trade/cart.js';
  154. import * as ProductSpuApi from '@/api/product/spu.js'
  155. import * as ProductFavoriteApi from '@/api/product/favorite.js';
  156. import * as Util from '@/utils/util.js';
  157. import * as ProductUtil from '@/utils/product.js';
  158. import * as PromotionActivityApi from '@/api/promotion/activity.js';
  159. export default {
  160. components: {
  161. recommend,
  162. productWindow,
  163. },
  164. data() {
  165. return {
  166. sysHeight: sysHeight,
  167. goodsHidden: false,
  168. footerswitch: true,
  169. hostProduct: [],
  170. hotPage: 1,
  171. hotLimit: 10,
  172. hotScroll: false,
  173. // 购物车列表
  174. cartList: { // 购物车列表
  175. valid: [], // 有效
  176. invalid: [] // 无效
  177. },
  178. isAllSelect: false, // 是否全选
  179. selectValue: [], // 选中的数据
  180. selectCountPrice: 0.00, // 选中的金额
  181. cartCount: 0, // 选中的商品数量
  182. canShow: false, // 是否可展示
  183. // 重选
  184. cartId: 0, // 重选的 cart id
  185. product_id: 0, // 重选的 SPU 编号
  186. attr: { // productWindow 组件,使用该属性
  187. cartAttr: false, // 是否打开属性的选择弹出
  188. // ↓↓↓ 属性数组,结构为:id = 属性编号;name = 属性编号的名字;values[].id = 属性值的编号,values[].name = 属性值的名字;index = 选中的属性值的名字
  189. properties: [],
  190. productSelect: {} // 选中的 SKU
  191. },
  192. spu: {}, // 商品 SPU 详情
  193. skuMap: [], // 商品 SKU Map
  194. attrValue: '', // 已选属性名的拼接,例如说 红色,大 这样的格式
  195. };
  196. },
  197. computed: mapGetters(['isLogin']),
  198. onLoad: function(options) {
  199. if (!this.isLogin) {
  200. toLogin();
  201. }
  202. },
  203. onShow: function() {
  204. this.canShow = false
  205. if (this.isLogin === true) {
  206. // 加载热门商品
  207. this.hotPage = 1;
  208. this.hotLimit = 10;
  209. this.hostProduct = [];
  210. this.hotScroll = false;
  211. this.loadend = false;
  212. this.getHostProduct()
  213. // 加载购物车列表
  214. this.footerswitch = true;
  215. this.hotScroll = false;
  216. this.cartList = {
  217. valid: [],
  218. invalid: []
  219. };
  220. this.isAllSelect = false; // 全选
  221. this.selectValue = []; // 选中的数据
  222. this.selectCountPrice = 0.00;
  223. this.cartCount = 0;
  224. this.getCartList();
  225. }
  226. },
  227. methods: {
  228. /**
  229. * 修改购物车
  230. */
  231. reGoCat: function() {
  232. const productSelect = this.skuMap[this.attrValue];
  233. // 如果有属性,没有选择,提示用户选择
  234. if (this.attr.properties.length &&
  235. productSelect === undefined) {
  236. return this.$util.Tips({
  237. title: "产品库存不足,请选择其它"
  238. });
  239. }
  240. TradeCartApi.resetCart({
  241. id: this.cartId,
  242. skuId: this.attr.productSelect.id,
  243. count: this.attr.productSelect.cart_num
  244. }).then(res => {
  245. this.attr.cartAttr = false;
  246. this.$util.Tips({
  247. title: this.$t(`添加购物车成功`)
  248. });
  249. this.cartList.valid = [];
  250. this.getCartList();
  251. }).catch(res => {
  252. return this.$util.Tips({
  253. title: res
  254. });
  255. });
  256. },
  257. /**
  258. * 重选 SKU
  259. */
  260. reElection: function(item) {
  261. this.getGoodsDetails(item)
  262. },
  263. /**
  264. * 获取产品详情
  265. */
  266. getGoodsDetails: function(item) {
  267. uni.showLoading({
  268. title: '加载中',
  269. mask: true
  270. });
  271. this.cartId = item.id;
  272. this.product_id = item.spu.id;
  273. ProductSpuApi.getSpuDetail(item.spu.id).then(res => {
  274. uni.hideLoading();
  275. this.attr.cartAttr = true;
  276. let spu = res.data;
  277. let properties = ProductUtil.convertProductPropertyList(res.data.skus);
  278. let skuMap = ProductUtil.convertProductSkuMap(res.data.skus);
  279. // 设置变量
  280. this.$set(this, 'spu', spu);
  281. this.$set(this.attr, 'properties', properties);
  282. this.$set(this, 'skuMap', skuMap);
  283. this.selectDefaultSku();
  284. }).catch(err => {
  285. uni.hideLoading();
  286. })
  287. },
  288. /**
  289. * 关闭 productWindow 弹窗
  290. */
  291. closeAttr: function () {
  292. this.$set(this.attr, "cartAttr", false);
  293. },
  294. /**
  295. * 属性变动赋值
  296. *
  297. * @param newSkuKey 新的 skuKey
  298. * @param propertyIndex properties 的下标
  299. * @param valueIndex values 的下标
  300. */
  301. ChangeAttr: function(newSkuKey, propertyIndex, valueIndex) {
  302. // SKU
  303. let sku = this.skuMap[newSkuKey];
  304. if (!sku) {
  305. return;
  306. }
  307. this.$set(this.attr.productSelect, "id", sku.id);
  308. this.$set(this.attr.productSelect, "picUrl", sku.picUrl);
  309. this.$set(this.attr.productSelect, "price", sku.price);
  310. this.$set(this.attr.productSelect, "stock", sku.stock);
  311. this.$set(this.attr.productSelect, "cart_num", 1);
  312. // SKU 关联属性
  313. this.$set(this.attr.properties[propertyIndex], 'index',
  314. this.attr.properties[propertyIndex].values[valueIndex].name);
  315. this.$set(this, "attrValue", newSkuKey);
  316. },
  317. /**
  318. * 查找默认选中的 sku,设置到 attr.productSelect 中
  319. *
  320. * 先找有库存的 SKU,否则找第一个 SKU
  321. */
  322. selectDefaultSku: function() {
  323. const properties = this.attr.properties;
  324. // 获得选中的属性值的名字,例如说 "黑色,大",则 skuKey = ["黑色", "大"]
  325. let skuKey = undefined;
  326. for (let key in this.skuMap) {
  327. if (this.skuMap[key].stock > 0) {
  328. skuKey = key.split(",");
  329. break;
  330. }
  331. }
  332. if (!skuKey) { // 如果找不到,则选中第一个
  333. skuKey = Object.keys(this.skuMap)[0].split(",");
  334. }
  335. // 使用 index 属性表示当前选中的,值为属性值的名字
  336. for (let i = 0; i < properties.length; i++) {
  337. this.$set(properties[i], "index", skuKey[i]);
  338. }
  339. let sku = this.skuMap[skuKey.join(",")];
  340. if (!sku) {
  341. return
  342. }
  343. this.$set(this.attr.productSelect, "spuName", this.spu.name);
  344. this.$set(this.attr.productSelect, "id", sku.id);
  345. this.$set(this.attr.productSelect, "picUrl", sku.picUrl);
  346. this.$set(this.attr.productSelect, "price", sku.price);
  347. this.$set(this.attr.productSelect, "stock", sku.stock);
  348. this.$set(this.attr.productSelect, "cart_num", 1);
  349. this.$set(this, "attrValue", skuKey.join(","));
  350. },
  351. /**
  352. * 购物车数量加和数量减
  353. *
  354. * @param changeValue true 增加;false 减少
  355. */
  356. ChangeCartNum: function(changeValue) {
  357. /// 获取当前 sku
  358. let sku = this.attr.productSelect;
  359. if (!sku) {
  360. return;
  361. }
  362. // 设置数量
  363. let stock = sku.stock || 0;
  364. if (changeValue) {
  365. sku.cart_num++;
  366. if (sku.cart_num > stock) {
  367. this.$set(this.attr.productSelect, "cart_num", stock);
  368. }
  369. } else {
  370. sku.cart_num--;
  371. if (sku.cart_num < 1) {
  372. this.$set(this.attr.productSelect, "cart_num", 1);
  373. }
  374. }
  375. },
  376. /**
  377. * 购物车手动填写
  378. */
  379. iptCartNum: function(number) {
  380. this.$set(this.attr.productSelect, 'cart_num', number ? number : 1);
  381. },
  382. /**
  383. * 删除选中的商品
  384. */
  385. subDel: function(event) {
  386. const selectValue = this.selectValue;
  387. if (selectValue.length === 0) {
  388. return this.$util.Tips({
  389. title: '请选择产品'
  390. });
  391. }
  392. TradeCartApi.deleteCart(selectValue).then(res => {
  393. this.getCartList();
  394. });
  395. },
  396. getSelectValueProductId: function() {
  397. const validList = this.cartList.valid;
  398. const selectValue = this.selectValue;
  399. let productId = [];
  400. if (selectValue.length > 0) {
  401. for (let index in validList) {
  402. if (this.inArray(validList[index].id, selectValue)) {
  403. productId.push(validList[index].spu.id);
  404. }
  405. }
  406. }
  407. return productId;
  408. },
  409. /**
  410. * 收藏选中的商品
  411. */
  412. subCollect: function(event) {
  413. const spuIds = this.getSelectValueProductId();
  414. if (spuIds.length === 0) {
  415. return this.$util.Tips({
  416. title: '请选择产品'
  417. });
  418. }
  419. ProductFavoriteApi.createFavoriteList(spuIds).then(res => {
  420. return this.$util.Tips({
  421. title: '收藏成功',
  422. icon: 'success'
  423. });
  424. }).catch(err => {
  425. return that.$util.Tips({
  426. title: err
  427. });
  428. });
  429. },
  430. /**
  431. * 立即下单
  432. */
  433. subOrder: function(event) {
  434. const selectValue = this.selectValue;
  435. if (selectValue.length === 0) {
  436. return this.$util.Tips({
  437. title: '请选择产品'
  438. });
  439. }
  440. uni.navigateTo({
  441. url: '/pages/users/order_confirm/index?cartIds=' + selectValue.join(',')
  442. });
  443. },
  444. /**
  445. * 预下单
  446. */
  447. getPreOrder: function() {
  448. let shoppingCartId = this.selectValue.map(item => {
  449. return {
  450. "shoppingCartId": Number(item)
  451. }
  452. })
  453. this.$Order.getPreOrder("shoppingCart", shoppingCartId);
  454. },
  455. /**
  456. * 全选 / 全不选
  457. */
  458. checkboxAllChange: function(event) {
  459. let value = event.detail.value;
  460. if (value.length > 0) {
  461. this.setAllSelectValue(true)
  462. } else {
  463. this.setAllSelectValue(false)
  464. }
  465. },
  466. /**
  467. * 全选 / 全不选
  468. */
  469. setAllSelectValue: function(selected) {
  470. const valid = this.cartList.valid;
  471. const values = [];
  472. for (const item of valid) {
  473. values.push(item.id.toString());
  474. }
  475. TradeCartApi.updateCartSelected({
  476. ids: values,
  477. selected: selected
  478. }).then(res => {
  479. this.getCartList()
  480. })
  481. },
  482. /**
  483. * 更新是否选中
  484. */
  485. checkboxChange: function(event) {
  486. // 由于 uniapp 不支持直接通过 event 获取到变更的值,所以只能通过比对
  487. const valid = this.cartList.valid;
  488. const oldValues = [];
  489. for (const item of valid) {
  490. if (item.canChecked && item.selected) {
  491. oldValues.push(item.id.toString());
  492. } else if (!this.footerswitch && item.selected) {
  493. oldValues.push(item.id.toString());
  494. }
  495. }
  496. const newValues = event.detail.value;
  497. // 情况一:选中
  498. const selectedItem = newValues.find(item => !oldValues.includes(item));
  499. if (selectedItem) {
  500. TradeCartApi.updateCartSelected({
  501. ids: [selectedItem],
  502. selected: true
  503. }).then(res => {
  504. this.getCartList()
  505. })
  506. return;
  507. }
  508. // 情况二:取消选中
  509. const cancelSelectedItem = oldValues.find(item => !newValues.includes(item));
  510. if (cancelSelectedItem) {
  511. TradeCartApi.updateCartSelected({
  512. ids: [cancelSelectedItem],
  513. selected: false
  514. }).then(res => {
  515. this.getCartList()
  516. })
  517. }
  518. },
  519. /**
  520. * 合计金额、数量
  521. */
  522. switchSelect: function() {
  523. // 是否全选
  524. const validList = this.cartList.valid;
  525. const selectValue = [];
  526. let isAllSelect = true;
  527. if (validList && validList.length > 0) {
  528. for (const item of validList) {
  529. if (item.canChecked) {
  530. if (item.selected) {
  531. selectValue.push(item.id);
  532. } else {
  533. isAllSelect = false;
  534. }
  535. } else {
  536. if (!this.footerswitch && item.selected) {
  537. selectValue.push(item.id);
  538. }
  539. if (!this.footerswitch && !item.selected) {
  540. isAllSelect = false;
  541. }
  542. }
  543. }
  544. }
  545. this.selectValue = selectValue;
  546. this.isAllSelect = isAllSelect;
  547. // 合计金额
  548. let selectCountPrice = 0.00;
  549. let cartCount = 0
  550. for (let index in validList) {
  551. if (this.inArray(validList[index].id, selectValue)
  552. && validList[index].sku) {
  553. selectCountPrice = this.$util.$h.Add(selectCountPrice, this.$util.$h.Mul(validList[index].count, validList[index].sku.price))
  554. cartCount += validList[index].count
  555. }
  556. }
  557. this.selectCountPrice = selectCountPrice;
  558. this.cartCount = cartCount;
  559. },
  560. /**
  561. * 减少购买数量
  562. */
  563. subCart: function(index) {
  564. let item = this.cartList.valid[index];
  565. if (item.count < 1) {
  566. this.$util.Tips({
  567. title: '商品数量不能减少!'
  568. });
  569. return;
  570. }
  571. // 本地先修改状态
  572. item.count = Number(item.count) - 1;
  573. if (item.count <= 1) {
  574. item.count = 1;
  575. item.numSub = true;
  576. } else {
  577. item.numSub = false;
  578. item.numAdd = false;
  579. }
  580. // 提交修改
  581. this.setCartNum(item.id, item.count);
  582. },
  583. /**
  584. * 增加购买数量
  585. */
  586. addCart: function(index) {
  587. // 本地先修改状态
  588. let item = this.cartList.valid[index];
  589. item.count = Number(item.count) + 1
  590. if (item.count >= item.sku.stock) {
  591. item.count = item.sku.stock;
  592. item.numAdd = true;
  593. item.numSub = false;
  594. } else {
  595. item.numAdd = false;
  596. item.numSub = false;
  597. }
  598. // 提交修改
  599. this.setCartNum(item.id, item.count);
  600. },
  601. /**
  602. * 修改购物项为指定数量
  603. */
  604. setCartNum(cartId, cartNum) {
  605. TradeCartApi.updateCartCount({
  606. id: cartId,
  607. count: cartNum
  608. }).then(res => {
  609. // 加载最新状态
  610. this.getCartList()
  611. });
  612. },
  613. getCartData() {
  614. return new Promise((resolve, reject) => {
  615. TradeCartApi.getCartList().then((res) => {
  616. resolve(res.data);
  617. }).catch(function(err) {
  618. this.canShow = true;
  619. this.$util.Tips({
  620. title: err
  621. });
  622. })
  623. });
  624. },
  625. async getCartList() {
  626. uni.showLoading({
  627. title: '加载中',
  628. mask: true
  629. });
  630. // 加载购物车
  631. const cartList = await this.getCartData();
  632. const validList = cartList.validList;
  633. const invalidList = cartList.invalidList;
  634. // 有效的购物车
  635. if (validList.length > 0) {
  636. for (let index in validList) {
  637. // 设置是否可减少(到底,不可减少)
  638. validList[index].numSub = validList[index].cartNum === 1;
  639. // 设置是否可添加(到顶,不可添加)
  640. const sku = validList[index].sku;
  641. validList[index].numAdd = sku && validList[index].count < sku.stock;
  642. // 设置为选中,并添加到 selectValue 数组中
  643. // why?库存不足时,可以引导选择该 SPU 对应的其它 SKU。而 invalidList 是 SPU 不存在或者库存彻底不足
  644. if (sku && sku.stock > 0) {
  645. validList[index].canChecked = true; // 是否可选中:是
  646. } else {
  647. validList[index].canChecked = false; // 是否可选中:否
  648. }
  649. }
  650. }
  651. this.$set(this.cartList, 'valid', validList);
  652. this.switchSelect();
  653. // 无效的购物车
  654. this.$set(this.cartList, 'invalid', invalidList);
  655. // 标记加载结束
  656. this.canShow = true;
  657. uni.hideLoading();
  658. },
  659. /**
  660. * 获得“热门推荐”
  661. */
  662. getHostProduct: function() {
  663. ProductSpuApi.getSpuPage({
  664. recommendType: 'hot',
  665. pageNo: this.hotPage,
  666. pageSize: this.hotLimit
  667. }).then(res => {
  668. const good_list = res.data.list;
  669. this.hotPage++
  670. this.hotScroll = good_list.length < this.hotLimit
  671. // 设置营销活动
  672. const spuIds = good_list.map(item => item.id);
  673. if (spuIds.length > 0) {
  674. PromotionActivityApi.getActivityListBySpuIds(spuIds).then(res => {
  675. ProductUtil.setActivityList(good_list, res.data);
  676. this.hostProduct = this.hostProduct.concat(good_list) // 放在此处,避免 Vue 监控不到数组里的元素变化
  677. });
  678. }
  679. });
  680. },
  681. goodsOpen: function() {
  682. this.goodsHidden = !this.goodsHidden;
  683. },
  684. /**
  685. * 切换到管理
  686. */
  687. manage: function() {
  688. this.footerswitch = !this.footerswitch;
  689. this.switchSelect();
  690. },
  691. /**
  692. * 清空
  693. */
  694. unsetCart: function() {
  695. const ids = [];
  696. for (let i = 0, len = this.cartList.invalid.length; i < len; i++) {
  697. ids.push(this.cartList.invalid[i].id);
  698. }
  699. TradeCartApi.deleteCart(ids).then(res => {
  700. this.$util.Tips({
  701. title: '清除成功'
  702. });
  703. this.getCartList()
  704. }).catch(res => {
  705. });
  706. },
  707. fen2yuan(price) {
  708. return Util.fen2yuan(price)
  709. },
  710. inArray: function(search, array) {
  711. for (let i in array) {
  712. if (array[i] === search) {
  713. return true;
  714. }
  715. }
  716. return false;
  717. },
  718. },
  719. onReachBottom() {
  720. if (this.cartList.valid.length === 0
  721. && this.cartList.invalid.length === 0
  722. && this.hotPage != 1) {
  723. this.getHostProduct();
  724. }
  725. }
  726. }
  727. </script>
  728. <style scoped lang="scss">
  729. .invalidClas {
  730. position: relative;
  731. z-index: 111;
  732. top: -120rpx;
  733. }
  734. .invalidClasNO {
  735. position: static;
  736. margin-top: 15px;
  737. }
  738. .cartBox {
  739. // background-color: #fff;
  740. }
  741. .shoppingCart {
  742. /* #ifdef H5 */
  743. // padding-bottom: 0;
  744. // padding-bottom: constant(safe-area-inset-bottom);
  745. // padding-bottom: env(safe-area-inset-bottom);
  746. /* #endif */
  747. }
  748. .shoppingCart .labelNav {
  749. height: 178rpx;
  750. padding: 30rpx 30rpx 0 30rpx;
  751. font-size: 22rpx;
  752. color: #fff;
  753. position: fixed;
  754. left: 0;
  755. width: 100%;
  756. box-sizing: border-box;
  757. background-color: $theme-color;
  758. z-index: 5;
  759. top: 0;
  760. }
  761. .shoppingCart .labelNav .item .iconfont {
  762. font-size: 25rpx;
  763. margin-right: 10rpx;
  764. }
  765. .shoppingCart .nav {
  766. width: 92%;
  767. height: 90rpx;
  768. background-color: #fff;
  769. padding: 0 24rpx;
  770. -webkit-box-sizing: border-box;
  771. box-sizing: border-box;
  772. font-size: 28rpx;
  773. color: #282828;
  774. position: fixed;
  775. left: 30rpx;
  776. z-index: 6;
  777. top: 94rpx;
  778. border-top-left-radius: 14rpx;
  779. border-top-right-radius: 14rpx;
  780. }
  781. .shoppingCart .nav .num {
  782. margin-left: 12rpx;
  783. }
  784. .shoppingCart .nav .administrate {
  785. font-size: 28rpx;
  786. color: #333333;
  787. }
  788. .shoppingCart .noCart {
  789. margin-top: 171rpx;
  790. background-color: #fff;
  791. padding-top: 0.1rpx;
  792. }
  793. .shoppingCart .noCart .pictrue {
  794. width: 414rpx;
  795. height: 336rpx;
  796. margin: 78rpx auto 56rpx auto;
  797. }
  798. .shoppingCart .noCart .pictrue image {
  799. width: 100%;
  800. height: 100%;
  801. }
  802. .shoppingCart .list {
  803. width: 100%;
  804. margin-top: 178rpx;
  805. overflow: hidden;
  806. border-bottom-left-radius: 14rpx;
  807. border-bottom-right-radius: 14rpx;
  808. }
  809. .shoppingCart .list .item {
  810. padding: 24rpx;
  811. background-color: #fff;
  812. }
  813. .shoppingCart .list .item .picTxt {
  814. width: 582rpx;
  815. position: relative;
  816. }
  817. .shoppingCart .list .item .picTxt .pictrue {
  818. width: 160rpx;
  819. height: 160rpx;
  820. }
  821. .shoppingCart .list .item .picTxt .pictrue image {
  822. width: 100%;
  823. height: 100%;
  824. border-radius: 6rpx;
  825. }
  826. .shoppingCart .list .item .picTxt .text {
  827. width: 396rpx;
  828. font-size: 28rpx;
  829. color: #282828;
  830. }
  831. .shoppingCart .list .item .picTxt .text .reColor {
  832. color: #999;
  833. }
  834. .shoppingCart .list .item .picTxt .text .reElection {
  835. margin-top: 20rpx;
  836. }
  837. .shoppingCart .list .item .picTxt .text .reElection .title {
  838. font-size: 24rpx;
  839. }
  840. .shoppingCart .list .item .picTxt .text .reElection .reBnt {
  841. width: 120rpx;
  842. height: 46rpx;
  843. border-radius: 23rpx;
  844. font-size: 26rpx;
  845. }
  846. .shoppingCart .list .item .picTxt .text .infor {
  847. font-size: 24rpx;
  848. color: #999999;
  849. margin-top: 16rpx;
  850. }
  851. .shoppingCart .list .item .picTxt .text .money {
  852. font-size: 32rpx;
  853. color: #E93323;
  854. margin-top: 28rpx;
  855. font-weight: 600;
  856. }
  857. .shoppingCart .list .item .picTxt .carnum {
  858. height: 47rpx;
  859. position: absolute;
  860. bottom: 7rpx;
  861. right: 0;
  862. }
  863. .shoppingCart .list .item .picTxt .carnum view {
  864. border: 1rpx solid #a4a4a4;
  865. width: 66rpx;
  866. text-align: center;
  867. height: 100%;
  868. line-height: 44rpx;
  869. font-size: 28rpx;
  870. color: #a4a4a4;
  871. }
  872. .shoppingCart .list .item .picTxt .carnum .reduce {
  873. border-right: 0;
  874. border-radius: 3rpx 0 0 3rpx;
  875. border-radius: 22rpx 0rpx 0rpx 22rpx;
  876. font-size: 34rpx;
  877. line-height: 40rpx;
  878. }
  879. .shoppingCart .list .item .picTxt .carnum .reduce.on {
  880. border-color: #e3e3e3;
  881. color: #dedede;
  882. }
  883. .shoppingCart .list .item .picTxt .carnum .plus {
  884. border-left: 0;
  885. border-radius: 0 3rpx 3rpx 0;
  886. border-radius: 0rpx 22rpx 22rpx 0rpx;
  887. font-size: 34rpx;
  888. line-height: 40rpx;
  889. }
  890. .shoppingCart .list .item .picTxt .carnum .num {
  891. color: #282828;
  892. }
  893. .shoppingCart .invalidGoods {
  894. background-color: #fff;
  895. margin-top: 30rpx;
  896. /* #ifdef MP */
  897. margin-top: 140rpx;
  898. /* #endif */
  899. }
  900. .shoppingCart .invalidGoods .goodsNav {
  901. width: 100%;
  902. height: 90rpx;
  903. padding: 0 24rpx;
  904. box-sizing: border-box;
  905. font-size: 28rpx;
  906. color: #333333;
  907. }
  908. .shoppingCart .invalidGoods .goodsNav .iconfont {
  909. color: #424242;
  910. font-size: 28rpx;
  911. margin-right: 17rpx;
  912. }
  913. .shoppingCart .invalidGoods .goodsNav .del {
  914. font-size: 26rpx;
  915. color: #333;
  916. }
  917. .shoppingCart .invalidGoods .goodsNav .del .icon-shanchu1 {
  918. color: #333;
  919. font-size: 33rpx;
  920. vertical-align: -2rpx;
  921. margin-right: 8rpx;
  922. }
  923. .shoppingCart .invalidGoods .goodsList .item {
  924. padding: 24rpx;
  925. }
  926. .shoppingCart .invalidGoods .goodsList .picTxt {
  927. width: 576rpx;
  928. }
  929. .shoppingCart .invalidGoods .goodsList .item .invalid {
  930. font-size: 22rpx;
  931. color: #CCCCCC;
  932. height: 36rpx;
  933. border-radius: 3rpx;
  934. text-align: center;
  935. line-height: 36rpx;
  936. }
  937. .shoppingCart .invalidGoods .goodsList .item .pictrue {
  938. width: 160rpx;
  939. height: 160rpx;
  940. }
  941. .shoppingCart .invalidGoods .goodsList .item .pictrue image {
  942. width: 100%;
  943. height: 100%;
  944. border-radius: 6rpx;
  945. }
  946. .shoppingCart .invalidGoods .goodsList .item .text {
  947. width: 396rpx;
  948. font-size: 28rpx;
  949. color: #999;
  950. height: 140rpx;
  951. }
  952. .shoppingCart .invalidGoods .goodsList .item .text .name {
  953. width: 100%;
  954. }
  955. .shoppingCart .invalidGoods .goodsList .item .text .infor {
  956. font-size: 24rpx;
  957. }
  958. .shoppingCart .invalidGoods .goodsList .item .text .end {
  959. font-size: 26rpx;
  960. color: #bbb;
  961. }
  962. .footer {
  963. z-index: 9;
  964. width: 100%;
  965. height: 100rpx;
  966. background-color: #fff;
  967. position: fixed;
  968. padding: 0 24rpx;
  969. box-sizing: border-box;
  970. border-top: 1rpx solid #eee;
  971. // border-bottom: 1px solid #EEEEEE;
  972. /* #ifdef H5 */
  973. bottom: 98rpx;
  974. /* #endif */
  975. /* #ifdef MP */
  976. bottom: 0;
  977. /* #endif */
  978. /* #ifndef MP */
  979. // bottom: 98rpx;
  980. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  981. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  982. /* #endif */
  983. }
  984. .footer .checkAll {
  985. font-size: 28rpx;
  986. color: #282828;
  987. margin-left: 14rpx;
  988. }
  989. .footer .money {
  990. font-size: 30rpx;
  991. .font-color {
  992. font-weight: 600;
  993. }
  994. }
  995. .footer .placeOrder {
  996. color: #fff;
  997. font-size: 30rpx;
  998. width: 226rpx;
  999. height: 70rpx;
  1000. border-radius: 50rpx;
  1001. text-align: center;
  1002. line-height: 70rpx;
  1003. margin-left: 22rpx;
  1004. }
  1005. .footer .button .bnt {
  1006. font-size: 28rpx;
  1007. color: #999;
  1008. border-radius: 50rpx;
  1009. border: 1px solid #999;
  1010. width: 160rpx;
  1011. height: 60rpx;
  1012. text-align: center;
  1013. line-height: 60rpx;
  1014. }
  1015. .footer .button form~form {
  1016. margin-left: 17rpx;
  1017. }
  1018. .uni-p-b-96 {
  1019. height: 96rpx;
  1020. }
  1021. </style>