cart.vue 9.6 KB

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