You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

482 lines
11 KiB

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