index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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.id" :checked="item.check">
  33. <text class="iconfont icon-shangdian"></text>
  34. {{item.shop}}
  35. </checkbox>
  36. </view>
  37. <checkbox-group name="checkbox" @change="goodsChange">
  38. <view class="content" v-for="(item, indexs) in item.goods" :key="indexs">
  39. <view class="box">
  40. <checkbox :value="item.id" :checked="item.check" />
  41. </view>
  42. <view class="img">
  43. <image src="../../static/logo.png"></image>
  44. <!-- <image class="image" :src="item.url&&item.url.length>0?item.url[0].url:''" mode=""></image> -->
  45. </view>
  46. <view class=" one_1" v-if="num==0"
  47. @click="toCommon('/pagesHome/order/detail',item)">
  48. <view class="name">{{item.name}}</view>
  49. <view class="info">
  50. <view class="title_1" v-if="item.kg"><text>规格:{{item.kg}}</text>
  51. </view>
  52. </view>
  53. </view>
  54. <view class="money" v-if="num==0">
  55. <view>¥{{item.money}}</view>
  56. <view>x{{item.num}}</view>
  57. </view>
  58. <view class="num" v-if="num==1">
  59. <uni-number-box @change="changeValue(item)" name="num" value="item"
  60. :min="1" v-model="item.num" />
  61. </view>
  62. <view class="del" v-if="num==1">
  63. <text class="iconfont icon-del-copy" @click="toDel(item)"></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._id ? e._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.id);
  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.id)))
  226. if (shop) {
  227. for (let val of shop.goods) {
  228. let p2 = value.find((i) => i == val.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(value);
  243. // 计算总额
  244. that.countMoney();
  245. },
  246. //商品全部选择true,店铺自动选择,
  247. goodsAllChange(value) {
  248. const that = this;
  249. let list = that.list;
  250. let shop = list.find(f => f.goods.find(i => value.find(s => s == i.id)))
  251. if (shop) {
  252. for (let val of shop.goods) {
  253. if (val.check == true) {
  254. shop.check = true;
  255. } else {
  256. shop.check = false;
  257. }
  258. }
  259. } else {
  260. list = list.map(i => {
  261. i.goods = i.goods.map(g => ({
  262. ...g,
  263. check: false
  264. }))
  265. return i;
  266. })
  267. }
  268. // 计算总额
  269. that.countMoney();
  270. },
  271. // 加减商品数量
  272. changeValue(value) {
  273. const that = this;
  274. const list = that.list;
  275. for (const val of list) {
  276. for (let s of val.goods) {
  277. if (s.id == value.id) {
  278. s.num = value.num
  279. }
  280. }
  281. }
  282. that.$set(that, `list`, list)
  283. // 计算总额
  284. that.countMoney(list);
  285. },
  286. // 删除
  287. toDel(e) {
  288. const that = this;
  289. const list = that.list;
  290. let shop = list.find(f => f.goods.find(i => i.check == true))
  291. if (shop) {
  292. uni.showModal({
  293. title: '提示',
  294. content: '确认要删除这些商品吗?',
  295. success: async function(res) {
  296. if (res.confirm) {}
  297. }
  298. });
  299. } else if (e.id) {
  300. uni.showModal({
  301. title: '提示',
  302. content: '是否确认删除该商品',
  303. success: async function(res) {
  304. if (res.confirm) {}
  305. }
  306. });
  307. } else {
  308. uni.showModal({
  309. title: '提示',
  310. content: '请选择要删除的商品',
  311. success: async function(res) {
  312. if (res.confirm) {}
  313. }
  314. });
  315. }
  316. },
  317. // 计算总额
  318. countMoney() {
  319. const that = this;
  320. const list = this.list;
  321. let totalMoney = 0;
  322. // 渲染结束执行下面方法
  323. that.$nextTick(() => {
  324. for (const val of list) {
  325. for (let s of val.goods) {
  326. if (s.check == true) {
  327. let total = Number(s.money) * Number(s.num);
  328. totalMoney += Number(total);
  329. }
  330. }
  331. }
  332. that.$set(that, `totalMoney`, totalMoney.toFixed(2))
  333. })
  334. },
  335. // 去结算
  336. toSettle() {},
  337. }
  338. }
  339. </script>
  340. <style lang="scss">
  341. .main {
  342. display: flex;
  343. flex-direction: column;
  344. width: 100vw;
  345. height: 92vh;
  346. background-color: var(--footColor);
  347. .top {
  348. display: flex;
  349. flex-direction: row;
  350. background-color: var(--mainColor);
  351. height: 35px;
  352. margin: 0 0 4px 0;
  353. padding: 5px 4vw;
  354. .text_1 {
  355. flex-grow: 1;
  356. line-height: 30px;
  357. }
  358. .text_2 {
  359. line-height: 35px;
  360. }
  361. }
  362. .one {
  363. text-align: center;
  364. margin: 2vw 0;
  365. .logo {
  366. margin: 10vw 0 2vw 0;
  367. .iconfont {
  368. font-size: 35vw;
  369. }
  370. }
  371. .one_1 {
  372. margin: 3vw 0;
  373. }
  374. button {
  375. background-color: var(--ff0Color);
  376. }
  377. }
  378. .two {
  379. position: relative;
  380. flex-grow: 1;
  381. margin: 0 2vw;
  382. .list {
  383. background-color: var(--mainColor);
  384. margin: 2vw 0 2vw 0;
  385. padding: 2vw 3vw;
  386. border-radius: 4px;
  387. .title {
  388. border-bottom: 1px solid var(--fcColor);
  389. padding: 0 0 2vw 0;
  390. font-size: var(--font18Size);
  391. text {
  392. margin: 0 1vw;
  393. }
  394. }
  395. .content {
  396. display: flex;
  397. flex-direction: row;
  398. padding: 3vw 0;
  399. font-size: var(--font16Size);
  400. border-bottom: 1px dashed var(--fcColor);
  401. .box {
  402. line-height: 20vw;
  403. }
  404. .img {
  405. width: 20vw;
  406. height: 20vw;
  407. border-radius: 2vw;
  408. border: 1px solid var(--fcColor);
  409. image {
  410. width: 20vw;
  411. height: 20vw;
  412. }
  413. }
  414. .one_1 {
  415. margin: 2.5vw;
  416. flex-grow: 1;
  417. .info {
  418. width: 100%;
  419. display: flex;
  420. flex-direction: row;
  421. .title_1 {
  422. font-size: 12px;
  423. color: #666;
  424. margin-top: 10px;
  425. text {
  426. background-color: #eee;
  427. padding: 5px;
  428. }
  429. }
  430. }
  431. }
  432. .money {
  433. margin-top: 2vw;
  434. text-align: right;
  435. flex-grow: 1;
  436. }
  437. .num {
  438. margin: 6vw 4vw;
  439. font-size: 20px;
  440. }
  441. .del {
  442. margin: 6vw 0;
  443. text-align: right;
  444. flex-grow: 1;
  445. }
  446. }
  447. }
  448. }
  449. .foot {
  450. background-color: var(--fffColor);
  451. display: flex;
  452. flex-direction: row;
  453. justify-content: space-between;
  454. height: 44px;
  455. padding: 0 0 0 6vw;
  456. border-right: 1px solid var(--f99Color);
  457. .total {
  458. flex-grow: 1;
  459. display: flex;
  460. align-content: flex-end;
  461. line-height: 40px;
  462. text {
  463. color: var(--ff0Color);
  464. }
  465. }
  466. .btn {
  467. button {
  468. width: 30vw;
  469. height: 44px;
  470. line-height: 44px;
  471. border-radius: 0px;
  472. background-color: var(--ff0Color);
  473. text-align: center;
  474. font-weight: normal;
  475. font-size: var(--font15Size);
  476. color: var(--fffColor);
  477. }
  478. }
  479. }
  480. }
  481. .scroll-view {
  482. position: absolute;
  483. top: 0;
  484. left: 0;
  485. right: 0;
  486. bottom: 0;
  487. .list-scroll-view {
  488. display: flex;
  489. flex-direction: column;
  490. }
  491. }
  492. // 复选框样式
  493. checkbox .wx-checkbox-input {
  494. width: 40rpx;
  495. height: 40rpx;
  496. border-radius: 50%;
  497. border-color: var(--f99Color);
  498. background-color: var(--mainColor);
  499. }
  500. // 复选框选中样式
  501. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  502. border-color: var(--ff0Color);
  503. background-color: var(--ff0Color);
  504. }
  505. // 复选框选中之后对号的样式
  506. checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  507. color: var(--fffColor);
  508. }
  509. </style>