cart.vue 13 KB

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