cart.vue 13 KB

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