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 { getCartList, getCartNum, cartDel } from '@/api/user.js';
  84. import { mapState } from 'vuex';
  85. import uniNumberBox from '@/components/uni-number-box.vue';
  86. import { saveUrl, interceptor } from '@/utils/loginUtils.js';
  87. export default {
  88. components: {
  89. uniNumberBox
  90. },
  91. data() {
  92. return {
  93. total: 0, //总价格
  94. allChecked: false, //全选状态 true|false
  95. empty: false, //空白页现实 true|false
  96. cartList: []
  97. };
  98. },
  99. onShow() {
  100. // 只有登录时才加载数据
  101. if (this.hasLogin) {
  102. this.loadData();
  103. }
  104. },
  105. watch: {
  106. //显示空白页
  107. cartList(e) {
  108. let empty = e.length === 0 ? true : false;
  109. if (this.empty !== empty) {
  110. this.empty = empty;
  111. }
  112. }
  113. },
  114. computed: {
  115. ...mapState('user', ['hasLogin'])
  116. },
  117. methods: {
  118. reduce(item,index) {
  119. if(item.cart_num == 1) {
  120. uni.showModal({
  121. content: '删除该商品?',
  122. success: e => {
  123. if (e.confirm) {
  124. this.deleteCartItem(index)
  125. }
  126. }
  127. });
  128. }else {
  129. item.cart_num--
  130. this.newNumberChange(item)
  131. }
  132. },
  133. add(item) {
  134. console.log(item)
  135. if(item.productInfo.stock > item.cart_num) {
  136. item.cart_num++
  137. this.newNumberChange(item)
  138. }else {
  139. return
  140. }
  141. },
  142. //请求数据
  143. async loadData() {
  144. let obj = this;
  145. getCartList({})
  146. .then(function(e) {
  147. // 获取当前购物车物品增加数量
  148. let nub = obj.cartList.length;
  149. // 获取之前对象数组
  150. let aArray = obj.cartList.reverse();
  151. // 获取返回数据对象数组
  152. let bArray = e.data.valid.reverse();
  153. obj.cartList = bArray
  154. .map((item, ind) => {
  155. // 设置返回数据默认为勾选状态
  156. item.checked = true;
  157. // 获取相同数组之前对象的数据
  158. let carlist = aArray[ind];
  159. // 判断之前是否已经加载完毕
  160. if (carlist && carlist.loaded == 'loaded') {
  161. item.loaded = 'loaded';
  162. }
  163. return item;
  164. })
  165. .reverse();
  166. obj.calcTotal(); //计算总价
  167. })
  168. .catch(function(e) {
  169. console.log(e);
  170. });
  171. },
  172. //监听image加载完成
  173. onImageLoad(key, index) {
  174. // 修改载入完成后图片class样式
  175. this.$set(this[key][index], 'loaded', 'loaded');
  176. },
  177. //监听image加载失败
  178. onImageError(key, index) {
  179. this[key][index].image = '/static/error/errorImage.jpg';
  180. },
  181. // 跳转到登录页
  182. navToLogin() {
  183. // 保存地址
  184. saveUrl();
  185. // 登录拦截
  186. interceptor();
  187. },
  188. //选中状态处理
  189. check(type, index) {
  190. if (type === 'item') {
  191. this.cartList[index].checked = !this.cartList[index].checked;
  192. } else {
  193. const checked = !this.allChecked;
  194. const list = this.cartList;
  195. list.forEach(item => {
  196. item.checked = checked;
  197. });
  198. this.allChecked = checked;
  199. }
  200. this.calcTotal(type);
  201. },
  202. //数量
  203. numberChange(data) {
  204. let arr = this.cartList[data.index];
  205. arr.cart_num = data.number;
  206. getCartNum({ id: arr.id, number: data.number })
  207. .then(e => {
  208. console.log(e);
  209. })
  210. .catch(function(e) {
  211. console.log(e);
  212. });
  213. this.calcTotal();
  214. },
  215. newNumberChange(item) {
  216. getCartNum({ id: item.id, number: item.cart_num })
  217. .then(e => {
  218. console.log(e);
  219. })
  220. .catch(function(e) {
  221. console.log(e);
  222. });
  223. this.calcTotal();
  224. },
  225. //删除
  226. deleteCartItem(index) {
  227. let list = this.cartList;
  228. let row = list[index];
  229. let id = row.id;
  230. cartDel({
  231. ids: id
  232. });
  233. this.cartList.splice(index, 1);
  234. uni.hideLoading();
  235. this.calcTotal();
  236. },
  237. //清空
  238. clearCart() {
  239. uni.showModal({
  240. content: '清空购物车?',
  241. success: e => {
  242. if (e.confirm) {
  243. let st = this.cartList.map(e => {
  244. return e.id;
  245. });
  246. cartDel({
  247. ids: st.join(',')
  248. }).then(e => {
  249. console.log(e);
  250. });
  251. this.cartList = [];
  252. }
  253. }
  254. });
  255. },
  256. //计算总价
  257. calcTotal() {
  258. let list = this.cartList;
  259. if (list.length === 0) {
  260. this.empty = true;
  261. return;
  262. }
  263. let total = 0;
  264. let checked = true;
  265. list.forEach(item => {
  266. if (item.checked === true) {
  267. total += item.productInfo.price * item.cart_num;
  268. } else if (checked === true) {
  269. checked = false;
  270. }
  271. });
  272. this.allChecked = checked;
  273. this.total = Number(total.toFixed(2));
  274. },
  275. //创建订单
  276. createOrder() {
  277. let list = this.cartList;
  278. let goodsData = [];
  279. list.forEach(item => {
  280. if (item.checked) {
  281. goodsData.push(item.id);
  282. }
  283. });
  284. uni.navigateTo({
  285. url: '/pages/order/createOrder?id=' + goodsData.join(',')
  286. });
  287. }
  288. }
  289. };
  290. </script>
  291. <style lang="scss">
  292. .container {
  293. padding-bottom: 134rpx;
  294. background-color: $page-color-base;
  295. padding: 30rpx;
  296. // padding-top: var(--status-bar-height);
  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: $font-color-disabled;
  319. .navigator {
  320. color: $uni-color-primary;
  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>