cart.vue 12 KB

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