mallMenu.vue 11 KB

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