cart.vue 10 KB

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