index.vue 13 KB

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