kylx365_db_admin.html 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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% {
  306. transform: rotate(0deg);
  307. }
  308. 100% {
  309. transform: rotate(360deg);
  310. }
  311. }
  312. .clear-btn {
  313. position: absolute;
  314. right: 12px;
  315. top: 50%;
  316. transform: translateY(-50%);
  317. width: 16px;
  318. height: 16px;
  319. background-color: #999;
  320. border-radius: 50%;
  321. display: flex;
  322. align-items: center;
  323. justify-content: center;
  324. cursor: pointer;
  325. transition: background-color 0.2s;
  326. }
  327. .clear-btn:hover {
  328. background-color: #666;
  329. }
  330. .clear-x {
  331. position: relative;
  332. width: 8px;
  333. height: 8px;
  334. }
  335. .clear-x:before,
  336. .clear-x:after {
  337. content: '';
  338. position: absolute;
  339. width: 8px;
  340. height: 2px;
  341. background-color: white;
  342. top: 3px;
  343. left: 0;
  344. }
  345. .clear-x:before {
  346. transform: rotate(45deg);
  347. }
  348. .clear-x:after {
  349. transform: rotate(-45deg);
  350. }
  351. .FlexRow {
  352. display: flex;
  353. flex-direction: row;
  354. }
  355. .FlexColumn {
  356. display: flex;
  357. flex-direction: column;
  358. }
  359. .table-list {
  360. flex: 1;
  361. overflow-y: auto;
  362. padding: 10px;
  363. }
  364. .table-item {
  365. padding: 10px;
  366. margin: 5px 0;
  367. background: white;
  368. border-radius: 4px;
  369. cursor: pointer;
  370. overflow-x: hidden;
  371. transition: background-color 0.2s;
  372. }
  373. .table-item:hover {
  374. background-color: #f0f0f0;
  375. }
  376. .table-item.active {
  377. background-color: #e6f3ff;
  378. border-left: 3px solid #4A90E2;
  379. }
  380. .btn-group {
  381. display: flex;
  382. justify-content: flex-end;
  383. margin-top: 10px;
  384. }
  385. .no-data {
  386. text-align: center;
  387. padding: 40px;
  388. color: #999;
  389. font-size: 16px;
  390. }
  391. .pagination {
  392. display: flex;
  393. justify-content: center;
  394. margin-top: 20px;
  395. padding: 10px;
  396. }
  397. .pagination-btn {
  398. padding: 5px 10px;
  399. margin: 0 5px;
  400. border: 1px solid #DDDDDD;
  401. border-radius: 4px;
  402. background: white;
  403. cursor: pointer;
  404. }
  405. .pagination-btn:hover {
  406. background: #F5F5F5;
  407. }
  408. .pagination-btn.active {
  409. background: #4A90E2;
  410. color: white;
  411. border-color: #4A90E2;
  412. }
  413. .pagination-btn.disabled {
  414. color: #CCCCCC;
  415. cursor: not-allowed;
  416. }
  417. </style>
  418. </head>
  419. <body class="container FlexRow">
  420. <div id="app" class="main00 FlexColumn">
  421. <div class="ListTop FlexRow">
  422. <div class="ListTop3 FlexRow" style="width: 100%; justify-content: space-between;">
  423. <div class="title">数据库管理</div>
  424. </div>
  425. </div>
  426. <div class="main0 FlexRow">
  427. <!-- 左侧表格列表 -->
  428. <div class="tables-panel" style="position: relative;">
  429. <div v-if="isTablesLoading" class="loading-overlay">
  430. <div class="loading-spinner"></div>
  431. </div>
  432. <div class="search-box FlexRow">
  433. <div style="position: relative;">
  434. <input type="text" class="search-input" v-model="searchText" @keyup.enter="searchTables"
  435. placeholder="搜索表格...">
  436. <div class="clear-btn" v-show="searchText" @click="clearSearch">
  437. <span class="clear-x"></span>
  438. </div>
  439. </div>
  440. <div class="btn33 FlexRow" @click="searchTables">
  441. <img title="搜索" alt="搜索"
  442. src="https://kylx365-1253256735.file.myqcloud.com/web/universalpic_search_gray_30x30.png"
  443. style="width: 20px; height: 20px;" />
  444. </div>
  445. </div>
  446. <!-- 表格列表 -->
  447. <div class="table-list">
  448. <div v-if="tables && tables.length > 0">
  449. <div v-for="(table, index) in filteredTables" :key="index" @click="selectTable(table)"
  450. :class="['table-item', { active: selectedTable === table }]">
  451. <div class="table-name">{{ table }}</div>
  452. <div class="table-comment" v-if="tableComments[table]">{{ tableComments[table] }}</div>
  453. </div>
  454. <div v-if="filteredTables.length === 0" class="no-data">
  455. 没有匹配的表格
  456. </div>
  457. </div>
  458. <div v-else-if="tables && tables.length === 0" class="no-data">
  459. 未找到表格
  460. </div>
  461. <div v-else class="no-data">
  462. 加载中...
  463. </div>
  464. </div>
  465. </div>
  466. <!-- 右侧内容区域 -->
  467. <div class="content-panel">
  468. <!-- SQL编辑器 -->
  469. <div class="sql-editor">
  470. <!-- 左侧SQL编辑区域 -->
  471. <div class="sql-editor-left">
  472. <textarea class="sql-textarea" v-model="sqlQuery" placeholder="输入SQL查询语句..."></textarea>
  473. <div class="btn-group">
  474. <select class="btn btn-default" v-model="selectedLimit" @change="updateQueryLimit"
  475. style="margin-right: 8px;">
  476. <option v-for="limit in limitOptions" :key="limit" :value="limit">
  477. LIMIT {{ limit }}
  478. </option>
  479. </select>
  480. <button type="button" class="btn btn-default" @click="clearQuery">清空</button>
  481. <button type="button" class="btn btn-primary" @click="executeQuery">执行</button>
  482. </div>
  483. </div>
  484. <!-- 右侧字段列表 -->
  485. <div class="sql-editor-right">
  486. <div v-if="isColumnsLoading" class="loading-overlay">
  487. <div class="loading-spinner"></div>
  488. </div>
  489. <div class="columns-list">
  490. <div v-if="tableColumnsList && tableColumnsList.length > 0">
  491. <div v-for="(column, index) in tableColumnsList" :key="index" class="column-item"
  492. @click="copyColumnName(column.name)" @dblclick="insertColumnName(column.name)">
  493. <div class="column-left">
  494. <div class="column-name">{{ column.name }}</div>
  495. <div class="column-comment" v-if="column.comment">{{ column.comment }}</div>
  496. </div>
  497. <div class="column-right">
  498. <div class="column-type">{{ column.type }}</div>
  499. </div>
  500. </div>
  501. </div>
  502. <div v-else class="no-data">
  503. 请选择表格查看字段列表
  504. </div>
  505. </div>
  506. </div>
  507. </div>
  508. <!-- 查询结果 -->
  509. <div class="results-panel" style="position: relative;">
  510. <div v-if="isQueryLoading" class="loading-overlay">
  511. <div class="loading-spinner"></div>
  512. </div>
  513. <div v-if="queryExecuted" class="table-container">
  514. <div v-if="allResults && allResults.length > 0">
  515. <div style="margin-bottom: 10px; color: #666;">
  516. 显示 {{ queryResults.length }} 条记录,共 {{ allResults.length }} 条
  517. </div>
  518. <div v-if="isQueryLoading" class="loading-indicator">
  519. <div class="spinner"></div>
  520. <div>数据加载中...</div>
  521. </div>
  522. <div v-else-if="!tableColumns || tableColumns.length === 0" class="no-data">
  523. 未检测到有效的表格列
  524. </div>
  525. <template v-else>
  526. <table class="data-table">
  527. <thead>
  528. <tr>
  529. <th v-for="(column, index) in tableColumns"
  530. :key="'th-'+index"
  531. v-show="!column.startsWith('__')">
  532. {{ column }}
  533. </th>
  534. </tr>
  535. </thead>
  536. <tbody>
  537. <tr v-for="(row, rowIndex) in queryResults"
  538. :key="'tr-'+rowIndex">
  539. <td v-for="(column, colIndex) in tableColumns"
  540. :key="'td-'+rowIndex+'-'+colIndex"
  541. v-show="!column.startsWith('__')">
  542. {{ formatTableCell(row[column]) }}
  543. </td>
  544. </tr>
  545. </tbody>
  546. </table>
  547. <div v-if="queryResults.length === 0" class="no-data">
  548. 没有可显示的数据
  549. <div v-if="allResults.length > 0" style="margin-top: 10px; font-size: 12px; color: #999;">
  550. 共 {{allResults.length}} 条数据
  551. </div>
  552. </div>
  553. </template>
  554. </div>
  555. <div v-else class="no-data">
  556. 查询未返回数据
  557. </div>
  558. </div>
  559. <div v-else-if="queryExecuted" class="no-data">
  560. 查询未返回数据
  561. </div>
  562. <div v-else class="no-data">
  563. 请选择表格并执行查询
  564. </div>
  565. </div>
  566. </div>
  567. </div>
  568. <!-- Toast提示 -->
  569. <div class="toast" :class="{ show: showToast, [toastType]: showToast }">{{ toastMessage }}</div>
  570. </div>
  571. <script>
  572. new Vue({
  573. el: '#app',
  574. data: {
  575. searchText: '',
  576. tables: [], // 存储表格名称列表
  577. tableComments: {}, // 存储表格注释,格式为 {表名: 注释}
  578. filteredTables: [],
  579. selectedTable: null,
  580. sqlQuery: '',
  581. queryResults: [],
  582. tableColumns: [],
  583. showToast: false,
  584. toastMessage: '',
  585. toastType: 'info',
  586. isTablesLoading: false,
  587. isQueryLoading: false,
  588. isColumnsLoading: false,
  589. queryExecuted: false,
  590. allResults: [],
  591. tableColumnsList: [], // 存储表格字段列表
  592. limitOptions: [10, 100, 500, 1000, 2000, 3000], // LIMIT选项
  593. selectedLimit: 100 // 默认选中的LIMIT值
  594. },
  595. computed: {
  596. },
  597. mounted() {
  598. this.loadTables();
  599. },
  600. methods: {
  601. // 加载数据库表格列表
  602. loadTables() {
  603. this.isTablesLoading = true;
  604. this.tableComments = {};
  605. // 调用后端API获取表格列表
  606. fetch('/api/GetKylx365Tables')
  607. .then(response => {
  608. // console.log('API响应状态:', response.status);
  609. if (!response.ok) {
  610. throw new Error(`HTTP error! status: ${response.status}`);
  611. }
  612. return response.json();
  613. })
  614. .then(data => {
  615. //console.log('API返回数据:', data);
  616. // 检查数据格式,处理可能的不同响应结构
  617. //console.log('检查数据格式:', data);
  618. // 尝试确定数据的实际结构
  619. let resultData = null;
  620. if (data && data.result && Array.isArray(data.result)) {
  621. //console.log('标准格式: data.result 是数组');
  622. resultData = data.result;
  623. } else if (data && Array.isArray(data)) {
  624. //console.log('替代格式: data 本身是数组');
  625. resultData = data;
  626. } else if (data && typeof data === 'object') {
  627. //console.log('检查对象中的数组属性');
  628. // 尝试在对象中找到数组属性
  629. for (const key in data) {
  630. if (Array.isArray(data[key])) {
  631. //console.log(`找到数组属性: ${key}`);
  632. resultData = data[key];
  633. break;
  634. }
  635. }
  636. }
  637. if (resultData && Array.isArray(resultData)) {
  638. //console.log('数据验证通过,开始处理数据');
  639. //console.log('数据结果长度:', resultData.length);
  640. if (resultData.length > 0 && typeof resultData[0] === 'object' && !Array.isArray(resultData[0])) {
  641. //console.log('检测到对象数组格式,第一个对象:', resultData[0]);
  642. //console.log('第一个对象的属性:', Object.keys(resultData[0]));
  643. // 检查并适应不同的属性名称
  644. const firstItem = resultData[0];
  645. const tableNameKey = 'table_name' in firstItem ? 'table_name' :
  646. 'tableName' in firstItem ? 'tableName' :
  647. 'name' in firstItem ? 'name' : null;
  648. const tableCommentKey = 'table_comment' in firstItem ? 'table_comment' :
  649. 'tableComment' in firstItem ? 'tableComment' :
  650. 'comment' in firstItem ? 'comment' : null;
  651. //console.log('使用的属性名:', { tableNameKey, tableCommentKey });
  652. if (tableNameKey) {
  653. // 如果返回的是对象数组(包含name和comment)
  654. this.tables = resultData.map(item => {
  655. const tableName = item[tableNameKey] || '';
  656. //console.log('处理表名:', tableName);
  657. return tableName;
  658. }).filter(name => name); // 过滤掉空表名
  659. //console.log('处理后的表格列表:', this.tables);
  660. // 存储表格注释
  661. if (tableCommentKey) {
  662. resultData.forEach(item => {
  663. const tableName = item[tableNameKey] || '';
  664. const tableComment = item[tableCommentKey] || '';
  665. if (tableName && tableComment) {
  666. this.tableComments[tableName] = tableComment;
  667. }
  668. });
  669. //console.log('处理后的表格注释:', this.tableComments);
  670. }
  671. } else {
  672. //console.error('无法找到表名属性');
  673. this.showToastMessage('数据格式错误:无法找到表名属性', 'error');
  674. }
  675. } else {
  676. //console.log('检测到简单数组格式');
  677. // 如果返回的只是表名数组
  678. this.tables = resultData.filter(name => {
  679. //console.log('处理表名:', name);
  680. return name && typeof name === 'string';
  681. });
  682. //console.log('处理后的表格列表:', this.tables);
  683. }
  684. this.filteredTables = [...this.tables];
  685. // console.log('更新后的过滤表格列表:', this.filteredTables);
  686. } else {
  687. this.tables = [];
  688. this.filteredTables = [];
  689. this.showToastMessage('获取表格列表失败', 'error');
  690. }
  691. this.isTablesLoading = false;
  692. })
  693. .catch(error => {
  694. //console.error('获取表格列表失败详细错误:', error);
  695. this.showToastMessage(`获取表格列表失败: ${error.message}`, 'error');
  696. this.isTablesLoading = false;
  697. });
  698. },
  699. // 搜索表格
  700. searchTables() {
  701. //console.log('开始搜索表格,搜索文本:', this.searchText);
  702. if (!this.searchText.trim()) {
  703. //console.log('搜索文本为空,显示所有表格');
  704. this.filteredTables = [...this.tables];
  705. return;
  706. }
  707. const searchTerm = this.searchText.toLowerCase();
  708. //console.log('过滤表格列表,搜索条件:', searchTerm);
  709. this.filteredTables = this.tables.filter(table => {
  710. const tableName = table.toLowerCase();
  711. const tableComment = (this.tableComments[table] || '').toLowerCase();
  712. const matches = tableName.includes(searchTerm) || tableComment.includes(searchTerm);
  713. // if (matches) {
  714. // console.log(`表格匹配: ${table} (${this.tableComments[table] || '无注释'})`);
  715. // }
  716. return matches;
  717. });
  718. //console.log('过滤后的表格数量:', this.filteredTables.length);
  719. },
  720. // 清空搜索
  721. clearSearch() {
  722. this.searchText = '';
  723. this.filteredTables = [...this.tables];
  724. },
  725. // 选择表格
  726. selectTable(table) {
  727. this.selectedTable = table;
  728. this.sqlQuery = `SELECT * FROM ${table} LIMIT 100;`;
  729. // 获取表格字段列表
  730. this.loadTableColumns(table);
  731. },
  732. // 加载表格字段列表
  733. loadTableColumns(tableName) {
  734. this.isColumnsLoading = true;
  735. this.tableColumnsList = [];
  736. // 调用API获取表格字段列表
  737. fetch(`/api/GetKylx365TableColumnByTable?table=${encodeURIComponent(tableName)}`)
  738. .then(response => response.json())
  739. .then(data => {
  740. //console.log('收到的原始数据:', data);
  741. // 验证数据格式
  742. if (!data) {
  743. //console.error('接收到空数据');
  744. throw new Error('接收到空数据');
  745. }
  746. // 尝试确定数据的实际结构
  747. let columnsData = null;
  748. if (data && data.result && Array.isArray(data.result)) {
  749. //console.log('标准格式: data.result 是数组');
  750. columnsData = data.result;
  751. } else if (data && Array.isArray(data)) {
  752. //console.log('替代格式: data 本身是数组');
  753. columnsData = data;
  754. } else if (data && typeof data === 'object') {
  755. //console.log('检查对象中的数组属性');
  756. // 尝试在对象中找到数组属性
  757. for (const key in data) {
  758. if (Array.isArray(data[key])) {
  759. //console.log(`找到数组属性: ${key}`);
  760. columnsData = data[key];
  761. break;
  762. }
  763. }
  764. }
  765. if (columnsData && Array.isArray(columnsData)) {
  766. //console.log('数据验证通过,开始处理数据');
  767. //console.log('字段数据长度:', columnsData.length);
  768. if (columnsData.length > 0) {
  769. // 检查第一个元素的格式
  770. const firstItem = columnsData[0];
  771. //console.log('第一个字段项:', firstItem);
  772. if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
  773. // 检查并适应不同的属性名称
  774. const nameKey = 'Field' in firstItem ? 'Field' :
  775. 'name' in firstItem ? 'name' :
  776. 'column_name' in firstItem ? 'column_name' :
  777. 'columnName' in firstItem ? 'columnName' :
  778. 'field' in firstItem ? 'field' : null;
  779. const typeKey = 'Type' in firstItem ? 'Type' :
  780. 'type' in firstItem ? 'type' :
  781. 'column_type' in firstItem ? 'column_type' :
  782. 'columnType' in firstItem ? 'columnType' :
  783. 'data_type' in firstItem ? 'data_type' : null;
  784. const commentKey = 'Comment' in firstItem ? 'Comment' :
  785. 'comment' in firstItem ? 'comment' :
  786. 'column_comment' in firstItem ? 'column_comment' :
  787. 'columnComment' in firstItem ? 'columnComment' : null;
  788. // 组合Default和Extra作为注释
  789. const getComment = (item) => {
  790. let comment = [];
  791. if (item['Default']) {
  792. comment.push(`默认值: ${item['Default']}`);
  793. }
  794. if (item['Extra'] && item['Extra'] !== '') {
  795. comment.push(item['Extra']);
  796. }
  797. if (item['Null'] === 'NO') {
  798. comment.push('不可为空');
  799. }
  800. return comment.join(', ');
  801. };
  802. //console.log('使用的属性名:', { nameKey, typeKey, commentKey });
  803. if (nameKey) {
  804. // 转换为标准格式
  805. this.tableColumnsList = columnsData.map(item => {
  806. return {
  807. name: item[nameKey] || '',
  808. type: typeKey ? (item[typeKey] || '') : '',
  809. comment: [
  810. commentKey ? (item[commentKey] || '') : '',
  811. getComment(item)
  812. ].filter(Boolean).join(' | ')
  813. };
  814. }).filter(col => col.name); // 过滤掉没有名称的列
  815. //console.log('处理后的字段列表:', this.tableColumnsList);
  816. } else {
  817. console.error('无法找到字段名属性');
  818. this.showToastMessage('数据格式错误:无法找到字段名属性', 'error');
  819. this.tableColumnsList = [];
  820. }
  821. } else if (typeof firstItem === 'string') {
  822. // 如果只是字段名数组
  823. this.tableColumnsList = columnsData.map(name => {
  824. return {
  825. name: name,
  826. type: '',
  827. comment: ''
  828. };
  829. });
  830. //console.log('处理后的字段列表(仅名称):', this.tableColumnsList);
  831. } else {
  832. console.error('未知的字段数据格式');
  833. this.showToastMessage('未知的字段数据格式', 'error');
  834. this.tableColumnsList = [];
  835. }
  836. } else {
  837. //console.log('字段列表为空');
  838. this.tableColumnsList = [];
  839. }
  840. } else {
  841. //console.error('无法找到有效的字段数据');
  842. this.tableColumnsList = [];
  843. this.showToastMessage('获取字段列表失败', 'error');
  844. }
  845. this.isColumnsLoading = false;
  846. })
  847. .catch(error => {
  848. this.showToastMessage('获取字段列表失败,请稍后重试', 'error');
  849. this.isColumnsLoading = false;
  850. });
  851. },
  852. // 清空查询
  853. clearQuery() {
  854. this.sqlQuery = '';
  855. },
  856. // 复制字段名称到剪贴板
  857. copyColumnName(columnName) {
  858. // 创建一个临时文本区域元素
  859. const textarea = document.createElement('textarea');
  860. textarea.value = columnName;
  861. textarea.setAttribute('readonly', '');
  862. textarea.style.position = 'absolute';
  863. textarea.style.left = '-9999px';
  864. document.body.appendChild(textarea);
  865. // 选择文本并复制
  866. textarea.select();
  867. let success = false;
  868. try {
  869. success = document.execCommand('copy');
  870. this.showToastMessage(`已复制字段 "${columnName}" 到剪贴板`, 'success');
  871. } catch (err) {
  872. success = false;
  873. this.showToastMessage('复制失败,请手动复制', 'error');
  874. console.error('复制到剪贴板失败:', err);
  875. }
  876. // 移除临时元素
  877. document.body.removeChild(textarea);
  878. // 如果浏览器支持现代剪贴板API,也尝试使用它
  879. if (navigator.clipboard && window.isSecureContext) {
  880. navigator.clipboard.writeText(columnName).catch(err => {
  881. console.log('剪贴板API失败,但已使用备用方法', err);
  882. });
  883. }
  884. return success;
  885. },
  886. // 插入字段名称到SQL查询(双击时触发)
  887. insertColumnName(columnName) {
  888. // 获取文本框元素
  889. const textarea = document.querySelector('.sql-textarea');
  890. // 如果文本框存在
  891. if (textarea) {
  892. // 获取当前光标位置
  893. const startPos = textarea.selectionStart;
  894. const endPos = textarea.selectionEnd;
  895. // 在光标位置插入字段名称
  896. const textBefore = this.sqlQuery.substring(0, startPos);
  897. const textAfter = this.sqlQuery.substring(endPos);
  898. // 更新SQL查询
  899. this.sqlQuery = textBefore + columnName + textAfter;
  900. // 设置新的光标位置
  901. this.$nextTick(() => {
  902. textarea.focus();
  903. textarea.selectionStart = startPos + columnName.length;
  904. textarea.selectionEnd = startPos + columnName.length;
  905. });
  906. this.showToastMessage(`已插入字段 "${columnName}" 到查询`, 'info');
  907. } else {
  908. // 如果无法获取文本框元素,则直接在末尾添加
  909. this.sqlQuery += ' ' + columnName;
  910. }
  911. },
  912. // 更新SQL查询中的LIMIT值
  913. updateQueryLimit() {
  914. // 检查SQL查询是否为空
  915. if (!this.sqlQuery.trim()) {
  916. return;
  917. }
  918. // 正则表达式匹配LIMIT子句
  919. const limitRegex = /\bLIMIT\s+\d+\s*;?\s*$/i;
  920. if (limitRegex.test(this.sqlQuery)) {
  921. // 如果已经有LIMIT子句,替换数字
  922. this.sqlQuery = this.sqlQuery.replace(limitRegex, `LIMIT ${this.selectedLimit}${this.sqlQuery.endsWith(';') ? ';' : ''}`);
  923. } else {
  924. // 如果没有LIMIT子句,添加到末尾
  925. // 检查SQL是否以分号结尾
  926. if (this.sqlQuery.trim().endsWith(';')) {
  927. // 在分号前添加LIMIT
  928. this.sqlQuery = this.sqlQuery.replace(/;\s*$/, ` LIMIT ${this.selectedLimit};`);
  929. } else {
  930. // 直接在末尾添加LIMIT
  931. this.sqlQuery = this.sqlQuery.trim() + ` LIMIT ${this.selectedLimit};`;
  932. }
  933. }
  934. this.showToastMessage(`已设置查询限制为 ${this.selectedLimit} 条记录`, 'info');
  935. },
  936. // 执行查询
  937. executeQuery() {
  938. if (!this.sqlQuery.trim()) {
  939. this.showToastMessage('请输入SQL查询语句', 'info');
  940. return;
  941. }
  942. this.isQueryLoading = true;
  943. this.queryExecuted = true;
  944. // 重置查询结果
  945. this.queryResults = [];
  946. this.tableColumns = [];
  947. this.allResults = [];
  948. // 调用后端API执行SQL查询,使用RunKylx365DBSql接口
  949. const apiUrl = '/api/RunKylx365DBSql';
  950. fetch(apiUrl, {
  951. method: 'POST',
  952. headers: {
  953. 'Content-Type': 'application/json',
  954. 'Accept': 'application/json'
  955. },
  956. body: JSON.stringify({
  957. sql: this.sqlQuery.trim()
  958. })
  959. })
  960. .then(response => {
  961. if (!response.ok) {
  962. throw new Error(`HTTP error! status: ${response.status}`);
  963. }
  964. return response.json();
  965. })
  966. .then(data => {
  967. // 检查数据格式
  968. let resultData = null;
  969. let errorMessage = null;
  970. // 检查标准格式 {"errcode": 10000, result: [...]}
  971. if (data && typeof data === 'object' && 'errcode' in data) {
  972. if (data.errcode !== 10000) {
  973. errorMessage = data.errmsg || '查询返回错误';
  974. console.error('查询返回错误:', errorMessage);
  975. } else if (data.result && Array.isArray(data.result)) {
  976. resultData = data.result;
  977. }
  978. }
  979. // 检查其他可能的格式
  980. else if (data && Array.isArray(data)) {
  981. resultData = data;
  982. } else if (data && typeof data === 'object') {
  983. // 尝试在对象中找到数组属性
  984. for (const key in data) {
  985. if (Array.isArray(data[key])) {
  986. resultData = data[key];
  987. break;
  988. }
  989. }
  990. }
  991. if (errorMessage) {
  992. console.error('查询返回错误:', errorMessage);
  993. this.showToastMessage(errorMessage, 'error');
  994. this.queryResults = [];
  995. this.tableColumns = [];
  996. } else if (resultData && Array.isArray(resultData)) {
  997. this.processQueryResults(resultData);
  998. } else {
  999. this.queryResults = [];
  1000. this.tableColumns = [];
  1001. this.showToastMessage('查询未返回数据', 'info');
  1002. }
  1003. this.isQueryLoading = false;
  1004. })
  1005. .catch(error => {
  1006. this.showToastMessage('执行查询失败,请稍后重试', 'error');
  1007. this.isQueryLoading = false;
  1008. });
  1009. },
  1010. // 处理查询结果
  1011. processQueryResults(results) {
  1012. // 确保结果是一个数组
  1013. if (!results || !Array.isArray(results) || results.length === 0) {
  1014. console.warn('结果为空或非数组');
  1015. this.$set(this, 'queryResults', []);
  1016. this.$set(this, 'tableColumns', []);
  1017. this.$set(this, 'allResults', []);
  1018. this.showToastMessage('查询未返回数据', 'info');
  1019. console.groupEnd();
  1020. return;
  1021. }
  1022. // 创建深拷贝避免引用问题
  1023. const safeResults = JSON.parse(JSON.stringify(results));
  1024. try {
  1025. // 直接使用原始结果数据
  1026. const formattedResults = results;
  1027. // 使用Vue.set确保响应式更新
  1028. this.$set(this, 'allResults', formattedResults);
  1029. // 提取表格列,排除Vue内部属性
  1030. if (formattedResults.length > 0) {
  1031. // 确保所有行有相同的列结构
  1032. const allColumns = new Set();
  1033. formattedResults.forEach(row => {
  1034. Object.keys(row).forEach(key => {
  1035. if (!key.startsWith('__')) {
  1036. allColumns.add(key);
  1037. }
  1038. });
  1039. });
  1040. const columns = Array.from(allColumns);
  1041. // 验证数据完整性
  1042. const missingData = formattedResults.filter(row => {
  1043. return columns.some(col => !(col in row));
  1044. });
  1045. if (missingData.length > 0) {
  1046. console.warn('发现数据不完整:', missingData);
  1047. }
  1048. this.$set(this, 'tableColumns', columns);
  1049. } else {
  1050. this.$set(this, 'tableColumns', []);
  1051. }
  1052. this.$set(this, 'queryResults', formattedResults);
  1053. this.showToastMessage(`查询成功,返回 ${formattedResults.length} 条记录`, 'success');
  1054. } catch (error) {
  1055. console.error('处理查询结果时出错:', error);
  1056. this.showToastMessage('处理查询结果时出错: ' + error.message, 'error');
  1057. this.queryResults = [];
  1058. this.tableColumns = [];
  1059. this.allResults = [];
  1060. }
  1061. },
  1062. // 格式化表格单元格内容
  1063. formatTableCell(value) {
  1064. if (value === null || value === undefined) {
  1065. return 'NULL';
  1066. }
  1067. if (typeof value === 'object') {
  1068. try {
  1069. return JSON.stringify(value);
  1070. } catch (e) {
  1071. return '[Object]';
  1072. }
  1073. }
  1074. if (typeof value === 'boolean') {
  1075. return value ? '是' : '否';
  1076. }
  1077. return value;
  1078. },
  1079. // 显示提示消息
  1080. showToastMessage(message, type = 'info') {
  1081. this.toastMessage = message;
  1082. this.toastType = type;
  1083. this.showToast = true;
  1084. // 3秒后自动隐藏
  1085. setTimeout(() => {
  1086. this.showToast = false;
  1087. }, 3000);
  1088. },
  1089. }
  1090. });
  1091. </script>
  1092. </body>
  1093. </html>