kylx365_db_admin.html 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  6. <title>数据库管理</title>
  7. <script src="https://kylx365-1253256735.file.myqcloud.com/js/jquery-1.10.2.min.js"></script>
  8. <script src="https://kylx365-1253256735.file.myqcloud.com/js/vue.min.js"></script>
  9. <style>
  10. /* 表格项样式 */
  11. .table-item {
  12. padding: 10px;
  13. margin: 5px 0;
  14. background: white;
  15. border-radius: 4px;
  16. cursor: pointer;
  17. transition: background-color 0.2s;
  18. }
  19. .table-comment {
  20. font-size: 12px;
  21. color: #999;
  22. margin-top: 2px;
  23. }
  24. .main00 {
  25. width: 100%;
  26. height: 100vh;
  27. min-height: 600px;
  28. background: white;
  29. display: flex;
  30. flex-direction: column;
  31. overflow: hidden;
  32. }
  33. .ListTop {
  34. width: 100%;
  35. height: 60px;
  36. background: white;
  37. border-bottom: 1px solid #EEEEEE;
  38. justify-content: flex-start;
  39. flex-shrink: 0;
  40. }
  41. .ListTop3 {
  42. margin-left: 40px;
  43. height: 50px;
  44. align-items: center;
  45. }
  46. .title {
  47. font-size: 24px;
  48. color: #333333;
  49. font-weight: bold;
  50. }
  51. .main0 {
  52. width: 100%;
  53. background: white;
  54. flex: 1;
  55. min-height: 0;
  56. overflow: hidden;
  57. display: flex;
  58. position: relative;
  59. align-items: flex-start;
  60. }
  61. .tables-panel {
  62. width: 300px;
  63. border-right: 1px solid #EEEEEE;
  64. background: #F9F9F9;
  65. display: flex;
  66. flex-direction: column;
  67. height: 100%;
  68. position: relative;
  69. flex-shrink: 0;
  70. }
  71. .search-box {
  72. padding: 15px;
  73. border-bottom: 1px solid #EEEEEE;
  74. background: white;
  75. width: 100%;
  76. justify-content: center;
  77. }
  78. .search-input {
  79. width: 200px;
  80. height: 32px;
  81. padding: 0 32px 0 12px;
  82. border: 1px solid #DDDDDD;
  83. border-radius: 4px;
  84. font-size: 14px;
  85. color: #333333;
  86. }
  87. .search-input:focus {
  88. border-color: #4A90E2;
  89. outline: none;
  90. }
  91. .btn33 {
  92. width: 32px;
  93. height: 32px;
  94. margin-left: 8px;
  95. border-radius: 4px;
  96. background: #F5F5F5;
  97. cursor: pointer;
  98. justify-content: center;
  99. align-items: center;
  100. }
  101. .btn33:hover {
  102. background: #EEEEEE;
  103. }
  104. .content-panel {
  105. width: calc(100% - 300px);
  106. height: 100%;
  107. display: flex;
  108. flex-direction: column;
  109. overflow: hidden;
  110. }
  111. .sql-editor {
  112. padding: 20px;
  113. border-bottom: 1px solid #EEEEEE;
  114. background: white;
  115. height: 200px;
  116. display: flex;
  117. flex-direction: row;
  118. }
  119. .sql-editor-left {
  120. flex: 1;
  121. display: flex;
  122. flex-direction: column;
  123. margin-right: 20px;
  124. }
  125. .sql-editor-right {
  126. width: 400px;
  127. display: flex;
  128. flex-direction: column;
  129. border-left: 1px solid #EEEEEE;
  130. padding-left: 20px;
  131. position: relative;
  132. }
  133. .columns-list {
  134. flex: 1;
  135. overflow-y: auto;
  136. border: 1px solid #DDDDDD;
  137. border-radius: 4px;
  138. background: #FFFFFF;
  139. }
  140. .column-item {
  141. padding: 8px 10px;
  142. border-bottom: 1px solid #EEEEEE;
  143. cursor: pointer;
  144. display: flex;
  145. flex-direction: row;
  146. justify-content: space-between;
  147. }
  148. .column-item:hover {
  149. background-color: #F0F7FF;
  150. }
  151. .column-left {
  152. display: flex;
  153. flex-direction: column;
  154. flex: 1;
  155. }
  156. .column-right {
  157. display: flex;
  158. align-items: center;
  159. min-width: 100px;
  160. padding-left: 10px;
  161. }
  162. .column-name {
  163. font-weight: bold;
  164. }
  165. .column-type {
  166. font-size: 13px;
  167. color: #666;
  168. background-color: #f5f5f5;
  169. padding: 2px 6px;
  170. border-radius: 3px;
  171. white-space: nowrap;
  172. }
  173. .column-comment {
  174. font-size: 12px;
  175. color: #999;
  176. margin-top: 2px;
  177. }
  178. .sql-textarea {
  179. width: 100%;
  180. height: 120px;
  181. padding: 10px;
  182. border: 1px solid #DDDDDD;
  183. border-radius: 4px;
  184. font-family: monospace;
  185. font-size: 14px;
  186. resize: none;
  187. margin-bottom: 10px;
  188. }
  189. .sql-textarea:focus {
  190. border-color: #4A90E2;
  191. outline: none;
  192. }
  193. .results-panel {
  194. flex: 1;
  195. overflow: auto;
  196. padding: 20px;
  197. background: white;
  198. }
  199. .table-container {
  200. width: 100%;
  201. overflow-x: auto;
  202. }
  203. .data-table {
  204. width: 100%;
  205. border-collapse: collapse;
  206. font-size: 14px;
  207. }
  208. .data-table th {
  209. background: #F5F5F5;
  210. padding: 10px;
  211. text-align: left;
  212. border: 1px solid #DDDDDD;
  213. position: sticky;
  214. top: 0;
  215. z-index: 10;
  216. }
  217. .data-table td {
  218. padding: 8px 10px;
  219. border: 1px solid #DDDDDD;
  220. max-width: 300px;
  221. overflow: hidden;
  222. text-overflow: ellipsis;
  223. white-space: nowrap;
  224. }
  225. .data-table tr:nth-child(even) {
  226. background-color: #F9F9F9;
  227. }
  228. .data-table tr:hover {
  229. background-color: #F0F7FF;
  230. }
  231. .btn {
  232. min-width: 30px;
  233. height: 36px;
  234. padding: 0 16px;
  235. margin-left: 12px;
  236. border: none;
  237. border-radius: 4px;
  238. cursor: pointer;
  239. font-size: 14px;
  240. transition: all 0.3s;
  241. }
  242. .btn-primary {
  243. background: #4A90E2;
  244. color: white;
  245. }
  246. .btn-primary:hover {
  247. background: #357ABD;
  248. }
  249. .btn-default {
  250. background: white;
  251. border: 1px solid #DDDDDD;
  252. color: #666666;
  253. }
  254. .btn-default:hover {
  255. background: #F5F5F5;
  256. border-color: #CCCCCC;
  257. }
  258. .toast {
  259. position: fixed;
  260. top: 20px;
  261. left: 50%;
  262. transform: translateX(-50%);
  263. padding: 12px 24px;
  264. background: rgba(0, 0, 0, 0.7);
  265. color: white;
  266. border-radius: 4px;
  267. z-index: 9999;
  268. opacity: 0;
  269. transition: opacity 0.3s;
  270. }
  271. .toast.show {
  272. opacity: 1;
  273. }
  274. .toast.success {
  275. background: #4CAF50;
  276. }
  277. .toast.error {
  278. background: #F44336;
  279. }
  280. .toast.info {
  281. background: #2196F3;
  282. }
  283. /* Loading样式 */
  284. .loading-overlay {
  285. position: absolute;
  286. top: 0;
  287. left: 0;
  288. width: 100%;
  289. height: 100%;
  290. background-color: rgba(255, 255, 255, 0.7);
  291. display: flex;
  292. justify-content: center;
  293. align-items: center;
  294. z-index: 100;
  295. }
  296. .loading-spinner {
  297. width: 50px;
  298. height: 50px;
  299. border: 5px solid #f3f3f3;
  300. border-top: 5px solid #4A90E2;
  301. border-radius: 50%;
  302. animation: spin 1s linear infinite;
  303. }
  304. @keyframes spin {
  305. 0% { transform: rotate(0deg); }
  306. 100% { transform: rotate(360deg); }
  307. }
  308. .clear-btn {
  309. position: absolute;
  310. right: 12px;
  311. top: 50%;
  312. transform: translateY(-50%);
  313. width: 16px;
  314. height: 16px;
  315. background-color: #999;
  316. border-radius: 50%;
  317. display: flex;
  318. align-items: center;
  319. justify-content: center;
  320. cursor: pointer;
  321. transition: background-color 0.2s;
  322. }
  323. .clear-btn:hover {
  324. background-color: #666;
  325. }
  326. .clear-x {
  327. position: relative;
  328. width: 8px;
  329. height: 8px;
  330. }
  331. .clear-x:before,
  332. .clear-x:after {
  333. content: '';
  334. position: absolute;
  335. width: 8px;
  336. height: 2px;
  337. background-color: white;
  338. top: 3px;
  339. left: 0;
  340. }
  341. .clear-x:before {
  342. transform: rotate(45deg);
  343. }
  344. .clear-x:after {
  345. transform: rotate(-45deg);
  346. }
  347. .FlexRow {
  348. display: flex;
  349. flex-direction: row;
  350. }
  351. .FlexColumn {
  352. display: flex;
  353. flex-direction: column;
  354. }
  355. .table-list {
  356. flex: 1;
  357. overflow-y: auto;
  358. padding: 10px;
  359. }
  360. .table-item {
  361. padding: 10px;
  362. margin: 5px 0;
  363. background: white;
  364. border-radius: 4px;
  365. cursor: pointer;
  366. overflow-x: hidden;
  367. transition: background-color 0.2s;
  368. }
  369. .table-item:hover {
  370. background-color: #f0f0f0;
  371. }
  372. .table-item.active {
  373. background-color: #e6f3ff;
  374. border-left: 3px solid #4A90E2;
  375. }
  376. .btn-group {
  377. display: flex;
  378. justify-content: flex-end;
  379. margin-top: 10px;
  380. }
  381. .no-data {
  382. text-align: center;
  383. padding: 40px;
  384. color: #999;
  385. font-size: 16px;
  386. }
  387. .pagination {
  388. display: flex;
  389. justify-content: center;
  390. margin-top: 20px;
  391. padding: 10px;
  392. }
  393. .pagination-btn {
  394. padding: 5px 10px;
  395. margin: 0 5px;
  396. border: 1px solid #DDDDDD;
  397. border-radius: 4px;
  398. background: white;
  399. cursor: pointer;
  400. }
  401. .pagination-btn:hover {
  402. background: #F5F5F5;
  403. }
  404. .pagination-btn.active {
  405. background: #4A90E2;
  406. color: white;
  407. border-color: #4A90E2;
  408. }
  409. .pagination-btn.disabled {
  410. color: #CCCCCC;
  411. cursor: not-allowed;
  412. }
  413. </style>
  414. </head>
  415. <body class="container FlexRow">
  416. <div id="app" class="main00 FlexColumn">
  417. <div class="ListTop FlexRow">
  418. <div class="ListTop3 FlexRow" style="width: 100%; justify-content: space-between;">
  419. <div class="title">数据库管理</div>
  420. </div>
  421. </div>
  422. <div class="main0 FlexRow">
  423. <!-- 左侧表格列表 -->
  424. <div class="tables-panel" style="position: relative;">
  425. <div v-if="isTablesLoading" class="loading-overlay">
  426. <div class="loading-spinner"></div>
  427. </div>
  428. <div class="search-box FlexRow">
  429. <div style="position: relative;">
  430. <input type="text" class="search-input" v-model="searchText" @keyup.enter="searchTables"
  431. placeholder="搜索表格...">
  432. <div class="clear-btn" v-show="searchText" @click="clearSearch">
  433. <span class="clear-x"></span>
  434. </div>
  435. </div>
  436. <div class="btn33 FlexRow" @click="searchTables">
  437. <img title="搜索" alt="搜索"
  438. src="https://kylx365-1253256735.file.myqcloud.com/web/universalpic_search_gray_30x30.png"
  439. style="width: 20px; height: 20px;" />
  440. </div>
  441. </div>
  442. <!-- 表格列表 -->
  443. <div class="table-list">
  444. <div v-if="tables && tables.length > 0">
  445. <div v-for="(table, index) in filteredTables"
  446. :key="index"
  447. @click="selectTable(table)"
  448. :class="['table-item', { active: selectedTable === table }]">
  449. <div class="table-name">{{ table }}</div>
  450. <div class="table-comment" v-if="tableComments[table]">{{ tableComments[table] }}</div>
  451. </div>
  452. <div v-if="filteredTables.length === 0" class="no-data">
  453. 没有匹配的表格
  454. </div>
  455. </div>
  456. <div v-else-if="tables && tables.length === 0"
  457. class="no-data">
  458. 未找到表格
  459. </div>
  460. <div v-else class="no-data">
  461. 加载中...
  462. </div>
  463. </div>
  464. </div>
  465. <!-- 右侧内容区域 -->
  466. <div class="content-panel">
  467. <!-- SQL编辑器 -->
  468. <div class="sql-editor">
  469. <!-- 左侧SQL编辑区域 -->
  470. <div class="sql-editor-left">
  471. <textarea class="sql-textarea" v-model="sqlQuery" placeholder="输入SQL查询语句..."></textarea>
  472. <div class="btn-group">
  473. <select class="btn btn-default" v-model="selectedLimit" @change="updateQueryLimit" style="margin-right: 8px;">
  474. <option v-for="limit in limitOptions" :key="limit" :value="limit">
  475. LIMIT {{ limit }}
  476. </option>
  477. </select>
  478. <button type="button" class="btn btn-default" @click="clearQuery">清空</button>
  479. <button type="button" class="btn btn-primary" @click="executeQuery">执行</button>
  480. </div>
  481. </div>
  482. <!-- 右侧字段列表 -->
  483. <div class="sql-editor-right">
  484. <div v-if="isColumnsLoading" class="loading-overlay">
  485. <div class="loading-spinner"></div>
  486. </div>
  487. <div class="columns-list">
  488. <div v-if="tableColumnsList && tableColumnsList.length > 0">
  489. <div v-for="(column, index) in tableColumnsList"
  490. :key="index"
  491. class="column-item"
  492. @click="copyColumnName(column.name)"
  493. @dblclick="insertColumnName(column.name)">
  494. <div class="column-left">
  495. <div class="column-name">{{ column.name }}</div>
  496. <div class="column-comment" v-if="column.comment">{{ column.comment }}</div>
  497. </div>
  498. <div class="column-right">
  499. <div class="column-type">{{ column.type }}</div>
  500. </div>
  501. </div>
  502. </div>
  503. <div v-else class="no-data">
  504. 请选择表格查看字段列表
  505. </div>
  506. </div>
  507. </div>
  508. </div>
  509. <!-- 查询结果 -->
  510. <div class="results-panel" style="position: relative;">
  511. <div v-if="isQueryLoading" class="loading-overlay">
  512. <div class="loading-spinner"></div>
  513. </div>
  514. <div v-if="queryExecuted && allResults && allResults.length > 0" class="table-container">
  515. <div style="margin-bottom: 10px; color: #666;">
  516. 显示 {{ queryResults.length }} 条记录,共 {{ allResults.length }} 条
  517. </div>
  518. <table class="data-table">
  519. <thead>
  520. <tr>
  521. <th v-for="(column, index) in tableColumns" :key="index">{{ column }}</th>
  522. </tr>
  523. </thead>
  524. <tbody>
  525. <template v-if="queryResults.length > 0">
  526. <tr v-for="(row, rowIndex) in queryResults" :key="rowIndex">
  527. <td v-for="(column, colIndex) in tableColumns" :key="colIndex">
  528. {{ row[column] || '' }}
  529. </td>
  530. </tr>
  531. </template>
  532. <template v-else>
  533. <tr>
  534. <td :colspan="tableColumns.length" style="text-align: center; color: #999;">
  535. 数据加载中...
  536. </td>
  537. </tr>
  538. </template>
  539. </tbody>
  540. </table>
  541. <!-- 分页控件 -->
  542. <div class="pagination" v-if="totalPages > 1 && allResults.length > pageSize">
  543. <button class="pagination-btn"
  544. :class="{ disabled: currentPage === 1 }"
  545. @click="changePage(1)">
  546. 首页
  547. </button>
  548. <button class="pagination-btn"
  549. :class="{ disabled: currentPage === 1 }"
  550. @click="changePage(currentPage - 1)">
  551. 上一页
  552. </button>
  553. <button v-for="page in displayedPages"
  554. :key="page"
  555. class="pagination-btn"
  556. :class="{ active: currentPage === page }"
  557. @click="changePage(page)">
  558. {{ page }}
  559. </button>
  560. <button class="pagination-btn"
  561. :class="{ disabled: currentPage === totalPages }"
  562. @click="changePage(currentPage + 1)">
  563. 下一页
  564. </button>
  565. <button class="pagination-btn"
  566. :class="{ disabled: currentPage === totalPages }"
  567. @click="changePage(totalPages)">
  568. 末页
  569. </button>
  570. </div>
  571. </div>
  572. <div v-else-if="queryExecuted" class="no-data">
  573. 查询未返回数据
  574. </div>
  575. <div v-else class="no-data">
  576. 请选择表格并执行查询
  577. </div>
  578. </div>
  579. </div>
  580. </div>
  581. <!-- Toast提示 -->
  582. <div class="toast" :class="{ show: showToast, [toastType]: showToast }">{{ toastMessage }}</div>
  583. </div>
  584. <script>
  585. new Vue({
  586. el: '#app',
  587. data: {
  588. searchText: '',
  589. tables: [], // 存储表格名称列表
  590. tableComments: {}, // 存储表格注释,格式为 {表名: 注释}
  591. filteredTables: [],
  592. selectedTable: null,
  593. sqlQuery: '',
  594. queryResults: [],
  595. tableColumns: [],
  596. showToast: false,
  597. toastMessage: '',
  598. toastType: 'info',
  599. isTablesLoading: false,
  600. isQueryLoading: false,
  601. isColumnsLoading: false,
  602. queryExecuted: false,
  603. currentPage: 1,
  604. pageSize: 100,
  605. totalPages: 1,
  606. allResults: [],
  607. tableColumnsList: [], // 存储表格字段列表
  608. limitOptions: [10, 100, 500, 1000, 2000, 3000], // LIMIT选项
  609. selectedLimit: 100 // 默认选中的LIMIT值
  610. },
  611. computed: {
  612. displayedPages() {
  613. const pages = [];
  614. const maxVisiblePages = 5;
  615. let startPage = Math.max(1, this.currentPage - Math.floor(maxVisiblePages / 2));
  616. let endPage = Math.min(this.totalPages, startPage + maxVisiblePages - 1);
  617. if (endPage - startPage + 1 < maxVisiblePages) {
  618. startPage = Math.max(1, endPage - maxVisiblePages + 1);
  619. }
  620. for (let i = startPage; i <= endPage; i++) {
  621. pages.push(i);
  622. }
  623. return pages;
  624. }
  625. },
  626. mounted() {
  627. this.loadTables();
  628. },
  629. methods: {
  630. // 加载数据库表格列表
  631. loadTables() {
  632. this.isTablesLoading = true;
  633. this.tableComments = {};
  634. // 调用后端API获取表格列表
  635. fetch('/api/GetKylx365Tables')
  636. .then(response => {
  637. console.log('API响应状态:', response.status);
  638. if (!response.ok) {
  639. throw new Error(`HTTP error! status: ${response.status}`);
  640. }
  641. return response.json();
  642. })
  643. .then(data => {
  644. //console.log('API返回数据:', data);
  645. // 检查数据格式,处理可能的不同响应结构
  646. //console.log('检查数据格式:', data);
  647. // 尝试确定数据的实际结构
  648. let resultData = null;
  649. if (data && data.result && Array.isArray(data.result)) {
  650. //console.log('标准格式: data.result 是数组');
  651. resultData = data.result;
  652. } else if (data && Array.isArray(data)) {
  653. //console.log('替代格式: data 本身是数组');
  654. resultData = data;
  655. } else if (data && typeof data === 'object') {
  656. //console.log('检查对象中的数组属性');
  657. // 尝试在对象中找到数组属性
  658. for (const key in data) {
  659. if (Array.isArray(data[key])) {
  660. //console.log(`找到数组属性: ${key}`);
  661. resultData = data[key];
  662. break;
  663. }
  664. }
  665. }
  666. if (resultData && Array.isArray(resultData)) {
  667. //console.log('数据验证通过,开始处理数据');
  668. //console.log('数据结果长度:', resultData.length);
  669. if (resultData.length > 0 && typeof resultData[0] === 'object' && !Array.isArray(resultData[0])) {
  670. //console.log('检测到对象数组格式,第一个对象:', resultData[0]);
  671. //console.log('第一个对象的属性:', Object.keys(resultData[0]));
  672. // 检查并适应不同的属性名称
  673. const firstItem = resultData[0];
  674. const tableNameKey = 'table_name' in firstItem ? 'table_name' :
  675. 'tableName' in firstItem ? 'tableName' :
  676. 'name' in firstItem ? 'name' : null;
  677. const tableCommentKey = 'table_comment' in firstItem ? 'table_comment' :
  678. 'tableComment' in firstItem ? 'tableComment' :
  679. 'comment' in firstItem ? 'comment' : null;
  680. //console.log('使用的属性名:', { tableNameKey, tableCommentKey });
  681. if (tableNameKey) {
  682. // 如果返回的是对象数组(包含name和comment)
  683. this.tables = resultData.map(item => {
  684. const tableName = item[tableNameKey] || '';
  685. //console.log('处理表名:', tableName);
  686. return tableName;
  687. }).filter(name => name); // 过滤掉空表名
  688. //console.log('处理后的表格列表:', this.tables);
  689. // 存储表格注释
  690. if (tableCommentKey) {
  691. resultData.forEach(item => {
  692. const tableName = item[tableNameKey] || '';
  693. const tableComment = item[tableCommentKey] || '';
  694. if (tableName && tableComment) {
  695. this.tableComments[tableName] = tableComment;
  696. }
  697. });
  698. //console.log('处理后的表格注释:', this.tableComments);
  699. }
  700. } else {
  701. //console.error('无法找到表名属性');
  702. this.showToastMessage('数据格式错误:无法找到表名属性', 'error');
  703. }
  704. } else {
  705. //console.log('检测到简单数组格式');
  706. // 如果返回的只是表名数组
  707. this.tables = resultData.filter(name => {
  708. //console.log('处理表名:', name);
  709. return name && typeof name === 'string';
  710. });
  711. //console.log('处理后的表格列表:', this.tables);
  712. }
  713. this.filteredTables = [...this.tables];
  714. // console.log('更新后的过滤表格列表:', this.filteredTables);
  715. } else {
  716. this.tables = [];
  717. this.filteredTables = [];
  718. this.showToastMessage('获取表格列表失败', 'error');
  719. }
  720. this.isTablesLoading = false;
  721. })
  722. .catch(error => {
  723. //console.error('获取表格列表失败详细错误:', error);
  724. this.showToastMessage(`获取表格列表失败: ${error.message}`, 'error');
  725. this.isTablesLoading = false;
  726. });
  727. },
  728. // 搜索表格
  729. searchTables() {
  730. //console.log('开始搜索表格,搜索文本:', this.searchText);
  731. if (!this.searchText.trim()) {
  732. //console.log('搜索文本为空,显示所有表格');
  733. this.filteredTables = [...this.tables];
  734. return;
  735. }
  736. const searchTerm = this.searchText.toLowerCase();
  737. //console.log('过滤表格列表,搜索条件:', searchTerm);
  738. this.filteredTables = this.tables.filter(table => {
  739. const tableName = table.toLowerCase();
  740. const tableComment = (this.tableComments[table] || '').toLowerCase();
  741. const matches = tableName.includes(searchTerm) || tableComment.includes(searchTerm);
  742. // if (matches) {
  743. // console.log(`表格匹配: ${table} (${this.tableComments[table] || '无注释'})`);
  744. // }
  745. return matches;
  746. });
  747. //console.log('过滤后的表格数量:', this.filteredTables.length);
  748. },
  749. // 清空搜索
  750. clearSearch() {
  751. this.searchText = '';
  752. this.filteredTables = [...this.tables];
  753. },
  754. // 选择表格
  755. selectTable(table) {
  756. this.selectedTable = table;
  757. this.sqlQuery = `SELECT * FROM ${table} LIMIT 100;`;
  758. // 获取表格字段列表
  759. this.loadTableColumns(table);
  760. },
  761. // 加载表格字段列表
  762. loadTableColumns(tableName) {
  763. this.isColumnsLoading = true;
  764. this.tableColumnsList = [];
  765. // 调用API获取表格字段列表
  766. fetch(`/api/GetKylx365TableColumnByTable?table=${encodeURIComponent(tableName)}`)
  767. .then(response => response.json())
  768. .then(data => {
  769. //console.log('收到的原始数据:', data);
  770. // 验证数据格式
  771. if (!data) {
  772. //console.error('接收到空数据');
  773. throw new Error('接收到空数据');
  774. }
  775. // 尝试确定数据的实际结构
  776. let columnsData = null;
  777. if (data && data.result && Array.isArray(data.result)) {
  778. //console.log('标准格式: data.result 是数组');
  779. columnsData = data.result;
  780. } else if (data && Array.isArray(data)) {
  781. //console.log('替代格式: data 本身是数组');
  782. columnsData = data;
  783. } else if (data && typeof data === 'object') {
  784. //console.log('检查对象中的数组属性');
  785. // 尝试在对象中找到数组属性
  786. for (const key in data) {
  787. if (Array.isArray(data[key])) {
  788. //console.log(`找到数组属性: ${key}`);
  789. columnsData = data[key];
  790. break;
  791. }
  792. }
  793. }
  794. if (columnsData && Array.isArray(columnsData)) {
  795. //console.log('数据验证通过,开始处理数据');
  796. //console.log('字段数据长度:', columnsData.length);
  797. if (columnsData.length > 0) {
  798. // 检查第一个元素的格式
  799. const firstItem = columnsData[0];
  800. //console.log('第一个字段项:', firstItem);
  801. if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
  802. // 检查并适应不同的属性名称
  803. const nameKey = 'Field' in firstItem ? 'Field' :
  804. 'name' in firstItem ? 'name' :
  805. 'column_name' in firstItem ? 'column_name' :
  806. 'columnName' in firstItem ? 'columnName' :
  807. 'field' in firstItem ? 'field' : null;
  808. const typeKey = 'Type' in firstItem ? 'Type' :
  809. 'type' in firstItem ? 'type' :
  810. 'column_type' in firstItem ? 'column_type' :
  811. 'columnType' in firstItem ? 'columnType' :
  812. 'data_type' in firstItem ? 'data_type' : null;
  813. const commentKey = 'Comment' in firstItem ? 'Comment' :
  814. 'comment' in firstItem ? 'comment' :
  815. 'column_comment' in firstItem ? 'column_comment' :
  816. 'columnComment' in firstItem ? 'columnComment' : null;
  817. // 组合Default和Extra作为注释
  818. const getComment = (item) => {
  819. let comment = [];
  820. if (item['Default']) {
  821. comment.push(`默认值: ${item['Default']}`);
  822. }
  823. if (item['Extra'] && item['Extra'] !== '') {
  824. comment.push(item['Extra']);
  825. }
  826. if (item['Null'] === 'NO') {
  827. comment.push('不可为空');
  828. }
  829. return comment.join(', ');
  830. };
  831. //console.log('使用的属性名:', { nameKey, typeKey, commentKey });
  832. if (nameKey) {
  833. // 转换为标准格式
  834. this.tableColumnsList = columnsData.map(item => {
  835. return {
  836. name: item[nameKey] || '',
  837. type: typeKey ? (item[typeKey] || '') : '',
  838. comment: [
  839. commentKey ? (item[commentKey] || '') : '',
  840. getComment(item)
  841. ].filter(Boolean).join(' | ')
  842. };
  843. }).filter(col => col.name); // 过滤掉没有名称的列
  844. //console.log('处理后的字段列表:', this.tableColumnsList);
  845. } else {
  846. console.error('无法找到字段名属性');
  847. this.showToastMessage('数据格式错误:无法找到字段名属性', 'error');
  848. this.tableColumnsList = [];
  849. }
  850. } else if (typeof firstItem === 'string') {
  851. // 如果只是字段名数组
  852. this.tableColumnsList = columnsData.map(name => {
  853. return {
  854. name: name,
  855. type: '',
  856. comment: ''
  857. };
  858. });
  859. //console.log('处理后的字段列表(仅名称):', this.tableColumnsList);
  860. } else {
  861. console.error('未知的字段数据格式');
  862. this.showToastMessage('未知的字段数据格式', 'error');
  863. this.tableColumnsList = [];
  864. }
  865. } else {
  866. //console.log('字段列表为空');
  867. this.tableColumnsList = [];
  868. }
  869. } else {
  870. //console.error('无法找到有效的字段数据');
  871. this.tableColumnsList = [];
  872. this.showToastMessage('获取字段列表失败', 'error');
  873. }
  874. this.isColumnsLoading = false;
  875. })
  876. .catch(error => {
  877. this.showToastMessage('获取字段列表失败,请稍后重试', 'error');
  878. this.isColumnsLoading = false;
  879. });
  880. },
  881. // 清空查询
  882. clearQuery() {
  883. this.sqlQuery = '';
  884. },
  885. // 复制字段名称到剪贴板
  886. copyColumnName(columnName) {
  887. // 创建一个临时文本区域元素
  888. const textarea = document.createElement('textarea');
  889. textarea.value = columnName;
  890. textarea.setAttribute('readonly', '');
  891. textarea.style.position = 'absolute';
  892. textarea.style.left = '-9999px';
  893. document.body.appendChild(textarea);
  894. // 选择文本并复制
  895. textarea.select();
  896. let success = false;
  897. try {
  898. success = document.execCommand('copy');
  899. this.showToastMessage(`已复制字段 "${columnName}" 到剪贴板`, 'success');
  900. } catch (err) {
  901. success = false;
  902. this.showToastMessage('复制失败,请手动复制', 'error');
  903. console.error('复制到剪贴板失败:', err);
  904. }
  905. // 移除临时元素
  906. document.body.removeChild(textarea);
  907. // 如果浏览器支持现代剪贴板API,也尝试使用它
  908. if (navigator.clipboard && window.isSecureContext) {
  909. navigator.clipboard.writeText(columnName).catch(err => {
  910. console.log('剪贴板API失败,但已使用备用方法', err);
  911. });
  912. }
  913. return success;
  914. },
  915. // 插入字段名称到SQL查询(双击时触发)
  916. insertColumnName(columnName) {
  917. // 获取文本框元素
  918. const textarea = document.querySelector('.sql-textarea');
  919. // 如果文本框存在
  920. if (textarea) {
  921. // 获取当前光标位置
  922. const startPos = textarea.selectionStart;
  923. const endPos = textarea.selectionEnd;
  924. // 在光标位置插入字段名称
  925. const textBefore = this.sqlQuery.substring(0, startPos);
  926. const textAfter = this.sqlQuery.substring(endPos);
  927. // 更新SQL查询
  928. this.sqlQuery = textBefore + columnName + textAfter;
  929. // 设置新的光标位置
  930. this.$nextTick(() => {
  931. textarea.focus();
  932. textarea.selectionStart = startPos + columnName.length;
  933. textarea.selectionEnd = startPos + columnName.length;
  934. });
  935. this.showToastMessage(`已插入字段 "${columnName}" 到查询`, 'info');
  936. } else {
  937. // 如果无法获取文本框元素,则直接在末尾添加
  938. this.sqlQuery += ' ' + columnName;
  939. }
  940. },
  941. // 更新SQL查询中的LIMIT值
  942. updateQueryLimit() {
  943. // 检查SQL查询是否为空
  944. if (!this.sqlQuery.trim()) {
  945. return;
  946. }
  947. // 正则表达式匹配LIMIT子句
  948. const limitRegex = /\bLIMIT\s+\d+\s*;?\s*$/i;
  949. if (limitRegex.test(this.sqlQuery)) {
  950. // 如果已经有LIMIT子句,替换数字
  951. this.sqlQuery = this.sqlQuery.replace(limitRegex, `LIMIT ${this.selectedLimit}${this.sqlQuery.endsWith(';') ? ';' : ''}`);
  952. } else {
  953. // 如果没有LIMIT子句,添加到末尾
  954. // 检查SQL是否以分号结尾
  955. if (this.sqlQuery.trim().endsWith(';')) {
  956. // 在分号前添加LIMIT
  957. this.sqlQuery = this.sqlQuery.replace(/;\s*$/, ` LIMIT ${this.selectedLimit};`);
  958. } else {
  959. // 直接在末尾添加LIMIT
  960. this.sqlQuery = this.sqlQuery.trim() + ` LIMIT ${this.selectedLimit};`;
  961. }
  962. }
  963. this.showToastMessage(`已设置查询限制为 ${this.selectedLimit} 条记录`, 'info');
  964. },
  965. // 执行查询
  966. executeQuery() {
  967. if (!this.sqlQuery.trim()) {
  968. this.showToastMessage('请输入SQL查询语句', 'info');
  969. return;
  970. }
  971. this.isQueryLoading = true;
  972. this.queryExecuted = true;
  973. this.currentPage = 1;
  974. // 重置查询结果
  975. this.queryResults = [];
  976. this.tableColumns = [];
  977. this.allResults = [];
  978. this.totalPages = 1;
  979. console.log('执行SQL查询:', this.sqlQuery.trim());
  980. // 调用后端API执行SQL查询,使用RunKylx365DBSql接口
  981. // 使用encodeURIComponent确保中文字符被正确编码
  982. const encodedSql = encodeURIComponent(this.sqlQuery.trim());
  983. const apiUrl = `/api/RunKylx365DBSql?sql=${encodedSql}`;
  984. console.log('API URL:', apiUrl);
  985. fetch(apiUrl, {
  986. method: 'GET',
  987. headers: {
  988. 'Accept': 'application/json'
  989. }
  990. })
  991. .then(response => {
  992. console.log('API响应状态:', response.status);
  993. if (!response.ok) {
  994. throw new Error(`HTTP error! status: ${response.status}`);
  995. }
  996. return response.json();
  997. })
  998. .then(data => {
  999. console.log('查询返回原始数据:', data); // 添加日志
  1000. // 检查数据格式
  1001. let resultData = null;
  1002. let errorMessage = null;
  1003. // 检查标准格式 {"errcode": 10000, result: [...]}
  1004. if (data && typeof data === 'object' && 'errcode' in data) {
  1005. console.log('检测到标准响应格式,errcode:', data.errcode);
  1006. if (data.errcode !== 10000) {
  1007. errorMessage = data.errmsg || '查询返回错误';
  1008. console.error('查询返回错误:', errorMessage);
  1009. } else if (data.result && Array.isArray(data.result)) {
  1010. console.log('标准格式: data.result 是数组');
  1011. resultData = data.result;
  1012. }
  1013. }
  1014. // 检查其他可能的格式
  1015. else if (data && Array.isArray(data)) {
  1016. console.log('替代格式: data 本身是数组');
  1017. resultData = data;
  1018. } else if (data && typeof data === 'object') {
  1019. console.log('检查对象中的数组属性');
  1020. // 尝试在对象中找到数组属性
  1021. for (const key in data) {
  1022. if (Array.isArray(data[key])) {
  1023. console.log(`找到数组属性: ${key}`);
  1024. resultData = data[key];
  1025. break;
  1026. }
  1027. }
  1028. }
  1029. if (errorMessage) {
  1030. this.showToastMessage(errorMessage, 'error');
  1031. this.queryResults = [];
  1032. this.tableColumns = [];
  1033. } else if (resultData && Array.isArray(resultData)) {
  1034. console.log('处理查询结果,数据长度:', resultData.length);
  1035. console.log('第一条记录:', JSON.stringify(resultData[0]));
  1036. this.processQueryResults(resultData);
  1037. } else {
  1038. console.log('未找到有效的查询结果数据');
  1039. this.queryResults = [];
  1040. this.tableColumns = [];
  1041. this.showToastMessage('查询未返回数据', 'info');
  1042. }
  1043. this.isQueryLoading = false;
  1044. })
  1045. .catch(error => {
  1046. this.showToastMessage('执行查询失败,请稍后重试', 'error');
  1047. this.isQueryLoading = false;
  1048. // 模拟数据用于开发测试
  1049. if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
  1050. console.log('在本地环境中使用模拟数据');
  1051. // 根据选择的表格生成模拟数据
  1052. let mockData = [];
  1053. if (this.selectedTable === 'users') {
  1054. mockData = [
  1055. { id: 1, username: 'admin', email: 'admin@example.com', password: '******', created_at: '2023-01-01 00:00:00', status: 1 },
  1056. { id: 2, username: 'user1', email: 'user1@example.com', password: '******', created_at: '2023-01-02 10:30:00', status: 1 },
  1057. { id: 3, username: 'user2', email: 'user2@example.com', password: '******', created_at: '2023-01-03 15:45:00', status: 0 }
  1058. ];
  1059. } else if (this.selectedTable === 'products') {
  1060. mockData = [
  1061. { id: 1, name: '产品A', price: '99.99', category_id: 1, stock: 100, description: '这是产品A的描述' },
  1062. { id: 2, name: '产品B', price: '199.99', category_id: 2, stock: 50, description: '这是产品B的描述' },
  1063. { id: 3, name: '产品C', price: '299.99', category_id: 1, stock: 75, description: '这是产品C的描述' }
  1064. ];
  1065. } else {
  1066. // 生成通用模拟数据
  1067. for (let i = 1; i <= 10; i++) {
  1068. mockData.push({
  1069. id: i,
  1070. name: `示例名称 ${i}`,
  1071. description: `这是示例描述 ${i}`,
  1072. created_at: new Date().toISOString().slice(0, 19).replace('T', ' ')
  1073. });
  1074. }
  1075. }
  1076. this.processQueryResults(mockData);
  1077. }
  1078. });
  1079. },
  1080. // 处理查询结果
  1081. processQueryResults(results) {
  1082. console.log('进入processQueryResults方法,结果类型:', typeof results);
  1083. console.log('结果是否为数组:', Array.isArray(results));
  1084. console.log('结果长度:', results ? results.length : 0);
  1085. if (!results || !Array.isArray(results) || results.length === 0) {
  1086. console.log('结果为空或非数组');
  1087. this.queryResults = [];
  1088. this.tableColumns = [];
  1089. this.allResults = [];
  1090. this.totalPages = 1;
  1091. this.showToastMessage('查询未返回数据', 'info');
  1092. return;
  1093. }
  1094. console.log('第一条记录:', JSON.stringify(results[0]));
  1095. try {
  1096. // 存储所有结果
  1097. this.allResults = [...results];
  1098. // 提取表格列
  1099. this.tableColumns = Object.keys(results[0]);
  1100. console.log('提取的表格列:', this.tableColumns);
  1101. // 计算总页数
  1102. this.totalPages = Math.ceil(results.length / this.pageSize);
  1103. console.log('计算的总页数:', this.totalPages);
  1104. // 显示第一页数据
  1105. this.changePage(1);
  1106. this.showToastMessage(`查询成功,返回 ${results.length} 条记录`, 'success');
  1107. } catch (error) {
  1108. console.error('处理查询结果时出错:', error);
  1109. this.showToastMessage('处理查询结果时出错: ' + error.message, 'error');
  1110. this.queryResults = [];
  1111. this.tableColumns = [];
  1112. this.allResults = [];
  1113. this.totalPages = 1;
  1114. }
  1115. },
  1116. // 切换页面
  1117. changePage(page) {
  1118. console.log('切换页面,目标页码:', page);
  1119. console.log('当前页码:', this.currentPage);
  1120. console.log('总页数:', this.totalPages);
  1121. console.log('所有结果数量:', this.allResults.length);
  1122. console.log('每页显示数量:', this.pageSize);
  1123. try {
  1124. // 验证页码
  1125. if (page < 1 || page > this.totalPages) {
  1126. console.log('页码超出范围');
  1127. return;
  1128. }
  1129. if (page === this.currentPage) {
  1130. console.log('已经在当前页面');
  1131. return;
  1132. }
  1133. this.currentPage = page;
  1134. // 计算当前页的数据
  1135. const startIndex = (page - 1) * this.pageSize;
  1136. const endIndex = Math.min(startIndex + this.pageSize, this.allResults.length);
  1137. console.log('计算的起始索引:', startIndex);
  1138. console.log('计算的结束索引:', endIndex);
  1139. // 直接使用原始数据,不进行转换
  1140. this.queryResults = this.allResults.slice(startIndex, endIndex);
  1141. console.log('当前页数据数量:', this.queryResults.length);
  1142. if (this.queryResults.length > 0) {
  1143. console.log('第一条记录:', JSON.stringify(this.queryResults[0]));
  1144. }
  1145. // 强制Vue更新视图
  1146. this.$forceUpdate();
  1147. } catch (error) {
  1148. console.error('切换页面时出错:', error);
  1149. this.showToastMessage('切换页面时出错: ' + error.message, 'error');
  1150. // 重置为第一页
  1151. this.currentPage = 1;
  1152. this.queryResults = this.allResults.slice(0, this.pageSize);
  1153. this.$forceUpdate();
  1154. }
  1155. },
  1156. // 显示提示消息
  1157. showToastMessage(message, type = 'info') {
  1158. this.toastMessage = message;
  1159. this.toastType = type;
  1160. this.showToast = true;
  1161. // 3秒后自动隐藏
  1162. setTimeout(() => {
  1163. this.showToast = false;
  1164. }, 3000);
  1165. }
  1166. }
  1167. });
  1168. </script>
  1169. </body>
  1170. </html>