cart.vue 9.6 KB

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