cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <template>
  2. <view class="card" v-if="!loadding" :data-theme='theme()' :class="theme() || ''">
  3. <!-- #ifdef APP-PLUS -->
  4. <header-bar></header-bar>
  5. <!-- #endif -->
  6. <!--产品列表-->
  7. <template v-if="tableData.length > 0">
  8. <view class="address-bar d-e-c">
  9. <view class="f30" @click="isEdit = !isEdit">
  10. <button class="f24" v-if="isEdit">完成</button>
  11. <button class="f24" v-else>编辑</button>
  12. </view>
  13. </view>
  14. <view class="section">
  15. <view class="supplier_list">
  16. <view class="item" v-for="(item, index) in tableData" :key="index">
  17. <label class="d-c-c" @click="checkItem(item, index)"><checkbox value="cb" class="checkbox" :checked="item.checked" /></label>
  18. <image :src="item.product_image" class="cover" mode="aspectFit"></image>
  19. <view class="info">
  20. <view class="title">{{ item.product_name }}</view>
  21. <view class="describe">{{ item.product_sku.product_attr }}</view>
  22. <view class="level-box count_choose">
  23. <view class="price">
  24. ¥
  25. <text class="num">{{ item.product_sku.product_price }}</text>
  26. </view>
  27. <view class="num-wrap">
  28. <view class="" @tap.stop="reduceFunc(item)" v-if="item.total_num > 1">
  29. <image class="reduce_icon" src="../../static/icon/reduce.png" mode=""></image>
  30. </view>
  31. <view class="text-wrap">{{item.total_num}}</view>
  32. <view class="" @tap.stop="addFunc(item)" v-if="item.total_num < item.product_sku.stock_num ">
  33. <image class="add_icon" src="../../static/icon/add.png" mode=""></image>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <template v-else>
  43. <view class="none-data-box cart_none">
  44. <image class="cart_none_img" src="/static/cart_none.png" mode="widthFix"></image>
  45. <text class="f26 gray9 pt10">购物车空空如也</text>
  46. <button type="default" class="btn-gcred mt30 ww100 none_btn" @tap="gotoShop">去购物</button>
  47. </view>
  48. </template>
  49. <!--底部按钮-->
  50. <view class="bottom-btns f28" v-if="tableData.length > 0">
  51. <label class="d-c-c mr20" @click="onCheckedAll()">
  52. <checkbox class="checkbox" :checked="checkedAll" value="cb" />
  53. 全选
  54. </label>
  55. <view class="d-e-c" v-if="!isEdit">
  56. <view class="total d-s-c flex-1 mr20">
  57. <text>合计:</text>
  58. <view class="price f22">
  59. ¥
  60. <text class="num f40">{{ totalPrice }}</text>
  61. </view>
  62. </view>
  63. <button type="primary" class="buy-btn" @click="Submit()">去结算</button>
  64. </view>
  65. <view class="" v-else><button type="primary" @click="onDelete()" class="delete-btn mr20">删除</button></view>
  66. </view>
  67. <!--推荐-->
  68. <recommendProduct :location="10"></recommendProduct>
  69. </view>
  70. </template>
  71. <script>
  72. import recommendProduct from '@/components/recommendProduct/recommendProduct.vue';
  73. export default {
  74. components: {
  75. recommendProduct
  76. },
  77. data() {
  78. return {
  79. /*是否加载完成*/
  80. loadding: true,
  81. isEdit: false,
  82. /*购物车商品*/
  83. tableData: [],
  84. arrIds: [],
  85. // 是否全选
  86. checkedAll: false,
  87. totalPrice: 0
  88. };
  89. },
  90. onLoad() {},
  91. onShow() {
  92. /*获取产品详情*/
  93. this.getData();
  94. },
  95. methods: {
  96. /*获取数据*/
  97. getData() {
  98. uni.showLoading({
  99. title: '加载中'
  100. });
  101. let self = this;
  102. self._get('order.cart/lists', {}, function(res) {
  103. uni.hideLoading();
  104. self.tableData = res.data;
  105. self.loadding = false;
  106. self._initGoodsChecked();
  107. });
  108. },
  109. /**
  110. * 初始化商品选中状态
  111. */
  112. _initGoodsChecked() {
  113. let self = this;
  114. let checkedData = self.getCheckedData();
  115. // 将商品设置选中
  116. self.tableData.forEach(item => {
  117. item.checked = self.inArray(`${item.product_id}_${item.product_sku_id}`, checkedData);
  118. });
  119. self.isEdit = false;
  120. self.checkedAll = checkedData.length == self.tableData.length;
  121. // 更新购物车已选商品总价格
  122. self.updateTotalPrice();
  123. },
  124. /**
  125. * 获取选中的商品
  126. */
  127. getCheckedData() {
  128. let checkedData = uni.getStorageSync('checkedData');
  129. return checkedData ? checkedData : [];
  130. },
  131. /*单选*/
  132. checkItem(e, index) {
  133. e.checked = !e.checked;
  134. this.$set(this.tableData, index, e);
  135. // 更新选中记录
  136. this.onUpdateChecked();
  137. // 更新购物车已选商品总价格
  138. this.updateTotalPrice();
  139. // 更新全选状态
  140. this.checkedAll = this.getCheckedData().length == this.tableData.length;
  141. },
  142. /**
  143. * 更新商品选中记录
  144. */
  145. onUpdateChecked() {
  146. let self = this,
  147. checkedData = [];
  148. self.tableData.forEach(item => {
  149. if (item.checked == true) {
  150. checkedData.push(`${item.product_id}_${item.product_sku_id}`);
  151. }
  152. });
  153. uni.setStorageSync('checkedData', checkedData);
  154. },
  155. /*全选*/
  156. onCheckedAll() {
  157. let self = this;
  158. self.checkedAll = !self.checkedAll;
  159. self.tableData.forEach(item => {
  160. item.checked = self.checkedAll;
  161. });
  162. self.updateTotalPrice();
  163. // 更新选中记录
  164. self.onUpdateChecked();
  165. },
  166. updateTotalPrice: function() {
  167. let total_price = 0;
  168. let items = this.tableData;
  169. for (let i = 0; i < items.length; i++) {
  170. if (items[i]['checked'] == true) {
  171. total_price += items[i]['total_num'] * items[i]['product_price'];
  172. }
  173. }
  174. /*保留2位小数*/
  175. this.totalPrice = total_price.toFixed(2);
  176. },
  177. /*去结算*/
  178. Submit() {
  179. let arrIds = [];
  180. this.tableData.forEach(item => {
  181. if (item.checked == true) {
  182. arrIds.push(`${item.product_id}_${item.product_sku_id}`);
  183. }
  184. });
  185. if (arrIds.length == 0) {
  186. uni.showToast({
  187. title: '请选择商品',
  188. icon: 'none'
  189. });
  190. return false;
  191. }
  192. this.gotoPage('/pages/order/confirm-order?order_type=cart&cart_ids=' + arrIds);
  193. },
  194. /*添加*/
  195. addFunc(item) {
  196. let self = this;
  197. let product_id = item.product_id;
  198. let product_sku_id = item.product_sku_id;
  199. uni.showLoading({
  200. title: '加载中'
  201. });
  202. self._post(
  203. 'order.cart/add',
  204. {
  205. product_id: product_id,
  206. product_sku_id: product_sku_id,
  207. total_num: 1
  208. },
  209. function(res) {
  210. self.loadding = false;
  211. self.getData();
  212. },
  213. function() {
  214. self.loadding = false;
  215. }
  216. );
  217. },
  218. /*减少*/
  219. reduceFunc(item) {
  220. let self = this;
  221. let product_id = item.product_id;
  222. let product_sku_id = item.product_sku_id;
  223. if (item.total_num <= 1) {
  224. return;
  225. }
  226. uni.showLoading({
  227. title: '加载中'
  228. });
  229. self._post(
  230. 'order.cart/sub',
  231. {
  232. product_id: product_id,
  233. product_sku_id: product_sku_id
  234. },
  235. function(res) {
  236. self.loadding = false;
  237. self.getData();
  238. },
  239. function() {
  240. self.loadding = false;
  241. }
  242. );
  243. },
  244. /*删除购物车*/
  245. onDelete() {
  246. let self = this;
  247. let cartIds = self.getCheckedIds();
  248. if (!cartIds.length) {
  249. self.showError('您还没有选择商品');
  250. return false;
  251. }
  252. uni.showModal({
  253. title: '提示',
  254. content: '您确定要移除选择的商品吗?',
  255. success(e) {
  256. e.confirm &&
  257. self._post(
  258. 'order.cart/delete',
  259. {
  260. product_sku_id: cartIds
  261. },
  262. function(res) {
  263. // 删除选中的商品
  264. self.onDeleteEvent(cartIds);
  265. self.getData();
  266. }
  267. );
  268. }
  269. });
  270. },
  271. /**
  272. * 获取已选中的商品
  273. */
  274. getCheckedIds() {
  275. let self = this;
  276. let arrIds = [];
  277. self.tableData.forEach(item => {
  278. if (item.checked === true) {
  279. arrIds.push(`${item.product_id}_${item.product_sku_id}`);
  280. }
  281. });
  282. return arrIds;
  283. },
  284. /**
  285. * 商品删除事件
  286. */
  287. onDeleteEvent(cartIds) {
  288. let self = this;
  289. cartIds.forEach(cartIndex => {
  290. self.tableData.forEach((item, productIndex) => {
  291. if (cartIndex == `${item.product_id}_${item.product_sku_id}`) {
  292. self.tableData.splice(productIndex, 1);
  293. }
  294. });
  295. });
  296. // 更新选中记录
  297. self.onUpdateChecked();
  298. return true;
  299. },
  300. /**
  301. * 是否在数组内
  302. */
  303. inArray(search, array) {
  304. for (var i in array) {
  305. if (array[i] == search) {
  306. return true;
  307. }
  308. }
  309. return false;
  310. },
  311. /*去购物*/
  312. gotoShop() {
  313. uni.switchTab({
  314. url: '/pages/index/index'
  315. });
  316. }
  317. }
  318. };
  319. </script>
  320. <style lang="scss">
  321. page {
  322. background-color: #f2f2f2;
  323. }
  324. .foot_ {
  325. height: 98rpx;
  326. width: 100%;
  327. }
  328. .card .scroll-Y {
  329. position: relative;
  330. }
  331. .card .checkbox {
  332. color: red;
  333. transform: scale(0.7);
  334. }
  335. .address-bar {
  336. padding: 20rpx;
  337. background-color: #f2f2f2;
  338. }
  339. .address-bar button {
  340. border: none;
  341. background: none;
  342. color: #333333;
  343. }
  344. .section {
  345. background: #f2f2f2;
  346. padding: 20rpx;
  347. }
  348. .section .item {
  349. margin: 0 26rpx;
  350. display: flex;
  351. align-items: center;
  352. // border: 1px solid #f2f2f2;
  353. padding-right: 19rpx;
  354. padding-bottom: 29rpx;
  355. padding-top: 29rpx;
  356. }
  357. .section .cover {
  358. width: 150rpx;
  359. height: 150rpx;
  360. border-radius: 8px;
  361. }
  362. .section .info {
  363. flex: 1;
  364. padding-left: 30rpx;
  365. box-sizing: border-box;
  366. overflow: hidden;
  367. }
  368. .section .title {
  369. width: 100%;
  370. font-size: 26rpx;
  371. display: -webkit-box;
  372. overflow: hidden;
  373. -webkit-line-clamp: 2;
  374. -webkit-box-orient: vertical;
  375. }
  376. .vender .list .describe {
  377. width: 100%;
  378. white-space: nowrap;
  379. overflow: hidden;
  380. text-overflow: ellipsis;
  381. }
  382. .section .describe {
  383. margin-top: 20rpx;
  384. font-size: 24rpx;
  385. color: #999999;
  386. display: -webkit-box;
  387. -webkit-box-orient: vertical;
  388. -webkit-line-clamp: 3;
  389. overflow: hidden;
  390. }
  391. .section .price {
  392. color: $dominant-color;
  393. // @include font_color("font_color");
  394. font-size: 24rpx;
  395. }
  396. .section .price .num {
  397. padding: 0 4rpx;
  398. font-size: 32rpx;
  399. }
  400. .section .level-box {
  401. margin-top: 20rpx;
  402. display: flex;
  403. justify-content: space-between;
  404. align-items: center;
  405. }
  406. .section .level-box .key {
  407. font-size: 24rpx;
  408. color: #999999;
  409. }
  410. .section .level-box .num-wrap {
  411. display: flex;
  412. justify-content: flex-end;
  413. align-items: center;
  414. }
  415. .section .level-box .icon-box {
  416. width: 33rpx;
  417. height: 33rpx;
  418. border: 1px solid #c5c5c5;
  419. background: #f2f2f2;
  420. }
  421. .section .level-box .icon-box .gray {
  422. color: #cccccc;
  423. }
  424. .section .level-box .icon-box .gray3 {
  425. color: #333333;
  426. }
  427. .section .level-box .text-wrap {
  428. margin: 0 20rpx;
  429. height: 33rpx;
  430. border: none;
  431. background: none;
  432. }
  433. .section .level-box .text-wrap input {
  434. padding: 0 4rpx;
  435. height: 33rpx;
  436. line-height: 1;
  437. width: 40rpx;
  438. font-size: 32rpx;
  439. text-align: center;
  440. display: flex;
  441. align-items: center;
  442. min-height: 33rpx;
  443. }
  444. .bottom-btns {
  445. position: fixed;
  446. width: 100%;
  447. padding: 0 0 0 20rpx;
  448. box-sizing: border-box;
  449. display: flex;
  450. justify-content: space-between;
  451. align-items: center;
  452. height: 90rpx;
  453. // /* bottom: calc(var(--window-bottom); */
  454. bottom: var(--window-bottom, 0);
  455. // bottom: 50px;
  456. left: 0;
  457. box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.1);
  458. background: #ffffff;
  459. z-index: 1000;
  460. }
  461. .bottom-btns .delete-btn {
  462. margin: 0;
  463. padding: 0 30rpx;
  464. height: 60rpx;
  465. line-height: 60rpx;
  466. border-radius: 30rpx;
  467. background: linear-gradient(90deg, #FF6B6B 4%, #F6220C 100%);
  468. font-size: 26rpx;
  469. }
  470. .bottom-btns .buy-btn {
  471. margin: 0;
  472. padding: 0 24rpx;
  473. height: 60rpx;
  474. line-height: 60rpx;
  475. border-radius: 30rpx;
  476. background: linear-gradient(90deg, #FF6B6B 4%, #F6220C 100%);
  477. font-size: 26rpx;
  478. }
  479. .bottom-btns .price {
  480. color: $dominant-color;
  481. }
  482. .supplier_list {
  483. overflow: hidden;
  484. background-color: #FFFFFF;
  485. border-radius: 15rpx;
  486. margin-bottom: 30rpx;
  487. }
  488. .supplier_list_tit {
  489. display: flex;
  490. align-items: center;
  491. margin-left: 28rpx;
  492. margin-top: 28rpx;
  493. }
  494. .icon-dianpu1 {
  495. font-size: 34rpx;
  496. color: #333333;
  497. margin: 0 17rpx;
  498. }
  499. .cart_none .cart_none_img {
  500. width: 348rpx;
  501. height: 222rpx;
  502. }
  503. .none_btn {
  504. font-size: 32rpx;
  505. border-radius: 40rpx;
  506. }
  507. .add_icon,
  508. .reduce_icon {
  509. width: 32rpx;
  510. height: 32rpx;
  511. }
  512. </style>