cart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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">随便逛逛>
  9. </navigator>
  10. </view>
  11. <view v-else class="empty-tips">
  12. 空空如也
  13. <view class="navigator" @click="navToLogin">去登陆></view>
  14. </view>
  15. </view>
  16. <view v-else>
  17. <!-- 列表 -->
  18. <view class="cart-list">
  19. <block v-for="(item, index) in cartList" :key="item.id">
  20. <view class="cart-item" :class="{ 'b-b': index !== cartList.length - 1 }">
  21. <view class="flex padding-r-20" @click="item.checked=!item.checked">
  22. <image v-if='item.checked' class="checkedGoods" src="../../static/icon/addressIconXz.png"
  23. mode="scaleToFill">
  24. </image>
  25. <view v-else class="icon-checked-box">
  26. </view>
  27. </view>
  28. <image class="image-wrapper" :src="item.productInfo.image" :class="[item.loaded]"
  29. mode="aspectFill" lazy-load @load="onImageLoad('cartList', index)"
  30. @error="onImageError('cartList', index)"></image>
  31. <view class="item-right">
  32. <view class="clamp title">{{ item.productInfo.store_name }}</view>
  33. <view class="attr">{{ item.productInfo.attrInfo.suk }}</view>
  34. <view class="flex">
  35. <text class="price">¥{{ item.productInfo.price }}</text>
  36. <uni-number-box class="step" :min="1" :max="item.productInfo.stock"
  37. :value="item.cart_num > item.productInfo.stock ? item.productInfo.stock : item.cart_num"
  38. :isMax="item.cart_num >= item.productInfo.stock ? true : false"
  39. :isMin="item.cart_num === 1" :index="index" @eventChange="numberChange">
  40. </uni-number-box>
  41. </view>
  42. <view class="server" v-if="item.day>0">
  43. (服务费:{{item.day_deducted}} 服务天数:{{item.day}})
  44. </view>
  45. </view>
  46. <image class="del-btn" src="../../static/icon/goodsExit.png" @click="deleteCartItem(index)"
  47. mode="scaleToFill"></image>
  48. </view>
  49. </block>
  50. </view>
  51. <!-- 底部菜单栏 -->
  52. <view class="action-section">
  53. <view class="flex leftBox">
  54. <view class="checkbox" @click="check('all')">
  55. <image class="icon-checked-box icon-checked" v-if='allChecked'
  56. src="../../static/icon/addressIconXz.png" mode="scaleToFill"></image>
  57. <view v-else class="icon-checked-box"></view>
  58. </view>
  59. <view class="margin-l-20">
  60. 全选
  61. </view>
  62. </view>
  63. <view class="total-box">
  64. <view class="font-size-base">
  65. <text>合计:</text>
  66. <text class="price">¥{{ total }}</text>
  67. </view>
  68. <view class="coupon">
  69. 共{{checkedNum}}件商品
  70. </view>
  71. </view>
  72. <button type="primary" class="no-border confirm-btn" @click="createOrder">立即购买</button>
  73. </view>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. import {
  79. getCartList,
  80. getCartNum,
  81. cartDel
  82. } from '@/api/user.js';
  83. import {
  84. mapState
  85. } from 'vuex';
  86. import {
  87. saveUrl,
  88. interceptor
  89. } from '@/utils/loginUtils.js';
  90. export default {
  91. data() {
  92. return {
  93. total: 0, //总价格
  94. allChecked: false, //全选状态 true|false
  95. empty: false, //空白页现实 true|false
  96. cartList: []
  97. };
  98. },
  99. onShow() {
  100. uni.setTabBarStyle({
  101. backgroundColor: '#FFFFFF'
  102. })
  103. // 只有登录时才加载数据
  104. if (this.hasLogin) {
  105. this.loadData();
  106. }
  107. },
  108. watch: {
  109. //显示空白页
  110. cartList(e) {
  111. let empty = e.length === 0 ? true : false;
  112. if (this.empty !== empty) {
  113. this.empty = empty;
  114. }
  115. }
  116. },
  117. computed: {
  118. ...mapState('user', ['hasLogin']),
  119. ...mapState('shop', ['shopDetail']),
  120. checkedNum() {
  121. let num = 0;
  122. for (var i = 0; i < this.cartList.length; i++) {
  123. if (this.cartList[i].checked) {
  124. num++
  125. }
  126. }
  127. return num
  128. },
  129. },
  130. methods: {
  131. //请求数据
  132. async loadData() {
  133. let obj = this;
  134. getCartList({
  135. store_id:obj.shopDetail.id
  136. })
  137. .then(function(e) {
  138. // 获取当前购物车物品增加数量
  139. let nub = obj.cartList.length;
  140. // 获取之前对象数组
  141. let aArray = obj.cartList.reverse();
  142. // 获取返回数据对象数组
  143. let bArray = e.data.valid.reverse();
  144. obj.cartList = bArray
  145. .map((item, ind) => {
  146. // 设置返回数据默认为勾选状态
  147. item.checked = true;
  148. // 获取相同数组之前对象的数据
  149. let carlist = aArray[ind];
  150. // 判断之前是否已经加载完毕
  151. if (carlist && carlist.loaded == 'loaded') {
  152. item.loaded = 'loaded';
  153. }
  154. return item;
  155. })
  156. .reverse();
  157. obj.calcTotal(); //计算总价
  158. })
  159. .catch(function(e) {
  160. console.log(e);
  161. });
  162. },
  163. //监听image加载完成
  164. onImageLoad(key, index) {
  165. // 修改载入完成后图片class样式
  166. this.$set(this[key][index], 'loaded', 'loaded');
  167. },
  168. //监听image加载失败
  169. onImageError(key, index) {
  170. this[key][index].image = '/static/error/errorImage.jpg';
  171. },
  172. // 跳转到登录页
  173. navToLogin() {
  174. // 保存地址
  175. saveUrl();
  176. // 登录拦截
  177. interceptor();
  178. },
  179. //选中状态处理
  180. check(type, index) {
  181. if (type === 'item') {
  182. this.cartList[index].checked = !this.cartList[index].checked;
  183. } else {
  184. const checked = !this.allChecked;
  185. const list = this.cartList;
  186. list.forEach(item => {
  187. item.checked = checked;
  188. });
  189. this.allChecked = checked;
  190. }
  191. this.calcTotal(type);
  192. },
  193. //数量
  194. numberChange(data) {
  195. let arr = this.cartList[data.index];
  196. arr.cart_num = data.number;
  197. getCartNum({
  198. id: arr.id,
  199. number: data.number
  200. })
  201. .then(e => {
  202. console.log(e);
  203. })
  204. .catch(function(e) {
  205. console.log(e);
  206. });
  207. this.calcTotal();
  208. },
  209. //删除
  210. deleteCartItem(index) {
  211. let list = this.cartList;
  212. let row = list[index];
  213. let id = row.id;
  214. uni.showModal({
  215. title: '提示',
  216. content: '是否删除'+row.productInfo.store_name,
  217. success: res => {
  218. if(res.confirm){
  219. cartDel({
  220. ids: id
  221. });
  222. this.cartList.splice(index, 1);
  223. uni.hideLoading();
  224. this.calcTotal();
  225. }
  226. }
  227. });
  228. },
  229. //清空
  230. // clearCart() {
  231. // uni.showModal({
  232. // content: '清空购物车?',
  233. // success: e => {
  234. // if (e.confirm) {
  235. // let st = this.cartList.map(e => {
  236. // return e.id;
  237. // });
  238. // cartDel({
  239. // ids: st.join(',')
  240. // }).then(e => {
  241. // console.log(e);
  242. // });
  243. // this.cartList = [];
  244. // }
  245. // }
  246. // });
  247. // },
  248. //计算总价
  249. calcTotal() {
  250. let list = this.cartList;
  251. if (list.length === 0) {
  252. this.empty = true;
  253. return;
  254. }
  255. let total = 0;
  256. let checked = true;
  257. list.forEach(item => {
  258. if (item.checked === true) {
  259. total += item.productInfo.price * item.cart_num;
  260. } else if (checked === true) {
  261. checked = false;
  262. }
  263. });
  264. this.allChecked = checked;
  265. this.total = Number(total.toFixed(2));
  266. },
  267. //创建订单
  268. createOrder() {
  269. let list = this.cartList;
  270. let goodsData = [];
  271. list.forEach(item => {
  272. if (item.checked) {
  273. goodsData.push(item.id);
  274. }
  275. });
  276. uni.navigateTo({
  277. url: '/pages/order/createOrder?id=' + goodsData.join(',')
  278. });
  279. }
  280. }
  281. };
  282. </script>
  283. <style lang="scss">
  284. .container {
  285. padding-bottom: 134rpx;
  286. background-color: $page-color-base;
  287. /* #ifdef MP-WEIXIN || H5 */
  288. padding-top: 30rpx;
  289. /* #endif */
  290. /* #ifdef APP */
  291. padding-top: var(--status-bar-height);
  292. /* #endif */
  293. /* 空白页 */
  294. .empty {
  295. position: fixed;
  296. left: 0;
  297. top: 0;
  298. width: 100%;
  299. height: 100vh;
  300. padding-bottom: 100rpx;
  301. display: flex;
  302. justify-content: center;
  303. flex-direction: column;
  304. align-items: center;
  305. background: #fff;
  306. .emptyImg {
  307. width: 300rpx;
  308. height: 250rpx;
  309. margin-bottom: 30rpx;
  310. }
  311. .empty-tips {
  312. display: flex;
  313. font-size: $font-sm + 2rpx;
  314. color: $font-color-disabled;
  315. .navigator {
  316. color: $uni-color-primary;
  317. margin-left: 16rpx;
  318. }
  319. }
  320. }
  321. }
  322. /* 购物车列表项 */
  323. .cart-list {
  324. padding: 0 30rpx;
  325. }
  326. .cart-item {
  327. display: flex;
  328. position: relative;
  329. padding: 20rpx;
  330. background-color: #FFFFFF;
  331. margin-bottom: 20rpx;
  332. border-radius: 20rpx;
  333. .icon-checked-box {
  334. width: 37rpx;
  335. height: 37rpx;
  336. z-index: 5;
  337. border: 1px solid $font-color-light;
  338. border-radius: 99rpx;
  339. }
  340. .checkedGoods {
  341. width: 37rpx;
  342. height: 37rpx;
  343. }
  344. .image-wrapper {
  345. width: 170rpx;
  346. height: 170rpx;
  347. flex-shrink: 0;
  348. position: relative;
  349. border-radius: 20rpx;
  350. }
  351. .checkbox {
  352. position: absolute;
  353. left: -16rpx;
  354. top: -16rpx;
  355. z-index: 8;
  356. font-size: 44rpx;
  357. line-height: 1;
  358. padding: 4rpx;
  359. color: $font-color-disabled;
  360. background: #fff;
  361. border-radius: 50px;
  362. }
  363. .item-right {
  364. overflow: hidden;
  365. position: relative;
  366. padding-left: 30rpx;
  367. flex-grow: 1;
  368. .title,
  369. .price {
  370. font-size: $font-base;
  371. color: $font-color-dark;
  372. }
  373. .attr {
  374. font-size: 24rpx;
  375. color: $font-color-light;
  376. height: 40rpx;
  377. line-height: 40rpx;
  378. }
  379. .server {
  380. font-size: 24rpx;
  381. color: $font-color-light;
  382. margin-top: 10rpx;
  383. }
  384. .price {
  385. font-size: 30rpx;
  386. color: $color-green;
  387. }
  388. }
  389. .del-btn {
  390. flex-shrink: 0;
  391. height: 40rpx;
  392. width: 40rpx;
  393. }
  394. }
  395. /* 底部栏 */
  396. .action-section {
  397. /* #ifdef H5 */
  398. margin-bottom: 100rpx;
  399. /* #endif */
  400. position: fixed;
  401. left: 0rpx;
  402. bottom: 0rpx;
  403. z-index: 95;
  404. display: flex;
  405. align-items: center;
  406. width: 750rpx;
  407. height: 100rpx;
  408. padding: 0 30rpx;
  409. background: rgba(255, 255, 255, 0.9);
  410. .leftBox {
  411. padding-right: 30rpx;
  412. border-right: 1px solid $font-color-disabled;
  413. }
  414. .checkbox {
  415. line-height: 1;
  416. font-size: 30rpx;
  417. .icon-checked-box {
  418. width: 37rpx;
  419. height: 37rpx;
  420. z-index: 5;
  421. border: 1px solid $font-color-light;
  422. border-radius: 99rpx;
  423. }
  424. .icon-checked {
  425. border: none;
  426. color: $base-color;
  427. }
  428. }
  429. .clear-btn {
  430. position: absolute;
  431. left: 26rpx;
  432. top: 0;
  433. z-index: 4;
  434. width: 0;
  435. height: 52rpx;
  436. line-height: 52rpx;
  437. padding-left: 38rpx;
  438. font-size: $font-base;
  439. color: #fff;
  440. background: $font-color-disabled;
  441. border-radius: 0 50px 50px 0;
  442. opacity: 0;
  443. transition: 0.2s;
  444. &.show {
  445. opacity: 1;
  446. width: 120rpx;
  447. }
  448. }
  449. .total-box {
  450. flex-grow: 1;
  451. padding-left: 30rpx;
  452. font-size: 30rpx;
  453. .price {
  454. color: $color-green;
  455. }
  456. .coupon {
  457. font-size: 24rpx;
  458. color: $font-color-light;
  459. }
  460. }
  461. .confirm-btn {
  462. padding: 0 70rpx;
  463. margin: 0;
  464. border-radius: 100px;
  465. height: 70rpx;
  466. line-height: 70rpx;
  467. font-size: 24rpx;
  468. background: $color-yellow;
  469. color: #714D01;
  470. }
  471. }
  472. /* 复选框选中状态 */
  473. .action-section .checkbox.checked,
  474. .cart-item .checkbox.checked {
  475. color: $base-color;
  476. }
  477. </style>