index.vue 14 KB

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