cart.vue 12 KB

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