chengjie 5 månader sedan
förälder
incheckning
c70a915b54
3 ändrade filer med 57 tillägg och 51 borttagningar
  1. 16 46
      public/mg/kylx365_db_admin.html
  2. 1 1
      src/api/web/routes.js
  3. 40 4
      src/api/web/webController.js

+ 16 - 46
public/mg/kylx365_db_admin.html

@@ -618,13 +618,8 @@
618 618
                                 </table>
619 619
                                 <div v-if="queryResults.length === 0" class="no-data">
620 620
                                     没有可显示的数据
621
-                                    <div v-if="allResults.length > 0" style="margin-top: 10px;">
622
-                                        <button @click="forceRender" class="btn btn-default" style="padding: 5px 10px; font-size: 12px;">
623
-                                            强制刷新显示
624
-                                        </button>
625
-                                        <div style="font-size: 12px; color: #999; margin-top: 5px;">
626
-                                            检测到 {{allResults.length}} 条数据但未显示
627
-                                        </div>
621
+                                    <div v-if="allResults.length > 0" style="margin-top: 10px; font-size: 12px; color: #999;">
622
+                                        共 {{allResults.length}} 条数据
628 623
                                     </div>
629 624
                                 </div>
630 625
                             </template>
@@ -1139,16 +1134,18 @@
1139 1134
                     console.log('查询状态: 开始执行,设置isQueryLoading为true');
1140 1135
 
1141 1136
                     // 调用后端API执行SQL查询,使用RunKylx365DBSql接口
1142
-                    // 使用encodeURIComponent确保中文字符被正确编码
1143
-                    const encodedSql = encodeURIComponent(this.sqlQuery.trim());
1144
-                    const apiUrl = `/api/RunKylx365DBSql?sql=${encodedSql}`;
1137
+                    const apiUrl = '/api/RunKylx365DBSql';
1145 1138
                     console.log('API URL:', apiUrl);
1146 1139
 
1147 1140
                     fetch(apiUrl, {
1148
-                        method: 'GET',
1141
+                        method: 'POST',
1149 1142
                         headers: {
1143
+                            'Content-Type': 'application/json',
1150 1144
                             'Accept': 'application/json'
1151
-                        }
1145
+                        },
1146
+                        body: JSON.stringify({
1147
+                            sql: this.sqlQuery.trim()
1148
+                        })
1152 1149
                     })
