index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. 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`,user)
  134. that.searchMarket();
  135. }
  136. },
  137. fail: (err) => {}
  138. })
  139. },
  140. // 查询购物车信息
  141. async searchMarket() {
  142. const that = this;
  143. const res = await that.$api(`/cart/selfCart`, 'GET', {
  144. customer: that.user.id
  145. });
  146. if (res.errcode == '0') {
  147. that.$set(that, `list`, res.data)
  148. }
  149. },
  150. // 转换金额
  151. showMoney(money) {
  152. const that = this;
  153. let moneys = `${money}`.split(".");
  154. if (moneys.length == 1) {
  155. money = money + ".00";
  156. } else if (moneys.length == 2) {
  157. money = money + "0";
  158. }
  159. return money;
  160. },
  161. // 编辑
  162. edit(num) {
  163. const that = this;
  164. that.$set(that, `num`, num)
  165. },
  166. //全选
  167. selectAll(e) {
  168. const that = this;
  169. const list = that.list;
  170. const isAll = that.isAll;
  171. let data = [];
  172. for (const val of list) {
  173. let a = isAll ? false : true;
  174. val.check = a;
  175. for (let s of val.goods) {
  176. s.check = a
  177. }
  178. data.push(val);
  179. }
  180. that.$set(that, `list`, data)
  181. // 计算总额
  182. that.countMoney();
  183. // 赋值是否全选
  184. that.$set(that, `isAll`, isAll ? false : true)
  185. },
  186. // 选择店铺
  187. marketChange(e) {
  188. const that = this;
  189. const list = this.list;
  190. const {
  191. value
  192. } = e.detail;
  193. for (const p1 of list) {
  194. let p2 = value.find((i) => i == p1.shop);
  195. let a = p2 ? true : false;
  196. p1.check = a;
  197. for (let s of p1.goods) {
  198. s.check = a
  199. }
  200. }
  201. that.$set(that, `list`, list);
  202. that.marketAllChange();
  203. // 计算总额
  204. that.countMoney();
  205. },
  206. //店铺全部选择true,全选自动选择,
  207. marketAllChange() {
  208. const that = this;
  209. let list = that.list;
  210. for (let val of list) {
  211. if (val.check == true) {
  212. that.$set(that, `isAll`, true)
  213. } else {
  214. that.$set(that, `isAll`, false)
  215. }
  216. }
  217. that.$set(that, `list`, list)
  218. // 计算总额
  219. that.countMoney();
  220. },
  221. //选择商品
  222. goodsChange(e) {
  223. const that = this;
  224. let list = that.list;
  225. const {
  226. value
  227. } = e.detail;
  228. let shop = list.find(f => f.goods.find(i => value.find(s => s == i.goodsSpec_id)))
  229. if (shop) {
  230. for (let val of shop.goods) {
  231. let p2 = value.find((i) => i == val.goodsSpec_id);
  232. if (p2) val.check = true;
  233. else val.check = false;
  234. }
  235. } else {
  236. list = list.map(i => {
  237. i.goods = i.goods.map(g => ({
  238. ...g,
  239. check: false
  240. }))
  241. return i;
  242. })
  243. }
  244. that.$set(that, `list`, list)
  245. that.goodsAllChange();
  246. // 计算总额
  247. that.countMoney();
  248. },
  249. //商品全部选择true,店铺自动选择,
  250. // 所有店铺为true,则全选为true
  251. goodsAllChange() {
  252. const that = this;
  253. let list = that.list;
  254. list = list.map(i => {
  255. const isAllSelect = i.goods.every(f => f.check);
  256. if (isAllSelect) i.check = true;
  257. else i.check = false;
  258. return i;
  259. })
  260. const allSelect = list.every(e => e.check)
  261. if (allSelect) this.$set(this, `isAll`, true)
  262. else this.$set(this, `isAll`, false)
  263. // 计算总额
  264. that.countMoney();
  265. },
  266. // 加减商品数量
  267. async changeValue(value) {
  268. const that = this;
  269. this.$nextTick(async () => {
  270. const {
  271. goodsSpec_id: goodsSpecId,
  272. num,
  273. cart_id: cartId
  274. } = value
  275. const res = await that.$api(`/cart/checkGoodsNum`, 'GET', {
  276. cartId,
  277. goodsSpecId,
  278. num
  279. });
  280. if (res.errcode === 0) {
  281. const {
  282. enough,
  283. total
  284. } = res.data
  285. if (!enough) {
  286. uni.showToast({
  287. title: `库存最大为${total}`,
  288. icon: 'error',
  289. });
  290. // 将该商品的库存量修改为最大值
  291. value.num = total;
  292. }
  293. }
  294. // 计算总额
  295. that.countMoney();
  296. })
  297. },
  298. // 删除, 接口,购物车删除,然后将该数据移除
  299. async toDel(e) {
  300. let list = this.list;
  301. uni.showModal({
  302. title: '提示',
  303. content: '请选择要删除的商品',
  304. success: async (res) => {
  305. if (!res.confirm) return
  306. if (e?.cart_id) {
  307. const result = await this.$api(`/cart/${e.cart_id}`, 'Delete');
  308. if (result.errcode === 0) {
  309. list = list.map(i => ({
  310. ...i,
  311. goods: i.goods.filter(f => f.cart_id !== e.cart_id)
  312. }))
  313. this.$set(this, `list`, list);
  314. // 检查店铺内是否还有商品
  315. this.checkShopGoodsExist();
  316. // 计算总额
  317. this.countMoney();
  318. }
  319. } else {
  320. const goodsList = list.map(i => i.goods).flat();
  321. const cartIds = goodsList.filter(i => i.check).map(i => i.cart_id)
  322. for (let val of cartIds) {
  323. const result = await this.$api(`/cart/${val}`, 'Delete');
  324. if (result.errcode === 0) {
  325. list = list.map(i => ({
  326. ...i,
  327. goods: i.goods.filter(f => f.cart_id !== val)
  328. }))
  329. this.$set(this, `list`, list);
  330. // 检查店铺内是否还有商品
  331. this.checkShopGoodsExist();
  332. // 计算总额
  333. this.countMoney();
  334. }
  335. }
  336. }
  337. }
  338. });
  339. },
  340. //检查店铺内是否还有商品
  341. checkShopGoodsExist() {
  342. let list = this.list;
  343. list = list.filter(f => f.goods && f.goods.length > 0)
  344. this.$set(this, `list`, list);
  345. },
  346. // 计算总额
  347. countMoney() {
  348. const that = this;
  349. const list = this.list;
  350. let totalMoney = 0;
  351. // 渲染结束执行下面方法
  352. that.$nextTick(() => {
  353. for (const val of list) {
  354. for (let s of val.goods) {
  355. if (s.check == true) {
  356. let total = Number(s.money) * Number(s.num);
  357. totalMoney += Number(total);
  358. }
  359. }
  360. }
  361. that.$set(that, `totalMoney`, totalMoney.toFixed(2))
  362. })
  363. },
  364. getFile(data) {
  365. const file = data.file;
  366. if (!file) return '';
  367. if (file.length && file.length > 0) return file[0].url
  368. },
  369. // 去结算
  370. async toSettle() {
  371. // 将选中的购物车放到数组中,传回服务端进行检查.然后拿着key去订单页请求数据
  372. const goodsList = this.list.map(i => i.goods).flat();
  373. const cartIds = goodsList.filter(i => i.check).map(i => i.cart_id)
  374. const res = await this.$api(`/util/checkCartBuy`, 'POST', {
  375. cartIds
  376. });
  377. if (res.errcode == '0') {
  378. const {
  379. data
  380. } = res
  381. if (data.result) {
  382. const key = data.key;
  383. uni.navigateTo({
  384. url: `/pagesHome/order/order?key=${key}`
  385. })
  386. }
  387. }
  388. },
  389. // 是否选中商品,控制提交订单按钮
  390. hasCheck() {
  391. return this.list.some(e => e.goods.some(eg => eg.check))
  392. }
  393. }
  394. }
  395. </script>
  396. <style lang="scss">
  397. .main {
  398. display: flex;
  399. flex-direction: column;
  400. width: 100vw;
  401. height: 92vh;
  402. background-color: var(--footColor);
  403. .top {
  404. display: flex;
  405. flex-direction: row;
  406. background-color: var(--mainColor);
  407. height: 35px;
  408. margin: 0 0 4px 0;
  409. padding: 5px 4vw;
  410. .text_1 {
  411. flex-grow: 1;
  412. line-height: 30px;
  413. }
  414. .text_2 {
  415. line-height: 35px;
  416. }
  417. }
  418. .one {
  419. text-align: center;
  420. margin: 2vw 0;
  421. .logo {
  422. margin: 10vw 0 2vw 0;
  423. .iconfont {
  424. font-size: 35vw;
  425. }
  426. }
  427. .one_1 {
  428. margin: 3vw 0;
  429. }
  430. button {
  431. background-color: var(--ff0Color);
  432. }
  433. }
  434. .two {
  435. position: relative;
  436. flex-grow: 1;
  437. margin: 0 2vw;
  438. .list {
  439. background-color: var(--mainColor);
  440. margin: 2vw 0 2vw 0;
  441. padding: 2vw 3vw;
  442. border-radius: 4px;
  443. .title {
  444. border-bottom: 1px solid var(--fcColor);
  445. padding: 0 0 2vw 0;
  446. font-size: var(--font18Size);
  447. text {
  448. margin: 0 1vw;
  449. }
  450. }
  451. .content {
  452. display: flex;
  453. flex-direction: row;
  454. padding: 3vw 0;
  455. font-size: var(--font16Size);
  456. border-bottom: 1px dashed var(--fcColor);
  457. .box {
  458. line-height: 20vw;
  459. }
  460. .img {
  461. width: 20vw;
  462. height: 20vw;
  463. border-radius: 2vw;
  464. border: 1px solid var(--fcColor);
  465. image {
  466. width: 20vw;
  467. height: 20vw;
  468. }
  469. }
  470. .one_1 {
  471. margin: 2.5vw;
  472. flex-grow: 1;
  473. .info {
  474. width: 100%;
  475. display: flex;
  476. flex-direction: row;
  477. .title_1 {
  478. font-size: 12px;
  479. color: #666;
  480. margin-top: 10px;
  481. text {
  482. background-color: #eee;
  483. padding: 5px;
  484. }
  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>