小程序端工程代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
16 KiB

  1. <template>
  2. <view class="shoppingCart">
  3. <view v-if="$store.getters.token||userInfo.uid">
  4. <view class="labelNav acea-row row-around row-middle">
  5. <view class="item">
  6. <text class="iconfont icon-xuanzhong"></text>100%正品保证
  7. </view>
  8. <view class="item">
  9. <text class="iconfont icon-xuanzhong"></text>所有商品精挑细选
  10. </view>
  11. <view class="item">
  12. <text class="iconfont icon-xuanzhong"></text>售后无忧
  13. </view>
  14. </view>
  15. <view class="nav acea-row row-between-wrapper">
  16. <view>
  17. 购物数量
  18. <text class="num font-color-red">{{ count }}</text>
  19. </view>
  20. <view v-if="cartList.valid.length > 0" class="administrate acea-row row-center-wrapper" @click="manage">
  21. {{ footerswitch ? '取消' : '管理' }}</view>
  22. </view>
  23. <view v-if="validList.length > 0 || cartList.invalid.length > 0">
  24. <view class="list">
  25. <view class="item acea-row row-between-wrapper" v-for="(item, cartListValidIndex) in validList"
  26. :key="cartListValidIndex">
  27. <view class="select-btn">
  28. <view class="checkbox-wrapper">
  29. <checkbox-group @change="switchSelect(cartListValidIndex)">
  30. <label class="well-check">
  31. <checkbox color="#eb3729" value :checked="item.checked"></checkbox>
  32. </label>
  33. </checkbox-group>
  34. </view>
  35. </view>
  36. <view class="picTxt acea-row row-between-wrapper">
  37. <view class="pictrue" @click="goGoodsCon(item)">
  38. <image :src="item.productInfo.attrInfo.image" v-if="item.productInfo.attrInfo" />
  39. <image :src="item.productInfo.image" v-else />
  40. </view>
  41. <view class="text">
  42. <view class="line1">{{ item.productInfo.storeName }}</view>
  43. <view class="infor line1" v-if="item.productInfo.attrInfo">属性{{ item.productInfo.attrInfo.sku }}</view>
  44. <view class="money">{{ item.truePrice }}</view>
  45. </view>
  46. <view class="carnum acea-row row-center-wrapper">
  47. <view class="reduce" :class="validList[cartListValidIndex].cartNum <= 1 ? 'on' : ''"
  48. @click.prevent="reduce(cartListValidIndex)">-</view>
  49. <view class="num">{{ item.cartNum }}</view>
  50. <view class="plus" v-if="validList[cartListValidIndex].attrInfo"
  51. :class="validList[cartListValidIndex].cartNum >= validList[cartListValidIndex].attrInfo.stock ? 'on' : ''"
  52. @click.prevent="plus(cartListValidIndex)">+</view>
  53. <view class="plus" v-else
  54. :class="validList[cartListValidIndex].cartNum >= validList[cartListValidIndex].stock ? 'on' : ''"
  55. @click.prevent="plus(cartListValidIndex)">+</view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <view class="invalidGoods" v-if="cartList.invalid.length > 0">
  61. <view class="goodsNav acea-row row-between-wrapper">
  62. <view @click="goodsOpen">
  63. <text class="iconfont" :class="goodsHidden === true ? 'icon-xiangyou' : 'icon-xiangxia'"></text>失效商品
  64. </view>
  65. <view class="del" @click="delInvalidGoods">
  66. <text class="iconfont icon-shanchu1"></text>清空
  67. </view>
  68. </view>
  69. <view class="goodsList" :hidden="goodsHidden">
  70. <view v-for="(item, cartListinvalidIndex) in cartList.invalid" :key="cartListinvalidIndex">
  71. <view @click="goGoodsCon(item)" class="item acea-row row-between-wrapper" v-if="item.productInfo">
  72. <view class="invalid acea-row row-center-wrapper">失效</view>
  73. <view class="pictrue">
  74. <image :src="item.productInfo.attrInfo.image" v-if="item.productInfo.attrInfo" />
  75. <image :src="item.productInfo.image" v-else />
  76. </view>
  77. <view class="text acea-row row-column-between">
  78. <view class="line1">{{ item.productInfo.storeName }}</view>
  79. <view class="infor line1" v-if="item.productInfo.attrInfo">属性{{ item.productInfo.attrInfo.sku }}
  80. </view>
  81. <view class="acea-row row-between-wrapper">
  82. <view class="end">该商品已下架</view>
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. <!--购物车暂无商品-->
  91. <view class="noCart" v-if="cartList.valid.length === 0 && cartList.invalid.length === 0">
  92. <view class="pictrue">
  93. <image :src="`${$VUE_APP_RESOURCES_URL}/images/noCart.png`" />
  94. </view>
  95. <Recommend></Recommend>
  96. </view>
  97. <view style="height:210rpx"></view>
  98. <view :class="{'footer acea-row row-between-wrapper':true,'footer-h5':isH5}" v-if="cartList.valid.length > 0">
  99. <view>
  100. <view class="select-btn">
  101. <view class="checkbox-wrapper">
  102. <!-- <label class="well-check">
  103. <input
  104. type="checkbox"
  105. name
  106. value
  107. :checked="isAllSelect && cartCount > 0"
  108. @click="allChecked"
  109. />
  110. <i class="icon"></i>
  111. <text class="checkAll">全选 ({{ cartCount }})</text>
  112. </label>-->
  113. <checkbox-group @change="allChecked">
  114. <label class="well-check">
  115. <checkbox color="#eb3729" value="allSelect" :checked="isAllSelect && cartCount > 0"></checkbox>
  116. <text class="checkAll">全选 ({{ cartCount }})</text>
  117. </label>
  118. </checkbox-group>
  119. </view>
  120. </view>
  121. </view>
  122. <view class="money acea-row row-middle" v-if="footerswitch === false">
  123. <text class="font-color-red">{{ countmoney }}</text>
  124. <view class="placeOrder bg-color-red" @click="placeOrder">立即下单</view>
  125. </view>
  126. <view class="button acea-row row-middle" v-else>
  127. <!-- <view class="bnt cart-color" @click="collectAll">收藏</view> -->
  128. <view class="bnt" @click="delgoods">删除</view>
  129. </view>
  130. </view>
  131. </view>
  132. <Authorization v-else />
  133. </view>
  134. </template>
  135. <script>
  136. import Recommend from "@/components/Recommend";
  137. import Authorization from "@/pages/authorization/index";
  138. import {
  139. mapGetters
  140. } from "vuex";
  141. import {
  142. getCartList,
  143. postCartDel,
  144. changeCartNum,
  145. getCartCount
  146. } from "@/api/store";
  147. import {
  148. postCollectAll
  149. } from "@/api/user";
  150. import {
  151. mul,
  152. add
  153. } from "@/utils/bc";
  154. import cookie from "@/utils/store/cookie";
  155. const CHECKED_IDS = "cart_checked";
  156. export default {
  157. name: "ShoppingCart",
  158. components: {
  159. Recommend,
  160. Authorization
  161. },
  162. props: {},
  163. data: function () {
  164. return {
  165. cartList: {
  166. invalid: [],
  167. valid: []
  168. },
  169. isH5: false,
  170. validList: [],
  171. isAllSelect: false,
  172. cartCount: 0,
  173. countmoney: 0,
  174. goodsHidden: true,
  175. footerswitch: false,
  176. count: 0,
  177. checkedIds: [],
  178. loaded: false
  179. };
  180. },
  181. computed: mapGetters(["userInfo", "token"]),
  182. // watch: {
  183. // $yroute(n) {
  184. // if (n.name === "ShoppingCart") {
  185. // this.carnum();
  186. // this.countMoney();
  187. // this.getCartList();
  188. // this.gainCount();
  189. // this.goodsHidden = true;
  190. // this.footerswitch = false;
  191. // }
  192. // },
  193. // cartList(list) {
  194. // this.validList = list.valid;
  195. // }
  196. // },
  197. watch: {
  198. userInfo(user) {
  199. if (user.uid) {
  200. this.carnum();
  201. this.countMoney();
  202. this.getCartList();
  203. this.gainCount();
  204. }
  205. },
  206. token(token) {
  207. if (this.userInfo.uid) {
  208. this.carnum();
  209. this.countMoney();
  210. this.getCartList();
  211. this.gainCount();
  212. }
  213. },
  214. cartList(list) {
  215. this.validList = list.valid;
  216. }
  217. },
  218. onShow: function () {
  219. // #ifdef H5
  220. this.isH5 = true
  221. // #endif
  222. console.log(this.userInfo)
  223. if (this.userInfo.uid) {
  224. this.carnum();
  225. this.countMoney();
  226. this.getCartList();
  227. this.gainCount();
  228. }
  229. },
  230. methods: {
  231. goGoodsCon(item) {
  232. this.$yrouter.push({
  233. path: "/pages/shop/GoodsCon/index",
  234. query: {
  235. id: item.productId
  236. }
  237. });
  238. },
  239. getCartList: function () {
  240. let that = this;
  241. getCartList().then(res => {
  242. that.cartList = res.data;
  243. let checkedIds = cookie.get(CHECKED_IDS) || [];
  244. if (!Array.isArray(checkedIds)) checkedIds = [];
  245. this.cartList.valid.forEach(cart => {
  246. if (checkedIds.indexOf(cart.id) !== -1) cart.checked = true;
  247. });
  248. if (checkedIds.length) {
  249. that.checkedIds = checkedIds;
  250. that.isAllSelect = checkedIds.length === this.cartList.valid.length;
  251. that.carnum();
  252. that.countMoney();
  253. }
  254. this.loaded = true;
  255. });
  256. },
  257. //删除商品;
  258. delgoods: function () {
  259. let that = this,
  260. id = [],
  261. valid = [],
  262. list = that.cartList.valid;
  263. list.forEach(function (val) {
  264. if (val.checked === true) {
  265. id.push(val.id);
  266. }
  267. });
  268. if (id.length === 0) {
  269. uni.showToast({
  270. title: "请选择产品",
  271. icon: "none",
  272. duration: 2000
  273. });
  274. return;
  275. }
  276. postCartDel(id).then(function () {
  277. list.forEach(function (val, i) {
  278. if (val.checked === false || val.checked === undefined)
  279. valid.push(list[i]);
  280. });
  281. that.$set(that.cartList, "valid", valid);
  282. that.carnum();
  283. that.countMoney();
  284. that.gainCount();
  285. that.getCartList();
  286. });
  287. },
  288. // //获取数量
  289. gainCount: function () {
  290. let that = this;
  291. getCartCount().then(res => {
  292. that.count = res.data.count;
  293. });
  294. },
  295. //清除失效产品;
  296. delInvalidGoods: function () {
  297. let that = this,
  298. id = [],
  299. list = that.cartList.invalid;
  300. list.forEach(function (val) {
  301. id.push(val.id);
  302. });
  303. postCartDel(id).then(function () {
  304. list.splice(0, list.length);
  305. that.gainCount();
  306. that.getCartList();
  307. });
  308. },
  309. //批量收藏;
  310. collectAll: function () {
  311. let that = this,
  312. data = {
  313. id: [],
  314. category: ""
  315. },
  316. list = that.cartList.valid;
  317. list.forEach(function (val) {
  318. if (val.checked === true) {
  319. data.id.push(val.product_id);
  320. data.category = val.type;
  321. }
  322. });
  323. if (data.id.length === 0) {
  324. uni.showToast({
  325. title: "请选择产品",
  326. icon: "none",
  327. duration: 2000
  328. });
  329. return;
  330. }
  331. postCollectAll(data).then(function () {
  332. uni.showToast({
  333. title: "收藏成功!",
  334. icon: "none",
  335. duration: 2000
  336. });
  337. });
  338. },
  339. //立即下单;
  340. placeOrder: function () {
  341. let that = this,
  342. list = that.cartList.valid,
  343. id = [];
  344. list.forEach(function (val) {
  345. if (val.checked === true) {
  346. id.push(val.id);
  347. }
  348. });
  349. if (id.length === 0) {
  350. uni.showToast({
  351. title: "请选择产品",
  352. icon: "none",
  353. duration: 2000
  354. });
  355. return;
  356. }
  357. this.$yrouter.push({
  358. path: "/pages/order/OrderSubmission/index",
  359. query: {
  360. id: id.join(",")
  361. }
  362. });
  363. },
  364. manage: function () {
  365. let that = this;
  366. that.footerswitch = !that.footerswitch;
  367. },
  368. goodsOpen: function () {
  369. let that = this;
  370. that.goodsHidden = !that.goodsHidden;
  371. },
  372. //加
  373. plus: function (index) {
  374. let that = this;
  375. let list = that.cartList.valid[index];
  376. list.cartNum++;
  377. if (list.attrInfo) {
  378. if (list.cartNum >= list.attrInfo.stock) {
  379. that.$set(list, "cart_num", list.attrInfo.stock);
  380. }
  381. } else {
  382. if (list.cartNum >= list.stock) {
  383. that.$set(list, "cart_num", list.stock);
  384. }
  385. }
  386. that.carnum();
  387. that.countMoney();
  388. that.syncCartNum(list);
  389. },
  390. //减
  391. reduce: function (index) {
  392. let that = this;
  393. let list = that.cartList.valid[index];
  394. if (list.cartNum <= 1) {
  395. uni.showToast({
  396. title: "已经是底线啦!",
  397. icon: "none",
  398. duration: 2000
  399. });
  400. return;
  401. }
  402. list.cartNum--;
  403. if (list.cartNum < 1) {
  404. that.$set(list, "cart_num", 1);
  405. }
  406. that.carnum();
  407. that.countMoney();
  408. that.syncCartNum(list);
  409. },
  410. syncCartNum(cart) {
  411. if (!cart.sync) {
  412. changeCartNum(cart.id, Math.max(cart.cartNum, 1) || 1)
  413. .then(res => {
  414. this.getCartList();
  415. this.gainCount();
  416. })
  417. .catch(error => {
  418. this.gainCount();
  419. uni.showToast({
  420. title: error.response.data.msg,
  421. icon: "none",
  422. duration: 2000
  423. });
  424. });
  425. }
  426. },
  427. //单选
  428. switchSelect: function (index) {
  429. let that = this,
  430. cart = that.cartList.valid[index],
  431. i = this.checkedIds.indexOf(cart.id);
  432. cart.checked = !cart.checked;
  433. if (i !== -1) this.checkedIds.splice(i, 1);
  434. if (cart.checked) {
  435. this.checkedIds.push(cart.id);
  436. }
  437. let len = that.cartList.valid.length;
  438. let selectnum = [];
  439. for (let i = 0; i < len; i++) {
  440. if (that.cartList.valid[i].checked === true) {
  441. selectnum.push(true);
  442. }
  443. }
  444. that.isAllSelect = selectnum.length === len;
  445. that.$set(that, "cartList", that.cartList);
  446. that.$set(that, "isAllSelect", that.isAllSelect);
  447. cookie.set(CHECKED_IDS, that.checkedIds);
  448. that.carnum();
  449. that.gainCount();
  450. that.countMoney();
  451. },
  452. //全选
  453. allChecked: function (e) {
  454. console.log(e);
  455. let that = this;
  456. let selectAllStatus = e.mp.detail.value[0] == "allSelect" ? true : false;
  457. console.log(selectAllStatus);
  458. // let selectAllStatus = that.isAllSelect;
  459. let checkedIds = [];
  460. // for (let i = 0; i < array.length; i++) {
  461. // array[i].checked = selectAllStatus;
  462. // checked.push()
  463. // }
  464. that.cartList.valid.forEach(cart => {
  465. cart.checked = selectAllStatus;
  466. if (selectAllStatus) {
  467. checkedIds.push(cart.id);
  468. }
  469. });
  470. let cartList = {
  471. ...that.cartList
  472. };
  473. that.cartList = [];
  474. that.cartList = cartList;
  475. console.log(this.cartList);
  476. this.$set(this, "cartList", this.cartList);
  477. this.$set(this, "isAllSelect", selectAllStatus);
  478. this.checkedIds = checkedIds;
  479. cookie.set(CHECKED_IDS, checkedIds);
  480. that.carnum();
  481. that.countMoney();
  482. this.$forceUpdate();
  483. },
  484. //数量
  485. carnum: function () {
  486. let that = this;
  487. var carnum = 0;
  488. var array = that.cartList.valid;
  489. for (let i = 0; i < array.length; i++) {
  490. if (array[i].checked === true) {
  491. carnum += parseInt(array[i].cartNum);
  492. }
  493. }
  494. that.$set(that, "cartCount", carnum);
  495. },
  496. //总共价钱;
  497. countMoney: function () {
  498. let that = this;
  499. let carmoney = 0;
  500. let array = that.cartList.valid;
  501. for (let i = 0; i < array.length; i++) {
  502. if (array[i].checked === true) {
  503. carmoney = add(carmoney, mul(array[i].cartNum, array[i].truePrice));
  504. }
  505. }
  506. that.countmoney = carmoney;
  507. }
  508. }
  509. };
  510. </script>
  511. <style lang="less">
  512. .footer-h5 {
  513. bottom: 50px
  514. }
  515. .shoppingCart{
  516. position: relative;
  517. .list{
  518. margin-top: 150rpx;
  519. padding-top: 15rpx;
  520. }
  521. }
  522. </style>