index.vue 14 KB

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