kylx365_db_admin.html 66 KB

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