cart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. weixinObj.hideAllNonBaseMenuItem();
  114. },
  115. watch: {
  116. //显示空白页
  117. cartList(e) {
  118. let empty = e.length === 0 ? true : false;
  119. if (this.empty !== empty) {
  120. this.empty = empty;
  121. }
  122. }
  123. },
  124. computed: {
  125. ...mapState('user', ['hasLogin'])
  126. },
  127. methods: {
  128. reduce(item, index) {
  129. if (item.cart_num == 1) {
  130. uni.showModal({
  131. content: '删除该商品?',
  132. success: e => {
  133. if (e.confirm) {
  134. this.deleteCartItem(index)
  135. }
  136. }
  137. });
  138. } else {
  139. item.cart_num--
  140. this.newNumberChange(item)
  141. }
  142. },
  143. add(item) {
  144. console.log(item)
  145. if (item.productInfo.stock > item.cart_num) {
  146. item.cart_num++
  147. this.newNumberChange(item)
  148. } else {
  149. return
  150. }
  151. },
  152. //请求数据
  153. async loadData() {
  154. let obj = this;
  155. getCartList({})
  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?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. position: absolute;
  362. top: 0;
  363. bottom: 0;
  364. left: -65rpx;
  365. margin: auto 0;
  366. height: 50rpx;
  367. z-index: 8;
  368. font-size: 44rpx;
  369. line-height: 1;
  370. padding: 4rpx;
  371. color: $font-color-disabled;
  372. background: #fff;
  373. border-radius: 50px;
  374. }
  375. .item-right {
  376. display: flex;
  377. flex-direction: column;
  378. flex: 1;
  379. overflow: hidden;
  380. position: relative;
  381. padding-left: 30rpx;
  382. .munbox {
  383. width: 144rpx;
  384. height: 44rpx;
  385. position: absolute;
  386. bottom: 0;
  387. right: 0;
  388. input {
  389. display: inline-block;
  390. text-align: center;
  391. }
  392. image {
  393. flex-shrink: 0;
  394. width: 44rpx;
  395. height: 44rpx;
  396. }
  397. }
  398. .title,
  399. .price {
  400. font-size: $font-base + 2rpx;
  401. color: $font-color-dark;
  402. height: 40rpx;
  403. line-height: 40rpx;
  404. }
  405. .attr {
  406. font-size: $font-sm + 2rpx;
  407. color: $font-color-light;
  408. height: 50rpx;
  409. line-height: 50rpx;
  410. font-size: 26rpx;
  411. font-family: PingFang SC;
  412. font-weight: bold;
  413. color: #999999;
  414. }
  415. .price {
  416. // height: 50rpx;
  417. // line-height: 50rpx;
  418. padding-top: 20rpx;
  419. font-size: 34rpx;
  420. font-family: PingFang SC;
  421. font-weight: bold;
  422. color: #FF4C4C;
  423. }
  424. .step {
  425. margin-top: 20rpx;
  426. }
  427. .title {
  428. font-size: 34rpx;
  429. font-family: PingFang SC;
  430. font-weight: bold;
  431. color: #333333;
  432. }
  433. }
  434. .del-btn {
  435. padding: 4rpx 10rpx;
  436. font-size: 34rpx;
  437. height: 50rpx;
  438. color: $font-color-light;
  439. }
  440. }
  441. /* 底部栏 */
  442. .action-section {
  443. /* #ifdef H5 */
  444. margin-bottom: 100rpx;
  445. /* #endif */
  446. position: fixed;
  447. left: 30rpx;
  448. bottom: 30rpx;
  449. z-index: 95;
  450. display: flex;
  451. align-items: center;
  452. width: 690rpx;
  453. height: 100rpx;
  454. padding: 0 30rpx;
  455. background: rgba(255, 255, 255, 0.9);
  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. }
  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. .price {
  503. font-size: 36rpx;
  504. color: $font-color-dark;
  505. }
  506. .coupon {
  507. font-size: $font-sm;
  508. color: $font-color-light;
  509. text {
  510. color: $font-color-dark;
  511. }
  512. }
  513. }
  514. .confirm-btn {
  515. padding: 0 38rpx;
  516. margin: 0;
  517. border-radius: 100px;
  518. height: 76rpx;
  519. line-height: 76rpx;
  520. font-size: $font-base + 2rpx;
  521. background: $base-color;
  522. }
  523. }
  524. /* 复选框选中状态 */
  525. .action-section .checkbox.checked,
  526. .cart-item .checkbox.checked {
  527. color: $base-color;
  528. }
  529. .cart-hand {
  530. width: 750rpx;
  531. height: 88rpx;
  532. background: #FFFFFF;
  533. font-size: 30rpx;
  534. font-family: PingFang SC;
  535. font-weight: bold;
  536. color: #333333;
  537. line-height: 88rpx;
  538. padding-left: 28rpx;
  539. padding-right: 26rpx;
  540. .hand-tit {
  541. text {
  542. color: #FF4C4C;
  543. }
  544. }
  545. .hand-btn {
  546. width: 164rpx;
  547. height: 62rpx;
  548. border: 2rpx solid #FF4C4C;
  549. border-radius: 31rpx;
  550. font-size: 26rpx;
  551. font-family: PingFang SC;
  552. font-weight: bold;
  553. color: #FF4C4C;
  554. line-height: 62rpx;
  555. text-align: center;
  556. }
  557. }
  558. </style>