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