1153 1150
                         .then(response => {
1154 1151
                             console.log('API响应状态:', response.status);
@@ -1280,30 +1277,8 @@
1280 1277
                     try {
1281 1278
                         console.log('原始结果数据:', results);
1282 1279
                         
1283
-                        // 格式化数据,特别是日期字段
1284
-                        const formattedResults = results.map(item => {
1285
-                            const newItem = {};
1286
-                            // 复制所有可枚举属性,确保是普通对象
1287
-                            for (const key in item) {
1288
-                                if (Object.prototype.hasOwnProperty.call(item, key) && !key.startsWith('__')) {
1289
-                                    newItem[key] = item[key];
1290
-                                }
1291
-                            }
1292
-                            
1293
-                            // 格式化日期字段
1294
-                            if (newItem.CreateTime) {
1295
-                                try {
1296
-                                    const date = new Date(newItem.CreateTime);
1297
-                                    if (!isNaN(date.getTime())) {
1298
-                                        newItem.CreateTime = date.toLocaleString();
1299
-                                    }
1300
-                                } catch (e) {
1301
-                                    console.warn('日期格式化失败:', e);
1302
-                                    newItem.CreateTime = String(newItem.CreateTime);
1303
-                                }
1304
-                            }
1305
-                            return newItem;
1306
-                        });
1280
+                        // 直接使用原始结果数据
1281
+                        const formattedResults = results;
1307 1282
 
1308 1283
                         console.log('格式化后的结果:', formattedResults);
1309 1284
                         
@@ -1344,8 +1319,10 @@
1344 1319
                         console.log('计算的总页数:', totalPages);
1345 1320
                         this.$set(this, 'totalPages', totalPages);
1346 1321
 
1347
-                        // 显示第一页数据
1348
-                        this.changePage(1);
1322
+                        // 直接设置第一页数据
1323
+                        const startIndex = 0;
1324
+                        const endIndex = Math.min(this.pageSize, formattedResults.length);
1325
+                        this.$set(this, 'queryResults', formattedResults.slice(startIndex, endIndex));
1349 1326
 
1350 1327
                         // 添加调试信息
1351 1328
                         console.log('处理后的最终数据状态:');
@@ -1354,13 +1331,6 @@
1354 1331
                         console.log('queryResults:', JSON.parse(JSON.stringify(this.queryResults)));
1355 1332
                         console.log('Vue实例数据:', this.$data);
1356 1333
 
1357
-                        // 强制更新视图
1358
-                        this.$nextTick(() => {
1359
-                            console.log('视图更新后检查:');
1360
-                            console.log('表格DOM:', document.querySelector('.data-table'));
1361
-                            console.log('表格行数:', document.querySelectorAll('.data-table tbody tr').length);
1362
-                        });
1363
-
1364 1334
                         this.showToastMessage(`查询成功,返回 ${formattedResults.length} 条记录`, 'success');
1365 1335
                     } catch (error) {
1366 1336
                         console.error('处理查询结果时出错:', error);
@@ -1465,7 +1435,7 @@
1465 1435
                     if (typeof value === 'boolean') {
1466 1436
                         return value ? '是' : '否';
1467 1437
                     }
1468
-                    return String(value);
1438
+                    return value;
1469 1439
                 },
1470 1440
 
1471 1441
                 // 显示提示消息

+ 1 - 1
src/api/web/routes.js

@@ -8,6 +8,6 @@ const router = new Router();
8 8
 router.get('/kylx365_db_admin',webController.Kylx365DBAdmin);
9 9
 router.get('/api/GetKylx365Tables',webController.GetKylx365Tables);
10 10
 router.get('/api/GetKylx365TableColumnByTable',webController.GetKylx365TableColumnByTable);
11
-router.get('/api/RunKylx365DBSql',webController.RunKylx365DBSql);
11
+router.post('/api/RunKylx365DBSql',webController.RunKylx365DBSql);
12 12
 
13 13
 export default router;

+ 40 - 4
src/api/web/webController.js

@@ -34,11 +34,47 @@ export async function GetKylx365TableColumnByTable(ctx) {
34 34
     ctx.body = {"errcode": 10000, result: result};
35 35
 };
36 36
         
37
+/**
38
+ * 执行kylx365数据库SQL查询
39
+ * @param {Object} ctx - Koa上下文对象
40
+ * @description 只允许执行SELECT查询语句,并对结果中的时间字段格式化和长文本截断
41
+ */
37 42
 export async function RunKylx365DBSql(ctx) {
38 43
     let param = {
39
-        SQL: ctx.query.sql || "",
44
+        SQL: ctx.request.body.sql || "",
40 45
     };
41
-    let result = await commonModel.RunSql(null,param.SQL);
42
-    console.log("记录数:"+result.length);
46
+    
47
+    // 只允许执行SELECT查询
48
+    if (!param.SQL.trim().toUpperCase().startsWith('SELECT')) {
49
+        ctx.body = {"errcode": 10001, errmsg: "只允许执行SELECT查询语句"};
50
+        return;
51
+    }
52
+
53
+    let result = await commonModel.RunSql(null, param.SQL);
54
+    console.log("记录数:" + result.length);
55
+    
56
+    // 处理查询结果
57
+    if (result && result.length > 0) {
58
+        result = result.map(item => {
59
+            const processedItem = {};
60
+            for (const [key, value] of Object.entries(item)) {
61
+                // 处理时间字段 - 仅处理Date对象或字段名包含日期时间关键词的
62
+                const isDateTimeField = key.toLowerCase().includes('time') || 
63
+                                      key.toLowerCase().includes('date') ||
64
+                                      key.toLowerCase().includes('timestamp');
65
+                if (value instanceof Date || (isDateTimeField && moment(value, moment.ISO_8601, true).isValid())) {
66
+                    processedItem[key] = moment(value).format('YYYY-MM-DD HH:mm:ss');
67
+                }
68
+                // 处理长文本字段
69
+                else if (typeof value === 'string' && value.length > 100) {
70
+                    processedItem[key] = value.substring(0, 100) + '...';
71
+                } else {
72
+                    processedItem[key] = value;
73
+                }
74
+            }
75
+            return processedItem;
76
+        });
77
+    }
78
+
43 79
     ctx.body = {"errcode": 10000, result: result};
44
-};
80
+};