kylx365_db_admin.html 53 KB

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