cart.vue 15 KB

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