cart.vue 12 KB

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