order.vue 14 KB

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