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. <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. let list = that.list;
  218. let goodsList = [];
  219. if (user && user._id) {
  220. that.$nextTick(async () => {
  221. let res;
  222. const obj = {
  223. num: value.num,
  224. total_money: that.$multiply(value.specs.money, value.num)
  225. }
  226. res = await that.$api(`/Cart/${value.cart_id}`, 'POST', obj)
  227. if (res.errcode == '0') {
  228. goodsList = list.map(i => ({
  229. ...i,
  230. list: i.list.map(f => {
  231. if (f.cart_id == value.cart_id) {
  232. f.num = obj.num
  233. f.total_money = obj.total_money
  234. }
  235. return f
  236. })
  237. }))
  238. that.$set(that, `list`, goodsList)
  239. } else {
  240. uni.showToast({
  241. title: res.errmsg,
  242. icon: 'none'
  243. })
  244. }
  245. // 计算总额
  246. that.countMoney();
  247. })
  248. } else {
  249. uni.navigateTo({
  250. url: `/pages/login/index`
  251. })
  252. }
  253. },
  254. // 删除, 接口,购物车删除,然后将该数据移除
  255. async toDel(e) {
  256. const that = this;
  257. let list = that.list;
  258. uni.showModal({
  259. title: '提示',
  260. content: '请选择要删除的商品',
  261. success: async (res) => {
  262. if (!res.confirm) return
  263. if (e?.cart_id) {
  264. const result = await that.$api(`/Cart/${e.cart_id}`, 'Delete');
  265. if (result.errcode === 0) {
  266. that.searchMarket();
  267. // 计算总额
  268. that.countMoney();
  269. }
  270. } else {
  271. let cartIds = [];
  272. for (let val of list) {
  273. for (let s of val.list) {
  274. if (s.check) cartIds.push(s.cart_id)
  275. }
  276. }
  277. const result = await that.$api(`/Cart/deleteMany`, 'Delete', {
  278. cartIds
  279. });
  280. if (result.errcode === 0) {
  281. that.searchMarket();
  282. // 计算总额
  283. that.countMoney();
  284. }
  285. }
  286. }
  287. });
  288. },
  289. // 计算总额
  290. countMoney() {
  291. const that = this;
  292. const list = that.list;
  293. let totalMoney = 0;
  294. // 渲染结束执行下面方法
  295. that.$nextTick(() => {
  296. for (const val of list) {
  297. for (let as of val.list) {
  298. if (as.check == true) {
  299. let total = that.$multiply(as.money, as.num);
  300. totalMoney += Number(total);
  301. }
  302. }
  303. }
  304. that.$set(that, `totalMoney`, totalMoney.toFixed(2))
  305. })
  306. },
  307. // 显示图片处理
  308. getFile(data) {
  309. let file
  310. if (data.specs.file && data.specs.file.length > 0) file = data.specs.file
  311. else file = data.goods.file;
  312. if (!file) return '';
  313. if (file.length && file.length > 0) return file[0].url
  314. },
  315. // 去结算
  316. async toSettle() {
  317. const that = this;
  318. let list = that.list;
  319. const user = that.user;
  320. if (user && user._id) {
  321. if (user.role == 'jdry' || user.role == 'sqry') {
  322. let cartIds = [];
  323. for (let val of list) {
  324. for (let s of val.list) {
  325. if (s.check) cartIds.push(s.cart_id)
  326. }
  327. }
  328. uni.showModal({
  329. title: '提示',
  330. content: '是否确定要提交?',
  331. success: async (arr) => {
  332. if (!arr.confirm) return
  333. const res = await that.$api(`/Cart/checkCartBuy`, 'POST', {
  334. cartIds,
  335. totalMoney: that.totalMoney
  336. });
  337. if (res.errcode == '0') {
  338. uni.navigateTo({
  339. url: `/pagesMy/order/index?status=${'0'}`
  340. })
  341. } else {
  342. uni.showToast({
  343. title: res.errmsg,
  344. icon: 'none'
  345. })
  346. }
  347. }
  348. });
  349. } else {
  350. uni.showToast({
  351. title: '角色不匹配 无法进行结算!',
  352. icon: 'none'
  353. })
  354. }
  355. } else {
  356. uni.navigateTo({
  357. url: `/pages/login/index`
  358. })
  359. }
  360. },
  361. // 是否选中商品,控制提交订单按钮
  362. hasCheck() {
  363. const that = this;
  364. return that.list.some(e => e.list.some(eg => eg.check))
  365. },
  366. // 清空总信息
  367. clearPages() {
  368. const that = this;
  369. that.$set(that, `list`, [])
  370. that.$set(that, `isAll`, false)
  371. that.$set(that, `totalMoney`, 0)
  372. that.$set(that, `num`, 0)
  373. },
  374. // 公共跳转
  375. toCommon(route, e) {
  376. if (e) {
  377. uni.navigateTo({
  378. url: `${route}?id=${e && e.goods._id}`
  379. })
  380. } else {
  381. uni.reLaunch({
  382. url: `${route}`
  383. })
  384. }
  385. },
  386. }
  387. }
  388. </script>
  389. <style lang="scss">
  390. .main {
  391. display: flex;
  392. flex-direction: column;
  393. width: 100vw;
  394. height: 100vh;
  395. background-color: var(--footColor);
  396. .one {
  397. text-align: center;
  398. margin: 2vw 0;
  399. .logo {
  400. margin: 10vw 0 2vw 0;
  401. .iconfont {
  402. font-size: 35vw;
  403. }
  404. }
  405. .one_1 {
  406. margin: 3vw 0;
  407. }
  408. button {
  409. background-color: var(--f3CColor);
  410. }
  411. }
  412. .two {
  413. display: flex;
  414. background-color: var(--mainColor);
  415. height: 35px;
  416. margin: 0 0 4px 0;
  417. padding: 5px 4vw;
  418. .text_1 {
  419. flex-grow: 1;
  420. line-height: 30px;
  421. }
  422. .text_2 {
  423. line-height: 35px;
  424. }
  425. }
  426. .thr {
  427. position: relative;
  428. flex-grow: 1;
  429. margin: 0 2vw;
  430. .list_1 {
  431. background-color: var(--mainColor);
  432. margin: 2vw 0;
  433. padding: 2vw 2.5vw;
  434. border-radius: 4px;
  435. .title {
  436. border-bottom: 1px solid var(--fcColor);
  437. padding: 0 0 2vw 0;
  438. font-size: var(--font18Size);
  439. text {
  440. margin: 0 1vw;
  441. }
  442. }
  443. .content {
  444. display: flex;
  445. padding: 2vw 0;
  446. font-size: var(--font16Size);
  447. border-bottom: 1px dashed var(--fcColor);
  448. .box {
  449. line-height: 20vw;
  450. }
  451. .img {
  452. width: 20vw;
  453. height: 20vw;
  454. border-radius: 2vw;
  455. border: 1px solid var(--fcColor);
  456. image {
  457. width: 20vw;
  458. height: 20vw;
  459. }
  460. }
  461. .thr_1 {
  462. width: 60vw;
  463. margin: 0 0 0 2vw;
  464. .left {
  465. display: flex;
  466. justify-content: space-between;
  467. .other {
  468. margin: 0 2vw;
  469. .money {
  470. text-align: right;
  471. flex-grow: 1;
  472. }
  473. }
  474. }
  475. .right {
  476. margin: 2vw 0 0 0;
  477. .title_1 {
  478. font-size: var(--font12Size);
  479. color: #666;
  480. text {
  481. white-space: nowrap;
  482. overflow: hidden;
  483. text-overflow: ellipsis;
  484. background-color: #eee;
  485. padding: 5px;
  486. }
  487. }
  488. }
  489. }
  490. .num {
  491. margin: 6vw 4vw;
  492. font-size: var(--font20Size);
  493. }
  494. .del {
  495. margin: 6vw 0;
  496. text-align: right;
  497. flex-grow: 1;
  498. }
  499. }
  500. }
  501. }
  502. .foot {
  503. background-color: var(--fffColor);
  504. display: flex;
  505. justify-content: space-between;
  506. height: 45px;
  507. padding: 0 0 0 6vw;
  508. border-right: 1px solid var(--f99Color);
  509. .total {
  510. flex-grow: 1;
  511. display: flex;
  512. align-content: flex-end;
  513. line-height: 40px;
  514. text {
  515. color: var(--f3CColor);
  516. }
  517. }
  518. .btn {
  519. button {
  520. width: 30vw;
  521. height: 45px;
  522. line-height: 45px;
  523. border-radius: 0px;
  524. background-color: var(--f3CColor);
  525. font-weight: normal;
  526. }
  527. }
  528. }
  529. }
  530. .scroll-view {
  531. position: absolute;
  532. top: 0;
  533. left: 0;
  534. right: 0;
  535. bottom: 0;
  536. .list-scroll-view {
  537. display: flex;
  538. flex-direction: column;
  539. }
  540. }
  541. // 复选框样式
  542. checkbox .wx-checkbox-input {
  543. width: 40rpx;
  544. height: 40rpx;
  545. border-radius: 50%;
  546. border-color: var(--f99Color);
  547. background-color: var(--mainColor);
  548. }
  549. // 复选框选中样式
  550. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  551. border-color: var(--f3CColor);
  552. background-color: var(--f3CColor);
  553. }
  554. // 复选框选中之后对号的样式
  555. checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  556. color: var(--fffColor);
  557. }
  558. </style>