cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <template>
  2. <view class="container">
  3. <!-- 空白页 -->
  4. <view v-if="(!userInfo.group || empty===true) && state != 'load'" class="empty">
  5. <image src="/static/emptyCart.jpg" mode="aspectFit"></image>
  6. <view v-if="userInfo.group" class="empty-tips">
  7. 空空如也
  8. <navigator class="navigator" url="../index/index" open-type="switchTab">随便逛逛></navigator>
  9. </view>
  10. <view v-else>
  11. <view class="get-user-info-text">您还没有购物车,你可以提供您的微信资料初始化购物车!</view>
  12. <button class="get-user-info" @click="getUserInfo">提供资料初始化购物车</button>
  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}" :style="{'background':item.isset?'':'#f5f5f5'}"
  20. @click="navTo(`/pages/product/product?id=${item.product_id}&flash=0`)"
  21. >
  22. <view class="image-wrapper">
  23. <image :src="item.image" class="loaded" mode="aspectFill"></image>
  24. <view v-if="item.isset == true" class="yticon icon-xuanzhong checkbox" :class="{checked: item.choose}" @click.stop="check('item', index)"></view>
  25. </view>
  26. <view class="item-right">
  27. <text class="clamp title">{{item.title}}</text>
  28. <text class="attr" v-if="item.spec">{{item.spec}}</text>
  29. <text class="price">¥{{item.nowPrice}} <text style="color:red"> {{cartPrice(item.oldPrice, item.nowPrice)}}</text></text>
  30. <uni-number-box class="step" :min="1" :max="item.stock" :disabled="item.number>=item.stock" :value="cartList[index].number"
  31. :isMax="item.number>=item.stock?true:false" :isMin="item.number===1" :index="index" @eventChange="numberChange"></uni-number-box>
  32. </view>
  33. <text class="del-btn yticon icon-lajitong" @click.stop="deleteCartItem(index)"></text>
  34. <text class="invalid" v-if="item.isset == false">失效</text>
  35. <text class="invalid" v-if="item.stock == 0 && item.isset == true">库存不足</text>
  36. </view>
  37. </block>
  38. </view>
  39. <!-- 底部菜单栏 -->
  40. <view class="action-section" v-if="state != 'load'">
  41. <view class="checkbox">
  42. <image :src="allChoose?'/static/selected.png':'/static/select.png'" mode="aspectFit" @click="check('all')"></image>
  43. <view class="clear-btn" :class="{show: allChoose}" @click="clearCart">
  44. 清空
  45. </view>
  46. </view>
  47. <view class="total-box">
  48. <text class="price">¥{{total}}</text>
  49. </view>
  50. <button type="primary" class="no-border confirm-btn" @click="createOrder">去结算</button>
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import {
  57. mapMutations,
  58. mapState
  59. } from 'vuex';
  60. import uniNumberBox from '@/components/uni-number-box.vue'
  61. export default {
  62. components: {
  63. uniNumberBox
  64. },
  65. data() {
  66. return {
  67. total: 0, //总价格
  68. allChoose: false, //全选状态 true|false
  69. empty: false, //空白页现实 true|false
  70. cartList: [],
  71. state : 'load',
  72. userInfo: uni.getStorageSync('userInfo')
  73. };
  74. },
  75. onLoad() {
  76. },
  77. onPullDownRefresh() {
  78. this.state = 'load';
  79. this.cartList = [];
  80. this.getCart();
  81. },
  82. onShow() {
  83. this.state = 'load';
  84. this.cartList = [];
  85. this.getCart();
  86. },
  87. watch: {
  88. //显示空白页
  89. cartList(e) {
  90. let empty = e.length === 0 ? true : false;
  91. if (this.empty !== empty) {
  92. this.empty = empty;
  93. }
  94. }
  95. },
  96. computed: {
  97. ...mapState(['hasLogin'])
  98. },
  99. methods: {
  100. ...mapMutations(['login', 'logout', 'setUserInfo']),
  101. async getCart() {
  102. let data = await this.$api.request('/cart');
  103. await this.$api.checkLogin();
  104. this.userInfo = uni.getStorageSync('userInfo');
  105. uni.stopPullDownRefresh();
  106. this.state = 'loaded';
  107. if (data){
  108. this.cartList = data;
  109. this.calcTotal();
  110. }
  111. },
  112. cartPrice(oldPrice, nowPrice) {
  113. let string = '';
  114. if (oldPrice < nowPrice) {
  115. let number = (nowPrice - oldPrice).toFixed(2);
  116. string = ' ↑涨价 ' + number + '元';
  117. } else if (oldPrice > nowPrice) {
  118. let number = (oldPrice - nowPrice).toFixed(2);
  119. string = ' ↓降价 ' + number + '元';
  120. }
  121. return string;
  122. },
  123. navToLogin() {
  124. uni.navigateTo({
  125. url: '/pages/public/login'
  126. })
  127. },
  128. //选中状态处理
  129. async check(type, index) {
  130. let trueArr = [];
  131. let falseArr = [];
  132. let oldChoose = [];
  133. const list = this.cartList;
  134. //保存旧的数据
  135. list.forEach(item => {
  136. if(item.choose){
  137. oldChoose.push(item.cart_id);
  138. }
  139. })
  140. //本地处理
  141. if (type === 'item') {
  142. this.cartList[index].choose = !this.cartList[index].choose;
  143. if (this.cartList[index].choose) {
  144. trueArr.push(this.cartList[index].cart_id);
  145. } else {
  146. falseArr.push(this.cartList[index].cart_id);
  147. }
  148. } else {
  149. const choose = !this.allChoose
  150. list.forEach(item => {
  151. item.choose = choose;
  152. if (item.isset) {
  153. if (choose) {
  154. trueArr.push(item.cart_id);
  155. } else {
  156. falseArr.push(item.cart_id);
  157. }
  158. }
  159. })
  160. this.allChoose = choose;
  161. }
  162. this.calcTotal(type);
  163. //远程处理
  164. let result = await this.$api.request('/cart/choose_change', 'POST', {trueArr,falseArr});
  165. if (!result) {
  166. //恢复原来勾选的状态
  167. list.forEach(item => {
  168. if (oldChoose.indexOf(item.cart_id) >= 0) {
  169. item.choose = 1;
  170. } else {
  171. item.choose = 0;
  172. }
  173. })
  174. this.calcTotal(type);
  175. }
  176. },
  177. //数量
  178. async numberChange(data) {
  179. let oldNumber = this.cartList[data.index].number;
  180. let newNumber = data.number;
  181. this.cartList[data.index].number = newNumber;
  182. this.calcTotal();
  183. let cart_id = this.cartList[data.index].cart_id;
  184. let result = await this.$api.request('/cart/number_change?id='+cart_id, 'GET', {number:newNumber}, false);
  185. if (!result) {
  186. this.cartList[data.index].number = oldNumber;
  187. this.calcTotal();
  188. }
  189. },
  190. //删除
  191. async deleteCartItem(index) {
  192. let list = this.cartList;
  193. let row = list[index];
  194. let id = row.cart_id;
  195. uni.showModal({
  196. content: '确认删除 ' + list[index].title + '?' ,
  197. success: async (e) => {
  198. if (e.confirm) {
  199. let result = await this.$api.request('/cart/delete?', 'POST', {id:id});
  200. if (result) {
  201. let tempCart = this.cartList.splice(index, 1);
  202. this.calcTotal();
  203. }
  204. }
  205. }
  206. })
  207. },
  208. //清空
  209. async clearCart() {
  210. let [error, res] = await uni.showModal({
  211. title: '确认清空?'
  212. });
  213. if (res.confirm) {
  214. let id = [];
  215. this.cartList.forEach(item=>{
  216. id.push(item.cart_id);
  217. });
  218. let data = this.$api.request('/cart/delete', 'POST',{id:id});
  219. let that = this;
  220. if (data) {
  221. setTimeout(function(){
  222. that.state = 'load';
  223. that.cartList = [];
  224. that.getCart();
  225. },300);
  226. }
  227. }
  228. },
  229. //计算总价
  230. calcTotal() {
  231. let list = this.cartList;
  232. if (list.length === 0) {
  233. this.empty = true;
  234. return;
  235. }
  236. let total = 0;
  237. let choose = true;
  238. list.forEach(item => {
  239. if (item.isset) {
  240. if (item.choose == 1) {
  241. total += item.nowPrice * item.number;
  242. } else if (choose === true) {
  243. choose = false;
  244. }
  245. }
  246. })
  247. this.allChoose = choose;
  248. this.total = total.toFixed(2);
  249. },
  250. async getUserInfo() {
  251. uni.getUserProfile({
  252. desc: '系统将获取你的用户信息',
  253. success: async (res) => {
  254. let [error, loginRes] = await uni.login({
  255. provider: 'weixin'
  256. });
  257. if (loginRes.hasOwnProperty('code')) {
  258. let data = await this.$api.request('/user/registerUserGroup?code=' + loginRes.code, 'POST', res.userInfo);
  259. const userInfo = await this.$wechatMiniLogin(true);
  260. this.login(userInfo)
  261. this.userInfo = userInfo
  262. }
  263. },
  264. fail: (res) => {
  265. this.$api.msg('注册失败')
  266. }
  267. })
  268. },
  269. //创建订单
  270. createOrder() {
  271. let list = this.cartList;
  272. let cartId = [];
  273. list.forEach(item => {
  274. if (item.choose) {
  275. cartId.push(item.cart_id);
  276. }
  277. })
  278. if (cartId.length == 0) {
  279. this.$api.msg('没有选中商品');
  280. return;
  281. }
  282. this.$api.navTo(`/pages/order/createOrder?cart=${cartId.join(',')}`);
  283. },
  284. navTo(url){
  285. this.$api.navTo(url);
  286. }
  287. }
  288. }
  289. </script>
  290. <style lang='scss'>
  291. .container {
  292. padding-bottom: 134upx;
  293. /* 空白页 */
  294. .empty {
  295. position: fixed;
  296. left: 0;
  297. top: 0;
  298. width: 100%;
  299. height: 100vh;
  300. padding-bottom: 100upx;
  301. display: flex;
  302. justify-content: center;
  303. flex-direction: column;
  304. align-items: center;
  305. background: #fff;
  306. image {
  307. width: 240upx;
  308. height: 160upx;
  309. margin-bottom: 30upx;
  310. }
  311. .empty-tips {
  312. display: flex;
  313. font-size: $font-sm+2upx;
  314. color: $font-color-disabled;
  315. .navigator {
  316. color: $uni-color-primary;
  317. margin-left: 16upx;
  318. }
  319. }
  320. }
  321. }
  322. /* 购物车列表项 */
  323. .cart-item {
  324. display: flex;
  325. position: relative;
  326. padding: 30upx 40upx;
  327. .image-wrapper {
  328. width: 230upx;
  329. height: 230upx;
  330. flex-shrink: 0;
  331. position: relative;
  332. image {
  333. border-radius: 8upx;
  334. }
  335. }
  336. .checkbox {
  337. position: absolute;
  338. left: -16upx;
  339. top: -16upx;
  340. z-index: 8;
  341. font-size: 44upx;
  342. line-height: 1;
  343. padding: 4upx;
  344. color: $font-color-disabled;
  345. background: #fff;
  346. border-radius: 50px;
  347. }
  348. .item-right {
  349. display: flex;
  350. flex-direction: column;
  351. flex: 1;
  352. overflow: hidden;
  353. position: relative;
  354. padding-left: 30upx;
  355. .title,
  356. .price {
  357. font-size: $font-base + 2upx;
  358. color: $font-color-dark;
  359. height: 40upx;
  360. line-height: 40upx;
  361. }
  362. .attr {
  363. font-size: $font-sm + 2upx;
  364. color: $font-color-light;
  365. height: 50upx;
  366. line-height: 50upx;
  367. }
  368. .price {
  369. height: 50upx;
  370. line-height: 50upx;
  371. }
  372. }
  373. .del-btn {
  374. padding: 4upx 10upx;
  375. font-size: 34upx;
  376. height: 50upx;
  377. color: $font-color-light;
  378. }
  379. .invalid {
  380. position: absolute;
  381. right: 0;
  382. bottom: 0;
  383. background: #999999;
  384. color: #ffffff;
  385. padding: 6upx 12upx;
  386. border-radius: 10upx;
  387. font-size: 26upx;
  388. margin-right: 50upx;
  389. margin-bottom: 32upx;
  390. }
  391. }
  392. /* 底部栏 */
  393. .action-section {
  394. /* #ifdef H5 */
  395. margin-bottom: 100upx;
  396. /* #endif */
  397. position: fixed;
  398. left: 30upx;
  399. bottom: 30upx;
  400. z-index: 95;
  401. display: flex;
  402. align-items: center;
  403. width: 690upx;
  404. height: 100upx;
  405. padding: 0 30upx;
  406. background: rgba(255, 255, 255, .9);
  407. box-shadow: 0 0 20upx 0 rgba(0, 0, 0, .5);
  408. border-radius: 16upx;
  409. .checkbox {
  410. height: 52upx;
  411. position: relative;
  412. image {
  413. width: 52upx;
  414. height: 100%;
  415. position: relative;
  416. z-index: 5;
  417. }
  418. }
  419. .clear-btn {
  420. position: absolute;
  421. left: 26upx;
  422. top: 0;
  423. z-index: 4;
  424. width: 0;
  425. height: 52upx;
  426. line-height: 52upx;
  427. padding-left: 38upx;
  428. font-size: $font-base;
  429. color: #fff;
  430. background: $font-color-disabled;
  431. border-radius: 0 50px 50px 0;
  432. opacity: 0;
  433. transition: .2s;
  434. &.show {
  435. opacity: 1;
  436. width: 120upx;
  437. }
  438. }
  439. .total-box {
  440. flex: 1;
  441. display: flex;
  442. flex-direction: column;
  443. text-align: right;
  444. padding-right: 40upx;
  445. .price {
  446. font-size: $font-lg;
  447. color: $font-color-dark;
  448. }
  449. }
  450. .confirm-btn {
  451. padding: 0 38upx;
  452. margin: 0;
  453. border-radius: 100px;
  454. height: 76upx;
  455. line-height: 76upx;
  456. font-size: $font-base + 2upx;
  457. background: $uni-color-primary;
  458. box-shadow: 1px 2px 5px rgba(217, 60, 93, 0.72)
  459. }
  460. }
  461. /* 复选框选中状态 */
  462. .action-section .checkbox.checked,
  463. .cart-item .checkbox.checked {
  464. color: $uni-color-primary;
  465. }
  466. button.get-user-info {
  467. margin: 0px auto;
  468. width: 280rpx;
  469. height: 60rpx;
  470. line-height: 60rpx;
  471. font-size: 14px;
  472. }
  473. .get-user-info-text {
  474. padding: 20px 80px;
  475. font-size: 16px;
  476. text-align: center;
  477. line-height: 32px;
  478. }
  479. </style>