cart.vue 14 KB

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