kylx365_db_admin.html 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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. const slicedData = this.allResults.slice(startIndex, endIndex);
  1141. this.queryResults = slicedData.map(item => {
  1142. // 确保每个属性都是基本类型
  1143. const plainItem = {};
  1144. Object.keys(item).forEach(key => {
  1145. plainItem[key] = item[key] !== null && item[key] !== undefined
  1146. ? String(item[key])
  1147. : '';
  1148. });
  1149. return plainItem;
  1150. });
  1151. console.log('当前页数据数量:', this.queryResults.length);
  1152. console.log('第一条记录:', JSON.stringify(this.queryResults[0]));
  1153. // 强制Vue更新视图
  1154. this.$forceUpdate();
  1155. } catch (error) {
  1156. console.error('切换页面时出错:', error);
  1157. this.showToastMessage('切换页面时出错: ' + error.message, 'error');
  1158. // 重置为第一页
  1159. this.currentPage = 1;
  1160. this.queryResults = this.allResults.slice(0, this.pageSize);
  1161. this.$forceUpdate();
  1162. }
  1163. },
  1164. // 显示提示消息
  1165. showToastMessage(message, type = 'info') {
  1166. this.toastMessage = message;
  1167. this.toastType = type;
  1168. this.showToast = true;
  1169. // 3秒后自动隐藏
  1170. setTimeout(() => {
  1171. this.showToast = false;
  1172. }, 3000);
  1173. }
  1174. }
  1175. });
  1176. </script>
  1177. </body>
  1178. </html>