Class.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <template>
  2. <view>
  3. <view class="u-wrap">
  4. <view class="u-menu-wrap">
  5. <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop"
  6. :scroll-into-view="itemId">
  7. <view v-for="(item, index) in tabbar" :key="index" class="u-tab-item"
  8. :class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
  9. <text class="u-line-1">{{ item.businessName }}</text>
  10. <view v-if="activesParentIds[item.id]" class="u-tab-item-radio flex_c_c">{{
  11. activesParentIds[item.id].length }}</view>
  12. </view>
  13. </scroll-view>
  14. <scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box"
  15. @scroll="rightScroll">
  16. <view class="page-view">
  17. <view class="class-item" :id="'item' + index" v-for="(item, index) in tabbar" :key="index">
  18. <view class="item-title">
  19. <text class="item-businessName">{{ item.businessName }}</text>
  20. <text class="item-title-text">{{ item.businessDescribe }}</text>
  21. </view>
  22. <view class="item-container">
  23. <view v-for="(item1, index1) in (item.children ? item.children : [item])" :key="index1"
  24. :class="[activesIds.includes(item1.id) ? 'thumb-box thumb-box-active ' : 'thumb-box']"
  25. @click="clickMenu(item1)">
  26. <image class="item-menu-image" :src="item1.businessIcon"></image>
  27. <view class="item-menu-name">{{ item1.businessName }}</view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. </view>
  34. </view>
  35. <view class="class-footer">
  36. <view class="footer-title flex_c_s">
  37. <view class="footer-text">已选服务</view>
  38. <!-- <view class="footer-text">{{ activeList.length }}/6</view> -->
  39. </view>
  40. <view class="footer-btns" v-if="activeList && activeList.length > 0">
  41. <view class="footer-btn" v-for="item in activeList" :key="item.id">
  42. {{ item.businessName || 'xx' }}
  43. <view class="footer-class-close" @click="deleteActiveList(item.id)"></view>
  44. </view>
  45. </view>
  46. <view class="footer-submit-box flex_c_c">
  47. <view class="footer-submit flex_c_c" @click="onSubmit">确定</view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import { getTreeList } from '@/api/volunteer'
  54. import { volunteerSeachgetTreeList } from "@/api/volunteerDetailsApi/details.js"
  55. import { getVolunteerInfo } from '@/api/volunteer.js'
  56. import CustomTabBar from '@/components/CustomTabBar/index.vue'
  57. import { computed } from 'vue'
  58. export default {
  59. components: { CustomTabBar },
  60. data() {
  61. return {
  62. scrollTop: 0, //tab标题的滚动条位置
  63. oldScrollTop: 0,
  64. current: 0, // 预设当前项的值
  65. menuHeight: 0, // 左边菜单的高度
  66. menuItemHeight: 0, // 左边菜单item的高度
  67. itemId: '', // 栏目右边scroll-view用于滚动的id
  68. tabbar: [],
  69. arr: [],
  70. scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
  71. timer: null, // 定时器
  72. userType: uni.getStorageSync('userType') || 1,
  73. activeList: [],
  74. }
  75. },
  76. computed: {
  77. activesIds() {
  78. return this.activeList.map(item => item.id);
  79. },
  80. activesParentIds() {
  81. const result = this.activeList.reduce((acc, item) => {
  82. const { parentId } = item;
  83. if (!acc[parentId]) {
  84. acc[parentId] = [];
  85. }
  86. acc[parentId].push(item.id); // 推入整个对象
  87. return acc;
  88. }, {});
  89. return result;
  90. }
  91. },
  92. onReady() {
  93. },
  94. onShow() {
  95. this.getData();
  96. this.getMenuItemTop()
  97. },
  98. onLoad(options) {
  99. const data = decodeURIComponent(options.data)
  100. console.log("TCL: options", )
  101. if(data){
  102. this.activeList = data ? JSON.parse(data) : [];
  103. }
  104. },
  105. methods: {
  106. deleteActiveList(id) {
  107. console.log("TCL: deleteActiveList -> id", id)
  108. this.activeList = this.activeList.filter(item => item.id !== id);
  109. },
  110. getData() {
  111. getTreeList({ parentId: '0' }).then(res => {
  112. this.tabbar = res.data;
  113. })
  114. },
  115. // 点击左边的栏目切换
  116. async swichMenu(index) {
  117. if (this.arr.length == 0) {
  118. await this.getMenuItemTop();
  119. }
  120. if (index == this.current) return;
  121. this.scrollRightTop = this.oldScrollTop;
  122. this.$nextTick(() => {
  123. this.scrollRightTop = this.arr[index];
  124. this.current = index;
  125. this.leftMenuStatus(index);
  126. })
  127. },
  128. // 获取一个目标元素的高度
  129. getElRect(elClass, dataVal) {
  130. new Promise((resolve, reject) => {
  131. const query = uni.createSelectorQuery().in(this);
  132. query.select('.' + elClass).fields({
  133. size: true
  134. }, res => {
  135. // 如果节点尚未生成,res值为null,循环调用执行
  136. if (!res) {
  137. setTimeout(() => {
  138. this.getElRect(elClass);
  139. }, 10);
  140. return;
  141. }
  142. this[dataVal] = res.height;
  143. resolve();
  144. }).exec();
  145. })
  146. },
  147. // 观测元素相交状态
  148. async observer() {
  149. this.tabbar.map((val, index) => {
  150. let observer = uni.createIntersectionObserver(this);
  151. // 检测右边scroll-view的id为itemxx的元素与right-box的相交状态
  152. // 如果跟.right-box底部相交,就动态设置左边栏目的活动状态
  153. observer.relativeTo('.right-box', {
  154. top: 0
  155. }).observe('#item' + index, res => {
  156. if (res.intersectionRatio > 0) {
  157. let id = res.id.substring(4);
  158. this.leftMenuStatus(id);
  159. }
  160. })
  161. })
  162. },
  163. // 设置左边菜单的滚动状态
  164. async leftMenuStatus(index) {
  165. this.current = index;
  166. // 如果为0,意味着尚未初始化
  167. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  168. await this.getElRect('menu-scroll-view', 'menuHeight');
  169. await this.getElRect('u-tab-item', 'menuItemHeight');
  170. }
  171. // 将菜单活动item垂直居中
  172. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  173. },
  174. // 获取右边菜单每个item到顶部的距离
  175. getMenuItemTop() {
  176. new Promise(resolve => {
  177. let selectorQuery = uni.createSelectorQuery();
  178. selectorQuery.selectAll('.class-item').boundingClientRect((rects) => {
  179. // 如果节点尚未生成,rects值为[](因为用selectAll,所以返回的是数组),循环调用执行
  180. if (!rects.length) {
  181. setTimeout(() => {
  182. this.getMenuItemTop();
  183. }, 10);
  184. return;
  185. }
  186. rects.forEach((rect) => {
  187. // 这里减去rects[0].top,是因为第一项顶部可能不是贴到导航栏(比如有个搜索框的情况)
  188. this.arr.push(rect.top - rects[0].top);
  189. resolve();
  190. })
  191. }).exec()
  192. })
  193. },
  194. // 右边菜单滚动
  195. async rightScroll(e) {
  196. this.oldScrollTop = e.detail.scrollTop;
  197. if (this.arr.length == 0) {
  198. await this.getMenuItemTop();
  199. }
  200. if (this.timer) return;
  201. if (!this.menuHeight) {
  202. await this.getElRect('menu-scroll-view', 'menuHeight');
  203. }
  204. setTimeout(() => { // 节流
  205. this.timer = null;
  206. // scrollHeight为右边菜单垂直中点位置
  207. let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
  208. for (let i = 0; i < this.arr.length; i++) {
  209. let height1 = this.arr[i];
  210. let height2 = this.arr[i + 1];
  211. // 如果不存在height2,意味着数据循环已经到了最后一个,设置左边菜单为最后一项即可
  212. if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
  213. this.leftMenuStatus(i);
  214. return;
  215. }
  216. }
  217. }, 10)
  218. },
  219. async clickMenu(record) {
  220. try {
  221. console.log(record, this.activesIds, this.activesParentIds);
  222. // if (this.activesIds && this.activesIds.length === 6) {
  223. // uni.showToast({
  224. // title: '最多可选择6个类目!',
  225. // icon: 'none',
  226. // });
  227. // return
  228. // }
  229. !this.activesIds.includes(record.id) ? this.activeList.push({ ...record, children: [] }) : uni.showToast({
  230. title: '已勾选该类目!',
  231. icon: 'none',
  232. });
  233. } catch (error) {
  234. console.log("TCL: clickMenu -> error", error)
  235. }
  236. },
  237. onSubmit() {
  238. try {
  239. const pages = getCurrentPages();
  240. const prevPage = pages[pages.length - 2];
  241. if (prevPage && prevPage.$vm) {
  242. // 假设上一页有一个方法叫 handleReturnData
  243. prevPage.$vm.activeProjectBack({
  244. serves: encodeURIComponent(JSON.stringify(this.activeList)),
  245. });
  246. }
  247. uni.navigateBack({
  248. delta: 1
  249. });
  250. } catch (error) {
  251. console.log("TCL: onSubmit -> error", error)
  252. }
  253. // uni.redirectTo({
  254. // url: `/pages_home/pages/certification/index?serves=${encodeURIComponent(JSON.stringify(this.activeList))}`
  255. // })
  256. console.log('this.activeList', this.activeList);
  257. }
  258. }
  259. }
  260. </script>
  261. <style lang="scss" scoped>
  262. .u-wrap {
  263. display: flex;
  264. flex-direction: column;
  265. position: fixed;
  266. top: 0px;
  267. left: 0px;
  268. right: 0px;
  269. // bottom: 150rpx;
  270. bottom: 0;
  271. // padding-bottom: 150px;
  272. }
  273. .u-search-box {
  274. padding: 18rpx 30rpx;
  275. }
  276. .u-menu-wrap {
  277. flex: 1;
  278. display: flex;
  279. overflow: hidden;
  280. }
  281. .u-search-inner {
  282. background-color: rgb(234, 234, 234);
  283. border-radius: 100rpx;
  284. display: flex;
  285. align-items: center;
  286. padding: 10rpx 16rpx;
  287. }
  288. .u-search-text {
  289. font-size: 26rpx;
  290. }
  291. .u-tab-view {
  292. width: 250rpx;
  293. height: 100%;
  294. }
  295. .u-tab-item {
  296. height: 100rpx;
  297. background: #f6f6f6;
  298. box-sizing: border-box;
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. font-family: PingFang SC;
  303. font-size: 26rpx;
  304. font-weight: 500;
  305. line-height: 42rpx;
  306. text-align: center;
  307. letter-spacing: normal;
  308. color: #818181;
  309. border-left: 4rpx solid #fff;
  310. position: relative;
  311. }
  312. .u-tab-item-radio {
  313. width: 32rpx;
  314. height: 32rpx;
  315. padding: 8rpx;
  316. border-radius: 50%;
  317. background: #F53F3F;
  318. position: absolute;
  319. right: 0px;
  320. font-size: 24rpx;
  321. color: #FFFFFF;
  322. }
  323. .u-tab-item-active {
  324. position: relative;
  325. background: #fff;
  326. }
  327. .u-tab-item-active::before {
  328. border-left: 4rpx solid rgba(221, 94, 69, 1) !important;
  329. }
  330. .u-tab-item-active::before {
  331. content: "";
  332. position: absolute;
  333. border-left: 4px solid $u-primary;
  334. height: 100%;
  335. left: -5rpx;
  336. top: 0;
  337. }
  338. .u-tab-view {
  339. height: 100%;
  340. background: #F6F6F6;
  341. }
  342. .right-box {
  343. background-color: rgb(250, 250, 250);
  344. }
  345. .page-view {
  346. // padding: 16rpx;
  347. }
  348. .class-item {
  349. // margin-bottom: 30rpx;
  350. background-color: #fff;
  351. padding: 34rpx;
  352. border-radius: 8rpx;
  353. }
  354. .class-item:last-child {
  355. min-height: 100vh;
  356. }
  357. .item-title {
  358. // font-size: 26rpx;
  359. // color: $u-main-color;
  360. // font-weight: bold;
  361. // text-align: center;
  362. display: flex;
  363. align-items: center;
  364. justify-content: space-between;
  365. border-radius: 14rpx;
  366. background: linear-gradient(180deg, #FFF9F3 0%, rgba(255, 255, 255, 0) 100%);
  367. }
  368. .item-businessName {
  369. font-family: PingFang SC;
  370. font-size: 26rpx;
  371. font-weight: 600;
  372. line-height: 42rpx;
  373. letter-spacing: normal;
  374. color: #262626;
  375. }
  376. .item-title-text {
  377. font-family: Yuppy SC;
  378. font-size: 24rpx;
  379. font-weight: 500;
  380. line-height: 42rpx;
  381. text-align: right;
  382. letter-spacing: normal;
  383. color: #E58182;
  384. }
  385. .item-menu-name {
  386. font-family: PingFang SC;
  387. font-size: 26rpx;
  388. font-weight: 500;
  389. line-height: 42rpx;
  390. text-align: center;
  391. letter-spacing: normal;
  392. color: #5D5D5D;
  393. }
  394. .item-container {
  395. // display: flex;
  396. // flex-wrap: wrap;
  397. display: grid;
  398. grid-template-columns: repeat(3, 1fr);
  399. gap: 12rpx;
  400. margin: 24rpx 0;
  401. }
  402. .thumb-box {
  403. // width: 33.333333%;
  404. display: flex;
  405. align-items: center;
  406. justify-content: center;
  407. flex-direction: column;
  408. // margin-top: 20rpx;
  409. // background: #f5f5f5;
  410. padding: 12rpx 0;
  411. }
  412. .thumb-box-active {
  413. .item-menu-image::before {
  414. content: '';
  415. width: 138rpx;
  416. height: 138rpx;
  417. position: absolute;
  418. left: 0px;
  419. top: 0px;
  420. background: rgba(216, 216, 216, 0.6);
  421. z-index: 2;
  422. border-radius: 50%;
  423. }
  424. .item-menu-image::after {
  425. content: '';
  426. width: 138rpx;
  427. height: 138rpx;
  428. position: absolute;
  429. left: 0px;
  430. top: 0px;
  431. z-index: 3;
  432. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAcCAYAAAAAwr0iAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAKOSURBVEiJxZVPSFRRFId/59zXH8JFS4sIA3MGdCO4KEo4zhC4CqMWLQrcii1ql7ugRS5aNNDCZSJBEdFSwXpeMDFwkYGjDgS1MHHRwoXQVPPuadE8m3nMc3zjON3d3HvP+b53z5l7Cf9hfBE5/pP5IQEF02r4qsgFZZ4mwlUQnfVaBZ4T8U4xPwLhLgAPAFR1kloBXxPpYGNeAdoXzqliJO37E3zY8EI2O8SGP0bg99K+PwEAhyqwlsk8APQNgJMR+JPw96H0QF6kzTM8BWCocl6BXCUcAJreA3mR9iPMs0roqV7Rl6l3czej+5tagoJI2mNejMJVdeFooMO1YpomUBC5DMPzIHREljYCpzfOWVusFdeUEpTh0wDaIktFCtzFLmuX42J3mzAv0naUedRzLhdnmxAOUozsBQfKJciLdHrMS44w/suY+U8iZw4KB3Syy/ef1cvB61kZ9AwvgZAuB/YdM7y0JiKNwkmxUgr0zn4+ggk0jIqLAgAIaCfDs4VsdrRW0KpIT/yXowTnbndbu7Mvgc1Abyn0dXSBAA/Qp4VsZiovsgv6LNLJhmdj4HCKsXp1j3DKL5WhFwS6HrNt2QXBtSKwfYJ58V+5osnow7cg6B+wtpRIIJQ4zTQDomzM3u8Afa18VCKjiMD1pqxd3y+8SgD4+1c0TDNEdClJEgCAYizl++NJw6puwm5rdwKng6q6kCQJKVY2nXucFA7E3ISJT4Jcf+qtfd+IQM23IDwJUqzUS6DQ543CYwVCid/OXYFir6YqmkDvNwrfU6AssVVybiBOQhW589ZuHJpAHYntH84l7vrEAlUSwFY45xS5Xmu3WyIQSiDYlSg65yYOCm9oFETS65nMgY8+HH8AWxQUPX3zeH8AAAAASUVORK5CYII=);
  433. background-position: center;
  434. background-size: 32rpx auto;
  435. background-repeat: no-repeat;
  436. }
  437. .item-menu-name {
  438. color: #FF6E51;
  439. }
  440. }
  441. .item-menu-image {
  442. width: 138rpx;
  443. height: 138rpx;
  444. margin-bottom: 12rpx;
  445. }
  446. .class-footer {
  447. position: fixed;
  448. bottom: 0rpx;
  449. left: 0px;
  450. right: 0;
  451. z-index: 999;
  452. background: #fff;
  453. padding-bottom: 60rpx;
  454. .footer-title {
  455. padding: 38rpx 28rpx;
  456. }
  457. .footer-btns {
  458. display: grid;
  459. grid-template-columns: repeat(3, 1fr); // 创建 3 列,每列等宽
  460. gap: 10rpx; // 设置列与行之间的间距
  461. padding: 0 26rpx 48rpx;
  462. .footer-btn {
  463. border-radius: 66rpx;
  464. box-sizing: border-box;
  465. border: 2rpx solid #FF6E51;
  466. padding: 16rpx 16rpx 16rpx 36rpx;
  467. font-size: 30rpx;
  468. font-weight: 500;
  469. color: #FF6E51;
  470. display: flex;
  471. align-items: center;
  472. gap: 14rpx;
  473. .footer-class-close {
  474. width: 34rpx;
  475. height: 34rpx;
  476. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB8AAAAfCAYAAAAfrhY5AAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAALgSURBVEiJ5ZdNS9xAGMfnzRBB0UJhdw/DIriga8B63BRLbz31Yl08iIUc21IQP8F+glLoocehUg8S62cIlcabVMhFVvCwUHbBg16C7M5LL91lk0xi1t1Dwf8pmZknv38yz0yeAeCxCo4awBibLxQK60KIBQgh4pxLwzCuOp3OieM4NxOHM8bM2dnZ90qpnV6v9yxt3NTU1G+M8f7t7e1Xx3Huxoa7rrsthPgipXySxygAAGCMrxFCu/V6/eBB8EajQSzLOux2uxt5oXEZhnEcBMFWo9HgWpNp4Gq16vV6vVcPBQMAgBBiuVQqvaxWq989z5PxfqQLsizrkHP+fBxwX91u94VlWYe6vgTcdd3tcT51ioEN13W34+2ROWeMmdPT039GSa68whhfh2FIh1dB5M1nZmY+pIEhhMC2bUAp1T6cUgps2wYQ6nNYCPF0bm7u3XBbBK6U2klzXqvVQLlc1hrog8vlMqjVammPAEKIt1o4Y2yec76aFthqtYCUEiCEIgb6YIQQkFKCVquVCuecrzLG5hPwQqGwnhr1D+77fsTA2tpaBOz7fiZcKQWLxaKdgAshFrLgOgNLS0u5wX1xzhcTcAihds3rDDSbzUhbs9nMBY5zBhdKqcQOpBOlFFQqlUhbpVJJXQVxDXMGcIzxVR7w8BxfXFxokzBLhJDLBLzT6ZyMAvZ9H5ydnSWSMMsAhFC1220/AXcc54YQcp4F1yVXPAmz4ISQ8+GCg8Q6v3HOP+kCT09PB7B4cvUNUEoH43TCGO8P3/8/e7vjOHcY44+TBgMAAEJoN15aaf8CR0dHPyb5WzUM43hzc/NNwpBucBAEW4SQXxMC/wyCYEvXpy2jPM+TKysr+6VSyRJCLI8BPg6C4HVaDZerepVSfhZCPM0LzVu93ruf1+v1gzAMqWmae1n7AIRQEULOTdPcC8OQ3gcG4IEnlmKxaHPOFyGESCklCSGX7XbbH/XE8nj1FxvKd+/WNl3DAAAAAElFTkSuQmCC);
  477. background-position: center;
  478. background-size: 34rpx auto;
  479. background-repeat: no-repeat;
  480. }
  481. }
  482. }
  483. .footer-submit-box {
  484. .footer-submit {
  485. border-radius: 14rpx;
  486. background: linear-gradient(180deg, #FD8F7C -75%, #FE534B 140%);
  487. width: 580rpx;
  488. height: 80rpx;
  489. font-family: PingFang SC;
  490. font-size: 30rpx;
  491. font-weight: 500;
  492. line-height: 42rpx;
  493. letter-spacing: normal;
  494. color: #FFFFFF;
  495. }
  496. }
  497. }
  498. .footer-text {
  499. font-size: 30rpx;
  500. font-weight: 500;
  501. line-height: 30rpx;
  502. color: rgba(0, 0, 0, 0.9);
  503. }
  504. </style>