cart.vue 9.7 KB

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