index.vue 13 KB

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