cart.vue 9.6 KB

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