cart.vue 9.6 KB

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