index.vue 18 KB

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