Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree.service.ts 5.4KB

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