index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. const result = await this.$api(`/cart/${e.cart_id}`, 'Delete');
  309. if (result.errcode === 0) {
  310. list = list.map(i => ({
  311. ...i,
  312. goods: i.goods.filter(f => f.cart_id !== e.cart_id)
  313. }))
  314. this.$set(this, `list`, list);
  315. // 检查店铺内是否还有商品
  316. this.checkShopGoodsExist();
  317. // 计算总额
  318. this.countMoney();
  319. }
  320. }
  321. });
  322. },
  323. //检查店铺内是否还有商品
  324. checkShopGoodsExist() {
  325. let list = this.list;
  326. list = list.filter(f => f.goods && f.goods.length > 0)
  327. this.$set(this, `list`, list);
  328. },
  329. // 计算总额
  330. countMoney() {
  331. const that = this;
  332. const list = this.list;
  333. let totalMoney = 0;
  334. // 渲染结束执行下面方法
  335. that.$nextTick(() => {
  336. for (const val of list) {
  337. for (let s of val.goods) {
  338. if (s.check == true) {
  339. let total = Number(s.money) * Number(s.num);
  340. totalMoney += Number(total);
  341. }
  342. }
  343. }
  344. that.$set(that, `totalMoney`, totalMoney.toFixed(2))
  345. })
  346. },
  347. getFile(data) {
  348. const file = data.file;
  349. if (!file) return '';
  350. if (file.length && file.length > 0) return file[0].url
  351. },
  352. // 去结算
  353. async toSettle() {
  354. // 将选中的购物车放到数组中,传回服务端进行检查.然后拿着key去订单页请求数据
  355. const goodsList = this.list.map(i => i.goods).flat();
  356. const cartIds = goodsList.filter(i => i.check).map(i => i.cart_id)
  357. const res = await this.$api(`/util/checkCartBuy`, 'POST', {
  358. cartIds
  359. });
  360. if (res.errcode == '0') {
  361. const {
  362. data
  363. } = res
  364. if (data.result) {
  365. const key = data.key;
  366. uni.navigateTo({
  367. url: `/pagesHome/order/order?key=${key}`
  368. })
  369. }
  370. }
  371. },
  372. // 是否选中商品,控制提交订单按钮
  373. hasCheck() {
  374. return this.list.some(e => e.goods.some(eg => eg.check))
  375. }
  376. }
  377. }
  378. </script>
  379. <style lang="scss">
  380. .main {
  381. display: flex;
  382. flex-direction: column;
  383. width: 100vw;
  384. height: 92vh;
  385. background-color: var(--footColor);
  386. .top {
  387. display: flex;
  388. flex-direction: row;
  389. background-color: var(--mainColor);
  390. height: 35px;
  391. margin: 0 0 4px 0;
  392. padding: 5px 4vw;
  393. .text_1 {
  394. flex-grow: 1;
  395. line-height: 30px;
  396. }
  397. .text_2 {
  398. line-height: 35px;
  399. }
  400. }
  401. .one {
  402. text-align: center;
  403. margin: 2vw 0;
  404. .logo {
  405. margin: 10vw 0 2vw 0;
  406. .iconfont {
  407. font-size: 35vw;
  408. }
  409. }
  410. .one_1 {
  411. margin: 3vw 0;
  412. }
  413. button {
  414. background-color: var(--ff0Color);
  415. }
  416. }
  417. .two {
  418. position: relative;
  419. flex-grow: 1;
  420. margin: 0 2vw;
  421. .list {
  422. background-color: var(--mainColor);
  423. margin: 2vw 0 2vw 0;
  424. padding: 2vw 3vw;
  425. border-radius: 4px;
  426. .title {
  427. border-bottom: 1px solid var(--fcColor);
  428. padding: 0 0 2vw 0;
  429. font-size: var(--font18Size);
  430. text {
  431. margin: 0 1vw;
  432. }
  433. }
  434. .content {
  435. display: flex;
  436. flex-direction: row;
  437. padding: 3vw 0;
  438. font-size: var(--font16Size);
  439. border-bottom: 1px dashed var(--fcColor);
  440. .box {
  441. line-height: 20vw;
  442. }
  443. .img {
  444. width: 20vw;
  445. height: 20vw;
  446. border-radius: 2vw;
  447. border: 1px solid var(--fcColor);
  448. image {
  449. width: 20vw;
  450. height: 20vw;
  451. }
  452. }
  453. .one_1 {
  454. margin: 2.5vw;
  455. flex-grow: 1;
  456. .info {
  457. width: 100%;
  458. display: flex;
  459. flex-direction: row;
  460. .title_1 {
  461. font-size: 12px;
  462. color: #666;
  463. margin-top: 10px;
  464. text {
  465. background-color: #eee;
  466. padding: 5px;
  467. }
  468. }
  469. }
  470. }
  471. .money {
  472. margin-top: 2vw;
  473. text-align: right;
  474. flex-grow: 1;
  475. }
  476. .num {
  477. margin: 6vw 4vw;
  478. font-size: 20px;
  479. }
  480. .del {
  481. margin: 6vw 0;
  482. text-align: right;
  483. flex-grow: 1;
  484. }
  485. }
  486. }
  487. }
  488. .foot {
  489. background-color: var(--fffColor);
  490. display: flex;
  491. flex-direction: row;
  492. justify-content: space-between;
  493. height: 44px;
  494. padding: 0 0 0 6vw;
  495. border-right: 1px solid var(--f99Color);
  496. .total {
  497. flex-grow: 1;
  498. display: flex;
  499. align-content: flex-end;
  500. line-height: 40px;
  501. text {
  502. color: var(--ff0Color);
  503. }
  504. }
  505. .btn {
  506. button {
  507. width: 30vw;
  508. height: 44px;
  509. line-height: 44px;
  510. border-radius: 0px;
  511. background-color: var(--ff0Color);
  512. text-align: center;
  513. font-weight: normal;
  514. font-size: var(--font15Size);
  515. color: var(--fffColor);
  516. }
  517. }
  518. }
  519. }
  520. .scroll-view {
  521. position: absolute;
  522. top: 0;
  523. left: 0;
  524. right: 0;
  525. bottom: 0;
  526. .list-scroll-view {
  527. display: flex;
  528. flex-direction: column;
  529. }
  530. }
  531. // 复选框样式
  532. checkbox .wx-checkbox-input {
  533. width: 40rpx;
  534. height: 40rpx;
  535. border-radius: 50%;
  536. border-color: var(--f99Color);
  537. background-color: var(--mainColor);
  538. }
  539. // 复选框选中样式
  540. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  541. border-color: var(--ff0Color);
  542. background-color: var(--ff0Color);
  543. }
  544. // 复选框选中之后对号的样式
  545. checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  546. color: var(--fffColor);
  547. }
  548. </style>