cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <template>
  2. <view class="container">
  3. <!-- 空白页 -->
  4. <view v-if="!hasLogin || empty === true" class="empty">
  5. <image src="/static/error/emptyCart.png" class="emptyImg" mode="aspectFit"></image>
  6. <view v-if="hasLogin" class="empty-tips">
  7. 空空如也
  8. <navigator class="navigator" v-if="hasLogin" url="../index/index" open-type="switchTab">随便逛逛></navigator>
  9. </view>
  10. <view v-else class="empty-tips">
  11. 空空如也
  12. <view class="navigator" @click="navToLogin">去登陆></view>
  13. </view>
  14. </view>
  15. <view v-else>
  16. <!-- 购物车头部 -->
  17. <view class="cart-hand flex">
  18. <view class="hand-tit">
  19. 购物车共 <text>{{ ' '+cartList.length}} 件</text>商品
  20. </view>
  21. <view class="hand-btn" @click="clearCart()">
  22. 清空购物车
  23. </view>
  24. </view>
  25. <!-- 列表 -->
  26. <view class="cart-list">
  27. <block v-for="(item, index) in cartList" :key="item.id">
  28. <view class="cart-item" :class="{ 'b-b': index !== cartList.length - 1 }">
  29. <view class="image-wrapper">
  30. <image
  31. :src="item.productInfo.image"
  32. :class="[item.loaded]"
  33. mode="aspectFill"
  34. lazy-load
  35. @load="onImageLoad('cartList', index)"
  36. @error="onImageError('cartList', index)"
  37. ></image>
  38. <view class="iconfont iconroundcheckfill checkbox" :class="{ checked: item.checked }" @click="check('item', index)"></view>
  39. </view>
  40. <view class="item-right">
  41. <text class="clamp title">{{ item.productInfo.store_name }}</text>
  42. <text class="attr">{{ item.productInfo.attrInfo.suk }}</text>
  43. <text class="price">¥{{ item.productInfo.price }}</text>
  44. <!-- <uni-number-box
  45. class="step"
  46. :min="1"
  47. :max="item.productInfo.stock"
  48. :value="item.cart_num > item.productInfo.stock ? item.productInfo.stock : item.cart_num"
  49. :isMax="item.cart_num >= item.productInfo.stock ? true : false"
  50. :isMin="item.cart_num === 1"
  51. :index="index"
  52. @eventChange="numberChange"
  53. ></uni-number-box> -->
  54. <view class="munbox flex" >
  55. <image src="../../static/icon/reduce.png" mode="" @click="reduce(item,index)"></image>
  56. <input type="number" :value="item.cart_num" disabled/>
  57. <image src="../../static/icon/add.png" mode="" @click="add(item)"></image>
  58. </view>
  59. </view>
  60. <!-- <text class="del-btn iconfont iconclose" @click="deleteCartItem(index)"></text> -->
  61. </view>
  62. </block>
  63. </view>
  64. <!-- 底部菜单栏 -->
  65. <view class="action-section">
  66. <view class="checkbox">
  67. <view class="iconfont iconroundcheckfill icon-checked-box" @click="check('all')" :class="{ 'icon-checked': allChecked }"></view>
  68. <!-- <view class="clear-btn" @click="allChecked ? clearCart() : ''" :class="{ show: allChecked }"><text>清空</text></view> -->
  69. </view>
  70. <view class="total-box">
  71. <text class="price">¥{{ total }}</text>
  72. <!-- <text class="coupon">
  73. 已优惠
  74. <text>74.35</text>
  75. </text> -->
  76. </view>
  77. <button type="primary" class="no-border confirm-btn" @click="createOrder">去结算</button>
  78. </view>
  79. </view>
  80. </view>
  81. </template>
  82. <script>
  83. import weixinObj from "@/plugin/jweixin-module/index.js";
  84. import { getCartList, getCartNum, cartDel } from '@/api/user.js';
  85. import { mapState } from 'vuex';
  86. import uniNumberBox from '@/components/uni-number-box.vue';
  87. import { saveUrl, interceptor } from '@/utils/loginUtils.js';
  88. export default {
  89. components: {
  90. uniNumberBox
  91. },
  92. data() {
  93. return {
  94. total: 0, //总价格
  95. allChecked: false, //全选状态 true|false
  96. empty: false, //空白页现实 true|false
  97. cartList: []
  98. };
  99. },
  100. onShow() {
  101. // 只有登录时才加载数据
  102. if (this.hasLogin) {
  103. this.loadData();
  104. }
  105. weixinObj.hideAllNonBaseMenuItem();
  106. },
  107. watch: {
  108. //显示空白页
  109. cartList(e) {
  110. let empty = e.length === 0 ? true : false;
  111. if (this.empty !== empty) {
  112. this.empty = empty;
  113. }
  114. }
  115. },
  116. computed: {
  117. ...mapState('user', ['hasLogin'])
  118. },
  119. methods: {
  120. reduce(item,index) {
  121. if(item.cart_num == 1) {
  122. uni.showModal({
  123. content: '删除该商品?',
  124. success: e => {
  125. if (e.confirm) {
  126. this.deleteCartItem(index)
  127. }
  128. }
  129. });
  130. }else {
  131. item.cart_num--
  132. this.newNumberChange(item)
  133. }
  134. },
  135. add(item) {
  136. console.log(item)
  137. if(item.productInfo.stock > item.cart_num) {
  138. item.cart_num++
  139. this.newNumberChange(item)
  140. }else {
  141. return
  142. }
  143. },
  144. //请求数据
  145. async loadData() {
  146. let obj = this;
  147. getCartList({})
  148. .then(function(e) {
  149. // 获取当前购物车物品增加数量
  150. let nub = obj.cartList.length;
  151. // 获取之前对象数组
  152. let aArray = obj.cartList.reverse();
  153. // 获取返回数据对象数组
  154. let bArray = e.data.valid.reverse();
  155. obj.cartList = bArray
  156. .map((item, ind) => {
  157. // 设置返回数据默认为勾选状态
  158. item.checked = true;
  159. // 获取相同数组之前对象的数据
  160. let carlist = aArray[ind];
  161. // 判断之前是否已经加载完毕
  162. if (carlist && carlist.loaded == 'loaded') {
  163. item.loaded = 'loaded';
  164. }
  165. return item;
  166. })
  167. .reverse();
  168. obj.calcTotal(); //计算总价
  169. })
  170. .catch(function(e) {
  171. console.log(e);
  172. });
  173. },
  174. //监听image加载完成
  175. onImageLoad(key, index) {
  176. // 修改载入完成后图片class样式
  177. this.$set(this[key][index], 'loaded', 'loaded');
  178. },
  179. //监听image加载失败
  180. onImageError(key, index) {
  181. this[key][index].image = '/static/error/errorImage.jpg';
  182. },
  183. // 跳转到登录页
  184. navToLogin() {
  185. // 保存地址
  186. saveUrl();
  187. // 登录拦截
  188. interceptor();
  189. },
  190. //选中状态处理
  191. check(type, index) {
  192. if (type === 'item') {
  193. this.cartList[index].checked = !this.cartList[index].checked;
  194. } else {
  195. const checked = !this.allChecked;
  196. const list = this.cartList;
  197. list.forEach(item => {
  198. item.checked = checked;
  199. });
  200. this.allChecked = checked;
  201. }
  202. this.calcTotal(type);
  203. },
  204. //数量
  205. numberChange(data) {
  206. let arr = this.cartList[data.index];
  207. arr.cart_num = data.number;
  208. getCartNum({ id: arr.id, number: data.number })
  209. .then(e => {
  210. console.log(e);
  211. })
  212. .catch(function(e) {
  213. console.log(e);
  214. });
  215. this.calcTotal();
  216. },
  217. newNumberChange(item) {
  218. getCartNum({ id: item.id, number: item.cart_num })
  219. .then(e => {
  220. console.log(e);
  221. })
  222. .catch(function(e) {
  223. console.log(e);
  224. });
  225. this.calcTotal();
  226. },
  227. //删除
  228. deleteCartItem(index) {
  229. let list = this.cartList;
  230. let row = list[index];
  231. let id = row.id;
  232. cartDel({
  233. ids: id
  234. });
  235. this.cartList.splice(index, 1);
  236. uni.hideLoading();
  237. this.calcTotal();
  238. },
  239. //清空
  240. clearCart() {
  241. uni.showModal({
  242. content: '清空购物车?',
  243. success: e => {
  244. if (e.confirm) {
  245. let st = this.cartList.map(e => {
  246. return e.id;
  247. });
  248. cartDel({
  249. ids: st.join(',')
  250. }).then(e => {
  251. console.log(e);
  252. });
  253. this.cartList = [];
  254. }
  255. }
  256. });
  257. },
  258. //计算总价
  259. calcTotal() {
  260. let list = this.cartList;
  261. if (list.length === 0) {
  262. this.empty = true;
  263. return;
  264. }
  265. let total = 0;
  266. let checked = true;
  267. list.forEach(item => {
  268. if (item.checked === true) {
  269. total += item.productInfo.price * item.cart_num;
  270. } else if (checked === true) {
  271. checked = false;
  272. }
  273. });
  274. this.allChecked = checked;
  275. this.total = Number(total.toFixed(2));
  276. },
  277. //创建订单
  278. createOrder() {
  279. let list = this.cartList;
  280. let goodsData = [];
  281. list.forEach(item => {
  282. if (item.checked) {
  283. goodsData.push(item.id);
  284. }
  285. });
  286. uni.navigateTo({
  287. url: '/pages/order/createOrder?id=' + goodsData.join(',')
  288. });
  289. }
  290. }
  291. };
  292. </script>
  293. <style lang="scss">
  294. .container {
  295. padding-bottom: 134rpx;
  296. background-color: $page-color-base;
  297. /* 空白页 */
  298. .empty {
  299. position: fixed;
  300. left: 0;
  301. top: 0;
  302. width: 100%;
  303. height: 100vh;
  304. padding-bottom: 100rpx;
  305. display: flex;
  306. justify-content: center;
  307. flex-direction: column;
  308. align-items: center;
  309. background: #fff;
  310. .emptyImg {
  311. width: 300rpx;
  312. height: 250rpx;
  313. margin-bottom: 30rpx;
  314. }
  315. .empty-tips {
  316. display: flex;
  317. font-size: $font-sm + 2rpx;
  318. color: $base-color;
  319. .navigator {
  320. color: $base-color;
  321. margin-left: 16rpx;
  322. }
  323. }
  324. }
  325. }
  326. /* 购物车列表项 */
  327. .cart-item {
  328. width: 710rpx;
  329. height: 210rpx;
  330. background: #FFFFFF;
  331. box-shadow: 0px 0px 10rpx 0rpx rgba(0, 0, 0, 0.1);
  332. border-radius: 10rpx;
  333. margin: 20rpx auto;
  334. display: flex;
  335. position: relative;
  336. padding: 30rpx 26rpx 30rpx 80rpx;
  337. .image-wrapper {
  338. width: 150rpx;
  339. height: 150rpx;
  340. flex-shrink: 0;
  341. position: relative;
  342. image {
  343. border-radius: 8rpx;
  344. }
  345. }
  346. .checkbox {
  347. position: absolute;
  348. top: 0;
  349. bottom: 0;
  350. left: -65rpx;
  351. margin: auto 0;
  352. height: 50rpx;
  353. z-index: 8;
  354. font-size: 44rpx;
  355. line-height: 1;
  356. padding: 4rpx;
  357. color: $font-color-disabled;
  358. background: #fff;
  359. border-radius: 50px;
  360. }
  361. .item-right {
  362. display: flex;
  363. flex-direction: column;
  364. flex: 1;
  365. overflow: hidden;
  366. position: relative;
  367. padding-left: 30rpx;
  368. .munbox {
  369. width: 144rpx;
  370. height: 44rpx;
  371. position: absolute;
  372. bottom: 0;
  373. right: 0;
  374. input {
  375. display: inline-block;
  376. text-align: center;
  377. }
  378. image {
  379. flex-shrink: 0;
  380. width: 44rpx;
  381. height: 44rpx;
  382. }
  383. }
  384. .title,
  385. .price {
  386. font-size: $font-base + 2rpx;
  387. color: $font-color-dark;
  388. height: 40rpx;
  389. line-height: 40rpx;
  390. }
  391. .attr {
  392. font-size: $font-sm + 2rpx;
  393. color: $font-color-light;
  394. height: 50rpx;
  395. line-height: 50rpx;
  396. font-size: 26rpx;
  397. font-family: PingFang SC;
  398. font-weight: bold;
  399. color: #999999;
  400. }
  401. .price {
  402. // height: 50rpx;
  403. // line-height: 50rpx;
  404. padding-top: 20rpx;
  405. font-size: 34rpx;
  406. font-family: PingFang SC;
  407. font-weight: bold;
  408. color:$base-color;
  409. }
  410. .step {
  411. margin-top: 20rpx;
  412. }
  413. .title {
  414. font-size: 34rpx;
  415. font-family: PingFang SC;
  416. font-weight: bold;
  417. color: #333333;
  418. }
  419. }
  420. .del-btn {
  421. padding: 4rpx 10rpx;
  422. font-size: 34rpx;
  423. height: 50rpx;
  424. color: $font-color-light;
  425. }
  426. }
  427. /* 底部栏 */
  428. .action-section {
  429. /* #ifdef H5 */
  430. margin-bottom: 100rpx;
  431. /* #endif */
  432. position: fixed;
  433. left: 30rpx;
  434. bottom: 30rpx;
  435. z-index: 95;
  436. display: flex;
  437. align-items: center;
  438. width: 690rpx;
  439. height: 100rpx;
  440. padding: 0 30rpx;
  441. background: rgba(255, 255, 255, 0.9);
  442. box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.5);
  443. border-radius: 16rpx;
  444. .checkbox {
  445. height: 52rpx;
  446. position: relative;
  447. .icon-checked-box {
  448. border-radius: 50rpx;
  449. background-color: #ffffff;
  450. width: 52rpx;
  451. height: 100%;
  452. position: relative;
  453. z-index: 5;
  454. font-size: 53rpx;
  455. line-height: 1;
  456. color: $font-color-light;
  457. }
  458. .icon-checked {
  459. color: $base-color;
  460. }
  461. }
  462. .clear-btn {
  463. position: absolute;
  464. left: 26rpx;
  465. top: 0;
  466. z-index: 4;
  467. width: 0;
  468. height: 52rpx;
  469. line-height: 52rpx;
  470. padding-left: 38rpx;
  471. font-size: $font-base;
  472. color: #fff;
  473. background: $font-color-disabled;
  474. border-radius: 0 50px 50px 0;
  475. opacity: 0;
  476. transition: 0.2s;
  477. &.show {
  478. opacity: 1;
  479. width: 120rpx;
  480. }
  481. }
  482. .total-box {
  483. flex: 1;
  484. display: flex;
  485. flex-direction: column;
  486. text-align: right;
  487. padding-right: 40rpx;
  488. .price {
  489. font-size: 36rpx;
  490. color: $font-color-dark;
  491. }
  492. .coupon {
  493. font-size: $font-sm;
  494. color: $font-color-light;
  495. text {
  496. color: $font-color-dark;
  497. }
  498. }
  499. }
  500. .confirm-btn {
  501. padding: 0 38rpx;
  502. margin: 0;
  503. border-radius: 100px;
  504. height: 76rpx;
  505. line-height: 76rpx;
  506. font-size: $font-base + 2rpx;
  507. background: $base-color;
  508. }
  509. }
  510. /* 复选框选中状态 */
  511. .action-section .checkbox.checked,
  512. .cart-item .checkbox.checked {
  513. color: $base-color;
  514. }
  515. .cart-hand {
  516. width: 750rpx;
  517. height: 88rpx;
  518. background: #FFFFFF;
  519. font-size: 30rpx;
  520. font-family: PingFang SC;
  521. font-weight: bold;
  522. color: #333333;
  523. line-height: 88rpx;
  524. padding-left: 28rpx;
  525. padding-right: 26rpx;
  526. .hand-tit {
  527. text {
  528. color: $base-color;
  529. }
  530. }
  531. .hand-btn {
  532. width: 164rpx;
  533. height: 62rpx;
  534. border: 2rpx solid $base-color;
  535. border-radius: 31rpx;
  536. font-size: 26rpx;
  537. font-family: PingFang SC;
  538. font-weight: bold;
  539. color: $base-color;
  540. line-height: 62rpx;
  541. text-align: center;
  542. }
  543. }
  544. </style>