cart.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <template>
  2. <app-layout>
  3. <view class="app-cart">
  4. <view class="app-announcement dir-left-nowrap main-between cross-center">
  5. <text class="app-announcement-text">商品库存有限,请尽快下单</text>
  6. <app-form-id @click="editSwitch">
  7. <text class="app-edit-text">{{editStatus ? '完成' : '编辑'}}</text>
  8. </app-form-id>
  9. </view>
  10. <view class="app-item" v-for="(mch, index) in listObj" :key="index" v-if="listObj.length > 0">
  11. <app-shop-product @change="change" :mch="mch" :editStatus="editStatus" @changeSingleRadio="changeSingleRadio" @changeRadioAll="changeRadioAll" ></app-shop-product>
  12. </view>
  13. <view class="no-cart" v-if="listObj.length === 0">
  14. <view class="cart-icon">
  15. <image class="cart-image" src="../../static/image/icon/nav-icon-cart.png"></image>
  16. </view>
  17. <view class="cart-text">购物车还是空的哦</view>
  18. </view>
  19. <view>
  20. <app-empty-bottom backgroundColor="#f7f7f7" :botBool="botBool" :height="Number(100)"></app-empty-bottom>
  21. </view>
  22. <view v-if="!tabbarbool" :style="{background: 'white', position: 'fixed', bottom: 0, height: `${getEmpty}rpx`, width: '750rpx'}"></view>
  23. <view class="app-settlement dir-left-nowrap main-between cross-center" :style="{bottom: `${tabbarbool ? BotHeight : getEmpty}rpx`}">
  24. <view class="app-radio dir-left-nowrap main-right cross-center">
  25. <app-radio type="round" @click="setALl" v-model="all"></app-radio>
  26. <text class="app-all-text">全选</text>
  27. <text class="app-price" v-if="!editStatus">总计: ¥{{priceNum}}</text>
  28. </view>
  29. <button :disabled="submitDis" :class="submitDis ? 'delete-disabled' : ''" class="app-delete" v-if="editStatus" @click="deleteProduct">
  30. 删除
  31. </button>
  32. <button :disabled="submitDis" :class="submitDis ? 'disabled-background' : ''" class="app-settlement-button" v-else @click="settlement">
  33. 去结算
  34. </button>
  35. </view>
  36. </view>
  37. </app-layout>
  38. </template>
  39. <script>
  40. import { mapState, mapGetters } from 'vuex';
  41. import appShopProduct from './components/app-shop-product/app-shop-product.vue';
  42. import appRadio from '../../components/basic-component/app-radio/app-radio.vue';
  43. import appIphoneX from '../../components/basic-component/app-iphone-x/app-iphone-x.vue';
  44. import appEmptyBottom from '../../components/basic-component/app-empty-bottom/app-empty-bottom.vue';
  45. export default {
  46. name: 'cart',
  47. components:{
  48. 'app-shop-product': appShopProduct,
  49. 'app-radio': appRadio,
  50. 'app-iphone-x': appIphoneX,
  51. 'app-empty-bottom': appEmptyBottom,
  52. },
  53. data() {
  54. return {
  55. editStatus: false,
  56. listObj: [],
  57. all: false,
  58. editList: [],
  59. priceNum: 0,
  60. edit: false,
  61. botBool: true,
  62. currentRoute: this.$platDiff.route(),
  63. tabbarbool: false,
  64. spike: -1,
  65. submitDis: true,
  66. }
  67. },
  68. computed:{
  69. ...mapState({
  70. tabBarHeight: (state) => {
  71. return state.gConfig.tabBarHeight;
  72. },
  73. iphoneHeight: (state) =>{
  74. return state.gConfig.iphoneHeight;
  75. },
  76. iphone: (state) => {
  77. return state.gConfig.iphone
  78. }
  79. }),
  80. ...mapGetters('iPhoneX', {
  81. BotHeight: 'getBotHeight',
  82. getEmpty: 'getEmpty',
  83. }),
  84. ...mapState({
  85. tabBarNavs: state => state.mallConfig.navbar.navs,
  86. }),
  87. },
  88. methods: {
  89. setALl(data) {
  90. this.selectAll(data);
  91. },
  92. // 编辑
  93. editSwitch() {
  94. this.all = false;
  95. for (let i = 0; i < this.listObj.length; i++) {
  96. this.listObj[i].is_active = false;
  97. for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
  98. this.listObj[i].goods_list[j].is_active = false;
  99. }
  100. }
  101. this.editStatus = !this.editStatus;
  102. },
  103. async getProductList() {
  104. uni.showLoading({
  105. title: '加载中'
  106. });
  107. const res = await this.$request({
  108. url: this.$api.cart.list,
  109. method: 'get',
  110. });
  111. if (res.code === 0) {
  112. this.listObj = res.data.list;
  113. this.spikeTime(res.data.list);
  114. }
  115. uni.hideLoading();
  116. },
  117. // 商城全选
  118. changeRadioAll(data) {
  119. for (let i = 0; i < this.listObj.length; i++) {
  120. let {mch_id , goods_list } = this.listObj[i];
  121. if (data.mch_id === mch_id) {
  122. for (let j = 0; j < goods_list.length; j++) {
  123. if (!this.editStatus) {
  124. if (goods_list[j].new_status === 0) goods_list[j].is_active = !data.is_active;
  125. } else {
  126. goods_list[j].is_active = !data.is_active;
  127. }
  128. }
  129. this.listObj[i].is_active = !data.is_active;
  130. }
  131. }
  132. },
  133. // 单选
  134. changeSingleRadio(mch, item) {
  135. for (let i = 0; i < this.listObj.length; i++) {
  136. if (this.listObj[i].mch_id === mch.mch_id) {
  137. let goods_list_len = mch.goods_list.length;
  138. let active_num = 0;
  139. for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
  140. if (item.id === this.listObj[i].goods_list[j].id) {
  141. this.listObj[i].goods_list[j].is_active = !this.listObj[i].goods_list[j].is_active;
  142. }
  143. if (this.listObj[i].goods_list[j].is_active) {
  144. active_num++;
  145. }
  146. if (this.editStatus === false && this.listObj[i].goods_list[j].new_status !== 0) {
  147. goods_list_len --;
  148. }
  149. }
  150. if (goods_list_len === active_num) {
  151. this.listObj[i].is_active = true;
  152. } else {
  153. this.listObj[i].is_active = false;
  154. }
  155. }
  156. }
  157. },
  158. selectAll(bool) {
  159. this.listObj.map(item => {
  160. item.is_active = bool;
  161. item.goods_list.map(good => {
  162. if (this.editStatus === false) {
  163. if (good.new_status === 0) {
  164. good.is_active = bool;
  165. }
  166. } else {
  167. good.is_active = bool;
  168. }
  169. })
  170. });
  171. },
  172. // 结算
  173. settlement() {
  174. let all_product = [];
  175. let is_miaosha = false;
  176. for (let i = 0; i < this.listObj.length; i++) {
  177. let mch = {
  178. mch_id: this.listObj[i].mch_id,
  179. goods_list: [],
  180. };
  181. for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
  182. let good = this.listObj[i].goods_list[j];
  183. if (good.new_status === 0 && good.is_active) {
  184. if (good.sign === 'miaosha') is_miaosha = true;
  185. let product = {
  186. id: good.goods_id,
  187. attr: [
  188. ],
  189. num: good.num,
  190. cart_id: good.id,
  191. goods_attr_id: good.attr_id,
  192. };
  193. for (let n in good.attrs.attr) {
  194. let attr = {
  195. attr_id: good.attrs.attr[n].attr_id,
  196. attr_group_id: good.attrs.attr[n].attr_group_id,
  197. };
  198. product.attr.push(attr);
  199. }
  200. mch.goods_list.push(product);
  201. }
  202. }
  203. if (mch.goods_list.length > 0) {
  204. all_product.push(mch);
  205. }
  206. }
  207. let jump_url = `/pages/order-submit/order-submit?mch_list=${JSON.stringify(all_product)}`;
  208. if (is_miaosha) {
  209. jump_url += `&preview_url=${encodeURIComponent(this.$api.miaosha.order_preview)}&submit_url=${encodeURIComponent(this.$api.miaosha.order_submit)}`;
  210. }
  211. this.$jump({
  212. open_type: 'navigate',
  213. url: jump_url,
  214. });
  215. setTimeout(()=> {
  216. this.listObj = [];
  217. }, 1000);
  218. },
  219. editNum() {
  220. let editList = [];
  221. for (let i = 0; i < this.listObj.length; i++) {
  222. let goods = this.listObj[i].goods_list;
  223. for (let j = 0; j < goods.length; j++) {
  224. if (goods[j].new_status === 0) {
  225. editList.push({
  226. goods_id: goods[j].goods_id,
  227. num: goods[j].num,
  228. attr: goods[j].attr_id,
  229. })
  230. }
  231. }
  232. }
  233. if (!editList.length) return;
  234. this.$request({
  235. method: 'post',
  236. url: this.$api.cart.edit,
  237. data: {
  238. list: JSON.stringify(editList)
  239. }
  240. }).then(() => {
  241. })
  242. },
  243. // 删除商品
  244. async deleteProduct() {
  245. let product_list = [];
  246. for (let i = 0 ; i < this.listObj.length; i++) {
  247. let goods = this.listObj[i].goods_list;
  248. for (let j = 0 ; j < goods.length; j++) {
  249. if (goods[j].is_active) {
  250. product_list.push({
  251. mch_id: goods[j].mch_id,
  252. id: goods[j].id,
  253. })
  254. }
  255. }
  256. }
  257. if (!product_list.length) return;
  258. const res = await this.$request({
  259. method: 'post',
  260. url: this.$api.cart.delete,
  261. data: {
  262. cart_id_list: JSON.stringify(product_list),
  263. }
  264. });
  265. if (res.code === 0) {
  266. this.editStatus = false;
  267. for (let j = 0; j < this.listObj.length; j++) {
  268. for (let i = 0; i < product_list.length; i++) {
  269. if (this.listObj[j].mch_id == product_list[i].mch_id) {
  270. for (let k = 0; k < this.listObj[j].goods_list.length; k++) {
  271. if (this.listObj[j].goods_list[k].id == product_list[i].id) {
  272. this.$delete(this.listObj[j].goods_list, k);
  273. }
  274. }
  275. if (this.listObj[j].goods_list.length === 0) {
  276. this.$delete(this.listObj, j);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. },
  283. b() {
  284. let currentRoute = this.currentRoute;
  285. for (let i = 0; i < this.tabBarNavs.length; i++) {
  286. if(currentRoute.includes(this.tabBarNavs[i].url.split('?')[0])) {
  287. return this.tabbarbool = true;
  288. }
  289. }
  290. return this.tabbarbool = false;
  291. },
  292. change(number, id, mch_id) {
  293. for (let i = 0; i < this.listObj.length; i++) {
  294. if (this.listObj[i].mch_id === mch_id) {
  295. for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
  296. if (this.listObj[i].goods_list[j].id === id) {
  297. this.listObj[i].goods_list[j].num = Number(number);
  298. return;
  299. }
  300. }
  301. }
  302. }
  303. },
  304. setTime(data) {
  305. let is_spike = false;
  306. for (let i = 0; i < data.length; i++) {
  307. for (let j = 0; j < data[i].goods_list.length; j++) {
  308. if (data[i].goods_list[j].sign === 'miaosha' && data[i].goods_list[j].miaosha_status === 1) {
  309. is_spike = true;
  310. data[i].goods_list[j].miaosha_time--;
  311. let second = parseInt(data[i].goods_list[j].miaosha_time);
  312. let minute = 0;
  313. let hour = 0;
  314. if (second > 60) {
  315. minute = parseInt(second / 60);
  316. second = parseInt(second % 60);
  317. if (minute > 60) {
  318. hour = parseInt(minute / 60);
  319. minute = parseInt(minute % 60);
  320. }
  321. }
  322. let timeDaily = {
  323. h: hour < 10 ? ('0' + hour) : hour,
  324. m: minute < 10 ? ('0' + minute) : minute,
  325. s: second < 10 ? ('0' + second) : second,
  326. };
  327. this.$set(this.listObj[i].goods_list[j], 'miaosha_string', `${timeDaily.h + ':' + timeDaily.m + ':' + timeDaily.s}`);
  328. }
  329. }
  330. }
  331. return is_spike;
  332. },
  333. spikeTime(data) {
  334. clearInterval(this.spike);
  335. let is_spike = this.setTime(data);
  336. if (!is_spike) return;
  337. this.spike = setInterval(() => {
  338. let is_spike = this.setTime(data);
  339. if (!is_spike) {
  340. clearInterval(this.spike);
  341. }
  342. }, 1000);
  343. }
  344. },
  345. onLoad() {
  346. setTimeout(() => {
  347. this.getProductList();
  348. this.all = false;
  349. }, 1000);
  350. },
  351. onShow() {
  352. this.submitDis = true;
  353. this.getProductList();
  354. this.all = false;
  355. },
  356. onHide() {
  357. this.editNum();
  358. clearInterval(this.spike);
  359. },
  360. onUnload() {
  361. this.editNum();
  362. clearInterval(this.spike);
  363. },
  364. watch:{
  365. listObj: {
  366. handler(listObj) {
  367. this.priceNum = 0;
  368. let listNum = 0;
  369. let activeIndex = 0;
  370. for (let i = 0; i < listObj.length; i++) {
  371. listNum += listObj[i].goods_list.length;
  372. let status = listObj[i].goods_list.length;
  373. let newStatus = 0;
  374. for (let j = 0; j < listObj[i].goods_list.length;j++) {
  375. if (listObj[i].goods_list[j].new_status !== 0) {
  376. newStatus++;
  377. }
  378. if (this.editStatus === false && listObj[i].goods_list[j].new_status !== 0) {
  379. listNum --;
  380. }
  381. if (listObj[i].goods_list[j].is_active) {
  382. if (!this.editStatus) {
  383. let num = Number(listObj[i].goods_list[j].attrs.price) * Number(listObj[i].goods_list[j].num);
  384. this.priceNum += num;
  385. }
  386. activeIndex++;
  387. }
  388. }
  389. if (status === newStatus) {
  390. this.listObj[i].new_status = 1;
  391. }
  392. }
  393. if (activeIndex === listNum) {
  394. if (listNum === 0 && activeIndex === 0) {
  395. this.all = false;
  396. } else {
  397. this.all = true;
  398. }
  399. } else {
  400. this.all = false;
  401. }
  402. if (activeIndex > 0) {
  403. this.submitDis = false;
  404. } else {
  405. this.submitDis = true;
  406. }
  407. this.priceNum = this.priceNum.toFixed(2);
  408. },
  409. deep: true,
  410. },
  411. tabBarNavs: {
  412. handler: function() {
  413. this.b();
  414. },
  415. immediate: true,
  416. }
  417. }
  418. }
  419. </script>
  420. <style lang="scss" scoped>
  421. .app-cart {
  422. background-color: #f7f7f7;
  423. position: absolute;
  424. top: 0;
  425. left: 0;
  426. width: #{750rpx};
  427. .app-announcement {
  428. width: 100%;
  429. height: #{72rpx};
  430. background-color: #ffffff;
  431. .app-announcement-text {
  432. font-size: #{26rpx};
  433. color: #999999;
  434. margin-left: #{24rpx};
  435. }
  436. .app-edit-text {
  437. font-size: #{26rpx};
  438. color: #353535;
  439. margin-right: #{32rpx};
  440. }
  441. }
  442. .app-settlement {
  443. width: 100%;
  444. height: #{100rpx};
  445. z-index: 1500;
  446. position: fixed;
  447. left: 0;
  448. border-top: #{1rpx} solid #e2e2e2;
  449. background-color: white;
  450. .app-radio {
  451. padding-left: #{23rpx};
  452. .app-price {
  453. margin-left: #{24rpx};
  454. font-size: #{28rpx};
  455. color: #ff4544;
  456. }
  457. }
  458. .app-all-text {
  459. margin-left: #{9rpx};
  460. font-size: #{25rpx};
  461. color: #3f3f3f;
  462. }
  463. .app-delete {
  464. width: #{140rpx};
  465. height: #{64rpx};
  466. line-height: #{64rpx};
  467. text-align: center;
  468. background-color: white;
  469. border-radius: #{32rpx};
  470. border: #{1rpx} solid #ff4544;
  471. color: #ff4544;
  472. font-size: #{28rpx};
  473. margin: #{0 24rpx 0 0};
  474. padding: 0;
  475. }
  476. .delete-disabled {
  477. color: #989898;
  478. border: #{1rpx} solid #989898;
  479. }
  480. .app-settlement-button {
  481. background-color: #ff4544;
  482. height: #{100rpx};
  483. width: #{250rpx};
  484. color: #ffffff;
  485. font-size: #{30rpx};
  486. line-height: #{100rpx};
  487. text-align: center;
  488. border-radius: 0;
  489. margin: 0;
  490. padding: 0;
  491. border: none;
  492. }
  493. .disabled-background {
  494. background-color: #989898;
  495. }
  496. }
  497. .no-cart {
  498. width: 100%;
  499. .cart-icon {
  500. width: #{160rpx};
  501. height: #{160rpx};
  502. border-radius: 50%;
  503. background-color: rgba(0, 0, 0, 0.1);
  504. margin: #{150rpx auto 40rpx};
  505. .cart-image {
  506. height: #{80rpx};
  507. width: #{80rpx};
  508. margin: #{40rpx};
  509. }
  510. }
  511. .cart-text {
  512. font-size: #{30rpx};
  513. color: #888;
  514. text-align: center;
  515. }
  516. }
  517. }
  518. </style>