cart.vue 12 KB

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