kylx365_db_admin.html 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. if (data && data.result && Array.isArray(data.result)) {
  760. console.log('数据格式验证通过,开始处理数据');
  761. this.tableColumnsList = data.result;
  762. } else {
  763. this.tableColumnsList = [];
  764. this.showToastMessage('获取字段列表失败', 'error');
  765. }
  766. this.isColumnsLoading = false;
  767. })
  768. .catch(error => {
  769. this.showToastMessage('获取字段列表失败,请稍后重试', 'error');
  770. this.isColumnsLoading = false;
  771. // 模拟数据用于开发测试
  772. if (tableName === 'users') {
  773. this.tableColumnsList = [
  774. { name: 'id', type: 'int(11)', comment: '用户ID' },
  775. { name: 'username', type: 'varchar(50)', comment: '用户名' },
  776. { name: 'email', type: 'varchar(100)', comment: '电子邮箱' },
  777. { name: 'password', type: 'varchar(255)', comment: '密码' },
  778. { name: 'created_at', type: 'datetime', comment: '创建时间' },
  779. { name: 'status', type: 'tinyint(1)', comment: '状态' }
  780. ];
  781. } else if (tableName === 'products') {
  782. this.tableColumnsList = [
  783. { name: 'id', type: 'int(11)', comment: '产品ID' },
  784. { name: 'name', type: 'varchar(100)', comment: '产品名称' },
  785. { name: 'price', type: 'decimal(10,2)', comment: '价格' },
  786. { name: 'category_id', type: 'int(11)', comment: '分类ID' },
  787. { name: 'stock', type: 'int(11)', comment: '库存' },
  788. { name: 'description', type: 'text', comment: '产品描述' }
  789. ];
  790. } else {
  791. this.tableColumnsList = [
  792. { name: 'id', type: 'int(11)', comment: '主键ID' },
  793. { name: 'name', type: 'varchar(100)', comment: '名称' },
  794. { name: 'description', type: 'text', comment: '描述' },
  795. { name: 'created_at', type: 'datetime', comment: '创建时间' }
  796. ];
  797. }
  798. });
  799. },
  800. // 清空查询
  801. clearQuery() {
  802. this.sqlQuery = '';
  803. },
  804. // 插入字段名称到SQL查询
  805. insertColumnName(columnName) {
  806. // 获取文本框元素
  807. const textarea = document.querySelector('.sql-textarea');
  808. // 如果文本框存在
  809. if (textarea) {
  810. // 获取当前光标位置
  811. const startPos = textarea.selectionStart;
  812. const endPos = textarea.selectionEnd;
  813. // 在光标位置插入字段名称
  814. const textBefore = this.sqlQuery.substring(0, startPos);
  815. const textAfter = this.sqlQuery.substring(endPos);
  816. // 更新SQL查询
  817. this.sqlQuery = textBefore + columnName + textAfter;
  818. // 设置新的光标位置
  819. this.$nextTick(() => {
  820. textarea.focus();
  821. textarea.selectionStart = startPos + columnName.length;
  822. textarea.selectionEnd = startPos + columnName.length;
  823. });
  824. } else {
  825. // 如果无法获取文本框元素,则直接在末尾添加
  826. this.sqlQuery += ' ' + columnName;
  827. }
  828. },
  829. // 执行查询
  830. executeQuery() {
  831. if (!this.sqlQuery.trim()) {
  832. this.showToastMessage('请输入SQL查询语句', 'info');
  833. return;
  834. }
  835. this.isQueryLoading = true;
  836. this.queryExecuted = true;
  837. this.currentPage = 1;
  838. // 这里应该调用后端API执行SQL查询
  839. fetch('/api/ExecuteSqlQuery', {
  840. method: 'POST',
  841. headers: {
  842. 'Content-Type': 'application/json'
  843. },
  844. body: JSON.stringify({
  845. query: this.sqlQuery
  846. })
  847. })
  848. .then(response => response.json())
  849. .then(data => {
  850. if (data && data.result) {
  851. this.processQueryResults(data.result);
  852. } else {
  853. this.queryResults = [];
  854. this.tableColumns = [];
  855. this.showToastMessage('查询未返回数据', 'info');
  856. }
  857. this.isQueryLoading = false;
  858. })
  859. .catch(error => {
  860. this.showToastMessage('执行查询失败,请稍后重试', 'error');
  861. this.isQueryLoading = false;
  862. // 模拟数据用于开发测试
  863. if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
  864. console.log('在本地环境中使用模拟数据');
  865. // 根据选择的表格生成模拟数据
  866. let mockData = [];
  867. if (this.selectedTable === 'users') {
  868. mockData = [
  869. { id: 1, username: 'admin', email: 'admin@example.com', password: '******', created_at: '2023-01-01 00:00:00', status: 1 },
  870. { id: 2, username: 'user1', email: 'user1@example.com', password: '******', created_at: '2023-01-02 10:30:00', status: 1 },
  871. { id: 3, username: 'user2', email: 'user2@example.com', password: '******', created_at: '2023-01-03 15:45:00', status: 0 }
  872. ];
  873. } else if (this.selectedTable === 'products') {
  874. mockData = [
  875. { id: 1, name: '产品A', price: '99.99', category_id: 1, stock: 100, description: '这是产品A的描述' },
  876. { id: 2, name: '产品B', price: '199.99', category_id: 2, stock: 50, description: '这是产品B的描述' },
  877. { id: 3, name: '产品C', price: '299.99', category_id: 1, stock: 75, description: '这是产品C的描述' }
  878. ];
  879. } else {
  880. // 生成通用模拟数据
  881. for (let i = 1; i <= 10; i++) {
  882. mockData.push({
  883. id: i,
  884. name: `示例名称 ${i}`,
  885. description: `这是示例描述 ${i}`,
  886. created_at: new Date().toISOString().slice(0, 19).replace('T', ' ')
  887. });
  888. }
  889. }
  890. this.processQueryResults(mockData);
  891. }
  892. });
  893. },
  894. // 处理查询结果
  895. processQueryResults(results) {
  896. if (!results || !Array.isArray(results) || results.length === 0) {
  897. this.queryResults = [];
  898. this.tableColumns = [];
  899. this.allResults = [];
  900. this.totalPages = 1;
  901. return;
  902. }
  903. // 存储所有结果
  904. this.allResults = [...results];
  905. // 提取表格列
  906. this.tableColumns = Object.keys(results[0]);
  907. // 计算总页数
  908. this.totalPages = Math.ceil(results.length / this.pageSize);
  909. // 显示第一页数据
  910. this.changePage(1);
  911. this.showToastMessage(`查询成功,返回 ${results.length} 条记录`, 'success');
  912. },
  913. // 切换页面
  914. changePage(page) {
  915. if (page < 1 || page > this.totalPages || page === this.currentPage) {
  916. return;
  917. }
  918. this.currentPage = page;
  919. // 计算当前页的数据
  920. const startIndex = (page - 1) * this.pageSize;
  921. const endIndex = Math.min(startIndex + this.pageSize, this.allResults.length);
  922. this.queryResults = this.allResults.slice(startIndex, endIndex);
  923. },
  924. // 显示提示消息
  925. showToastMessage(message, type = 'info') {
  926. this.toastMessage = message;
  927. this.toastType = type;
  928. this.showToast = true;
  929. // 3秒后自动隐藏
  930. setTimeout(() => {
  931. this.showToast = false;
  932. }, 3000);
  933. }
  934. }
  935. });
  936. </script>
  937. </body>
  938. </html>