kylx365_db_admin.html 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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;">
  550. <button @click="forceRender" class="btn btn-default" style="padding: 5px 10px; font-size: 12px;">
  551. 强制刷新显示
  552. </button>
  553. <div style="font-size: 12px; color: #999; margin-top: 5px;">
  554. 检测到 {{allResults.length}} 条数据但未显示
  555. </div>
  556. </div>
  557. </div>
  558. </template>
  559. <div v-if="queryResults && queryResults.length > 0" style="margin-top: 10px; color: #666; font-size: 12px;">
  560. 调试信息:
  561. 表头列数: {{ tableColumns.length }},
  562. 数据行数: {{ queryResults.length }},
  563. 第一条数据: {{ JSON.stringify(queryResults[0]) }}
  564. </div>
  565. </div>
  566. <div v-else class="no-data">
  567. 查询未返回数据
  568. </div>
  569. <!-- 分页控件 -->
  570. <div class="pagination" v-if="totalPages > 1 && allResults.length > pageSize">
  571. <button class="pagination-btn" :class="{ disabled: currentPage === 1 }"
  572. @click="changePage(1)">
  573. 首页
  574. </button>
  575. <button class="pagination-btn" :class="{ disabled: currentPage === 1 }"
  576. @click="changePage(currentPage - 1)">
  577. 上一页
  578. </button>
  579. <button v-for="page in displayedPages" :key="page" class="pagination-btn"
  580. :class="{ active: currentPage === page }" @click="changePage(page)">
  581. {{ page }}
  582. </button>
  583. <button class="pagination-btn" :class="{ disabled: currentPage === totalPages }"
  584. @click="changePage(currentPage + 1)">
  585. 下一页
  586. </button>
  587. <button class="pagination-btn" :class="{ disabled: currentPage === totalPages }"
  588. @click="changePage(totalPages)">
  589. 末页
  590. </button>
  591. </div>
  592. </div>
  593. <div v-else-if="queryExecuted" class="no-data">
  594. 查询未返回数据
  595. </div>
  596. <div v-else class="no-data">
  597. 请选择表格并执行查询
  598. </div>
  599. </div>
  600. </div>
  601. </div>
  602. <!-- Toast提示 -->
  603. <div class="toast" :class="{ show: showToast, [toastType]: showToast }">{{ toastMessage }}</div>
  604. </div>
  605. <script>
  606. new Vue({
  607. el: '#app',
  608. data: {
  609. searchText: '',
  610. tables: [], // 存储表格名称列表
  611. tableComments: {}, // 存储表格注释,格式为 {表名: 注释}
  612. filteredTables: [],
  613. selectedTable: null,
  614. sqlQuery: '',
  615. queryResults: [],
  616. tableColumns: [],
  617. showToast: false,
  618. toastMessage: '',
  619. toastType: 'info',
  620. isTablesLoading: false,
  621. isQueryLoading: false,
  622. isColumnsLoading: false,
  623. queryExecuted: false,
  624. currentPage: 1,
  625. pageSize: 100,
  626. totalPages: 1,
  627. allResults: [],
  628. tableColumnsList: [], // 存储表格字段列表
  629. limitOptions: [10, 100, 500, 1000, 2000, 3000], // LIMIT选项
  630. selectedLimit: 100 // 默认选中的LIMIT值
  631. },
  632. computed: {
  633. displayedPages() {
  634. const pages = [];
  635. const maxVisiblePages = 5;
  636. let startPage = Math.max(1, this.currentPage - Math.floor(maxVisiblePages / 2));
  637. let endPage = Math.min(this.totalPages, startPage + maxVisiblePages - 1);
  638. if (endPage - startPage + 1 < maxVisiblePages) {
  639. startPage = Math.max(1, endPage - maxVisiblePages + 1);
  640. }
  641. for (let i = startPage; i <= endPage; i++) {
  642. pages.push(i);
  643. }
  644. return pages;
  645. }
  646. },
  647. mounted() {
  648. this.loadTables();
  649. },
  650. methods: {
  651. // 加载数据库表格列表
  652. loadTables() {
  653. this.isTablesLoading = true;
  654. this.tableComments = {};
  655. // 调用后端API获取表格列表
  656. fetch('/api/GetKylx365Tables')
  657. .then(response => {
  658. console.log('API响应状态:', response.status);
  659. if (!response.ok) {
  660. throw new Error(`HTTP error! status: ${response.status}`);
  661. }
  662. return response.json();
  663. })
  664. .then(data => {
  665. //console.log('API返回数据:', data);
  666. // 检查数据格式,处理可能的不同响应结构
  667. //console.log('检查数据格式:', data);
  668. // 尝试确定数据的实际结构
  669. let resultData = null;
  670. if (data && data.result && Array.isArray(data.result)) {
  671. //console.log('标准格式: data.result 是数组');
  672. resultData = data.result;
  673. } else if (data && Array.isArray(data)) {
  674. //console.log('替代格式: data 本身是数组');
  675. resultData = data;
  676. } else if (data && typeof data === 'object') {
  677. //console.log('检查对象中的数组属性');
  678. // 尝试在对象中找到数组属性
  679. for (const key in data) {
  680. if (Array.isArray(data[key])) {
  681. //console.log(`找到数组属性: ${key}`);
  682. resultData = data[key];
  683. break;
  684. }
  685. }
  686. }
  687. if (resultData && Array.isArray(resultData)) {
  688. //console.log('数据验证通过,开始处理数据');
  689. //console.log('数据结果长度:', resultData.length);
  690. if (resultData.length > 0 && typeof resultData[0] === 'object' && !Array.isArray(resultData[0])) {
  691. //console.log('检测到对象数组格式,第一个对象:', resultData[0]);
  692. //console.log('第一个对象的属性:', Object.keys(resultData[0]));
  693. // 检查并适应不同的属性名称
  694. const firstItem = resultData[0];
  695. const tableNameKey = 'table_name' in firstItem ? 'table_name' :
  696. 'tableName' in firstItem ? 'tableName' :
  697. 'name' in firstItem ? 'name' : null;
  698. const tableCommentKey = 'table_comment' in firstItem ? 'table_comment' :
  699. 'tableComment' in firstItem ? 'tableComment' :
  700. 'comment' in firstItem ? 'comment' : null;
  701. //console.log('使用的属性名:', { tableNameKey, tableCommentKey });
  702. if (tableNameKey) {
  703. // 如果返回的是对象数组(包含name和comment)
  704. this.tables = resultData.map(item => {
  705. const tableName = item[tableNameKey] || '';
  706. //console.log('处理表名:', tableName);
  707. return tableName;
  708. }).filter(name => name); // 过滤掉空表名
  709. //console.log('处理后的表格列表:', this.tables);
  710. // 存储表格注释
  711. if (tableCommentKey) {
  712. resultData.forEach(item => {
  713. const tableName = item[tableNameKey] || '';
  714. const tableComment = item[tableCommentKey] || '';
  715. if (tableName && tableComment) {
  716. this.tableComments[tableName] = tableComment;
  717. }
  718. });
  719. //console.log('处理后的表格注释:', this.tableComments);
  720. }
  721. } else {
  722. //console.error('无法找到表名属性');
  723. this.showToastMessage('数据格式错误:无法找到表名属性', 'error');
  724. }
  725. } else {
  726. //console.log('检测到简单数组格式');
  727. // 如果返回的只是表名数组
  728. this.tables = resultData.filter(name => {
  729. //console.log('处理表名:', name);
  730. return name && typeof name === 'string';
  731. });
  732. //console.log('处理后的表格列表:', this.tables);
  733. }
  734. this.filteredTables = [...this.tables];
  735. // console.log('更新后的过滤表格列表:', this.filteredTables);
  736. } else {
  737. this.tables = [];
  738. this.filteredTables = [];
  739. this.showToastMessage('获取表格列表失败', 'error');
  740. }
  741. this.isTablesLoading = false;
  742. })
  743. .catch(error => {
  744. //console.error('获取表格列表失败详细错误:', error);
  745. this.showToastMessage(`获取表格列表失败: ${error.message}`, 'error');
  746. this.isTablesLoading = false;
  747. });
  748. },
  749. // 搜索表格
  750. searchTables() {
  751. //console.log('开始搜索表格,搜索文本:', this.searchText);
  752. if (!this.searchText.trim()) {
  753. //console.log('搜索文本为空,显示所有表格');
  754. this.filteredTables = [...this.tables];
  755. return;
  756. }
  757. const searchTerm = this.searchText.toLowerCase();
  758. //console.log('过滤表格列表,搜索条件:', searchTerm);
  759. this.filteredTables = this.tables.filter(table => {
  760. const tableName = table.toLowerCase();
  761. const tableComment = (this.tableComments[table] || '').toLowerCase();
  762. const matches = tableName.includes(searchTerm) || tableComment.includes(searchTerm);
  763. // if (matches) {
  764. // console.log(`表格匹配: ${table} (${this.tableComments[table] || '无注释'})`);
  765. // }
  766. return matches;
  767. });
  768. //console.log('过滤后的表格数量:', this.filteredTables.length);
  769. },
  770. // 清空搜索
  771. clearSearch() {
  772. this.searchText = '';
  773. this.filteredTables = [...this.tables];
  774. },
  775. // 选择表格
  776. selectTable(table) {
  777. this.selectedTable = table;
  778. this.sqlQuery = `SELECT * FROM ${table} LIMIT 100;`;
  779. // 获取表格字段列表
  780. this.loadTableColumns(table);
  781. },
  782. // 加载表格字段列表
  783. loadTableColumns(tableName) {
  784. this.isColumnsLoading = true;
  785. this.tableColumnsList = [];
  786. // 调用API获取表格字段列表
  787. fetch(`/api/GetKylx365TableColumnByTable?table=${encodeURIComponent(tableName)}`)
  788. .then(response => response.json())
  789. .then(data => {
  790. //console.log('收到的原始数据:', data);
  791. // 验证数据格式
  792. if (!data) {
  793. //console.error('接收到空数据');
  794. throw new Error('接收到空数据');
  795. }
  796. // 尝试确定数据的实际结构
  797. let columnsData = null;
  798. if (data && data.result && Array.isArray(data.result)) {
  799. //console.log('标准格式: data.result 是数组');
  800. columnsData = data.result;
  801. } else if (data && Array.isArray(data)) {
  802. //console.log('替代格式: data 本身是数组');
  803. columnsData = data;
  804. } else if (data && typeof data === 'object') {
  805. //console.log('检查对象中的数组属性');
  806. // 尝试在对象中找到数组属性
  807. for (const key in data) {
  808. if (Array.isArray(data[key])) {
  809. //console.log(`找到数组属性: ${key}`);
  810. columnsData = data[key];
  811. break;
  812. }
  813. }
  814. }
  815. if (columnsData && Array.isArray(columnsData)) {
  816. //console.log('数据验证通过,开始处理数据');
  817. //console.log('字段数据长度:', columnsData.length);
  818. if (columnsData.length > 0) {
  819. // 检查第一个元素的格式
  820. const firstItem = columnsData[0];
  821. //console.log('第一个字段项:', firstItem);
  822. if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
  823. // 检查并适应不同的属性名称
  824. const nameKey = 'Field' in firstItem ? 'Field' :
  825. 'name' in firstItem ? 'name' :
  826. 'column_name' in firstItem ? 'column_name' :
  827. 'columnName' in firstItem ? 'columnName' :
  828. 'field' in firstItem ? 'field' : null;
  829. const typeKey = 'Type' in firstItem ? 'Type' :
  830. 'type' in firstItem ? 'type' :
  831. 'column_type' in firstItem ? 'column_type' :
  832. 'columnType' in firstItem ? 'columnType' :
  833. 'data_type' in firstItem ? 'data_type' : null;
  834. const commentKey = 'Comment' in firstItem ? 'Comment' :
  835. 'comment' in firstItem ? 'comment' :
  836. 'column_comment' in firstItem ? 'column_comment' :
  837. 'columnComment' in firstItem ? 'columnComment' : null;
  838. // 组合Default和Extra作为注释
  839. const getComment = (item) => {
  840. let comment = [];
  841. if (item['Default']) {
  842. comment.push(`默认值: ${item['Default']}`);
  843. }
  844. if (item['Extra'] && item['Extra'] !== '') {
  845. comment.push(item['Extra']);
  846. }
  847. if (item['Null'] === 'NO') {
  848. comment.push('不可为空');
  849. }
  850. return comment.join(', ');
  851. };
  852. //console.log('使用的属性名:', { nameKey, typeKey, commentKey });
  853. if (nameKey) {
  854. // 转换为标准格式
  855. this.tableColumnsList = columnsData.map(item => {
  856. return {
  857. name: item[nameKey] || '',
  858. type: typeKey ? (item[typeKey] || '') : '',
  859. comment: [
  860. commentKey ? (item[commentKey] || '') : '',
  861. getComment(item)
  862. ].filter(Boolean).join(' | ')
  863. };
  864. }).filter(col => col.name); // 过滤掉没有名称的列
  865. //console.log('处理后的字段列表:', this.tableColumnsList);
  866. } else {
  867. console.error('无法找到字段名属性');
  868. this.showToastMessage('数据格式错误:无法找到字段名属性', 'error');
  869. this.tableColumnsList = [];
  870. }
  871. } else if (typeof firstItem === 'string') {
  872. // 如果只是字段名数组
  873. this.tableColumnsList = columnsData.map(name => {
  874. return {
  875. name: name,
  876. type: '',
  877. comment: ''
  878. };
  879. });
  880. //console.log('处理后的字段列表(仅名称):', this.tableColumnsList);
  881. } else {
  882. console.error('未知的字段数据格式');
  883. this.showToastMessage('未知的字段数据格式', 'error');
  884. this.tableColumnsList = [];
  885. }
  886. } else {
  887. //console.log('字段列表为空');
  888. this.tableColumnsList = [];
  889. }
  890. } else {
  891. //console.error('无法找到有效的字段数据');
  892. this.tableColumnsList = [];
  893. this.showToastMessage('获取字段列表失败', 'error');
  894. }
  895. this.isColumnsLoading = false;
  896. })
  897. .catch(error => {
  898. this.showToastMessage('获取字段列表失败,请稍后重试', 'error');
  899. this.isColumnsLoading = false;
  900. });
  901. },
  902. // 清空查询
  903. clearQuery() {
  904. this.sqlQuery = '';
  905. },
  906. // 复制字段名称到剪贴板
  907. copyColumnName(columnName) {
  908. // 创建一个临时文本区域元素
  909. const textarea = document.createElement('textarea');
  910. textarea.value = columnName;
  911. textarea.setAttribute('readonly', '');
  912. textarea.style.position = 'absolute';
  913. textarea.style.left = '-9999px';
  914. document.body.appendChild(textarea);
  915. // 选择文本并复制
  916. textarea.select();
  917. let success = false;
  918. try {
  919. success = document.execCommand('copy');
  920. this.showToastMessage(`已复制字段 "${columnName}" 到剪贴板`, 'success');
  921. } catch (err) {
  922. success = false;
  923. this.showToastMessage('复制失败,请手动复制', 'error');
  924. console.error('复制到剪贴板失败:', err);
  925. }
  926. // 移除临时元素
  927. document.body.removeChild(textarea);
  928. // 如果浏览器支持现代剪贴板API,也尝试使用它
  929. if (navigator.clipboard && window.isSecureContext) {
  930. navigator.clipboard.writeText(columnName).catch(err => {
  931. console.log('剪贴板API失败,但已使用备用方法', err);
  932. });
  933. }
  934. return success;
  935. },
  936. // 插入字段名称到SQL查询(双击时触发)
  937. insertColumnName(columnName) {
  938. // 获取文本框元素
  939. const textarea = document.querySelector('.sql-textarea');
  940. // 如果文本框存在
  941. if (textarea) {
  942. // 获取当前光标位置
  943. const startPos = textarea.selectionStart;
  944. const endPos = textarea.selectionEnd;
  945. // 在光标位置插入字段名称
  946. const textBefore = this.sqlQuery.substring(0, startPos);
  947. const textAfter = this.sqlQuery.substring(endPos);
  948. // 更新SQL查询
  949. this.sqlQuery = textBefore + columnName + textAfter;
  950. // 设置新的光标位置
  951. this.$nextTick(() => {
  952. textarea.focus();
  953. textarea.selectionStart = startPos + columnName.length;
  954. textarea.selectionEnd = startPos + columnName.length;
  955. });
  956. this.showToastMessage(`已插入字段 "${columnName}" 到查询`, 'info');
  957. } else {
  958. // 如果无法获取文本框元素,则直接在末尾添加
  959. this.sqlQuery += ' ' + columnName;
  960. }
  961. },
  962. // 更新SQL查询中的LIMIT值
  963. updateQueryLimit() {
  964. // 检查SQL查询是否为空
  965. if (!this.sqlQuery.trim()) {
  966. return;
  967. }
  968. // 正则表达式匹配LIMIT子句
  969. const limitRegex = /\bLIMIT\s+\d+\s*;?\s*$/i;
  970. if (limitRegex.test(this.sqlQuery)) {
  971. // 如果已经有LIMIT子句,替换数字
  972. this.sqlQuery = this.sqlQuery.replace(limitRegex, `LIMIT ${this.selectedLimit}${this.sqlQuery.endsWith(';') ? ';' : ''}`);
  973. } else {
  974. // 如果没有LIMIT子句,添加到末尾
  975. // 检查SQL是否以分号结尾
  976. if (this.sqlQuery.trim().endsWith(';')) {
  977. // 在分号前添加LIMIT
  978. this.sqlQuery = this.sqlQuery.replace(/;\s*$/, ` LIMIT ${this.selectedLimit};`);
  979. } else {
  980. // 直接在末尾添加LIMIT
  981. this.sqlQuery = this.sqlQuery.trim() + ` LIMIT ${this.selectedLimit};`;
  982. }
  983. }
  984. this.showToastMessage(`已设置查询限制为 ${this.selectedLimit} 条记录`, 'info');
  985. },
  986. // 执行查询
  987. executeQuery() {
  988. if (!this.sqlQuery.trim()) {
  989. this.showToastMessage('请输入SQL查询语句', 'info');
  990. return;
  991. }
  992. this.isQueryLoading = true;
  993. this.queryExecuted = true;
  994. this.currentPage = 1;
  995. // 重置查询结果
  996. this.queryResults = [];
  997. this.tableColumns = [];
  998. this.allResults = [];
  999. this.totalPages = 1;
  1000. console.log('执行SQL查询:', this.sqlQuery.trim());
  1001. console.log('查询状态: 开始执行,设置isQueryLoading为true');
  1002. // 调用后端API执行SQL查询,使用RunKylx365DBSql接口
  1003. // 使用encodeURIComponent确保中文字符被正确编码
  1004. const encodedSql = encodeURIComponent(this.sqlQuery.trim());
  1005. const apiUrl = `/api/RunKylx365DBSql?sql=${encodedSql}`;
  1006. console.log('API URL:', apiUrl);
  1007. fetch(apiUrl, {
  1008. method: 'GET',
  1009. headers: {
  1010. 'Accept': 'application/json'
  1011. }
  1012. })
  1013. .then(response => {
  1014. console.log('API响应状态:', response.status);
  1015. if (!response.ok) {
  1016. throw new Error(`HTTP error! status: ${response.status}`);
  1017. }
  1018. return response.json();
  1019. })
  1020. .then(data => {
  1021. console.log('查询返回原始数据:', data); // 添加日志
  1022. // 检查数据格式
  1023. let resultData = null;
  1024. let errorMessage = null;
  1025. // 检查标准格式 {"errcode": 10000, result: [...]}
  1026. if (data && typeof data === 'object' && 'errcode' in data) {
  1027. console.log('检测到标准响应格式,errcode:', data.errcode);
  1028. if (data.errcode !== 10000) {
  1029. errorMessage = data.errmsg || '查询返回错误';
  1030. console.error('查询返回错误:', errorMessage);
  1031. } else if (data.result && Array.isArray(data.result)) {
  1032. console.log('标准格式: data.result 是数组');
  1033. resultData = data.result;
  1034. }
  1035. }
  1036. // 检查其他可能的格式
  1037. else if (data && Array.isArray(data)) {
  1038. console.log('替代格式: data 本身是数组');
  1039. resultData = data;
  1040. } else if (data && typeof data === 'object') {
  1041. console.log('检查对象中的数组属性');
  1042. // 尝试在对象中找到数组属性
  1043. for (const key in data) {
  1044. if (Array.isArray(data[key])) {
  1045. console.log(`找到数组属性: ${key}`);
  1046. resultData = data[key];
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. if (errorMessage) {
  1052. console.error('查询返回错误:', errorMessage);
  1053. this.showToastMessage(errorMessage, 'error');
  1054. this.queryResults = [];
  1055. this.tableColumns = [];
  1056. } else if (resultData && Array.isArray(resultData)) {
  1057. console.log('处理查询结果,数据长度:', resultData.length);
  1058. console.log('第一条记录:', JSON.stringify(resultData[0]));
  1059. this.processQueryResults(resultData);
  1060. } else {
  1061. console.log('未找到有效的查询结果数据');
  1062. this.queryResults = [];
  1063. this.tableColumns = [];
  1064. this.showToastMessage('查询未返回数据', 'info');
  1065. }
  1066. console.log('查询状态: 执行完成,设置isQueryLoading为false');
  1067. this.isQueryLoading = false;
  1068. })
  1069. .catch(error => {
  1070. this.showToastMessage('执行查询失败,请稍后重试', 'error');
  1071. this.isQueryLoading = false;
  1072. // 模拟数据用于开发测试
  1073. if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
  1074. console.log('在本地环境中使用模拟数据');
  1075. // 根据选择的表格生成模拟数据
  1076. let mockData = [];
  1077. if (this.selectedTable === 'users') {
  1078. mockData = [
  1079. { id: 1, username: 'admin', email: 'admin@example.com', password: '******', created_at: '2023-01-01 00:00:00', status: 1 },
  1080. { id: 2, username: 'user1', email: 'user1@example.com', password: '******', created_at: '2023-01-02 10:30:00', status: 1 },
  1081. { id: 3, username: 'user2', email: 'user2@example.com', password: '******', created_at: '2023-01-03 15:45:00', status: 0 }
  1082. ];
  1083. } else if (this.selectedTable === 'products') {
  1084. mockData = [
  1085. { id: 1, name: '产品A', price: '99.99', category_id: 1, stock: 100, description: '这是产品A的描述' },
  1086. { id: 2, name: '产品B', price: '199.99', category_id: 2, stock: 50, description: '这是产品B的描述' },
  1087. { id: 3, name: '产品C', price: '299.99', category_id: 1, stock: 75, description: '这是产品C的描述' }
  1088. ];
  1089. } else {
  1090. // 生成通用模拟数据
  1091. for (let i = 1; i <= 10; i++) {
  1092. mockData.push({
  1093. id: i,
  1094. name: `示例名称 ${i}`,
  1095. description: `这是示例描述 ${i}`,
  1096. created_at: new Date().toISOString().slice(0, 19).replace('T', ' ')
  1097. });
  1098. }
  1099. }
  1100. this.processQueryResults(mockData);
  1101. }
  1102. });
  1103. },
  1104. // 处理查询结果
  1105. processQueryResults(results) {
  1106. console.group('处理查询结果');
  1107. console.log('原始结果:', results);
  1108. console.log('结果类型:', typeof results);
  1109. console.log('是否为数组:', Array.isArray(results));
  1110. console.log('结果长度:', results ? results.length : 0);
  1111. // 确保结果是一个数组
  1112. if (!results || !Array.isArray(results) || results.length === 0) {
  1113. console.warn('结果为空或非数组');
  1114. this.$set(this, 'queryResults', []);
  1115. this.$set(this, 'tableColumns', []);
  1116. this.$set(this, 'allResults', []);
  1117. this.$set(this, 'totalPages', 1);
  1118. this.showToastMessage('查询未返回数据', 'info');
  1119. console.groupEnd();
  1120. return;
  1121. }
  1122. // 创建深拷贝避免引用问题
  1123. const safeResults = JSON.parse(JSON.stringify(results));
  1124. console.log('第一条记录:', JSON.stringify(results[0]));
  1125. console.log('第一条记录详细属性:', Object.keys(results[0]));
  1126. console.log('第一条记录值:', results[0]);
  1127. try {
  1128. console.log('原始结果数据:', results);
  1129. // 格式化数据,特别是日期字段
  1130. const formattedResults = results.map(item => {
  1131. const newItem = {};
  1132. // 复制所有可枚举属性,确保是普通对象
  1133. for (const key in item) {
  1134. if (Object.prototype.hasOwnProperty.call(item, key) && !key.startsWith('__')) {
  1135. newItem[key] = item[key];
  1136. }
  1137. }
  1138. // 格式化日期字段
  1139. if (newItem.CreateTime) {
  1140. try {
  1141. const date = new Date(newItem.CreateTime);
  1142. if (!isNaN(date.getTime())) {
  1143. newItem.CreateTime = date.toLocaleString();
  1144. }
  1145. } catch (e) {
  1146. console.warn('日期格式化失败:', e);
  1147. newItem.CreateTime = String(newItem.CreateTime);
  1148. }
  1149. }
  1150. return newItem;
  1151. });
  1152. console.log('格式化后的结果:', formattedResults);
  1153. // 使用Vue.set确保响应式更新
  1154. this.$set(this, 'allResults', formattedResults);
  1155. // 提取表格列,排除Vue内部属性
  1156. if (formattedResults.length > 0) {
  1157. // 确保所有行有相同的列结构
  1158. const allColumns = new Set();
  1159. formattedResults.forEach(row => {
  1160. Object.keys(row).forEach(key => {
  1161. if (!key.startsWith('__')) {
  1162. allColumns.add(key);
  1163. }
  1164. });
  1165. });
  1166. const columns = Array.from(allColumns);
  1167. console.log('提取的有效表格列:', columns);
  1168. // 验证数据完整性
  1169. const missingData = formattedResults.filter(row => {
  1170. return columns.some(col => !(col in row));
  1171. });
  1172. if (missingData.length > 0) {
  1173. console.warn('发现数据不完整:', missingData);
  1174. }
  1175. this.$set(this, 'tableColumns', columns);
  1176. } else {
  1177. this.$set(this, 'tableColumns', []);
  1178. }
  1179. // 计算总页数
  1180. const totalPages = Math.ceil(formattedResults.length / this.pageSize);
  1181. console.log('计算的总页数:', totalPages);
  1182. this.$set(this, 'totalPages', totalPages);
  1183. // 显示第一页数据
  1184. this.changePage(1);
  1185. // 添加调试信息
  1186. console.log('处理后的最终数据状态:');
  1187. console.log('allResults:', JSON.parse(JSON.stringify(this.allResults)));
  1188. console.log('tableColumns:', JSON.parse(JSON.stringify(this.tableColumns)));
  1189. console.log('queryResults:', JSON.parse(JSON.stringify(this.queryResults)));
  1190. console.log('Vue实例数据:', this.$data);
  1191. // 强制更新视图
  1192. this.$nextTick(() => {
  1193. console.log('视图更新后检查:');
  1194. console.log('表格DOM:', document.querySelector('.data-table'));
  1195. console.log('表格行数:', document.querySelectorAll('.data-table tbody tr').length);
  1196. });
  1197. this.showToastMessage(`查询成功,返回 ${formattedResults.length} 条记录`, 'success');
  1198. } catch (error) {
  1199. console.error('处理查询结果时出错:', error);
  1200. this.showToastMessage('处理查询结果时出错: ' + error.message, 'error');
  1201. this.queryResults = [];
  1202. this.tableColumns = [];
  1203. this.allResults = [];
  1204. this.totalPages = 1;
  1205. }
  1206. },
  1207. // 切换页面
  1208. changePage(page) {
  1209. console.group('切换页面');
  1210. console.log('目标页码:', page);
  1211. console.log('当前页码:', this.currentPage);
  1212. console.log('总页数:', this.totalPages);
  1213. console.log('所有结果数量:', this.allResults ? this.allResults.length : 0);
  1214. console.log('每页显示数量:', this.pageSize);
  1215. console.log('当前queryResults:', this.queryResults);
  1216. console.log('当前tableColumns:', this.tableColumns);
  1217. try {
  1218. // 验证页码
  1219. if (page < 1 || page > this.totalPages) {
  1220. console.warn('页码超出范围');
  1221. console.groupEnd();
  1222. return;
  1223. }
  1224. if (page === this.currentPage) {
  1225. console.log('已经在当前页面');
  1226. console.groupEnd();
  1227. return;
  1228. }
  1229. this.$set(this, 'currentPage', page);
  1230. if (!this.allResults || this.allResults.length === 0) {
  1231. console.warn('没有可用的结果数据');
  1232. this.$set(this, 'queryResults', []);
  1233. console.groupEnd();
  1234. return;
  1235. }
  1236. // 计算当前页的数据
  1237. const startIndex = (page - 1) * this.pageSize;
  1238. const endIndex = Math.min(startIndex + this.pageSize, this.allResults.length);
  1239. console.log('计算的起始索引:', startIndex);
  1240. console.log('计算的结束索引:', endIndex);
  1241. // 创建新的数组引用确保Vue能检测到变化
  1242. const newResults = this.allResults.slice(startIndex, endIndex);
  1243. console.log('新页数据数量:', newResults.length);
  1244. if (newResults.length > 0) {
  1245. console.log('第一条记录:', JSON.stringify(newResults[0]));
  1246. console.log('第一条记录的列:', Object.keys(newResults[0]));
  1247. }
  1248. // 使用Vue.set确保响应式更新
  1249. this.$set(this, 'queryResults', newResults);
  1250. // 额外检查数据绑定
  1251. this.$nextTick(() => {
  1252. console.log('视图更新后检查数据:');
  1253. console.log('表格列:', this.tableColumns);
  1254. console.log('查询结果:', this.queryResults);
  1255. console.log('表格列与数据匹配检查:');
  1256. if (this.queryResults.length > 0 && this.tableColumns.length > 0) {
  1257. const firstRow = this.queryResults[0];
  1258. const missingColumns = this.tableColumns.filter(col => !(col in firstRow));
  1259. if (missingColumns.length > 0) {
  1260. console.warn('以下列在数据中缺失:', missingColumns);
  1261. }
  1262. }
  1263. console.groupEnd();
  1264. });
  1265. } catch (error) {
  1266. console.error('切换页面时出错:', error);
  1267. this.showToastMessage('切换页面时出错: ' + error.message, 'error');
  1268. // 重置为第一页
  1269. this.currentPage = 1;
  1270. this.$set(this, 'queryResults', this.allResults ? this.allResults.slice(0, this.pageSize) : []);
  1271. }
  1272. },
  1273. // 格式化表格单元格内容
  1274. formatTableCell(value) {
  1275. if (value === null || value === undefined) {
  1276. return 'NULL';
  1277. }
  1278. if (typeof value === 'object') {
  1279. try {
  1280. return JSON.stringify(value);
  1281. } catch (e) {
  1282. return '[Object]';
  1283. }
  1284. }
  1285. if (typeof value === 'boolean') {
  1286. return value ? '是' : '否';
  1287. }
  1288. return String(value);
  1289. },
  1290. // 显示提示消息
  1291. showToastMessage(message, type = 'info') {
  1292. this.toastMessage = message;
  1293. this.toastType = type;
  1294. this.showToast = true;
  1295. // 3秒后自动隐藏
  1296. setTimeout(() => {
  1297. this.showToast = false;
  1298. }, 3000);
  1299. },
  1300. // 强制重新渲染表格
  1301. forceRender() {
  1302. console.group('强制重新渲染');
  1303. this.isRendering = true;
  1304. // 显示加载状态
  1305. this.showToastMessage('正在强制刷新数据展示...', 'info');
  1306. console.log('当前数据状态:', {
  1307. allResults: this.allResults,
  1308. queryResults: this.queryResults,
  1309. tableColumns: this.tableColumns
  1310. });
  1311. // 强制更新数据
  1312. const temp = JSON.parse(JSON.stringify(this.allResults));
  1313. this.$set(this, 'allResults', []);
  1314. this.$nextTick(() => {
  1315. this.$set(this, 'allResults', temp);
  1316. this.changePage(this.currentPage);
  1317. // 二次检查确保渲染完成
  1318. this.$nextTick(() => {
  1319. const rows = document.querySelectorAll('.data-table tbody tr');
  1320. console.log('强制更新后行数:', rows.length);
  1321. if (rows.length === 0 && temp.length > 0) {
  1322. console.warn('强制刷新失败,尝试最终方案');
  1323. this.$set(this, 'queryResults', []);
  1324. this.$nextTick(() => {
  1325. this.$set(this, 'queryResults', temp.slice(0, this.itemsPerPage));
  1326. });
  1327. }
  1328. this.isRendering = false;
  1329. this.showToastMessage('数据展示已刷新', 'success');
  1330. console.log('强制更新完成');
  1331. });
  1332. });
  1333. console.groupEnd();
  1334. },
  1335. // 调试方法:打印当前数据状态
  1336. debugDataState() {
  1337. console.group('当前数据状态');
  1338. console.log('allResults:', JSON.parse(JSON.stringify(this.allResults)));
  1339. console.log('queryResults:', JSON.parse(JSON.stringify(this.queryResults)));
  1340. console.log('tableColumns:', JSON.parse(JSON.stringify(this.tableColumns)));
  1341. console.log('当前页码:', this.currentPage);
  1342. console.log('总页数:', this.totalPages);
  1343. // 检查DOM状态
  1344. this.$nextTick(() => {
  1345. console.log('DOM状态检查:');
  1346. console.log('表格元素:', document.querySelector('.data-table'));
  1347. console.log('表头列数:', document.querySelectorAll('.data-table thead th').length);
  1348. console.log('数据行数:', document.querySelectorAll('.data-table tbody tr').length);
  1349. });
  1350. console.groupEnd();
  1351. },
  1352. // 添加数据变化监视器
  1353. watch: {
  1354. queryResults: {
  1355. handler(newVal) {
  1356. console.log('queryResults changed:', newVal.length);
  1357. this.$nextTick(() => {
  1358. const rows = document.querySelectorAll('.data-table tbody tr');
  1359. if (newVal.length > 0 && rows.length === 0) {
  1360. console.warn('数据存在但未渲染,触发强制刷新');
  1361. this.forceRender();
  1362. }
  1363. });
  1364. },
  1365. deep: true
  1366. }
  1367. }
  1368. }
  1369. });
  1370. </script>
  1371. </body>
  1372. </html>