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.
 
 
 
 
 
 

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