chengjie 5 月之前
父節點
當前提交
ee68847dc3
共有 2 個文件被更改,包括 97 次插入2 次删除
  1. 96 2
      public/mg/kylx365_db_admin.html
  2. 1 0
      src/api/web/webController.js

+ 96 - 2
public/mg/kylx365_db_admin.html

@@ -862,10 +862,104 @@
862 862
                                 throw new Error('接收到空数据');
863 863
                             }
864 864
                             
865
+                            // 尝试确定数据的实际结构
866
+                            let columnsData = null;
867
+                            
865 868
                             if (data && data.result && Array.isArray(data.result)) {
866
-                                console.log('数据格式验证通过,开始处理数据');
867
-                                this.tableColumnsList = data.result;
869
+                                console.log('标准格式: data.result 是数组');
870
+                                columnsData = data.result;
871
+                            } else if (data && Array.isArray(data)) {
872
+                                console.log('替代格式: data 本身是数组');
873
+                                columnsData = data;
874
+                            } else if (data && typeof data === 'object') {
875
+                                console.log('检查对象中的数组属性');
876
+                                // 尝试在对象中找到数组属性
877
+                                for (const key in data) {
878
+                                    if (Array.isArray(data[key])) {
879
+                                        console.log(`找到数组属性: ${key}`);
880
+                                        columnsData = data[key];
881
+                                        break;
882
+                                    }
883
+                                }
884
+                            }
885
+                            
886
+                            if (columnsData && Array.isArray(columnsData)) {
887
+                                console.log('数据验证通过,开始处理数据');
888
+                                console.log('字段数据长度:', columnsData.length);
889
+                                
890
+                                if (columnsData.length > 0) {
891
+                                    // 检查第一个元素的格式
892
+                                    const firstItem = columnsData[0];
893
+                                    console.log('第一个字段项:', firstItem);
894
+                                    
895
+                                    if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
896
+                                        // 检查并适应不同的属性名称
897
+                                        const nameKey = 'Field' in firstItem ? 'Field' :
898
+                                                      'name' in firstItem ? 'name' : 
899
+                                                      'column_name' in firstItem ? 'column_name' : 
900
+                                                      'columnName' in firstItem ? 'columnName' : 
901
+                                                      'field' in firstItem ? 'field' : null;
902
+                                        
903
+                                        const typeKey = 'Type' in firstItem ? 'Type' :
904
+                                                      'type' in firstItem ? 'type' : 
905
+                                                      'column_type' in firstItem ? 'column_type' : 
906
+                                                      'columnType' in firstItem ? 'columnType' : 
907
+                                                      'data_type' in firstItem ? 'data_type' : null;
908
+                                        
909
+                                        // 组合Default和Extra作为注释
910
+                                        const getComment = (item) => {
911
+                                            let comment = [];
912
+                                            if (item['Default']) {
913
+                                                comment.push(`默认值: ${item['Default']}`);
914
+                                            }
915
+                                            if (item['Extra'] && item['Extra'] !== '') {
916
+                                                comment.push(item['Extra']);
917
+                                            }
918
+                                            if (item['Null'] === 'NO') {
919
+                                                comment.push('不可为空');
920
+                                            }
921
+                                            return comment.join(', ');
922
+                                        };
923
+                                        
924
+                                        console.log('使用的属性名:', { nameKey, typeKey, commentKey });
925
+                                        
926
+                                        if (nameKey) {
927
+                                            // 转换为标准格式
928
+                                            this.tableColumnsList = columnsData.map(item => {
929
+                                                return {
930
+                                                    name: item[nameKey] || '',
931
+                                                    type: typeKey ? (item[typeKey] || '') : '',
932
+                                                    comment: commentKey ? (item[commentKey] || '') : ''
933
+                                                };
934
+                                            }).filter(col => col.name); // 过滤掉没有名称的列
935
+                                            
936
+                                            console.log('处理后的字段列表:', this.tableColumnsList);
937
+                                        } else {
938
+                                            console.error('无法找到字段名属性');
939
+                                            this.showToastMessage('数据格式错误:无法找到字段名属性', 'error');
940
+                                            this.tableColumnsList = [];
941
+                                        }
942
+                                    } else if (typeof firstItem === 'string') {
943
+                                        // 如果只是字段名数组
944
+                                        this.tableColumnsList = columnsData.map(name => {
945
+                                            return {
946
+                                                name: name,
947
+                                                type: '',
948
+                                                comment: ''
949
+                                            };
950
+                                        });
951
+                                        console.log('处理后的字段列表(仅名称):', this.tableColumnsList);
952
+                                    } else {
953
+                                        console.error('未知的字段数据格式');
954
+                                        this.showToastMessage('未知的字段数据格式', 'error');
955
+                                        this.tableColumnsList = [];
956
+                                    }
957
+                                } else {
958
+                                    console.log('字段列表为空');
959
+                                    this.tableColumnsList = [];
960
+                                }
868 961
                             } else {
962
+                                console.error('无法找到有效的字段数据');
869 963
                                 this.tableColumnsList = [];
870 964
                                 this.showToastMessage('获取字段列表失败', 'error');
871 965
                             }

+ 1 - 0
src/api/web/webController.js

@@ -30,6 +30,7 @@ export async function GetKylx365TableColumnByTable(ctx) {
30 30
         TableName: ctx.query.table || "",
31 31
     };
32 32
     let result = await commonModel.RunSql(null,"SHOW COLUMNS FROM `"+param.TableName+"`;");
33
+    console.log("字段数:"+result.length);
33 34
     ctx.body = {"errcode": 10000, result: result};
34 35
 };
35 36