index.vue 13 KB

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