| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- import { Observable, of, catchError, map } from 'rxjs';
- import { TreeNode } from '../models/tree-node.model';
- import { ConfigService } from './config.service';
-
- @Injectable({
- providedIn: 'root'
- })
- export class TreeService {
- constructor(
- private http: HttpClient,
- private config: ConfigService
- ) {}
-
- getTree(): Observable<TreeNode[]> {
- console.log('TreeService.getTree() 调用');
- console.log('config.useMockData:', this.config.useMockData);
- console.log('config.apiBaseUrl:', this.config.apiBaseUrl);
-
- if (this.config.useMockData) {
- console.log('使用模拟树数据');
- const mockTree = this.getMockTree();
- console.log('模拟数据节点数:', mockTree.length);
- // 调试:打印第一个节点的图标信息
- if (mockTree.length > 0 && mockTree[0].children) {
- console.log('[模拟数据]第一个分组节点图标:', mockTree[0].icon);
- console.log('[模拟数据]第一个分组子节点图标:', mockTree[0].children.map(c => ({name: c.name, icon: c.icon})));
- }
- console.log('模拟数据结构:', JSON.stringify(mockTree, null, 2));
- return of(mockTree);
- }
-
- const apiUrl = `${this.config.apiBaseUrl}/projects/tree`;
- console.log('请求树数据:', apiUrl);
-
- return this.http.get<any>(apiUrl).pipe(
- map(response => {
- console.log('树数据响应:', response);
- // 后端返回 {success: true, data: [...]}
- if (response && response.success && response.data) {
- console.log('提取data字段,节点数:', response.data.length);
- // 调试:打印第一个节点的图标信息
- if (response.data.length > 0 && response.data[0].children) {
- console.log('第一个分组节点图标:', response.data[0].icon);
- console.log('第一个分组子节点图标:', response.data[0].children.map((c: any) => ({name: c.name, icon: c.icon})));
- }
- return response.data;
- } else {
- console.warn('响应格式不符合预期,返回空数组');
- return [];
- }
- }),
- catchError((error) => {
- console.error('获取树数据失败:', error);
- console.error('错误详情:', error.status, error.message, error.url);
- // 认证失败或其他错误时返回空数组,不显示模拟数据
- console.warn('API请求失败,回退到模拟数据');
- const mockTree = this.getMockTree();
- console.log('回退模拟数据节点数:', mockTree.length);
- return of(mockTree);
- })
- );
- }
-
- private getMockTree(): TreeNode[] {
- // 模拟树数据,基于Go后端返回的结构
- return [
- {
- id: 'home',
- name: '首页',
- icon: 'home',
- type: 'group',
- children: [
- {
- id: 'readme',
- name: '说明',
- icon: 'description',
- type: 'page',
- route: '/home/readme'
- }
- ]
- },
- {
- id: 'service-group',
- name: '服务',
- icon: 'settings',
- type: 'group',
- children: [
- {
- id: 'service-register',
- name: '注册服务配置',
- icon: 'app_registration',
- type: 'page',
- route: '/service/register-config'
- },
- {
- id: 'service-management',
- name: '微服务管理',
- icon: 'dns',
- type: 'page',
- route: '/service/management'
- },
- {
- id: 'service-config',
- name: '微服务配置管理',
- icon: 'settings_applications',
- type: 'page',
- route: '/service/config-management'
- },
- {
- id: 'boot-config',
- name: '微服务启动配置管理',
- icon: 'play_circle',
- type: 'page',
- route: '/service/boot-config'
- }
- ]
- },
- {
- id: 'user-group',
- name: '项目',
- icon: 'folder',
- type: 'group',
- children: [
- {
- id: 'project-management',
- name: '项目管理',
- icon: 'folder_open',
- type: 'page',
- route: '/project/list'
- },
- {
- id: 'agent-management',
- name: 'Agent管理',
- icon: 'smart_toy',
- type: 'page',
- route: '/agent/list'
- },
- {
- id: 'skill-management',
- name: 'Skill管理',
- icon: 'build',
- type: 'page',
- route: '/skill/list'
- }
- ]
- },
- {
- id: 'tenant-group',
- name: '租户管理',
- icon: 'apartment',
- type: 'group',
- children: [
- {
- id: 'tenant-management',
- name: '租户管理',
- icon: 'apartment',
- type: 'page',
- route: '/tenant/list'
- },
- {
- id: 'role-management',
- name: '角色管理',
- icon: 'admin_panel_settings',
- type: 'page',
- route: '/role/list'
- },
- {
- id: 'user-management',
- name: '用户管理',
- icon: 'people',
- type: 'page',
- route: '/user/list'
- }
- ]
- }
- ];
- }
- }
|