index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. <template>
  2. <view class="content">
  3. <view class="top" v-if="list.length !=0">
  4. <view class="text_1">
  5. <checkbox class="checkbox" @click="selectAll(false)" :checked="isAll">全选</checkbox>
  6. </view>
  7. <view class="text_2" v-if="num==0" @click="edit(1)">编辑</view>
  8. <view class="text_2" v-if="num==1" @click="edit(0)">完成</view>
  9. </view>
  10. <view class="one" v-if="list.length==0&&user._id">
  11. <view class="logo"><text class="iconfont icon-gouwuche"></text></view>
  12. <view class="one_1">购物车空空如也~</view>
  13. <view class="btn">
  14. <button type="primary" size="mini" @click="toCommon('/pages/home/index')">去逛逛</button>
  15. </view>
  16. </view>
  17. <view class="two" v-if="list.length !=0">
  18. <scroll-view scroll-y="true" class="scroll-view">
  19. <view class="list-scroll-view">
  20. <checkbox-group name="checkbox" @change="marketChange">
  21. <view class="list" v-for="(item, index) in list" :key="index">
  22. <view class="list_2">
  23. <view class="title">
  24. <checkbox :value="item.shop" :checked="item.check">
  25. <text class="iconfont icon-shangdian"></text>
  26. {{item.shop_name}}
  27. </checkbox>
  28. </view>
  29. <checkbox-group name="checkbox" @change="goodsChange">
  30. <view class="content" v-for="gs in item.goods" :key="gs.goodsSpec_id">
  31. <view class="box">
  32. <checkbox :value="gs.goodsSpec_id" :checked="gs.check" />
  33. </view>
  34. <view class="img">
  35. <image :src="getFile(gs)"></image>
  36. </view>
  37. <view class="one_1" v-if="num==0"
  38. @click="toCommon('/pagesHome/order/detail',gs)">
  39. <view class="name">{{gs.goods_name}}</view>
  40. <view class="info">
  41. <view class="title_1" v-if="gs.goodsSpec_name">
  42. <text>规格:{{gs.goodsSpec_name}}</text>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="money" v-if="num==0">
  47. <view>¥{{gs.price}}</view>
  48. <view>x{{gs.num}}</view>
  49. </view>
  50. <view class="num" v-if="num==1">
  51. <uni-number-box @change="changeValue(gs)" name="num" value="gs" :min="1"
  52. v-model="gs.num" />
  53. </view>
  54. <view class="del" v-if="num==1">
  55. <text class="iconfont icon-del-copy" @click="toDel(gs)"></text>
  56. </view>
  57. </view>
  58. </checkbox-group>
  59. </view>
  60. </view>
  61. </checkbox-group>
  62. </view>
  63. </scroll-view>
  64. </view>
  65. <view class="foot" v-if="list.length !=0">
  66. <view class="total">总价:<text>¥{{totalMoney}}(不含运费)</text></view>
  67. <view class="btn" v-if="hasCheck()">
  68. <button type="primary" size="mini" @click="toSettle()" v-if="num==0">提交订单</button>
  69. <button type="primary" size="mini" @click="toDel()" v-if="num==1">删除</button>
  70. </view>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. export default {
  76. data() {
  77. return {
  78. // 用户
  79. user: {},
  80. // 购物车列表
  81. list: [],
  82. // 全选
  83. isAll: false,
  84. // 编辑/完成按钮传的数字
  85. num: 0,
  86. // 总额
  87. totalMoney: 0,
  88. }
  89. },
  90. onShow() {
  91. const that = this;
  92. that.searchToken();
  93. },
  94. methods: {
  95. searchToken() {
  96. const that = this;
  97. try {
  98. const res = uni.getStorageSync('token');
  99. if (res) {
  100. that.$set(that, `user`, res);
  101. that.searchMarket();
  102. }
  103. } catch (e) {
  104. uni.showToast({
  105. title: err.errmsg,
  106. icon: 'error',
  107. duration: 2000
  108. });
  109. }
  110. },
  111. // 查询购物车信息
  112. async searchMarket() {
  113. const that = this;
  114. const res = await that.$api(`/Cart`, 'GET', {
  115. user: that.user._id
  116. });
  117. if (res.errcode == '0') that.$set(that, `list`, res.data)
  118. },
  119. // 编辑
  120. edit(num) {
  121. const that = this;
  122. that.$set(that, `num`, num)
  123. },
  124. //全选
  125. selectAll(e) {
  126. const that = this;
  127. const list = that.list;
  128. const isAll = that.isAll;
  129. let data = [];
  130. for (const val of list) {
  131. let a = isAll ? false : true;
  132. val.check = a;
  133. for (let s of val.goods) {
  134. s.check = a
  135. }
  136. data.push(val);
  137. }
  138. that.$set(that, `list`, data)
  139. // 计算总额
  140. that.countMoney();
  141. // 赋值是否全选
  142. that.$set(that, `isAll`, isAll ? false : true)
  143. },
  144. // 选择店铺
  145. marketChange(e) {
  146. const that = this;
  147. const list = this.list;
  148. const {
  149. value
  150. } = e.detail;
  151. for (const p1 of list) {
  152. let p2;
  153. // 判断是套装还是正常商品
  154. if (p1.is_set == '0') p2 = value.find((i) => i == p1._id);
  155. else p2 = value.find((i) => i == p1.shop);
  156. let a = p2 ? true : false;
  157. p1.check = a;
  158. for (let s of p1.goods) {
  159. s.check = a
  160. }
  161. }
  162. that.$set(that, `list`, list);
  163. that.marketAllChange();
  164. // 计算总额
  165. that.countMoney();
  166. },
  167. //店铺全部选择true,全选自动选择,
  168. marketAllChange() {
  169. const that = this;
  170. let list = that.list;
  171. for (let val of list) {
  172. if (val.check == true) {
  173. that.$set(that, `isAll`, true)
  174. } else {
  175. that.$set(that, `isAll`, false)
  176. }
  177. }
  178. that.$set(that, `list`, list)
  179. // 计算总额
  180. that.countMoney();
  181. },
  182. //选择商品
  183. goodsChange(e) {
  184. const that = this;
  185. let list = that.list;
  186. const {
  187. value
  188. } = e.detail;
  189. let shop = list.find(f => f.goods.find(i => value.find(s => s == i.goodsSpec_id)))
  190. if (shop) {
  191. for (let val of shop.goods) {
  192. let p2 = value.find((i) => i == val.goodsSpec_id);
  193. if (p2) val.check = true;
  194. else val.check = false;
  195. }
  196. } else {
  197. list = list.map(i => {
  198. i.goods = i.goods.map(g => ({
  199. ...g,
  200. check: false
  201. }))
  202. return i;
  203. })
  204. }
  205. that.$set(that, `list`, list)
  206. that.goodsAllChange();
  207. // 计算总额
  208. that.countMoney();
  209. },
  210. //商品全部选择true,店铺自动选择,
  211. // 所有店铺为true,则全选为true
  212. goodsAllChange() {
  213. const that = this;
  214. let list = that.list;
  215. list = list.map(i => {
  216. const isAllSelect = i.goods.every(f => f.check);
  217. if (isAllSelect) i.check = true;
  218. else i.check = false;
  219. return i;
  220. })
  221. const allSelect = list.every(e => e.check)
  222. if (allSelect) this.$set(this, `isAll`, true)
  223. else this.$set(this, `isAll`, false)
  224. // 计算总额
  225. that.countMoney();
  226. },
  227. // 加减商品数量
  228. async changeValue(value) {
  229. const that = this;
  230. this.$nextTick(async () => {
  231. let res;
  232. const {
  233. goodsSpec_id: goodsSpecId,
  234. num,
  235. cart_id: cartId
  236. } = value
  237. res = await that.$api(`/cart/checkGoodsNum`, 'GET', {
  238. cartId,
  239. goodsSpecId,
  240. num
  241. });
  242. if (res.errcode === 0) {
  243. const {
  244. enough,
  245. total
  246. } = res.data
  247. if (!enough) {
  248. uni.showToast({
  249. title: `库存最大为${total}`,
  250. icon: 'error',
  251. });
  252. // 将该商品的库存量修改为最大值
  253. value.num = total;
  254. }
  255. }
  256. // 计算总额
  257. that.countMoney();
  258. })
  259. },
  260. // 删除, 接口,购物车删除,然后将该数据移除
  261. async toDel(e) {
  262. let list = this.list;
  263. let set_list = [];
  264. let goods_list = [];
  265. uni.showModal({
  266. title: '提示',
  267. content: '请选择要删除的商品',
  268. success: async (res) => {
  269. if (!res.confirm) return
  270. if (e?.cart_id) {
  271. const result = await this.$api(`/cart/${e.cart_id}`, 'Delete');
  272. if (result.errcode === 0) {
  273. // 判断是套装还是正常商品
  274. set_list = list.filter(i => i.is_set == '0')
  275. goods_list = list.filter(i => i.is_set !== '0')
  276. set_list = set_list.filter(i => i.cart_id !== e.cart_id)
  277. goods_list = goods_list.map(i => ({
  278. ...i,
  279. goods: i.goods.filter(f => f.cart_id !== e.cart_id)
  280. }))
  281. this.$set(this, `list`, [...set_list, ...goods_list]);
  282. // 检查店铺内是否还有商品
  283. this.checkShopGoodsExist();
  284. // 计算总额
  285. this.countMoney();
  286. }
  287. } else {
  288. let goodsList = [];
  289. let cartIds = [];
  290. for (let val of this.list) {
  291. // 判断是套装还是正常商品
  292. if (val.is_set == '0') goodsList.push(val)
  293. else goodsList.push(val.goods)
  294. }
  295. for (let val of goodsList) {
  296. // 判断是套装还是正常商品
  297. if (val.is_set == '0') {
  298. if (val.check) cartIds.push(val.cart_id)
  299. } else {
  300. for (let set of val) {
  301. if (set.check) cartIds.push(set.cart_id)
  302. }
  303. }
  304. }
  305. set_list = list.filter(i => i.is_set == '0')
  306. goods_list = list.filter(i => i.is_set !== '0')
  307. for (let val of cartIds) {
  308. const result = await this.$api(`/cart/${val}`, 'Delete');
  309. if (result.errcode === 0) {
  310. // 判断是套装还是正常商品
  311. set_list = set_list.filter(i => i.cart_id !== val)
  312. goods_list = goods_list.map(i => ({
  313. ...i,
  314. goods: i.goods.filter(f => f.cart_id !== val)
  315. }))
  316. this.$set(this, `list`, [...set_list, ...goods_list]);
  317. // 检查店铺内是否还有商品
  318. this.checkShopGoodsExist();
  319. // 计算总额
  320. this.countMoney();
  321. }
  322. }
  323. }
  324. }
  325. });
  326. },
  327. //检查店铺内是否还有商品
  328. checkShopGoodsExist() {
  329. let list = this.list;
  330. list = list.filter(f => f.goods && f.goods.length > 0)
  331. this.$set(this, `list`, list);
  332. },
  333. // 计算总额
  334. countMoney() {
  335. const that = this;
  336. const list = that.list;
  337. let totalMoney = 0;
  338. // 渲染结束执行下面方法
  339. that.$nextTick(() => {
  340. for (const val of list) {
  341. // 判断是套装还是正常商品
  342. if (val.is_set == '0') {
  343. if (val.check == true) {
  344. let total = that.$multiply(val.sell_money, val.num);
  345. totalMoney += Number(total);
  346. }
  347. } else {
  348. for (let s of val.goods) {
  349. if (s.check == true) {
  350. let total = that.$multiply(s.price, s.num);
  351. totalMoney += Number(total);
  352. }
  353. }
  354. }
  355. }
  356. that.$set(that, `totalMoney`, totalMoney.toFixed(2))
  357. })
  358. },
  359. getFile(data) {
  360. const file = data.file;
  361. if (!file) return '';
  362. if (file.length && file.length > 0) return file[0].url
  363. },
  364. // 去结算
  365. async toSettle() {
  366. // 将选中的购物车放到数组中,传回服务端进行检查.然后拿着key去订单页请求数据
  367. let goodsList = [];
  368. let cartIds = [];
  369. for (let val of this.list) {
  370. // 判断是套装还是正常商品
  371. if (val.is_set == '0') goodsList.push(val)
  372. else goodsList.push(val.goods)
  373. }
  374. for (let val of goodsList) {
  375. // 判断是套装还是正常商品
  376. if (val.is_set == '0') {
  377. if (val.check) cartIds.push(val.cart_id)
  378. } else {
  379. for (let set of val) {
  380. if (set.check) cartIds.push(set.cart_id)
  381. }
  382. }
  383. }
  384. const res = await this.$api(`/util/checkCartBuy`, 'POST', {
  385. cartIds
  386. });
  387. if (res.errcode == '0') {
  388. const {
  389. data
  390. } = res
  391. if (data.result) {
  392. const key = data.key;
  393. uni.navigateTo({
  394. url: `/pagesHome/order/order?key=${key}`
  395. })
  396. }
  397. }
  398. },
  399. // 是否选中商品,控制提交订单按钮
  400. hasCheck() {
  401. return this.list.some(e => e.goods.some(eg => eg.check))
  402. },
  403. // 公共跳转
  404. toCommon(route, e) {
  405. uni.navigateTo({
  406. url: `${route}?id=${e && e.goods_id}`
  407. })
  408. },
  409. }
  410. }
  411. </script>
  412. <style lang="scss">
  413. .content {
  414. display: flex;
  415. flex-direction: column;
  416. width: 100vw;
  417. height: 92vh;
  418. background-color: var(--footColor);
  419. .top {
  420. display: flex;
  421. flex-direction: row;
  422. background-color: var(--mainColor);
  423. height: 35px;
  424. margin: 0 0 4px 0;
  425. padding: 5px 4vw;
  426. .text_1 {
  427. flex-grow: 1;
  428. line-height: 30px;
  429. }
  430. .text_2 {
  431. line-height: 35px;
  432. }
  433. }
  434. .one {
  435. text-align: center;
  436. margin: 2vw 0;
  437. .logo {
  438. margin: 10vw 0 2vw 0;
  439. .iconfont {
  440. font-size: 35vw;
  441. }
  442. }
  443. .one_1 {
  444. margin: 3vw 0;
  445. }
  446. button {
  447. background-color: var(--f3CColor);
  448. }
  449. }
  450. .two {
  451. position: relative;
  452. flex-grow: 1;
  453. margin: 0 2vw;
  454. .list_1 {
  455. background-color: var(--mainColor);
  456. margin: 2vw 0 2vw 0;
  457. padding: 2vw 3vw;
  458. border-radius: 4px;
  459. .title {
  460. border-bottom: 1px solid var(--fcColor);
  461. padding: 0 0 2vw 0;
  462. font-size: var(--font18Size);
  463. text {
  464. margin: 0 1vw;
  465. }
  466. }
  467. .content {
  468. display: flex;
  469. flex-direction: row;
  470. padding: 2vw 0;
  471. font-size: var(--font16Size);
  472. border-bottom: 1px dashed var(--fcColor);
  473. .img {
  474. width: 20vw;
  475. height: 20vw;
  476. border-radius: 2vw;
  477. border: 1px solid var(--fcColor);
  478. image {
  479. width: 20vw;
  480. height: 20vw;
  481. }
  482. }
  483. .one_1 {
  484. margin: 0 2.5vw;
  485. flex-grow: 1;
  486. .info {
  487. width: 100%;
  488. display: flex;
  489. flex-direction: row;
  490. .title_1 {
  491. font-size: 12px;
  492. color: #666;
  493. margin-top: 10px;
  494. text {
  495. background-color: #eee;
  496. padding: 5px;
  497. }
  498. }
  499. }
  500. }
  501. .money {
  502. font-size: 14px;
  503. margin-top: 2vw;
  504. text-align: right;
  505. flex-grow: 1;
  506. }
  507. }
  508. .total {
  509. display: flex;
  510. justify-content: space-between;
  511. padding: 1vw;
  512. .total_1 {
  513. font-size: 20px;
  514. color: var(--fFB1Color);
  515. text {
  516. font-size: 12px;
  517. }
  518. }
  519. .total_2 {
  520. display: flex;
  521. justify-content: space-between;
  522. align-items: center;
  523. .del {
  524. margin: 0 0 0 3vw;
  525. }
  526. }
  527. }
  528. }
  529. .list_2 {
  530. background-color: var(--mainColor);
  531. margin: 2vw 0 2vw 0;
  532. padding: 2vw 3vw;
  533. border-radius: 4px;
  534. .title {
  535. border-bottom: 1px solid var(--fcColor);
  536. padding: 0 0 2vw 0;
  537. font-size: var(--font18Size);
  538. text {
  539. margin: 0 1vw;
  540. }
  541. }
  542. .content {
  543. display: flex;
  544. flex-direction: row;
  545. padding: 2vw 0;
  546. font-size: var(--font16Size);
  547. border-bottom: 1px dashed var(--fcColor);
  548. .box {
  549. line-height: 20vw;
  550. }
  551. .img {
  552. width: 20vw;
  553. height: 20vw;
  554. border-radius: 2vw;
  555. border: 1px solid var(--fcColor);
  556. image {
  557. width: 20vw;
  558. height: 20vw;
  559. }
  560. }
  561. .one_1 {
  562. margin: 0 2.5vw;
  563. flex-grow: 1;
  564. .info {
  565. width: 100%;
  566. display: flex;
  567. flex-direction: row;
  568. .title_1 {
  569. font-size: 12px;
  570. color: #666;
  571. margin-top: 10px;
  572. text {
  573. background-color: #eee;
  574. padding: 5px;
  575. }
  576. }
  577. .gift {
  578. margin: 2vw 1vw 0 0;
  579. font-size: 12px;
  580. color: #FFA500;
  581. border: 1px solid #FFA500;
  582. border-radius: 5px;
  583. padding: 0 1vw;
  584. }
  585. .act {
  586. margin: 2vw 0 0 0;
  587. font-size: 12px;
  588. border-radius: 5px;
  589. padding: 0 1vw;
  590. border: 1px solid var(--fFB1Color);
  591. color: var(--fFB1Color);
  592. }
  593. }
  594. }
  595. .money {
  596. margin-top: 2vw;
  597. text-align: right;
  598. flex-grow: 1;
  599. }
  600. .num {
  601. margin: 6vw 4vw;
  602. font-size: 20px;
  603. }
  604. .del {
  605. margin: 6vw 0;
  606. text-align: right;
  607. flex-grow: 1;
  608. }
  609. }
  610. }
  611. }
  612. .foot {
  613. background-color: var(--fffColor);
  614. display: flex;
  615. flex-direction: row;
  616. justify-content: space-between;
  617. height: 44px;
  618. padding: 0 0 0 6vw;
  619. border-right: 1px solid var(--f99Color);
  620. .total {
  621. flex-grow: 1;
  622. display: flex;
  623. align-content: flex-end;
  624. line-height: 40px;
  625. text {
  626. color: var(--fFB1Color);
  627. }
  628. }
  629. .btn {
  630. button {
  631. width: 30vw;
  632. height: 44px;
  633. line-height: 44px;
  634. border-radius: 0px;
  635. background-color: var(--fFB1Color);
  636. text-align: center;
  637. font-weight: normal;
  638. font-size: var(--font15Size);
  639. color: var(--fffColor);
  640. }
  641. }
  642. }
  643. }
  644. .scroll-view {
  645. position: absolute;
  646. top: 0;
  647. left: 0;
  648. right: 0;
  649. bottom: 0;
  650. .list-scroll-view {
  651. display: flex;
  652. flex-direction: column;
  653. }
  654. }
  655. // 复选框样式
  656. checkbox .wx-checkbox-input {
  657. width: 40rpx;
  658. height: 40rpx;
  659. border-radius: 50%;
  660. border-color: var(--f99Color);
  661. background-color: var(--mainColor);
  662. }
  663. // 复选框选中样式
  664. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  665. border-color: var(--fFB1Color);
  666. background-color: var(--fFB1Color);
  667. }
  668. // 复选框选中之后对号的样式
  669. checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  670. color: var(--fffColor);
  671. }
  672. </style>