kylx365_db_admin.html 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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. position: relative;
  199. }
  200. .table-container {
  201. width: 100%;
  202. overflow-x: auto;
  203. max-height: calc(100vh - 380px); /* 减去其他元素的高度 */
  204. overflow-y: auto;
  205. position: relative; /* 为内部元素提供定位上下文 */
  206. }
  207. .data-table {
  208. width: 100%;
  209. border-collapse: separate; /* 改为separate以支持边框样式 */
  210. border-spacing: 0; /* 消除单元格间距 */
  211. font-size: 14px;
  212. }
  213. .data-table th {
  214. background: #F5F5F5;
  215. padding: 10px;
  216. text-align: left;
  217. border: 1px solid #DDDDDD;
  218. position: sticky;
  219. top: 0;
  220. z-index: 10;
  221. box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
  222. }
  223. /* 确保表头单元格背景色完整覆盖 */
  224. .data-table thead th {
  225. background: #F5F5F5;
  226. }
  227. /* 优化表头在滚动时的视觉效果 */
  228. .table-container:not(:hover) .data-table th {
  229. transition: box-shadow 0.3s ease;
  230. }
  231. .table-container:hover .data-table th {
  232. box-shadow: 0 3px 5px -2px rgba(0, 0, 0, 0.2);
  233. }
  234. .data-table td {
  235. padding: 8px 10px;
  236. border: 1px solid #DDDDDD;
  237. max-width: 300px;
  238. overflow: hidden;
  239. text-overflow: ellipsis;
  240. white-space: nowrap;
  241. }
  242. .data-table tr:nth-child(even) {
  243. background-color: #F9F9F9;
  244. }
  245. .data-table tr:hover {
  246. background-color: #F0F7FF;
  247. }
  248. .btn {
  249. min-width: 30px;
  250. height: 36px;
  251. padding: 0 16px;
  252. margin-left: 12px;
  253. border: none;
  254. border-radius: 4px;
  255. cursor: pointer;
  256. font-size: 14px;
  257. transition: all 0.3s;
  258. }
  259. .btn-primary {
  260. background: #4A90E2;
  261. color: white;
  262. }
  263. .btn-primary:hover {
  264. background: #357ABD;
  265. }
  266. .btn-default {
  267. background: white;
  268. border: 1px solid #DDDDDD;
  269. color: #666666;
  270. }
  271. .btn-default:hover {
  272. background: #F5F5F5;
  273. border-color: #CCCCCC;
  274. }
  275. .toast {
  276. position: fixed;
  277. top: 20px;
  278. left: 50%;
  279. transform: translateX(-50%);
  280. padding: 12px 24px;
  281. background: rgba(0, 0, 0, 0.7);
  282. color: white;
  283. border-radius: 4px;
  284. z-index: 9999;
  285. opacity: 0;
  286. transition: opacity 0.3s;
  287. }
  288. .toast.show {
  289. opacity: 1;
  290. }
  291. .toast.success {
  292. background: #4CAF50;
  293. }
  294. .toast.error {
  295. background: #F44336;
  296. }
  297. .toast.info {
  298. background: #2196F3;
  299. }
  300. /* Loading样式 */
  301. .loading-overlay {
  302. position: absolute;
  303. top: 0;
  304. left: 0;
  305. width: 100%;
  306. height: 100%;
  307. background-color: rgba(255, 255, 255, 0.7);
  308. display: flex;
  309. justify-content: center;
  310. align-items: center;
  311. z-index: 100;
  312. }
  313. .loading-spinner {
  314. width: 50px;
  315. height: 50px;
  316. border: 5px solid #f3f3f3;
  317. border-top: 5px solid #4A90E2;
  318. border-radius: 50%;
  319. animation: spin 1s linear infinite;
  320. }
  321. @keyframes spin {
  322. 0% {
  323. transform: rotate(0deg);
  324. }
  325. 100% {
  326. transform: rotate(360deg);
  327. }
  328. }
  329. .clear-btn {
  330. position: absolute;
  331. right: 12px;
  332. top: 50%;
  333. transform: translateY(-50%);
  334. width: 16px;
  335. height: 16px;
  336. background-color: #999;
  337. border-radius: 50%;
  338. display: flex;
  339. align-items: center;
  340. justify-content: center;
  341. cursor: pointer;
  342. transition: background-color 0.2s;
  343. }
  344. .clear-btn:hover {
  345. background-color: #666;
  346. }
  347. .clear-x {
  348. position: relative;
  349. width: 8px;
  350. height: 8px;
  351. }
  352. .clear-x:before,
  353. .clear-x:after {
  354. content: '';
  355. position: absolute;
  356. width: 8px;
  357. height: 2px;
  358. background-color: white;
  359. top: 3px;
  360. left: 0;
  361. }
  362. .clear-x:before {
  363. transform: rotate(45deg);
  364. }
  365. .clear-x:after {
  366. transform: rotate(-45deg);
  367. }
  368. .FlexRow {
  369. display: flex;
  370. flex-direction: row;
  371. }
  372. .FlexColumn {
  373. display: flex;
  374. flex-direction: column;
  375. }
  376. .table-list {
  377. flex: 1;
  378. overflow-y: auto;
  379. padding: 10px;
  380. }
  381. .table-item {
  382. padding: 10px;
  383. margin: 5px 0;
  384. background: white;
  385. border-radius: 4px;
  386. cursor: pointer;
  387. overflow-x: hidden;
  388. transition: background-color 0.2s;
  389. }
  390. .table-item:hover {
  391. background-color: #f0f0f0;
  392. }
  393. .table-item.active {
  394. background-color: #e6f3ff;
  395. border-left: 3px solid #4A90E2;
  396. }
  397. .btn-group {
  398. display: flex;
  399. justify-content: flex-end;
  400. margin-top: 10px;
  401. }
  402. .no-data {
  403. text-align: center;
  404. padding: 40px;
  405. color: #999;
  406. font-size: 16px;
  407. }
  408. .pagination {
  409. display: flex;
  410. justify-content: center;
  411. margin-top: 20px;
  412. padding: 10px;
  413. }
  414. .pagination-btn {
  415. padding: 5px 10px;
  416. margin: 0 5px;
  417. border: 1px solid #DDDDDD;
  418. border-radius: 4px;
  419. background: white;
  420. cursor: pointer;
  421. }
  422. .pagination-btn:hover {
  423. background: #F5F5F5;
  424. }
  425. .pagination-btn.active {
  426. background: #4A90E2;
  427. color: white;
  428. border-color: #4A90E2;
  429. }
  430. .pagination-btn.disabled {
  431. color: #CCCCCC;
  432. cursor: not-allowed;
  433. }
  434. /* 完整内容弹出层样式 - 改进版 */
  435. .complete-content-overlay {
  436. position: fixed;
  437. top: 0;
  438. left: 0;
  439. right: 0;
  440. bottom: 0;
  441. display: flex;
  442. justify-content: center;
  443. align-items: center;
  444. z-index: 1000;
  445. background-color: rgba(0, 0, 0, 0.5);
  446. pointer-events: auto;
  447. }
  448. .complete-content-container {
  449. width: 80%;
  450. max-width: 800px;
  451. max-height: 80vh;
  452. background-color: white;
  453. border-radius: 8px;
  454. padding: 20px;
  455. padding-top: 50px; /* 为固定的关闭按钮留出空间 */
  456. position: relative;
  457. box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  458. overflow: auto;
  459. animation: fadeIn 0.3s ease-out;
  460. }
  461. @keyframes fadeIn {
  462. from {
  463. opacity: 0;
  464. transform: translateY(20px);
  465. }
  466. to {
  467. opacity: 1;
  468. transform: translateY(0);
  469. }
  470. }
  471. .close-btn {
  472. position: absolute;
  473. right: 15px; /* 根据弹窗宽度计算位置 */
  474. top: 15px; /* 根据弹窗位置计算 */
  475. z-index: 1010; /* 确保按钮在最上层 */
  476. width: 40px;
  477. height: 40px;
  478. display: flex;
  479. align-items: center;
  480. justify-content: center;
  481. background: rgba(255, 255, 255, 0.9);
  482. border: none;
  483. border-radius: 50%;
  484. font-size: 24px;
  485. cursor: pointer;
  486. color: #999;
  487. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  488. }
  489. .close-btn:hover {
  490. color: #333;
  491. background: #fff;
  492. }
  493. .complete-content {
  494. white-space: pre-wrap;
  495. word-break: break-word;
  496. font-family: monospace;
  497. line-height: 1.5;
  498. }
  499. </style>
  500. </head>
  501. <body class="container FlexRow">
  502. <div id="app" class="main00 FlexColumn">
  503. <div class="ListTop FlexRow">
  504. <div class="ListTop3 FlexRow" style="width: 100%; justify-content: space-between;">
  505. <div class="title">数据库管理</div>
  506. </div>
  507. </div>
  508. <div class="main0 FlexRow">
  509. <!-- 左侧表格列表 -->
  510. <div class="tables-panel" style="position: relative;">
  511. <div v-if="isTablesLoading" class="loading-overlay">
  512. <div class="loading-spinner"></div>
  513. </div>
  514. <div class="search-box FlexRow">
  515. <div style="position: relative;">
  516. <input type="text" class="search-input" v-model="searchText" @keyup.enter="searchTables"
  517. placeholder="搜索表格...">
  518. <div class="clear-btn" v-show="searchText" @click="clearSearch">
  519. <span class="clear-x"></span>
  520. </div>
  521. </div>
  522. <div class="btn33 FlexRow" @click="searchTables">
  523. <img title="搜索" alt="搜索"
  524. src="https://kylx365-1253256735.file.myqcloud.com/web/universalpic_search_gray_30x30.png"
  525. style="width: 20px; height: 20px;" />
  526. </div>
  527. </div>
  528. <!-- 表格列表 -->
  529. <div class="table-list">
  530. <div v-if="tables && tables.length > 0">
  531. <div v-for="(table, index) in filteredTables" :key="index" @click="selectTable(table)"
  532. :class="['table-item', { active: selectedTable === table }]">
  533. <div class="table-name">{{ table }}</div>
  534. <div class="table-comment" v-if="tableComments[table]">{{ tableComments[table] }}</div>
  535. </div>
  536. <div v-if="filteredTables.length === 0" class="no-data">
  537. 没有匹配的表格
  538. </div>
  539. </div>
  540. <div v-else-if="tables && tables.length === 0" class="no-data">
  541. 未找到表格
  542. </div>
  543. <div v-else class="no-data">
  544. 加载中...
  545. </div>
  546. </div>
  547. </div>
  548. <!-- 右侧内容区域 -->
  549. <div class="content-panel">
  550. <!-- SQL编辑器 -->
  551. <div class="sql-editor">
  552. <!-- 左侧SQL编辑区域 -->
  553. <div class="sql-editor-left">
  554. <textarea class="sql-textarea" v-model="sqlQuery" placeholder="输入SQL查询语句..."></textarea>
  555. <div class="btn-group">
  556. <select class="btn btn-default" v-model="selectedLimit" @change="updateQueryLimit"
  557. style="margin-right: 8px;">
  558. <option v-for="limit in limitOptions" :key="limit" :value="limit">
  559. LIMIT {{ limit }}
  560. </option>
  561. </select>
  562. <button type="button" class="btn btn-default" @click="clearQuery">清空</button>
  563. <button type="button" class="btn btn-primary" @click="executeQuery">执行</button>
  564. </div>
  565. </div>
  566. <!-- 右侧字段列表 -->
  567. <div class="sql-editor-right">
  568. <div v-if="isColumnsLoading" class="loading-overlay">
  569. <div class="loading-spinner"></div>
  570. </div>
  571. <div class="columns-list">
  572. <div v-if="tableColumnsList && tableColumnsList.length > 0">
  573. <div v-for="(column, index) in tableColumnsList" :key="index" class="column-item"
  574. @click="copyColumnName(column.name)" @dblclick="insertColumnName(column.name)">
  575. <div class="column-left">
  576. <div class="column-name">{{ column.name }}</div>
  577. <div class="column-comment" v-if="column.comment">{{ column.comment }}</div>
  578. </div>
  579. <div class="column-right">
  580. <div class="column-type">{{ column.type }}</div>
  581. </div>
  582. </div>
  583. </div>
  584. <div v-else class="no-data">
  585. 请选择表格查看字段列表
  586. </div>
  587. </div>
  588. </div>
  589. </div>
  590. <!-- 查询结果 -->
  591. <div class="results-panel" style="position: relative;">
  592. <div v-if="isQueryLoading" class="loading-overlay">
  593. <div class="loading-spinner"></div>
  594. </div>
  595. <div v-if="queryExecuted" class="table-container">
  596. <div v-if="allResults && allResults.length > 0">
  597. <div style="margin-bottom: 10px; color: #666;">
  598. 显示 {{ queryResults.length }} 条记录,共 {{ allResults.length }} 条
  599. </div>
  600. <div v-if="isQueryLoading" class="loading-indicator">
  601. <div class="spinner"></div>
  602. <div>数据加载中...</div>
  603. </div>
  604. <div v-else-if="!tableColumns || tableColumns.length === 0" class="no-data">
  605. 未检测到有效的表格列
  606. </div>
  607. <template v-else>
  608. <table class="data-table">
  609. <thead>
  610. <tr>
  611. <th v-for="(column, index) in tableColumns"
  612. :key="'th-'+index"
  613. v-show="!column.startsWith('__')">
  614. {{ column }}
  615. </th>
  616. </tr>
  617. </thead>
  618. <tbody>
  619. <tr v-for="(row, rowIndex) in queryResults"
  620. :key="'tr-'+rowIndex">
  621. <td v-for="(column, colIndex) in tableColumns"
  622. :key="'td-'+rowIndex+'-'+colIndex"
  623. v-show="!column.startsWith('__')"
  624. @dblclick="formatTableCell(row[column]).endsWith('...') ? showCompleteField(row, column) : copyColumnName(formatTableCell(row[column]))"
  625. >
  626. {{ formatTableCell(row[column]) }}
  627. </td>
  628. </tr>
  629. </tbody>
  630. </table>
  631. <div v-if="queryResults.length === 0" class="no-data">
  632. 没有可显示的数据
  633. <div v-if="allResults.length > 0" style="margin-top: 10px; font-size: 12px; color: #999;">
  634. 共 {{allResults.length}} 条数据
  635. </div>
  636. </div>
  637. </template>
  638. </div>
  639. <div v-else class="no-data">
  640. 查询未返回数据
  641. </div>
  642. </div>
  643. <div v-else-if="queryExecuted" class="no-data">
  644. 查询未返回数据
  645. </div>
  646. <div v-else class="no-data">
  647. 请选择表格并执行查询
  648. </div>
  649. </div>
  650. </div>
  651. </div>
  652. <!-- Toast提示 -->
  653. <div class="toast" :class="{ show: showToast, [toastType]: showToast }">{{ toastMessage }}</div>
  654. <!-- 完整内容弹出层 -->
  655. <div v-if="showCompleteContent" class="complete-content-overlay" @click.self="closeCompleteContent">
  656. <div class="complete-content-container">
  657. <button class="close-btn" @click="closeCompleteContent">×</button>
  658. <div class="complete-content" @dblclick="copyToClipboard(completeContent)">
  659. {{ completeContent }}
  660. </div>
  661. </div>
  662. </div>
  663. </div>
  664. <script>
  665. new Vue({
  666. el: '#app',
  667. data: {
  668. searchText: '',
  669. tables: [], // 存储表格名称列表
  670. tableComments: {}, // 存储表格注释,格式为 {表名: 注释}
  671. filteredTables: [],
  672. selectedTable: null,
  673. sqlQuery: '',
  674. queryResults: [],
  675. tableColumns: [],
  676. showToast: false,
  677. toastMessage: '',
  678. toastType: 'info',
  679. isTablesLoading: false,
  680. isQueryLoading: false,
  681. isColumnsLoading: false,
  682. queryExecuted: false,
  683. allResults: [],
  684. tableColumnsList: [], // 存储表格字段列表
  685. limitOptions: [10, 100, 500, 1000, 2000, 3000], // LIMIT选项
  686. selectedLimit: 100, // 默认选中的LIMIT值
  687. showCompleteContent: false,
  688. completeContent: '',
  689. currentRow: null,
  690. currentColumn: null
  691. },
  692. computed: {
  693. },
  694. mounted() {
  695. this.loadTables();
  696. },
  697. methods: {
  698. // 加载数据库表格列表
  699. loadTables() {
  700. this.isTablesLoading = true;
  701. this.tableComments = {};
  702. // 调用后端API获取表格列表
  703. fetch('/api/GetKylx365Tables')
  704. .then(response => {
  705. // console.log('API响应状态:', response.status);
  706. if (!response.ok) {
  707. throw new Error(`HTTP error! status: ${response.status}`);
  708. }
  709. return response.json();
  710. })
  711. .then(data => {
  712. //console.log('API返回数据:', data);
  713. // 检查数据格式,处理可能的不同响应结构
  714. //console.log('检查数据格式:', data);
  715. // 尝试确定数据的实际结构
  716. let resultData = null;
  717. if (data && data.result && Array.isArray(data.result)) {
  718. //console.log('标准格式: data.result 是数组');
  719. resultData = data.result;
  720. } else if (data && Array.isArray(data)) {
  721. //console.log('替代格式: data 本身是数组');
  722. resultData = data;
  723. } else if (data && typeof data === 'object') {
  724. //console.log('检查对象中的数组属性');
  725. // 尝试在对象中找到数组属性
  726. for (const key in data) {
  727. if (Array.isArray(data[key])) {
  728. //console.log(`找到数组属性: ${key}`);
  729. resultData = data[key];
  730. break;
  731. }
  732. }
  733. }
  734. if (resultData && Array.isArray(resultData)) {
  735. //console.log('数据验证通过,开始处理数据');
  736. //console.log('数据结果长度:', resultData.length);
  737. if (resultData.length > 0 && typeof resultData[0] === 'object' && !Array.isArray(resultData[0])) {
  738. //console.log('检测到对象数组格式,第一个对象:', resultData[0]);
  739. //console.log('第一个对象的属性:', Object.keys(resultData[0]));
  740. // 检查并适应不同的属性名称
  741. const firstItem = resultData[0];
  742. const tableNameKey = 'table_name' in firstItem ? 'table_name' :
  743. 'tableName' in firstItem ? 'tableName' :
  744. 'name' in firstItem ? 'name' : null;
  745. const tableCommentKey = 'table_comment' in firstItem ? 'table_comment' :
  746. 'tableComment' in firstItem ? 'tableComment' :
  747. 'comment' in firstItem ? 'comment' : null;
  748. //console.log('使用的属性名:', { tableNameKey, tableCommentKey });
  749. if (tableNameKey) {
  750. // 如果返回的是对象数组(包含name和comment)
  751. this.tables = resultData.map(item => {
  752. const tableName = item[tableNameKey] || '';
  753. //console.log('处理表名:', tableName);
  754. return tableName;
  755. }).filter(name => name); // 过滤掉空表名
  756. //console.log('处理后的表格列表:', this.tables);
  757. // 存储表格注释
  758. if (tableCommentKey) {
  759. resultData.forEach(item => {
  760. const tableName = item[tableNameKey] || '';
  761. const tableComment = item[tableCommentKey] || '';
  762. if (tableName && tableComment) {
  763. this.tableComments[tableName] = tableComment;
  764. }
  765. });
  766. //console.log('处理后的表格注释:', this.tableComments);
  767. }
  768. } else {
  769. //console.error('无法找到表名属性');
  770. this.showToastMessage('数据格式错误:无法找到表名属性', 'error');
  771. }
  772. } else {
  773. //console.log('检测到简单数组格式');
  774. // 如果返回的只是表名数组
  775. this.tables = resultData.filter(name => {
  776. //console.log('处理表名:', name);
  777. return name && typeof name === 'string';
  778. });
  779. //console.log('处理后的表格列表:', this.tables);
  780. }
  781. this.filteredTables = [...this.tables];
  782. // console.log('更新后的过滤表格列表:', this.filteredTables);
  783. } else {
  784. this.tables = [];
  785. this.filteredTables = [];
  786. this.showToastMessage('获取表格列表失败', 'error');
  787. }
  788. this.isTablesLoading = false;
  789. })
  790. .catch(error => {
  791. //console.error('获取表格列表失败详细错误:', error);
  792. this.showToastMessage(`获取表格列表失败: ${error.message}`, 'error');
  793. this.isTablesLoading = false;
  794. });
  795. },
  796. // 搜索表格
  797. searchTables() {
  798. //console.log('开始搜索表格,搜索文本:', this.searchText);
  799. if (!this.searchText.trim()) {
  800. //console.log('搜索文本为空,显示所有表格');
  801. this.filteredTables = [...this.tables];
  802. return;
  803. }
  804. const searchTerm = this.searchText.toLowerCase();
  805. //console.log('过滤表格列表,搜索条件:', searchTerm);
  806. this.filteredTables = this.tables.filter(table => {
  807. const tableName = table.toLowerCase();
  808. const tableComment = (this.tableComments[table] || '').toLowerCase();
  809. const matches = tableName.includes(searchTerm) || tableComment.includes(searchTerm);
  810. // if (matches) {
  811. // console.log(`表格匹配: ${table} (${this.tableComments[table] || '无注释'})`);
  812. // }
  813. return matches;
  814. });
  815. //console.log('过滤后的表格数量:', this.filteredTables.length);
  816. },
  817. // 清空搜索
  818. clearSearch() {
  819. this.searchText = '';
  820. this.filteredTables = [...this.tables];
  821. },
  822. // 选择表格
  823. selectTable(table) {
  824. this.selectedTable = table;
  825. // 获取表格字段列表
  826. this.loadTableColumns(table);
  827. // 监听字段列表变化,当加载完成后构建查询语句
  828. this.$watch('tableColumnsList', (newVal) => {
  829. if (newVal && newVal.length > 0) {
  830. const columns = newVal.map(col => `\`${col.name}\``).join(',');
  831. this.sqlQuery = `SELECT ${columns} FROM \`${table}\` LIMIT 100;`;
  832. } else {
  833. this.sqlQuery = `SELECT * FROM \`${table}\` LIMIT 100;`;
  834. }
  835. }, {immediate: true});
  836. },
  837. // 加载表格字段列表
  838. loadTableColumns(tableName) {
  839. this.isColumnsLoading = true;
  840. this.tableColumnsList = [];
  841. // 调用API获取表格字段列表
  842. fetch(`/api/GetKylx365TableColumnByTable?table=${encodeURIComponent(tableName)}`)
  843. .then(response => response.json())
  844. .then(data => {
  845. //console.log('收到的原始数据:', data);
  846. // 验证数据格式
  847. if (!data) {
  848. //console.error('接收到空数据');
  849. throw new Error('接收到空数据');
  850. }
  851. // 尝试确定数据的实际结构
  852. let columnsData = null;
  853. if (data && data.result && Array.isArray(data.result)) {
  854. //console.log('标准格式: data.result 是数组');
  855. columnsData = data.result;
  856. } else if (data && Array.isArray(data)) {
  857. //console.log('替代格式: data 本身是数组');
  858. columnsData = data;
  859. } else if (data && typeof data === 'object') {
  860. //console.log('检查对象中的数组属性');
  861. // 尝试在对象中找到数组属性
  862. for (const key in data) {
  863. if (Array.isArray(data[key])) {
  864. //console.log(`找到数组属性: ${key}`);
  865. columnsData = data[key];
  866. break;
  867. }
  868. }
  869. }
  870. if (columnsData && Array.isArray(columnsData)) {
  871. //console.log('数据验证通过,开始处理数据');
  872. //console.log('字段数据长度:', columnsData.length);
  873. if (columnsData.length > 0) {
  874. // 检查第一个元素的格式
  875. const firstItem = columnsData[0];
  876. //console.log('第一个字段项:', firstItem);
  877. if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
  878. // 检查并适应不同的属性名称
  879. const nameKey = 'Field' in firstItem ? 'Field' :
  880. 'name' in firstItem ? 'name' :
  881. 'column_name' in firstItem ? 'column_name' :
  882. 'columnName' in firstItem ? 'columnName' :
  883. 'field' in firstItem ? 'field' : null;
  884. const typeKey = 'Type' in firstItem ? 'Type' :
  885. 'type' in firstItem ? 'type' :
  886. 'column_type' in firstItem ? 'column_type' :
  887. 'columnType' in firstItem ? 'columnType' :
  888. 'data_type' in firstItem ? 'data_type' : null;
  889. const commentKey = 'Comment' in firstItem ? 'Comment' :
  890. 'comment' in firstItem ? 'comment' :
  891. 'column_comment' in firstItem ? 'column_comment' :
  892. 'columnComment' in firstItem ? 'columnComment' : null;
  893. // 组合Default和Extra作为注释
  894. const getComment = (item) => {
  895. let comment = [];
  896. if (item['Default']) {
  897. comment.push(`默认值: ${item['Default']}`);
  898. }
  899. if (item['Extra'] && item['Extra'] !== '') {
  900. comment.push(item['Extra']);
  901. }
  902. if (item['Null'] === 'NO') {
  903. comment.push('不可为空');
  904. }
  905. return comment.join(', ');
  906. };
  907. //console.log('使用的属性名:', { nameKey, typeKey, commentKey });
  908. if (nameKey) {
  909. // 转换为标准格式
  910. this.tableColumnsList = columnsData.map(item => {
  911. return {
  912. name: item[nameKey] || '',
  913. type: typeKey ? (item[typeKey] || '') : '',
  914. comment: [
  915. commentKey ? (item[commentKey] || '') : '',
  916. getComment(item)
  917. ].filter(Boolean).join(' | ')
  918. };
  919. }).filter(col => col.name); // 过滤掉没有名称的列
  920. //console.log('处理后的字段列表:', this.tableColumnsList);
  921. } else {
  922. console.error('无法找到字段名属性');
  923. this.showToastMessage('数据格式错误:无法找到字段名属性', 'error');
  924. this.tableColumnsList = [];
  925. }
  926. } else if (typeof firstItem === 'string') {
  927. // 如果只是字段名数组
  928. this.tableColumnsList = columnsData.map(name => {
  929. return {
  930. name: name,
  931. type: '',
  932. comment: ''
  933. };
  934. });
  935. //console.log('处理后的字段列表(仅名称):', this.tableColumnsList);
  936. } else {
  937. console.error('未知的字段数据格式');
  938. this.showToastMessage('未知的字段数据格式', 'error');
  939. this.tableColumnsList = [];
  940. }
  941. } else {
  942. //console.log('字段列表为空');
  943. this.tableColumnsList = [];
  944. }
  945. } else {
  946. //console.error('无法找到有效的字段数据');
  947. this.tableColumnsList = [];
  948. this.showToastMessage('获取字段列表失败', 'error');
  949. }
  950. this.isColumnsLoading = false;
  951. })
  952. .catch(error => {
  953. this.showToastMessage('获取字段列表失败,请稍后重试', 'error');
  954. this.isColumnsLoading = false;
  955. });
  956. },
  957. // 清空查询
  958. clearQuery() {
  959. this.sqlQuery = '';
  960. },
  961. // 复制字段名称到剪贴板
  962. copyColumnName(columnName) {
  963. // 创建一个临时文本区域元素
  964. const textarea = document.createElement('textarea');
  965. textarea.value = columnName;
  966. textarea.setAttribute('readonly', '');
  967. textarea.style.position = 'absolute';
  968. textarea.style.left = '-9999px';
  969. document.body.appendChild(textarea);
  970. // 选择文本并复制
  971. textarea.select();
  972. let success = false;
  973. try {
  974. success = document.execCommand('copy');
  975. this.showToastMessage(`已复制字段 "${columnName}" 到剪贴板`, 'success');
  976. } catch (err) {
  977. success = false;
  978. this.showToastMessage('复制失败,请手动复制', 'error');
  979. console.error('复制到剪贴板失败:', err);
  980. }
  981. // 移除临时元素
  982. document.body.removeChild(textarea);
  983. // 如果浏览器支持现代剪贴板API,也尝试使用它
  984. if (navigator.clipboard && window.isSecureContext) {
  985. navigator.clipboard.writeText(columnName).catch(err => {
  986. console.log('剪贴板API失败,但已使用备用方法', err);
  987. });
  988. }
  989. return success;
  990. },
  991. // 插入字段名称到SQL查询(双击时触发)
  992. insertColumnName(columnName) {
  993. // 获取文本框元素
  994. const textarea = document.querySelector('.sql-textarea');
  995. // 添加反引号的字段名
  996. const quotedColumnName = `\`${columnName}\``;
  997. // 如果文本框存在
  998. if (textarea) {
  999. // 获取当前光标位置
  1000. const startPos = textarea.selectionStart;
  1001. const endPos = textarea.selectionEnd;
  1002. // 在光标位置插入字段名称
  1003. const textBefore = this.sqlQuery.substring(0, startPos);
  1004. const textAfter = this.sqlQuery.substring(endPos);
  1005. // 更新SQL查询
  1006. this.sqlQuery = textBefore + quotedColumnName + textAfter;
  1007. // 设置新的光标位置
  1008. this.$nextTick(() => {
  1009. textarea.focus();
  1010. textarea.selectionStart = startPos + quotedColumnName.length;
  1011. textarea.selectionEnd = startPos + quotedColumnName.length;
  1012. });
  1013. this.showToastMessage(`已插入字段 "${columnName}" 到查询`, 'info');
  1014. } else {
  1015. // 如果无法获取文本框元素,则直接在末尾添加
  1016. this.sqlQuery += ' ' + quotedColumnName;
  1017. }
  1018. },
  1019. // 更新SQL查询中的LIMIT值
  1020. updateQueryLimit() {
  1021. // 检查SQL查询是否为空
  1022. if (!this.sqlQuery.trim()) {
  1023. return;
  1024. }
  1025. // 正则表达式匹配LIMIT子句
  1026. const limitRegex = /\bLIMIT\s+\d+\s*;?\s*$/i;
  1027. if (limitRegex.test(this.sqlQuery)) {
  1028. // 如果已经有LIMIT子句,替换数字
  1029. this.sqlQuery = this.sqlQuery.replace(limitRegex, `LIMIT ${this.selectedLimit}${this.sqlQuery.endsWith(';') ? ';' : ''}`);
  1030. } else {
  1031. // 如果没有LIMIT子句,添加到末尾
  1032. // 检查SQL是否以分号结尾
  1033. if (this.sqlQuery.trim().endsWith(';')) {
  1034. // 在分号前添加LIMIT
  1035. this.sqlQuery = this.sqlQuery.replace(/;\s*$/, ` LIMIT ${this.selectedLimit};`);
  1036. } else {
  1037. // 直接在末尾添加LIMIT
  1038. this.sqlQuery = this.sqlQuery.trim() + ` LIMIT ${this.selectedLimit};`;
  1039. }
  1040. }
  1041. this.showToastMessage(`已设置查询限制为 ${this.selectedLimit} 条记录`, 'info');
  1042. },
  1043. // 执行查询
  1044. executeQuery() {
  1045. if (!this.sqlQuery.trim()) {
  1046. this.showToastMessage('请输入SQL查询语句', 'info');
  1047. return;
  1048. }
  1049. this.isQueryLoading = true;
  1050. this.queryExecuted = true;
  1051. // 重置查询结果
  1052. this.queryResults = [];
  1053. this.tableColumns = [];
  1054. this.allResults = [];
  1055. // 调用后端API执行SQL查询,使用RunKylx365DBSql接口
  1056. const apiUrl = '/api/RunKylx365DBSql';
  1057. fetch(apiUrl, {
  1058. method: 'POST',
  1059. headers: {
  1060. 'Content-Type': 'application/json',
  1061. 'Accept': 'application/json'
  1062. },
  1063. body: JSON.stringify({
  1064. sql: this.sqlQuery.trim()
  1065. })
  1066. })
  1067. .then(response => {
  1068. if (!response.ok) {
  1069. throw new Error(`HTTP error! status: ${response.status}`);
  1070. }
  1071. return response.json();
  1072. })
  1073. .then(data => {
  1074. // 检查数据格式
  1075. let resultData = null;
  1076. let errorMessage = null;
  1077. // 检查标准格式 {"errcode": 10000, result: [...]}
  1078. if (data && typeof data === 'object' && 'errcode' in data) {
  1079. if (data.errcode !== 10000) {
  1080. errorMessage = data.errmsg || '查询返回错误';
  1081. console.error('查询返回错误:', errorMessage);
  1082. } else if (data.result && Array.isArray(data.result)) {
  1083. resultData = data.result;
  1084. }
  1085. }
  1086. // 检查其他可能的格式
  1087. else if (data && Array.isArray(data)) {
  1088. resultData = data;
  1089. } else if (data && typeof data === 'object') {
  1090. // 尝试在对象中找到数组属性
  1091. for (const key in data) {
  1092. if (Array.isArray(data[key])) {
  1093. resultData = data[key];
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. if (errorMessage) {
  1099. console.error('查询返回错误:', errorMessage);
  1100. this.showToastMessage(errorMessage, 'error');
  1101. this.queryResults = [];
  1102. this.tableColumns = [];
  1103. } else if (resultData && Array.isArray(resultData)) {
  1104. this.processQueryResults(resultData);
  1105. } else {
  1106. this.queryResults = [];
  1107. this.tableColumns = [];
  1108. this.showToastMessage('查询未返回数据', 'info');
  1109. }
  1110. this.isQueryLoading = false;
  1111. })
  1112. .catch(error => {
  1113. this.showToastMessage('执行查询失败,请稍后重试', 'error');
  1114. this.isQueryLoading = false;
  1115. });
  1116. },
  1117. // 处理查询结果
  1118. processQueryResults(results) {
  1119. // 确保结果是一个数组
  1120. if (!results || !Array.isArray(results) || results.length === 0) {
  1121. console.warn('结果为空或非数组');
  1122. this.$set(this, 'queryResults', []);
  1123. this.$set(this, 'tableColumns', []);
  1124. this.$set(this, 'allResults', []);
  1125. this.showToastMessage('查询未返回数据', 'info');
  1126. console.groupEnd();
  1127. return;
  1128. }
  1129. // 创建深拷贝避免引用问题
  1130. const safeResults = JSON.parse(JSON.stringify(results));
  1131. try {
  1132. // 直接使用原始结果数据
  1133. const formattedResults = results;
  1134. // 使用Vue.set确保响应式更新
  1135. this.$set(this, 'allResults', formattedResults);
  1136. // 提取表格列,排除Vue内部属性
  1137. if (formattedResults.length > 0) {
  1138. // 确保所有行有相同的列结构
  1139. const allColumns = new Set();
  1140. formattedResults.forEach(row => {
  1141. Object.keys(row).forEach(key => {
  1142. if (!key.startsWith('__')) {
  1143. allColumns.add(key);
  1144. }
  1145. });
  1146. });
  1147. const columns = Array.from(allColumns);
  1148. // 验证数据完整性
  1149. const missingData = formattedResults.filter(row => {
  1150. return columns.some(col => !(col in row));
  1151. });
  1152. if (missingData.length > 0) {
  1153. console.warn('发现数据不完整:', missingData);
  1154. }
  1155. this.$set(this, 'tableColumns', columns);
  1156. } else {
  1157. this.$set(this, 'tableColumns', []);
  1158. }
  1159. this.$set(this, 'queryResults', formattedResults);
  1160. this.showToastMessage(`查询成功,返回 ${formattedResults.length} 条记录`, 'success');
  1161. } catch (error) {
  1162. console.error('处理查询结果时出错:', error);
  1163. this.showToastMessage('处理查询结果时出错: ' + error.message, 'error');
  1164. this.queryResults = [];
  1165. this.tableColumns = [];
  1166. this.allResults = [];
  1167. }
  1168. },
  1169. // 格式化表格单元格内容
  1170. formatTableCell(value) {
  1171. if (value === null || value === undefined) {
  1172. return 'NULL';
  1173. }
  1174. if (typeof value === 'object') {
  1175. try {
  1176. return JSON.stringify(value);
  1177. } catch (e) {
  1178. return '[Object]';
  1179. }
  1180. }
  1181. if (typeof value === 'boolean') {
  1182. return value ? '是' : '否';
  1183. }
  1184. return value;
  1185. },
  1186. // 显示完整字段内容
  1187. showCompleteField(row, column) {
  1188. // 检查当前单元格内容是否有省略
  1189. const cellContent = this.formatTableCell(row[column]);
  1190. if (!cellContent.includes('...')) {
  1191. return;
  1192. }
  1193. this.currentRow = row;
  1194. this.currentColumn = column;
  1195. this.showCompleteContent = true;
  1196. this.completeContent = '加载中...';
  1197. // 查找主键字段 - 优先使用实际存在的列名
  1198. let primaryKey = null;
  1199. // 检查当前行是否有id字段
  1200. if ('id' in row) {
  1201. primaryKey = 'id';
  1202. }
  1203. // 检查是否有ID字段(大写)
  1204. else if ('ID' in row) {
  1205. primaryKey = 'ID';
  1206. }
  1207. // 检查表字段列表中是否有id字段
  1208. else if (this.tableColumnsList.some(col => col.name.toLowerCase() === 'id')) {
  1209. primaryKey = 'id';
  1210. }
  1211. // 检查表字段列表中是否有ID字段(大写)
  1212. else if (this.tableColumnsList.some(col => col.name === 'ID')) {
  1213. primaryKey = 'ID';
  1214. }
  1215. // 使用第一个存在的字段作为主键
  1216. else if (this.tableColumnsList.length > 0) {
  1217. const firstCol = this.tableColumnsList[0].name;
  1218. if (firstCol in row) {
  1219. primaryKey = firstCol;
  1220. }
  1221. }
  1222. if (!primaryKey || !row[primaryKey]) {
  1223. this.completeContent = '无法确定主键字段或主键值为空';
  1224. return;
  1225. }
  1226. // 构建查询SQL
  1227. const sql = `SELECT \`${column}\` FROM \`${this.selectedTable}\` WHERE \`${primaryKey}\`='${row[primaryKey]}';`;
  1228. // 调用API获取完整数据
  1229. fetch('/api/RunKylx365DBSql', {
  1230. method: 'POST',
  1231. headers: {
  1232. 'Content-Type': 'application/json',
  1233. },
  1234. body: JSON.stringify({
  1235. sql: sql,
  1236. IsCompleteField: true
  1237. })
  1238. })
  1239. .then(response => response.json())
  1240. .then(data => {
  1241. if (data && data.result && data.result.length > 0) {
  1242. this.completeContent = this.formatTableCell(data.result[0][column]);
  1243. } else {
  1244. this.completeContent = '未获取到完整数据';
  1245. }
  1246. })
  1247. .catch(error => {
  1248. this.completeContent = '获取完整数据失败: ' + error.message;
  1249. });
  1250. },
  1251. // 关闭完整内容弹出层
  1252. closeCompleteContent() {
  1253. this.showCompleteContent = false;
  1254. this.completeContent = '';
  1255. this.currentRow = null;
  1256. this.currentColumn = null;
  1257. },
  1258. // 复制到剪贴板
  1259. copyToClipboard(text) {
  1260. navigator.clipboard.writeText(text).then(() => {
  1261. this.showToastMessage('已复制到剪贴板', 'success');
  1262. }).catch(err => {
  1263. this.showToastMessage('复制失败: ' + err, 'error');
  1264. });
  1265. },
  1266. // 显示提示消息
  1267. showToastMessage(message, type = 'info') {
  1268. this.toastMessage = message;
  1269. this.toastType = type;
  1270. this.showToast = true;
  1271. // 3秒后自动隐藏
  1272. setTimeout(() => {
  1273. this.showToast = false;
  1274. }, 3000);
  1275. },
  1276. }
  1277. });
  1278. </script>
  1279. </body>
  1280. </html>