index.vue 14 KB

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