cart.vue 9.7 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. <text @click="gotods">空空如也</text>
  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.integral }}积分</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 { tabbar1 } from '@/utils/tabbar.js';
  70. import { getCartList, getCartNum, cartDel } from '@/api/user.js';
  71. import { mapState } from 'vuex';
  72. import uniNumberBox from '@/components/uni-number-box.vue';
  73. import { saveUrl, interceptor } from '@/utils/loginUtils.js';
  74. export default {
  75. components: {
  76. uniNumberBox
  77. },
  78. data() {
  79. return {
  80. current: 2,
  81. tabbar: tabbar1,
  82. total: 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. console.log(e+"111");
  113. // 获取当前购物车物品增加数量
  114. let nub = obj.cartList.length;
  115. // 获取之前对象数组
  116. let aArray = obj.cartList.reverse();
  117. // 获取返回数据对象数组
  118. let bArray = e.data.valid.reverse();
  119. obj.cartList = bArray
  120. .map((item, ind) => {
  121. // 设置返回数据默认为勾选状态
  122. item.checked = true;
  123. // 获取相同数组之前对象的数据
  124. let carlist = aArray[ind];
  125. // 判断之前是否已经加载完毕
  126. if (carlist && carlist.loaded == 'loaded') {
  127. item.loaded = 'loaded';
  128. }
  129. return item;
  130. })
  131. .reverse();
  132. obj.calcTotal(); //计算总价
  133. })
  134. .catch(function(e) {
  135. console.log(e);
  136. });
  137. },
  138. //监听image加载完成
  139. onImageLoad(key, index) {
  140. // 修改载入完成后图片class样式
  141. this.$set(this[key][index], 'loaded', 'loaded');
  142. },
  143. //监听image加载失败
  144. onImageError(key, index) {
  145. this[key][index].image = '/static/error/errorImage.jpg';
  146. },
  147. // 跳转到登录页
  148. navToLogin() {
  149. // 保存地址
  150. saveUrl();
  151. // 登录拦截
  152. interceptor();
  153. },
  154. //选中状态处理
  155. check(type, index) {
  156. if (type === 'item') {
  157. this.cartList[index].checked = !this.cartList[index].checked;
  158. } else {
  159. const checked = !this.allChecked;
  160. const list = this.cartList;
  161. list.forEach(item => {
  162. item.checked = checked;
  163. });
  164. this.allChecked = checked;
  165. }
  166. this.calcTotal(type);
  167. },
  168. //数量
  169. numberChange(data) {
  170. let arr = this.cartList[data.index];
  171. arr.cart_num = data.number;
  172. getCartNum({ id: arr.id, number: data.number })
  173. .then(e => {
  174. console.log(e);
  175. })
  176. .catch(function(e) {
  177. console.log(e);
  178. });
  179. this.calcTotal();
  180. },
  181. //删除
  182. deleteCartItem(index) {
  183. let list = this.cartList;
  184. let row = list[index];
  185. let id = row.id;
  186. cartDel({
  187. ids: id
  188. });
  189. this.cartList.splice(index, 1);
  190. uni.hideLoading();
  191. this.calcTotal();
  192. },
  193. //清空
  194. clearCart() {
  195. uni.showModal({
  196. content: '清空购物车?',
  197. success: e => {
  198. if (e.confirm) {
  199. let st = this.cartList.map(e => {
  200. return e.id;
  201. });
  202. cartDel({
  203. ids: st.join(',')
  204. }).then(e => {
  205. console.log(e);
  206. });
  207. this.cartList = [];
  208. }
  209. }
  210. });
  211. },
  212. gotods() {
  213. console.log('dianji')
  214. window.location.href = 'http://ygs.hqgjsmc.com/'
  215. },
  216. //计算总价
  217. calcTotal() {
  218. let list = this.cartList;
  219. if (list.length === 0) {
  220. this.empty = true;
  221. return;
  222. }
  223. let total = 0;
  224. let checked = true;
  225. list.forEach(item => {
  226. if (item.checked === true) {
  227. total += item.integral * item.cart_num;
  228. } else if (checked === true) {
  229. checked = false;
  230. }
  231. });
  232. this.allChecked = checked;
  233. this.total = Number(total.toFixed(2));
  234. },
  235. //创建订单
  236. createOrder() {
  237. let list = this.cartList;
  238. let goodsData = [];
  239. list.forEach(item => {
  240. if (item.checked) {
  241. goodsData.push(item.id);
  242. }
  243. });
  244. uni.navigateTo({
  245. url: '/pages/order/createOrder?id=' + goodsData.join(',')
  246. });
  247. }
  248. }
  249. };
  250. </script>
  251. <style lang="scss">
  252. .container {
  253. padding-bottom: 134rpx;
  254. background-color: $page-color-base;
  255. /* 空白页 */
  256. .empty {
  257. position: fixed;
  258. left: 0;
  259. top: 0;
  260. width: 100%;
  261. height: 100vh;
  262. padding-bottom: 100rpx;
  263. display: flex;
  264. justify-content: center;
  265. flex-direction: column;
  266. align-items: center;
  267. background: #fff;
  268. .emptyImg {
  269. width: 300rpx;
  270. height: 250rpx;
  271. margin-bottom: 30rpx;
  272. }
  273. .empty-tips {
  274. display: flex;
  275. font-size: $font-sm + 2rpx;
  276. color: $font-color-disabled;
  277. .navigator {
  278. color: $uni-color-primary;
  279. margin-left: 16rpx;
  280. }
  281. }
  282. }
  283. }
  284. /* 购物车列表项 */
  285. .cart-item {
  286. display: flex;
  287. position: relative;
  288. padding: 30rpx 40rpx;
  289. .image-wrapper {
  290. width: 230rpx;
  291. height: 230rpx;
  292. flex-shrink: 0;
  293. position: relative;
  294. image {
  295. border-radius: 8rpx;
  296. }
  297. }
  298. .checkbox {
  299. position: absolute;
  300. left: -16rpx;
  301. top: -16rpx;
  302. z-index: 8;
  303. font-size: 44rpx;
  304. line-height: 1;
  305. padding: 4rpx;
  306. color: $font-color-disabled;
  307. background: #fff;
  308. border-radius: 50px;
  309. }
  310. .item-right {
  311. display: flex;
  312. flex-direction: column;
  313. flex: 1;
  314. overflow: hidden;
  315. position: relative;
  316. padding-left: 30rpx;
  317. .title,
  318. .price {
  319. font-size: $font-base + 2rpx;
  320. color: $font-color-dark;
  321. height: 40rpx;
  322. line-height: 40rpx;
  323. }
  324. .attr {
  325. font-size: $font-sm + 2rpx;
  326. color: $font-color-light;
  327. height: 50rpx;
  328. line-height: 50rpx;
  329. }
  330. .price {
  331. height: 50rpx;
  332. line-height: 50rpx;
  333. }
  334. .step {
  335. margin-top: 20rpx;
  336. }
  337. }
  338. .del-btn {
  339. padding: 4rpx 10rpx;
  340. font-size: 34rpx;
  341. height: 50rpx;
  342. color: $font-color-light;
  343. }
  344. }
  345. /* 底部栏 */
  346. .action-section {
  347. /* #ifdef H5 */
  348. margin-bottom: 100rpx;
  349. /* #endif */
  350. position: fixed;
  351. left: 30rpx;
  352. bottom: 30rpx;
  353. z-index: 95;
  354. display: flex;
  355. align-items: center;
  356. width: 690rpx;
  357. height: 100rpx;
  358. padding: 0 30rpx;
  359. background: rgba(255, 255, 255, 0.9);
  360. box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.5);
  361. border-radius: 16rpx;
  362. .checkbox {
  363. height: 52rpx;
  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: 0;
  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: right;
  405. padding-right: 40rpx;
  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>