cart.vue 13 KB

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