cart.vue 13 KB

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