cart2.vue 15 KB

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