cart.vue 9.9 KB

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