|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+import { Injectable } from '@angular/core';
|
|
|
2
|
+import { HttpClient } from '@angular/common/http';
|
|
|
3
|
+import { Observable, of, catchError, map } from 'rxjs';
|
|
|
4
|
+import { TreeNode } from '../models/tree-node.model';
|
|
|
5
|
+import { ConfigService } from 'base-core';
|
|
|
6
|
+
|
|
|
7
|
+@Injectable({
|
|
|
8
|
+ providedIn: 'root'
|
|
|
9
|
+})
|
|
|
10
|
+export class TreeService {
|
|
|
11
|
+ constructor(
|
|
|
12
|
+ private http: HttpClient,
|
|
|
13
|
+ private config: ConfigService
|
|
|
14
|
+ ) {}
|
|
|
15
|
+
|
|
|
16
|
+ getTree(): Observable<TreeNode[]> {
|
|
|
17
|
+ console.log('TreeService.getTree() 调用');
|
|
|
18
|
+ console.log('config.useMockData:', this.config.useMockData);
|
|
|
19
|
+ console.log('config.apiBaseUrl:', this.config.apiBaseUrl);
|
|
|
20
|
+
|
|
|
21
|
+ if (this.config.useMockData) {
|
|
|
22
|
+ console.log('使用模拟树数据');
|
|
|
23
|
+ const mockTree = this.getMockTree();
|
|
|
24
|
+ console.log('模拟数据节点数:', mockTree.length);
|
|
|
25
|
+ // 调试:打印第一个节点的图标信息
|
|
|
26
|
+ if (mockTree.length > 0 && mockTree[0].children) {
|
|
|
27
|
+ console.log('[模拟数据]第一个分组节点图标:', mockTree[0].icon);
|
|
|
28
|
+ console.log('[模拟数据]第一个分组子节点图标:', mockTree[0].children.map(c => ({name: c.name, icon: c.icon})));
|
|
|
29
|
+ }
|
|
|
30
|
+ console.log('模拟数据结构:', JSON.stringify(mockTree, null, 2));
|
|
|
31
|
+ return of(mockTree);
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ const apiUrl = `${this.config.apiBaseUrl}/projects/tree`;
|
|
|
35
|
+ console.log('请求树数据:', apiUrl);
|
|
|
36
|
+
|
|
|
37
|
+ return this.http.get<any>(apiUrl).pipe(
|
|
|
38
|
+ map(response => {
|
|
|
39
|
+ console.log('树数据响应:', response);
|
|
|
40
|
+ // 后端返回 {success: true, data: [...]}
|
|
|
41
|
+ if (response && response.success && response.data) {
|
|
|
42
|
+ console.log('提取data字段,节点数:', response.data.length);
|
|
|
43
|
+ // 调试:打印第一个节点的图标信息
|
|
|
44
|
+ if (response.data.length > 0 && response.data[0].children) {
|
|
|
45
|
+ console.log('第一个分组节点图标:', response.data[0].icon);
|
|
|
46
|
+ console.log('第一个分组子节点图标:', response.data[0].children.map((c: any) => ({name: c.name, icon: c.icon})));
|
|
|
47
|
+ }
|
|
|
48
|
+ return response.data;
|
|
|
49
|
+ } else {
|
|
|
50
|
+ console.warn('响应格式不符合预期,返回空数组');
|
|
|
51
|
+ return [];
|
|
|
52
|
+ }
|
|
|
53
|
+ }),
|
|
|
54
|
+ catchError((error) => {
|
|
|
55
|
+ console.error('获取树数据失败:', error);
|
|
|
56
|
+ console.error('错误详情:', error.status, error.message, error.url);
|
|
|
57
|
+ // 认证失败或其他错误时返回空数组,不显示模拟数据
|
|
|
58
|
+ console.warn('API请求失败,回退到模拟数据');
|
|
|
59
|
+ const mockTree = this.getMockTree();
|
|
|
60
|
+ console.log('回退模拟数据节点数:', mockTree.length);
|
|
|
61
|
+ return of(mockTree);
|
|
|
62
|
+ })
|
|
|
63
|
+ );
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ private getMockTree(): TreeNode[] {
|
|
|
67
|
+ // 模拟树数据,基于Go后端返回的结构
|
|
|
68
|
+ return [
|
|
|
69
|
+ {
|
|
|
70
|
+ id: 'home',
|
|
|
71
|
+ name: '首页',
|
|
|
72
|
+ icon: 'home',
|
|
|
73
|
+ type: 'group',
|
|
|
74
|
+ children: [
|
|
|
75
|
+ {
|
|
|
76
|
+ id: 'readme',
|
|
|
77
|
+ name: '说明',
|
|
|
78
|
+ icon: 'description',
|
|
|
79
|
+ type: 'page',
|
|
|
80
|
+ route: '/home/readme'
|
|
|
81
|
+ }
|
|
|
82
|
+ ]
|
|
|
83
|
+ },
|
|
|
84
|
+ {
|
|
|
85
|
+ id: 'service-group',
|
|
|
86
|
+ name: '服务',
|
|
|
87
|
+ icon: 'settings',
|
|
|
88
|
+ type: 'group',
|
|
|
89
|
+ children: [
|
|
|
90
|
+ {
|
|
|
91
|
+ id: 'service-register',
|
|
|
92
|
+ name: '注册服务配置',
|
|
|
93
|
+ icon: 'app_registration',
|
|
|
94
|
+ type: 'page',
|
|
|
95
|
+ route: '/service/register-config'
|
|
|
96
|
+ },
|
|
|
97
|
+ {
|
|
|
98
|
+ id: 'service-management',
|
|
|
99
|
+ name: '微服务管理',
|
|
|
100
|
+ icon: 'dns',
|
|
|
101
|
+ type: 'page',
|
|
|
102
|
+ route: '/service/management'
|
|
|
103
|
+ },
|
|
|
104
|
+ {
|
|
|
105
|
+ id: 'service-config',
|
|
|
106
|
+ name: '微服务配置管理',
|
|
|
107
|
+ icon: 'settings_applications',
|
|
|
108
|
+ type: 'page',
|
|
|
109
|
+ route: '/service/config-management'
|
|
|
110
|
+ },
|
|
|
111
|
+ {
|
|
|
112
|
+ id: 'boot-config',
|
|
|
113
|
+ name: '微服务启动配置管理',
|
|
|
114
|
+ icon: 'play_circle',
|
|
|
115
|
+ type: 'page',
|
|
|
116
|
+ route: '/service/boot-config'
|
|
|
117
|
+ }
|
|
|
118
|
+ ]
|
|
|
119
|
+ },
|
|
|
120
|
+ {
|
|
|
121
|
+ id: 'user-group',
|
|
|
122
|
+ name: '项目',
|
|
|
123
|
+ icon: 'folder',
|
|
|
124
|
+ type: 'group',
|
|
|
125
|
+ children: [
|
|
|
126
|
+ {
|
|
|
127
|
+ id: 'project-management',
|
|
|
128
|
+ name: '项目管理',
|
|
|
129
|
+ icon: 'folder_open',
|
|
|
130
|
+ type: 'page',
|
|
|
131
|
+ route: '/project/list'
|
|
|
132
|
+ },
|
|
|
133
|
+ {
|
|
|
134
|
+ id: 'agent-management',
|
|
|
135
|
+ name: 'Agent管理',
|
|
|
136
|
+ icon: 'smart_toy',
|
|
|
137
|
+ type: 'page',
|
|
|
138
|
+ route: '/agent/list'
|
|
|
139
|
+ },
|
|
|
140
|
+ {
|
|
|
141
|
+ id: 'skill-management',
|
|
|
142
|
+ name: 'Skill管理',
|
|
|
143
|
+ icon: 'build',
|
|
|
144
|
+ type: 'page',
|
|
|
145
|
+ route: '/skill/list'
|
|
|
146
|
+ }
|
|
|
147
|
+ ]
|
|
|
148
|
+ },
|
|
|
149
|
+ {
|
|
|
150
|
+ id: 'tenant-group',
|
|
|
151
|
+ name: '租户管理',
|
|
|
152
|
+ icon: 'apartment',
|
|
|
153
|
+ type: 'group',
|
|
|
154
|
+ children: [
|
|
|
155
|
+ {
|
|
|
156
|
+ id: 'tenant-management',
|
|
|
157
|
+ name: '租户管理',
|
|
|
158
|
+ icon: 'apartment',
|
|
|
159
|
+ type: 'page',
|
|
|
160
|
+ route: '/tenant/list'
|
|
|
161
|
+ },
|
|
|
162
|
+ {
|
|
|
163
|
+ id: 'role-management',
|
|
|
164
|
+ name: '角色管理',
|
|
|
165
|
+ icon: 'admin_panel_settings',
|
|
|
166
|
+ type: 'page',
|
|
|
167
|
+ route: '/role/list'
|
|
|
168
|
+ },
|
|
|
169
|
+ {
|
|
|
170
|
+ id: 'user-management',
|
|
|
171
|
+ name: '用户管理',
|
|
|
172
|
+ icon: 'people',
|
|
|
173
|
+ type: 'page',
|
|
|
174
|
+ route: '/user/list'
|
|
|
175
|
+ }
|
|
|
176
|
+ ]
|
|
|
177
|
+ }
|
|
|
178
|
+ ];
|
|
|
179
|
+ }
|
|
|
180
|
+}
|