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.
 
 
 
 
 
 

484 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">To Settle</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. cancelText:'Cancel',
  195. confirmText:'OK',
  196. content: 'Confirm delete ' + 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: 'Confirm empty?'
  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. //创建订单
  251. createOrder() {
  252. let list = this.cartList;
  253. let cartId = [];
  254. list.forEach(item => {
  255. if (item.choose) {
  256. cartId.push(item.cart_id);
  257. }
  258. })
  259. if (cartId.length == 0) {
  260. this.$api.msg('没有选中商品');
  261. return;
  262. }
  263. this.$api.navTo(`/pages/order/createOrder?cart=${cartId.join(',')}`);
  264. },
  265. navTo(url){
  266. this.$api.navTo(url);
  267. }
  268. }
  269. }
  270. </script>
  271. <style lang='scss'>
  272. .container {
  273. padding-bottom: 134upx;
  274. /* 空白页 */
  275. .empty {
  276. position: fixed;
  277. left: 0;
  278. top: 0;
  279. width: 100%;
  280. height: 100vh;
  281. padding-bottom: 100upx;
  282. display: flex;
  283. justify-content: center;
  284. flex-direction: column;
  285. align-items: center;
  286. background: #fff;
  287. image {
  288. width: 240upx;
  289. height: 160upx;
  290. margin-bottom: 30upx;
  291. }
  292. .empty-tips {
  293. display: flex;
  294. font-size: $font-sm+2upx;
  295. color: $font-color-disabled;
  296. .navigator {
  297. color: $uni-color-primary;
  298. margin-left: 16upx;
  299. }
  300. }
  301. }
  302. }
  303. /* 购物车列表项 */
  304. .cart-item {
  305. display: flex;
  306. position: relative;
  307. padding: 30upx 40upx;
  308. .image-wrapper {
  309. width: 230upx;
  310. height: 230upx;
  311. flex-shrink: 0;
  312. position: relative;
  313. image {
  314. border-radius: 8upx;
  315. }
  316. }
  317. .checkbox {
  318. position: absolute;
  319. left: -16upx;
  320. top: -16upx;
  321. z-index: 8;
  322. font-size: 44upx;
  323. line-height: 1;
  324. padding: 4upx;
  325. color: $font-color-disabled;
  326. background: #fff;
  327. border-radius: 50px;
  328. }
  329. .item-right {
  330. display: flex;
  331. flex-direction: column;
  332. flex: 1;
  333. overflow: hidden;
  334. position: relative;
  335. padding-left: 30upx;
  336. .title,
  337. .price {
  338. font-size: $font-base + 2upx;
  339. color: $font-color-dark;
  340. height: 40upx;
  341. line-height: 40upx;
  342. }
  343. .attr {
  344. font-size: $font-sm + 2upx;
  345. color: $font-color-light;
  346. height: 50upx;
  347. line-height: 50upx;
  348. }
  349. .price {
  350. height: 50upx;
  351. line-height: 50upx;
  352. }
  353. }
  354. .del-btn {
  355. padding: 4upx 10upx;
  356. font-size: 34upx;
  357. height: 50upx;
  358. color: $font-color-light;
  359. }
  360. .invalid {
  361. position: absolute;
  362. right: 0;
  363. bottom: 0;
  364. background: #999999;
  365. color: #ffffff;
  366. padding: 6upx 12upx;
  367. border-radius: 10upx;
  368. font-size: 26upx;
  369. margin-right: 50upx;
  370. margin-bottom: 32upx;
  371. }
  372. }
  373. /* 底部栏 */
  374. .action-section {
  375. /* #ifdef H5 */
  376. margin-bottom: 100upx;
  377. /* #endif */
  378. position: fixed;
  379. left: 30upx;
  380. bottom: 30upx;
  381. z-index: 95;
  382. display: flex;
  383. align-items: center;
  384. width: 690upx;
  385. height: 100upx;
  386. padding: 0 30upx;
  387. background: rgba(255, 255, 255, .9);
  388. box-shadow: 0 0 20upx 0 rgba(0, 0, 0, .5);
  389. border-radius: 16upx;
  390. .checkbox {
  391. height: 52upx;
  392. position: relative;
  393. image {
  394. width: 52upx;
  395. height: 100%;
  396. position: relative;
  397. z-index: 5;
  398. }
  399. }
  400. .clear-btn {
  401. position: absolute;
  402. left: 26upx;
  403. top: 0;
  404. z-index: 4;
  405. width: 0;
  406. height: 52upx;
  407. line-height: 52upx;
  408. padding-left: 38upx;
  409. font-size: $font-base;
  410. color: #fff;
  411. background: $font-color-disabled;
  412. border-radius: 0 50px 50px 0;
  413. opacity: 0;
  414. transition: .2s;
  415. &.show {
  416. opacity: 1;
  417. width: 120upx;
  418. }
  419. }
  420. .total-box {
  421. flex: 1;
  422. display: flex;
  423. flex-direction: column;
  424. text-align: right;
  425. padding-right: 40upx;
  426. .price {
  427. font-size: $font-lg;
  428. color: $font-color-dark;
  429. }
  430. }
  431. .confirm-btn {
  432. padding: 0 38upx;
  433. margin: 0;
  434. border-radius: 100px;
  435. height: 76upx;
  436. line-height: 76upx;
  437. font-size: $font-base + 2upx;
  438. background: $uni-color-primary;
  439. box-shadow: 1px 2px 5px rgba(217, 60, 93, 0.72)
  440. }
  441. }
  442. /* 复选框选中状态 */
  443. .action-section .checkbox.checked,
  444. .cart-item .checkbox.checked {
  445. color: $uni-color-primary;
  446. }
  447. </style>