cart.vue 11 KB

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