cart.vue 14 KB

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