index.vue 14 KB

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