cart.vue 13 KB

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