kylx365_db_admin.html 48 KB

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