mallMenu.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <view class="u-wrap">
  3. <view class="u-menu-wrap">
  4. <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop"
  5. :scroll-into-view="itemId">
  6. <view v-for="(item, index) in tabbar" :key="index" class="u-tab-item"
  7. :class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
  8. <text class="u-line-1">{{ item.businessName }}</text>
  9. </view>
  10. </scroll-view>
  11. <scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box"
  12. @scroll="rightScroll">
  13. <view class="page-view">
  14. <view class="class-item" :id="'item' + index" v-for="(item, index) in tabbar" :key="index">
  15. <view class="item-title">
  16. <text>{{ item.businessName }}</text>
  17. </view>
  18. <view class="item-container">
  19. <view class="thumb-box"
  20. v-for="(item1, index1) in (item.children?item.children:[item])"
  21. :key="index1" @click="clickMenu(item,item1)">
  22. <image class="item-menu-image" :src="item1.businessIcon"></image>
  23. <view class="item-menu-name">{{ item1.businessName }}</view>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </scroll-view>
  29. </view>
  30. <CustomTabBar page="class"/>
  31. </view>
  32. </template>
  33. <script>
  34. import { getTreeList } from '@/api/volunteer'
  35. import { volunteerSeachgetTreeList } from "@/api/volunteerDetailsApi/details.js"
  36. import { getVolunteerInfo } from '@/api/volunteer.js'
  37. import CustomTabBar from '@/components/CustomTabBar/index.vue'
  38. export default {
  39. components:{CustomTabBar},
  40. data() {
  41. return {
  42. scrollTop: 0, //tab标题的滚动条位置
  43. oldScrollTop: 0,
  44. current: 0, // 预设当前项的值
  45. menuHeight: 0, // 左边菜单的高度
  46. menuItemHeight: 0, // 左边菜单item的高度
  47. itemId: '', // 栏目右边scroll-view用于滚动的id
  48. tabbar: [],
  49. arr: [],
  50. scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
  51. timer: null, // 定时器
  52. userType: uni.getStorageSync('userType')
  53. }
  54. },
  55. onReady() {
  56. },
  57. onShow() {
  58. this.getData();
  59. this.getMenuItemTop()
  60. },
  61. methods: {
  62. getData() {
  63. getTreeList({ parentId: '0' }).then(res => {
  64. this.tabbar = res.data;
  65. })
  66. },
  67. // 点击左边的栏目切换
  68. async swichMenu(index) {
  69. if (this.arr.length == 0) {
  70. await this.getMenuItemTop();
  71. }
  72. if (index == this.current) return;
  73. this.scrollRightTop = this.oldScrollTop;
  74. this.$nextTick(() => {
  75. this.scrollRightTop = this.arr[index];
  76. this.current = index;
  77. this.leftMenuStatus(index);
  78. })
  79. },
  80. // 获取一个目标元素的高度
  81. getElRect(elClass, dataVal) {
  82. new Promise((resolve, reject) => {
  83. const query = uni.createSelectorQuery().in(this);
  84. query.select('.' + elClass).fields({
  85. size: true
  86. }, res => {
  87. // 如果节点尚未生成,res值为null,循环调用执行
  88. if (!res) {
  89. setTimeout(() => {
  90. this.getElRect(elClass);
  91. }, 10);
  92. return;
  93. }
  94. this[dataVal] = res.height;
  95. resolve();
  96. }).exec();
  97. })
  98. },
  99. // 观测元素相交状态
  100. async observer() {
  101. this.tabbar.map((val, index) => {
  102. let observer = uni.createIntersectionObserver(this);
  103. // 检测右边scroll-view的id为itemxx的元素与right-box的相交状态
  104. // 如果跟.right-box底部相交,就动态设置左边栏目的活动状态
  105. observer.relativeTo('.right-box', {
  106. top: 0
  107. }).observe('#item' + index, res => {
  108. if (res.intersectionRatio > 0) {
  109. let id = res.id.substring(4);
  110. this.leftMenuStatus(id);
  111. }
  112. })
  113. })
  114. },
  115. // 设置左边菜单的滚动状态
  116. async leftMenuStatus(index) {
  117. this.current = index;
  118. // 如果为0,意味着尚未初始化
  119. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  120. await this.getElRect('menu-scroll-view', 'menuHeight');
  121. await this.getElRect('u-tab-item', 'menuItemHeight');
  122. }
  123. // 将菜单活动item垂直居中
  124. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  125. },
  126. // 获取右边菜单每个item到顶部的距离
  127. getMenuItemTop() {
  128. new Promise(resolve => {
  129. let selectorQuery = uni.createSelectorQuery();
  130. selectorQuery.selectAll('.class-item').boundingClientRect((rects) => {
  131. // 如果节点尚未生成,rects值为[](因为用selectAll,所以返回的是数组),循环调用执行
  132. if (!rects.length) {
  133. setTimeout(() => {
  134. this.getMenuItemTop();
  135. }, 10);
  136. return;
  137. }
  138. rects.forEach((rect) => {
  139. // 这里减去rects[0].top,是因为第一项顶部可能不是贴到导航栏(比如有个搜索框的情况)
  140. this.arr.push(rect.top - rects[0].top);
  141. resolve();
  142. })
  143. }).exec()
  144. })
  145. },
  146. // 右边菜单滚动
  147. async rightScroll(e) {
  148. this.oldScrollTop = e.detail.scrollTop;
  149. if (this.arr.length == 0) {
  150. await this.getMenuItemTop();
  151. }
  152. if (this.timer) return;
  153. if (!this.menuHeight) {
  154. await this.getElRect('menu-scroll-view', 'menuHeight');
  155. }
  156. setTimeout(() => { // 节流
  157. this.timer = null;
  158. // scrollHeight为右边菜单垂直中点位置
  159. let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
  160. for (let i = 0; i < this.arr.length; i++) {
  161. let height1 = this.arr[i];
  162. let height2 = this.arr[i + 1];
  163. // 如果不存在height2,意味着数据循环已经到了最后一个,设置左边菜单为最后一项即可
  164. if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
  165. this.leftMenuStatus(i);
  166. return;
  167. }
  168. }
  169. }, 10)
  170. },
  171. async clickMenu(service,record) {
  172. console.log(service,record);
  173. const { id } = service;
  174. const key = id;
  175. if (this.userType == '2') {
  176. const res = await getVolunteerInfo({
  177. serviceCategory: key
  178. });
  179. const parmas = { ...service, key,name:service.businessName,record };
  180. if (res.code === 200 && res.data) {
  181. //已有注册,跳转详情页面
  182. uni.navigateTo({
  183. url: `/pages_home/pages/details/index?data=${encodeURIComponent(JSON.stringify(parmas))}`
  184. })
  185. return
  186. }
  187. [1, 2] ? uni.navigateTo({
  188. url: `/pages_home/pages/register/index?data=${encodeURIComponent(JSON.stringify(parmas))}`
  189. }) : uni.showToast({
  190. title: '敬请期待',
  191. icon: 'none'
  192. })
  193. return;
  194. }
  195. // 动态获取 parentId
  196. const params = {
  197. parentId: key
  198. }
  199. const res = await volunteerSeachgetTreeList(params)
  200. // 只有第一条和第二条可以跳转
  201. if (key === '1' || key === '2') {
  202. uni.navigateTo({
  203. url: `/pages_home/pages/client/details?dataList=${encodeURIComponent(JSON.stringify(res.data))}&id=${record.id}`
  204. });
  205. } else {
  206. // 其他条目提示“敬请期待”
  207. uni.showToast({
  208. title: '敬请期待',
  209. icon: 'none'
  210. });
  211. }
  212. }
  213. }
  214. }
  215. </script>
  216. <style lang="scss" scoped>
  217. .u-wrap {
  218. display: flex;
  219. flex-direction: column;
  220. position: fixed;
  221. top: 0px;
  222. left: 0px;
  223. right: 0px;
  224. bottom: 150rpx;
  225. }
  226. .u-search-box {
  227. padding: 18rpx 30rpx;
  228. }
  229. .u-menu-wrap {
  230. flex: 1;
  231. display: flex;
  232. overflow: hidden;
  233. }
  234. .u-search-inner {
  235. background-color: rgb(234, 234, 234);
  236. border-radius: 100rpx;
  237. display: flex;
  238. align-items: center;
  239. padding: 10rpx 16rpx;
  240. }
  241. .u-search-text {
  242. font-size: 26rpx;
  243. color: $u-tips-color;
  244. margin-left: 10rpx;
  245. }
  246. .u-tab-view {
  247. width: 200rpx;
  248. height: 100%;
  249. }
  250. .u-tab-item {
  251. height: 110rpx;
  252. background: #f6f6f6;
  253. box-sizing: border-box;
  254. display: flex;
  255. align-items: center;
  256. justify-content: center;
  257. font-size: 26rpx;
  258. color: #444;
  259. font-weight: 400;
  260. line-height: 1;
  261. }
  262. .u-tab-item-active {
  263. position: relative;
  264. color: rgba(221, 94, 69, 1);
  265. font-size: 30rpx;
  266. font-weight: 600;
  267. background: #fff;
  268. }
  269. .u-tab-item-active::before {
  270. border-left: 4px solid rgba(221, 94, 69, 1) !important;
  271. }
  272. .u-tab-item-active::before {
  273. content: "";
  274. position: absolute;
  275. border-left: 4px solid $u-primary;
  276. height: 32rpx;
  277. left: 0;
  278. top: 39rpx;
  279. }
  280. .u-tab-view {
  281. height: 100%;
  282. }
  283. .right-box {
  284. background-color: rgb(250, 250, 250);
  285. }
  286. .page-view {
  287. padding: 16rpx;
  288. }
  289. .class-item {
  290. margin-bottom: 30rpx;
  291. background-color: #fff;
  292. padding: 16rpx;
  293. border-radius: 8rpx;
  294. }
  295. .class-item:last-child {
  296. min-height: 100vh;
  297. }
  298. .item-title {
  299. font-size: 26rpx;
  300. color: $u-main-color;
  301. font-weight: bold;
  302. text-align: center;
  303. }
  304. .item-menu-name {
  305. font-weight: normal;
  306. font-size: 28rpx;
  307. color: $u-main-color;
  308. }
  309. .item-container {
  310. // display: flex;
  311. // flex-wrap: wrap;
  312. display: grid;
  313. grid-template-columns: repeat(3, 1fr);
  314. gap: 12rpx;
  315. margin: 24rpx 0;
  316. }
  317. .thumb-box {
  318. // width: 33.333333%;
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. flex-direction: column;
  323. // margin-top: 20rpx;
  324. // background: #f5f5f5;
  325. padding: 12rpx 0;
  326. }
  327. .item-menu-image {
  328. width: 120rpx;
  329. height: 120rpx;
  330. margin-bottom: 12rpx;
  331. }
  332. </style>