index.vue 14 KB

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