暫無描述
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.

service-register-config.component.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { Component, AfterViewInit, inject } from '@angular/core';
  2. import { ConfigMetaService } from '../../services/config-meta.service';
  3. import { AuthService } from '../../services/auth.service';
  4. import { TabulatorMapper } from '../../utils/tabulator-mapper.util';
  5. import { ConfigService, StickyHeaderComponent, StickyHeaderButton } from 'base-core';
  6. import { CommonModule } from '@angular/common';
  7. // 声明全局类型
  8. declare var Tabulator: any;
  9. @Component({
  10. imports: [CommonModule, StickyHeaderComponent],
  11. selector: 'app-service-register-config',
  12. templateUrl: './service-register-config.component.html',
  13. styleUrl: './service-register-config.component.scss'
  14. })
  15. export class ServiceRegisterConfigComponent implements AfterViewInit {
  16. private tabulator!: any;
  17. private totalCount: number = 0;
  18. private configMetaService = inject(ConfigMetaService);
  19. private config = inject(ConfigService);
  20. private authService = inject(AuthService);
  21. title = '注册服务配置';
  22. hintText = '点击"注册"按钮将同步所有配置元信息到数据库,并显示配置列表。';
  23. headerButtons: StickyHeaderButton[] = [
  24. { title: '注册', name: 'register', icon: 'add', color: 'primary' },
  25. { title: '刷新', name: 'refresh', icon: 'refresh', color: 'accent' }
  26. ];
  27. //registering = false;
  28. // 外部查询条件预留字段(用于将来扩展)
  29. externalFilters: any = {};
  30. ngAfterViewInit(): void {
  31. console.log('ServiceRegisterConfigComponent ngAfterViewInit');
  32. console.log('Tabulator global available:', typeof Tabulator !== 'undefined');
  33. console.log('ConfigService状态:', {
  34. useMockData: this.config.useMockData,
  35. apiBaseUrl: this.config.apiBaseUrl
  36. });
  37. console.log('ConfigMetaService API URL:', this.configMetaService.getListUrl());
  38. // 使用 setTimeout 确保 DOM 已渲染
  39. setTimeout(() => {
  40. console.log('Initializing Tabulator...');
  41. this.initTabulator();
  42. }, 100);
  43. }
  44. /**
  45. * 初始化表格 - 标准远程分页版本
  46. */
  47. private initTabulator(): void {
  48. console.log('initTabulator called');
  49. console.log('Table element exists:', document.getElementById('tabulator-table'));
  50. // 获取Basic认证信息
  51. const basicAuth = this.authService.getBasicAuth();
  52. console.log('Basic认证信息:', basicAuth ? '有' : '无');
  53. this.tabulator = new Tabulator('#tabulator-table', {
  54. // 列定义 - 使用现有列配置
  55. columns: [
  56. { title: 'ID', field: 'id', sorter: 'number', headerFilter: 'input', width: 220 },
  57. { title: '配置名称', field: 'configName', sorter: 'string', headerFilter: 'input', headerFilterFunc: 'like',width: 110 },
  58. { title: '字段名', field: 'fieldName', sorter: 'string', headerFilter: 'input', headerFilterFunc: 'like' ,width: 130},
  59. { title: '字段类型', field: 'fieldType', sorter: 'string', headerFilter: 'input',width: 120, headerFilterParams: { values: ['string', 'number', 'boolean', 'array', 'object'] } },
  60. { title: '描述', field: 'fieldDesc', sorter: 'string', headerFilter: 'input', headerFilterFunc: 'like' }
  61. ],
  62. // 标准远程分页配置
  63. pagination: true,
  64. paginationMode: 'remote',
  65. paginationSize: 20,
  66. paginationSizeSelector: [10, 20, 50, 100],
  67. sortMode: 'remote',
  68. filterMode: 'remote',
  69. // 使用标准AJAX配置,通过代理调用后端API
  70. ajaxURL: '/api/config/meta/list',
  71. ajaxConfig: {
  72. method: 'POST',
  73. headers: {
  74. 'Authorization': basicAuth ? `Basic ${basicAuth}` : '',
  75. 'Content-Type': 'application/json'
  76. }
  77. },
  78. ajaxContentType: 'json',
  79. ajaxParams: function() {
  80. // 获取调用时的上下文信息
  81. const callTime = new Date().toISOString();
  82. const stack = new Error().stack; // 获取调用堆栈
  83. console.group("=== ajaxParams被调用 ===");
  84. console.log("调用时间:", callTime);
  85. console.log("当前页码:", this.getPage());
  86. console.log("筛选条件:", this.getFilters(true));
  87. console.log("排序条件:", this.getSorters());
  88. // console.log("调用来源:", stack.split('\n')[2]?.trim() || "未知");
  89. console.groupEnd();
  90. return {
  91. page: this.getPage() || 1,
  92. size: this.getPageSize() || 20,
  93. filter: this.getFilters(true) || [],
  94. sort: this.getSorters() || []
  95. };
  96. },
  97. // 参数生成器,将Tabulator参数转换为后端期望格式
  98. ajaxURLGenerator:function(url: string, config: any, params: any){
  99. //ajaxParamsGenerator: (url: string, config: any, params: any) => {
  100. console.log('=== Tabulator AJAX参数生成器被调用 ===');
  101. console.log('请求URL:', url);
  102. console.log('请求配置:', config);
  103. console.log('原始params类型:', typeof params);
  104. console.log('原始params完整结构:', JSON.stringify(params, null, 2));
  105. // 详细分析params对象
  106. if (params) {
  107. console.log('params所有键:', Object.keys(params));
  108. console.log('page:', params.page);
  109. console.log('size:', params.size);
  110. console.log('sort类型:', typeof params.sort);
  111. console.log('sort值:', params.sort);
  112. console.log('filter类型:', typeof params.filter);
  113. console.log('filter值:', params.filter);
  114. // 检查可能的别名
  115. console.log('sorters存在?:', 'sorters' in params);
  116. console.log('filters存在?:', 'filters' in params);
  117. // 检查是否有外部查询条件的预留字段
  118. console.log('当前外部查询条件预留字段:', this.externalFilters || '未定义');
  119. } else {
  120. console.log('params为undefined或null');
  121. params = {};
  122. }
  123. // 构建请求参数,使用TabulatorMapper转换为后端期望格式
  124. // Tabulator 6.x发送的是 sort/filter(单数),但后端期望 sorters/filters(复数)
  125. // const request = TabulatorMapper.buildServerRequest(
  126. // params.page || 1,
  127. //params.size || 20,
  128. //params.sorts || [], // Tabulator使用单数'sort'
  129. //params.filters || [] // Tabulator使用单数'filter'
  130. //);
  131. // console.log('生成的请求参数:', JSON.stringify(request, null, 2));
  132. const fullUrl = url + '?params=' + encodeURIComponent(JSON.stringify(params));
  133. console.log('完整URL:', fullUrl);
  134. console.log('=== 参数生成器执行结束 ===');
  135. return fullUrl;
  136. // 返回完整的请求配置对象
  137. // return {
  138. // method: 'POST', // 使用POST方法
  139. // headers: {
  140. // 'Content-Type': 'application/json',
  141. // 'Authorization': 'Basic YWRtaW46MTIz', // 你的认证信息
  142. // // 可以添加其他需要的headers
  143. // },
  144. // body: JSON.stringify({
  145. // filter: params.filter || [], // 根据后端期望的字段名调整
  146. // page: params.page || 1,
  147. // size: params.size || 20,
  148. // sort: params.sorter || [] // 根据后端期望的字段名调整
  149. // })
  150. //};
  151. },
  152. // 响应处理,将后端响应转换为Tabulator期望格式
  153. // ajaxResponse: (url: string, params: any, response: any) => {
  154. // console.log('Tabulator AJAX响应处理', { url, params, response });
  155. // // 检查响应格式
  156. // if (response && typeof response === 'object') {
  157. // // 后端返回格式:{success: true, data: [...], totalCount: N, last_page: N}
  158. // const data = response.data || [];
  159. // const totalCount = response.totalCount || 0;
  160. // const pageSize = params.size || 20;
  161. // const lastPage = response.last_page || Math.ceil(totalCount / pageSize);
  162. // return {
  163. // data: data,
  164. // last_page: lastPage
  165. // };
  166. // }
  167. // // 如果响应格式不符合预期,返回空数据
  168. // console.warn('响应格式不符合预期:', response);
  169. // return {
  170. // data: [],
  171. // last_page: 0
  172. // };
  173. // },
  174. tableAjaxError: (error: any) => {
  175. console.error('Tabulator AJAX错误:', error);
  176. if (error.status === 401 || error.status === 403) {
  177. console.warn('认证失败,跳转到登录页面');
  178. this.authService.logout();
  179. }
  180. },
  181. // 表格样式
  182. layout: 'fitColumns',
  183. responsiveLayout: 'collapse',
  184. height: '800px',
  185. // 初始加载
  186. ajaxInitialLoad: true,
  187. // 分页位置
  188. paginationCounter: 'rows',
  189. paginationButtonCount: 5
  190. });
  191. }
  192. onRegister() {
  193. console.log('注册按钮被点击');
  194. //this.registering = true;
  195. this.headerButtons[0].loading = true;
  196. this.configMetaService.initConfigMeta().subscribe({
  197. next: (result: any) => {
  198. // this.registering = false;
  199. this.headerButtons[0].loading = false;
  200. //this.errorMessage = null;
  201. if (result.success) {
  202. console.log('注册完成:', result.data);
  203. // 注册后切换到真实API数据
  204. //this.debugInfo.useMockData = false;
  205. //this.updateDataConfig();
  206. // 重新加载数据
  207. //this.loadData(1);
  208. console.log('注册成功,数据已重新加载');
  209. } else {
  210. console.error('注册失败:', result);
  211. }
  212. },
  213. error: (error) => {
  214. // this.registering = false;
  215. this.headerButtons[0].loading = false;
  216. console.error('注册请求失败:', error);
  217. }
  218. });
  219. }
  220. onHeaderButtonAction(name: string) {
  221. console.log(`头部按钮点击: ${name}`);
  222. switch (name) {
  223. case 'register':
  224. this.onRegister();
  225. break;
  226. case 'refresh':
  227. this.refresh();
  228. break;
  229. default:
  230. console.warn(`未知按钮操作: ${name}`);
  231. }
  232. }
  233. /**
  234. * 刷新表格 - 重新加载数据
  235. */
  236. refresh(): void {
  237. if (this.tabulator) {
  238. this.headerButtons[1].loading = true;
  239. this.tabulator.replaceData();
  240. // 简单延迟后清除loading状态
  241. setTimeout(() => {
  242. this.headerButtons[1].loading = false;
  243. }, 500);
  244. }
  245. }
  246. /**
  247. * 重置筛选和排序
  248. */
  249. reset(): void {
  250. if (this.tabulator) {
  251. this.tabulator.clearFilter();
  252. this.tabulator.clearSort();
  253. }
  254. }
  255. /**
  256. * 刷新表格 - 模板别名
  257. */
  258. refreshTable(): void {
  259. this.refresh();
  260. }
  261. /**
  262. * 重置表格 - 模板别名
  263. */
  264. resetTable(): void {
  265. this.reset();
  266. }
  267. /**
  268. * 导出数据
  269. */
  270. exportData(): void {
  271. if (this.tabulator) {
  272. this.tabulator.download('csv', 'config-meta-data.csv');
  273. }
  274. }
  275. }