index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. <!-- <image class="image" :src="gs.url&&gs.url.length>0?gs.url[0].url:''" mode=""></image> -->
  45. </view>
  46. <view class=" one_1" v-if="num==0" @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"><text>规格:{{gs.goodsSpec_name}}</text>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="money" v-if="num==0">
  54. <view>¥{{gs.money}}</view>
  55. <view>x{{gs.num}}</view>
  56. </view>
  57. <view class="num" v-if="num==1">
  58. <uni-number-box @change="changeValue(gs)" name="num" value="gs" :min="1" v-model="gs.num" />
  59. </view>
  60. <view class="del" v-if="num==1">
  61. <text class="iconfont icon-del-copy" @click="toDel(gs)"></text>
  62. </view>
  63. </view>
  64. </checkbox-group>
  65. </view>
  66. </view>
  67. </checkbox-group>
  68. </view>
  69. </scroll-view>
  70. </view>
  71. <view class="foot" v-if="list.length !=0">
  72. <view class="total">总价:<text>¥{{totalMoney}}(不含运费)</text></view>
  73. <view class="btn">
  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. </mobile-frame>
  80. </template>
  81. <script>
  82. export default {
  83. data() {
  84. return {
  85. frameStyle: {
  86. useBar: true
  87. },
  88. // 用户
  89. user: {},
  90. // 购物车列表
  91. list: [],
  92. // 全选
  93. isAll: false,
  94. // 编辑/完成按钮传的数字
  95. num: 0,
  96. // 总额
  97. totalMoney: 0,
  98. };
  99. },
  100. onShow: function () {
  101. const that = this;
  102. // 监听登录
  103. that.watchLogin();
  104. },
  105. methods: {
  106. toPath(e) {
  107. if (e && e.route) uni.redirectTo({
  108. url: `/${e.route}`
  109. })
  110. },
  111. // 公共跳转
  112. toCommon(route, e) {
  113. uni.navigateTo({
  114. url: `${route}?id=${e && e.goods_id}`
  115. })
  116. },
  117. // 监听登录
  118. watchLogin() {
  119. const that = this;
  120. uni.getStorage({
  121. key: 'token',
  122. success: (res) => {
  123. let user = that.$jwt(res.data);
  124. if (user) {
  125. that.$set(that, `user`, {
  126. id: user.id,
  127. })
  128. that.searchMarket();
  129. }
  130. },
  131. fail: (err) => { }
  132. })
  133. },
  134. // 查询购物车信息
  135. async searchMarket() {
  136. const that = this;
  137. const res = await that.$api(`/cart/selfCart`, 'GET', {
  138. customer: that.user.id
  139. });
  140. if (res.errcode == '0') {
  141. console.log(res.data);
  142. that.$set(that, `list`, res.data)
  143. }
  144. },
  145. // 转换金额
  146. showMoney(money) {
  147. const that = this;
  148. let moneys = `${money}`.split(".");
  149. if (moneys.length == 1) {
  150. money = money + ".00";
  151. } else if (moneys.length == 2) {
  152. money = money + "0";
  153. }
  154. return money;
  155. },
  156. // 编辑
  157. edit(num) {
  158. const that = this;
  159. that.$set(that, `num`, num)
  160. },
  161. //全选
  162. selectAll(e) {
  163. const that = this;
  164. const list = that.list;
  165. const isAll = that.isAll;
  166. let data = [];
  167. for (const val of list) {
  168. let a = isAll ? false : true;
  169. val.check = a;
  170. for (let s of val.goods) {
  171. s.check = a
  172. }
  173. data.push(val);
  174. }
  175. that.$set(that, `list`, data)
  176. // 计算总额
  177. that.countMoney();
  178. // 赋值是否全选
  179. that.$set(that, `isAll`, isAll ? false : true)
  180. },
  181. // 选择店铺
  182. marketChange(e) {
  183. const that = this;
  184. const list = this.list;
  185. const {
  186. value
  187. } = e.detail;
  188. for (const p1 of list) {
  189. let p2 = value.find((i) => i == p1.shop);
  190. let a = p2 ? true : false;
  191. p1.check = a;
  192. for (let s of p1.goods) {
  193. s.check = a
  194. }
  195. }
  196. that.$set(that, `list`, list);
  197. that.marketAllChange();
  198. // 计算总额
  199. that.countMoney();
  200. },
  201. //店铺全部选择true,全选自动选择,
  202. marketAllChange() {
  203. const that = this;
  204. let list = that.list;
  205. for (let val of list) {
  206. if (val.check == true) {
  207. that.$set(that, `isAll`, true)
  208. } else {
  209. that.$set(that, `isAll`, false)
  210. }
  211. }
  212. that.$set(that, `list`, list)
  213. // 计算总额
  214. that.countMoney();
  215. },
  216. //选择商品
  217. goodsChange(e) {
  218. const that = this;
  219. let list = that.list;
  220. const {
  221. value
  222. } = e.detail;
  223. let shop = list.find(f => f.goods.find(i => value.find(s => s == i.goodsSpec_id)))
  224. if (shop) {
  225. for (let val of shop.goods) {
  226. let p2 = value.find((i) => i == val.goodsSpec_id);
  227. if (p2) val.check = true;
  228. else val.check = false;
  229. }
  230. } else {
  231. list = list.map(i => {
  232. i.goods = i.goods.map(g => ({
  233. ...g,
  234. check: false
  235. }))
  236. return i;
  237. })
  238. }
  239. that.$set(that, `list`, list)
  240. that.goodsAllChange();
  241. // 计算总额
  242. that.countMoney();
  243. },
  244. //商品全部选择true,店铺自动选择,
  245. // 所有店铺为true,则全选为true
  246. goodsAllChange() {
  247. const that = this;
  248. let list = that.list;
  249. list = list.map(i => {
  250. const isAllSelect = i.goods.every(f => f.check);
  251. if (isAllSelect) i.check = true;
  252. else i.check = false;
  253. return i;
  254. })
  255. const allSelect = list.every(e => e.check)
  256. if (allSelect) this.$set(this, `isAll`, true)
  257. else this.$set(this, `isAll`, false)
  258. // 计算总额
  259. that.countMoney();
  260. },
  261. // 加减商品数量
  262. async changeValue(value) {
  263. const that = this;
  264. this.$nextTick(async () => {
  265. const { goodsSpec_id: goodsSpecId, num } = value
  266. const res = await that.$api(`/cart/checkGoodsNum`, 'GET', {
  267. goodsSpecId,
  268. num
  269. });
  270. if (res.errcode === 0) {
  271. const { enough, total } = res.data
  272. if (!enough) {
  273. uni.showToast({
  274. title: `库存最大为${total}`,
  275. icon: 'error',
  276. });
  277. // 将该商品的库存量修改为最大值
  278. value.num = total;
  279. }
  280. }
  281. // 计算总额
  282. that.countMoney();
  283. })
  284. },
  285. // 删除, 接口,购物车删除,然后将该数据移除
  286. async toDel(e) {
  287. let list = this.list;
  288. uni.showModal({
  289. title: '提示',
  290. content: '请选择要删除的商品',
  291. success: async (res) => {
  292. if (!res.confirm) return
  293. const result = await this.$api(`/cart/${e.cart_id}`, 'Delete');
  294. if (result.errcode === 0) {
  295. list = list.map(i => ({ ...i, goods: i.goods.filter(f => f.cart_id !== e.cart_id) }))
  296. this.$set(this, `list`, list);
  297. // 检查店铺内是否还有商品
  298. this.checkShopGoodsExist();
  299. // 计算总额
  300. this.countMoney();
  301. }
  302. }
  303. });
  304. },
  305. //检查店铺内是否还有商品
  306. checkShopGoodsExist() {
  307. let list = this.list;
  308. list = list.filter(f => f.goods && f.goods.length > 0)
  309. this.$set(this, `list`, list);
  310. },
  311. // 计算总额
  312. countMoney() {
  313. const that = this;
  314. const list = this.list;
  315. let totalMoney = 0;
  316. // 渲染结束执行下面方法
  317. that.$nextTick(() => {
  318. for (const val of list) {
  319. for (let s of val.goods) {
  320. if (s.check == true) {
  321. let total = Number(s.money) * Number(s.num);
  322. totalMoney += Number(total);
  323. }
  324. }
  325. }
  326. that.$set(that, `totalMoney`, totalMoney.toFixed(2))
  327. })
  328. },
  329. getFile(data) {
  330. const file = data.file;
  331. if (!file) return '';
  332. if (file.length && file.length > 0) return file[0].url
  333. },
  334. // 去结算
  335. toSettle() { },
  336. }
  337. }
  338. </script>
  339. <style lang="scss">
  340. .main {
  341. display: flex;
  342. flex-direction: column;
  343. width: 100vw;
  344. height: 92vh;
  345. background-color: var(--footColor);
  346. .top {
  347. display: flex;
  348. flex-direction: row;
  349. background-color: var(--mainColor);
  350. height: 35px;
  351. margin: 0 0 4px 0;
  352. padding: 5px 4vw;
  353. .text_1 {
  354. flex-grow: 1;
  355. line-height: 30px;
  356. }
  357. .text_2 {
  358. line-height: 35px;
  359. }
  360. }
  361. .one {
  362. text-align: center;
  363. margin: 2vw 0;
  364. .logo {
  365. margin: 10vw 0 2vw 0;
  366. .iconfont {
  367. font-size: 35vw;
  368. }
  369. }
  370. .one_1 {
  371. margin: 3vw 0;
  372. }
  373. button {
  374. background-color: var(--ff0Color);
  375. }
  376. }
  377. .two {
  378. position: relative;
  379. flex-grow: 1;
  380. margin: 0 2vw;
  381. .list {
  382. background-color: var(--mainColor);
  383. margin: 2vw 0 2vw 0;
  384. padding: 2vw 3vw;
  385. border-radius: 4px;
  386. .title {
  387. border-bottom: 1px solid var(--fcColor);
  388. padding: 0 0 2vw 0;
  389. font-size: var(--font18Size);
  390. text {
  391. margin: 0 1vw;
  392. }
  393. }
  394. .content {
  395. display: flex;
  396. flex-direction: row;
  397. padding: 3vw 0;
  398. font-size: var(--font16Size);
  399. border-bottom: 1px dashed var(--fcColor);
  400. .box {
  401. line-height: 20vw;
  402. }
  403. .img {
  404. width: 20vw;
  405. height: 20vw;
  406. border-radius: 2vw;
  407. border: 1px solid var(--fcColor);
  408. image {
  409. width: 20vw;
  410. height: 20vw;
  411. }
  412. }
  413. .one_1 {
  414. margin: 2.5vw;
  415. flex-grow: 1;
  416. .info {
  417. width: 100%;
  418. display: flex;
  419. flex-direction: row;
  420. .title_1 {
  421. font-size: 12px;
  422. color: #666;
  423. margin-top: 10px;
  424. text {
  425. background-color: #eee;
  426. padding: 5px;
  427. }
  428. }
  429. }
  430. }
  431. .money {
  432. margin-top: 2vw;
  433. text-align: right;
  434. flex-grow: 1;
  435. }
  436. .num {
  437. margin: 6vw 4vw;
  438. font-size: 20px;
  439. }
  440. .del {
  441. margin: 6vw 0;
  442. text-align: right;
  443. flex-grow: 1;
  444. }
  445. }
  446. }
  447. }
  448. .foot {
  449. background-color: var(--fffColor);
  450. display: flex;
  451. flex-direction: row;
  452. justify-content: space-between;
  453. height: 44px;
  454. padding: 0 0 0 6vw;
  455. border-right: 1px solid var(--f99Color);
  456. .total {
  457. flex-grow: 1;
  458. display: flex;
  459. align-content: flex-end;
  460. line-height: 40px;
  461. text {
  462. color: var(--ff0Color);
  463. }
  464. }
  465. .btn {
  466. button {
  467. width: 30vw;
  468. height: 44px;
  469. line-height: 44px;
  470. border-radius: 0px;
  471. background-color: var(--ff0Color);
  472. text-align: center;
  473. font-weight: normal;
  474. font-size: var(--font15Size);
  475. color: var(--fffColor);
  476. }
  477. }
  478. }
  479. }
  480. .scroll-view {
  481. position: absolute;
  482. top: 0;
  483. left: 0;
  484. right: 0;
  485. bottom: 0;
  486. .list-scroll-view {
  487. display: flex;
  488. flex-direction: column;
  489. }
  490. }
  491. // 复选框样式
  492. checkbox .wx-checkbox-input {
  493. width: 40rpx;
  494. height: 40rpx;
  495. border-radius: 50%;
  496. border-color: var(--f99Color);
  497. background-color: var(--mainColor);
  498. }
  499. // 复选框选中样式
  500. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  501. border-color: var(--ff0Color);
  502. background-color: var(--ff0Color);
  503. }
  504. // 复选框选中之后对号的样式
  505. checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  506. color: var(--fffColor);
  507. }
  508. </style>