index.vue 14 KB

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