cart.vue 13 KB

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