carts.vue 11 KB

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