index.vue 14 KB

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