mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	Compare commits
	
		
			29 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					cf2bc6785c | ||
| 
						 | 
					98a4c92576 | ||
| 
						 | 
					b1ee9b65ff | ||
| 
						 | 
					99cc4c5e5e | ||
| 
						 | 
					226bb8f089 | ||
| 
						 | 
					37ed5134e8 | ||
| 
						 | 
					0f54d4a472 | ||
| 
						 | 
					64805360d6 | ||
| 
						 | 
					7f69fe2ad9 | ||
| 
						 | 
					f913510d3c | ||
| 
						 | 
					f2d9e7786d | ||
| 
						 | 
					e1afb1ed54 | ||
| 
						 | 
					12f8cf0111 | ||
| 
						 | 
					daa2ef5203 | ||
| 
						 | 
					1e3e183930 | ||
| 
						 | 
					366563a0fe | ||
| 
						 | 
					577802e5ad | ||
| 
						 | 
					76d6fc3ba5 | ||
| 
						 | 
					f0540559bb | ||
| 
						 | 
					802e379f60 | ||
| 
						 | 
					8c9253da80 | ||
| 
						 | 
					5271bd21e8 | ||
| 
						 | 
					db554ebdc9 | ||
| 
						 | 
					1c18a01bf6 | ||
| 
						 | 
					729a3d7028 | ||
| 
						 | 
					b88923a128 | ||
| 
						 | 
					fe8cd93c78 | ||
| 
						 | 
					64b49dae2e | ||
| 
						 | 
					edbbbca5f9 | 
							
								
								
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							@@ -1,6 +1,6 @@
 | 
				
			|||||||
                                 Apache License
 | 
					                                 Apache License
 | 
				
			||||||
                           Version 2.0, January 2004
 | 
					                           Version 2.0, January 2004
 | 
				
			||||||
                        http://www.apache.org/licenses/
 | 
					                        http://www.apache.org/licenses/ 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 | 
					   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										36
									
								
								README.en.md
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								README.en.md
									
									
									
									
									
								
							@@ -1,36 +0,0 @@
 | 
				
			|||||||
# mayfly-go
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Description
 | 
					 | 
				
			||||||
golang实现linux运维等
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Software Architecture
 | 
					 | 
				
			||||||
Software architecture description
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Installation
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  xxxx
 | 
					 | 
				
			||||||
2.  xxxx
 | 
					 | 
				
			||||||
3.  xxxx
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Instructions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  xxxx
 | 
					 | 
				
			||||||
2.  xxxx
 | 
					 | 
				
			||||||
3.  xxxx
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Contribution
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  Fork the repository
 | 
					 | 
				
			||||||
2.  Create Feat_xxx branch
 | 
					 | 
				
			||||||
3.  Commit your code
 | 
					 | 
				
			||||||
4.  Create Pull Request
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Gitee Feature
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
 | 
					 | 
				
			||||||
2.  Gitee blog [blog.gitee.com](https://blog.gitee.com)
 | 
					 | 
				
			||||||
3.  Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
 | 
					 | 
				
			||||||
4.  The most valuable open source project [GVP](https://gitee.com/gvp)
 | 
					 | 
				
			||||||
5.  The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
 | 
					 | 
				
			||||||
6.  The most popular members  [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
 | 
					 | 
				
			||||||
							
								
								
									
										22
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								README.md
									
									
									
									
									
								
							@@ -1,7 +1,25 @@
 | 
				
			|||||||
# 🌈mayfly-go
 | 
					# 🌈mayfly-go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<p align="center">
 | 
				
			||||||
 | 
					  <a href="https://gitee.com/objs/mayfly-go" target="_blank">
 | 
				
			||||||
 | 
					    <img src="https://gitee.com/objs/mayfly-go/badge/star.svg?theme=white" alt="star"/>
 | 
				
			||||||
 | 
					    <img src="https://gitee.com/objs/mayfly-go/badge/fork.svg" alt="fork"/>
 | 
				
			||||||
 | 
					  </a>
 | 
				
			||||||
 | 
					  <a href="https://github.com/may-fly/mayfly-go" target="_blank">
 | 
				
			||||||
 | 
					    <img src="https://img.shields.io/github/stars/may-fly/mayfly-go.svg?style=social" alt="github star"/>
 | 
				
			||||||
 | 
					    <img src="https://img.shields.io/github/forks/may-fly/mayfly-go.svg?style=social" alt="github fork"/>
 | 
				
			||||||
 | 
					  </a>
 | 
				
			||||||
 | 
					  <a href="https://github.com/golang/go" target="_blank">
 | 
				
			||||||
 | 
					    <img src="https://img.shields.io/badge/Golang-1.18%2B-yellow.svg" alt="golang"/>
 | 
				
			||||||
 | 
					  </a>
 | 
				
			||||||
 | 
					  <a href="https://cn.vuejs.org" target="_blank">
 | 
				
			||||||
 | 
					    <img src="https://img.shields.io/badge/Vue-3.x-green.svg" alt="vue">
 | 
				
			||||||
 | 
					  </a>
 | 
				
			||||||
 | 
					</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### 介绍
 | 
					### 介绍
 | 
				
			||||||
简单基于DDD(领域驱动设计)分层架构实现的web版 **linux、数据库(mysql)、redis、mongo统一管理操作平台**
 | 
					基于DDD分层实现的web版 **linux(终端 文件 脚本 进程)、数据库(mysql postgres)、redis(单机 哨兵 集群)、mongo统一管理操作平台**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### 开发语言与主要框架
 | 
					### 开发语言与主要框架
 | 
				
			||||||
@@ -10,7 +28,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### 交流及问题反馈加 QQ 群
 | 
					### 交流及问题反馈加 QQ 群
 | 
				
			||||||
<a target="_blank" href="https://qm.qq.com/cgi-bin/qm/qr?jump_from=webapi">119699946</a>
 | 
					<a target="_blank" href="https://qm.qq.com/cgi-bin/qm/qr?k=IdJSHW0jTMhmWFHBUS9a83wxtrxDDhFj&jump_from=webapi">119699946</a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### 系统相关资料
 | 
					### 系统相关资料
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,9 +30,9 @@ function buildWeb() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    echo_yellow "-------------------打包前端开始-------------------"
 | 
					    echo_yellow "-------------------打包前端开始-------------------"
 | 
				
			||||||
    yarn run build
 | 
					    yarn run build
 | 
				
			||||||
    if [ "${copy2Server}" == "1" ] ; then
 | 
					    if [ "${copy2Server}" == "2" ] ; then
 | 
				
			||||||
        echo_green '将打包后的静态文件拷贝至server/static'
 | 
					        echo_green '将打包后的静态文件拷贝至server/static/static'
 | 
				
			||||||
        rm -rf ${server_folder}/static && mkdir -p ${server_folder}/static && cp -r ${web_folder}/dist/* ${server_folder}/static
 | 
					        rm -rf ${server_folder}/static/static && mkdir -p ${server_folder}/static/static && cp -r ${web_folder}/dist/* ${server_folder}/static/static
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
    echo_yellow ">>>>>>>>>>>>>>>>>>>打包前端结束<<<<<<<<<<<<<<<<<<<<\n"
 | 
					    echo_yellow ">>>>>>>>>>>>>>>>>>>打包前端结束<<<<<<<<<<<<<<<<<<<<\n"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -44,6 +44,7 @@ function build() {
 | 
				
			|||||||
    toFolder=$1
 | 
					    toFolder=$1
 | 
				
			||||||
    os=$2
 | 
					    os=$2
 | 
				
			||||||
    arch=$3
 | 
					    arch=$3
 | 
				
			||||||
 | 
					    copyStatic=$4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    echo_yellow "-------------------${os}-${arch}打包构建开始-------------------"
 | 
					    echo_yellow "-------------------${os}-${arch}打包构建开始-------------------"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -67,8 +68,10 @@ function build() {
 | 
				
			|||||||
    echo_green "移动二进制文件至'${toFolder}'"
 | 
					    echo_green "移动二进制文件至'${toFolder}'"
 | 
				
			||||||
    mv ${server_folder}/${execFileName} ${toFolder}
 | 
					    mv ${server_folder}/${execFileName} ${toFolder}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    echo_green "拷贝前端静态页面至'${toFolder}/static'"
 | 
					    if [ "${copy2Server}" == "1" ] ; then
 | 
				
			||||||
    mkdir -p ${toFolder}/static && cp -r ${web_folder}/dist/* ${toFolder}/static
 | 
					        echo_green "拷贝前端静态页面至'${toFolder}/static'"
 | 
				
			||||||
 | 
					        mkdir -p ${toFolder}/static && cp -r ${web_folder}/dist/* ${toFolder}/static
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    echo_green "拷贝脚本等资源文件[config.yml、mayfly-go.sql、readme.txt、startup.sh、shutdown.sh]"
 | 
					    echo_green "拷贝脚本等资源文件[config.yml、mayfly-go.sql、readme.txt、startup.sh、shutdown.sh]"
 | 
				
			||||||
    cp ${server_folder}/config.yml ${toFolder}
 | 
					    cp ${server_folder}/config.yml ${toFolder}
 | 
				
			||||||
@@ -81,15 +84,15 @@ function build() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function buildLinuxAmd64() {
 | 
					function buildLinuxAmd64() {
 | 
				
			||||||
    build "$1/mayfly-go-linux-amd64" "linux" "amd64"
 | 
					    build "$1/mayfly-go-linux-amd64" "linux" "amd64" $2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function buildLinuxArm64() {
 | 
					function buildLinuxArm64() {
 | 
				
			||||||
    build "$1/mayfly-go-linux-arm64" "linux" "arm64"
 | 
					    build "$1/mayfly-go-linux-arm64" "linux" "arm64" $2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function buildWindows() {
 | 
					function buildWindows() {
 | 
				
			||||||
    build "$1/mayfly-go-windows" "windows" "amd64"
 | 
					    build "$1/mayfly-go-windows" "windows" "amd64" $2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function runBuild() {
 | 
					function runBuild() {
 | 
				
			||||||
@@ -103,28 +106,23 @@ function runBuild() {
 | 
				
			|||||||
    cd ${toPath}
 | 
					    cd ${toPath}
 | 
				
			||||||
    toPath=`pwd`
 | 
					    toPath=`pwd`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    read -p "是否构建前端[0|其他->否 1->是 2->构建并拷贝至server/static]: " runBuildWeb
 | 
					    read -p "是否构建前端[0|其他->否 1->是 2->构建并拷贝至server/static/static]: " runBuildWeb
 | 
				
			||||||
    read -p "请选择构建版本[0|其他->全部 1->linux-amd64 2->linux-arm64 3->windows]: " buildType
 | 
					    read -p "请选择构建版本[0|其他->全部 1->linux-amd64 2->linux-arm64 3->windows]: " buildType
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if [ "${runBuildWeb}" == "1" ];then
 | 
					    buildWeb ${runBuildWeb}
 | 
				
			||||||
        buildWeb
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    if [ "${runBuildWeb}" == "2" ];then
 | 
					 | 
				
			||||||
        buildWeb 1
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if [ "${buildType}" == "1" ];then
 | 
					    if [ "${buildType}" == "1" ];then
 | 
				
			||||||
        buildLinuxAmd64 ${toPath}
 | 
					        buildLinuxAmd64 ${toPath} ${runBuildWeb}
 | 
				
			||||||
        exit;
 | 
					        exit;
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if [ "${buildType}" == "2" ];then
 | 
					    if [ "${buildType}" == "2" ];then
 | 
				
			||||||
        buildLinuxArm64 ${toPath}
 | 
					        buildLinuxArm64 ${toPath} ${runBuildWeb}
 | 
				
			||||||
        exit;
 | 
					        exit;
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if [ "${buildType}" == "3" ];then
 | 
					    if [ "${buildType}" == "3" ];then
 | 
				
			||||||
        buildWindows ${toPath}
 | 
					        buildWindows ${toPath} ${runBuildWeb}
 | 
				
			||||||
        exit;
 | 
					        exit;
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,8 +18,7 @@
 | 
				
			|||||||
	</head>
 | 
						</head>
 | 
				
			||||||
	<body>
 | 
						<body>
 | 
				
			||||||
		<div id="app"></div>
 | 
							<div id="app"></div>
 | 
				
			||||||
        <script type="text/javascript" src="./config.js"></script>
 | 
					        <script type="application/javascript" src="./config.js"></script>
 | 
				
			||||||
		<script type="module" src="/src/main.ts"></script>
 | 
							<script type="module" src="/src/main.ts"></script>
 | 
				
			||||||
		<!-- <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=wsijQt8sLXrCW71YesmispvYHitfG9gv&s=1"></script> -->
 | 
					 | 
				
			||||||
	</body>
 | 
						</body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5123
									
								
								mayfly_go_web/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										5123
									
								
								mayfly_go_web/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -7,25 +7,26 @@
 | 
				
			|||||||
    "lint-fix": "eslint --fix --ext .js --ext .jsx --ext .vue src/"
 | 
					    "lint-fix": "eslint --fix --ext .js --ext .jsx --ext .vue src/"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
    "@element-plus/icons-vue": "^2.0.4",
 | 
					    "@element-plus/icons-vue": "^2.0.9",
 | 
				
			||||||
    "axios": "^0.27.2",
 | 
					    "axios": "^0.27.2",
 | 
				
			||||||
    "codemirror": "^5.65.5",
 | 
					    "codemirror": "^5.65.5",
 | 
				
			||||||
    "countup.js": "^2.0.7",
 | 
					    "countup.js": "^2.0.7",
 | 
				
			||||||
    "cropperjs": "^1.5.11",
 | 
					    "cropperjs": "^1.5.11",
 | 
				
			||||||
    "echarts": "^5.3.2",
 | 
					    "echarts": "^5.3.3",
 | 
				
			||||||
    "element-plus": "^2.2.4",
 | 
					    "element-plus": "^2.2.14",
 | 
				
			||||||
    "jsoneditor": "^9.8.0",
 | 
					    "jsencrypt": "^3.2.1",
 | 
				
			||||||
 | 
					    "jsoneditor": "^9.9.0",
 | 
				
			||||||
    "lodash": "^4.17.21",
 | 
					    "lodash": "^4.17.21",
 | 
				
			||||||
    "mitt": "^3.0.0",
 | 
					    "mitt": "^3.0.0",
 | 
				
			||||||
    "nprogress": "^0.2.0",
 | 
					    "nprogress": "^0.2.0",
 | 
				
			||||||
    "screenfull": "^5.1.0",
 | 
					    "screenfull": "^6.0.2",
 | 
				
			||||||
    "sortablejs": "^1.13.0",
 | 
					    "sortablejs": "^1.13.0",
 | 
				
			||||||
    "sql-formatter": "^6.1.2",
 | 
					    "sql-formatter": "^9.2.0",
 | 
				
			||||||
    "vue": "^3.2.37",
 | 
					    "vue": "^3.2.37",
 | 
				
			||||||
    "vue-clipboard3": "^1.0.1",
 | 
					    "vue-clipboard3": "^1.0.1",
 | 
				
			||||||
    "vue-router": "^4.0.15",
 | 
					    "vue-router": "^4.1.2",
 | 
				
			||||||
    "vuex": "^4.0.2",
 | 
					    "vuex": "^4.0.2",
 | 
				
			||||||
    "xterm": "^4.18.0",
 | 
					    "xterm": "^4.19.0",
 | 
				
			||||||
    "xterm-addon-fit": "^0.5.0"
 | 
					    "xterm-addon-fit": "^0.5.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
@@ -43,8 +44,8 @@
 | 
				
			|||||||
    "prettier": "^2.3.0",
 | 
					    "prettier": "^2.3.0",
 | 
				
			||||||
    "sass": "^1.45.1",
 | 
					    "sass": "^1.45.1",
 | 
				
			||||||
    "sass-loader": "^12.4.0",
 | 
					    "sass-loader": "^12.4.0",
 | 
				
			||||||
    "typescript": "^4.2.4",
 | 
					    "typescript": "^4.7.4",
 | 
				
			||||||
    "vite": "^2.9.10",
 | 
					    "vite": "^2.9.13",
 | 
				
			||||||
    "vue-eslint-parser": "^8.0.1"
 | 
					    "vue-eslint-parser": "^8.0.1"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "browserslist": [
 | 
					  "browserslist": [
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
window.globalConfig = {
 | 
					window.globalConfig = {
 | 
				
			||||||
    "BaseApiUrl": "http://localhost:8888",
 | 
					    // 默认为空,以访问根目录为api请求地址。若前后端分离部署可单独配置该后端api请求地址
 | 
				
			||||||
    "BaseWsUrl": "ws://localhost:8888"
 | 
					    "BaseApiUrl": "",
 | 
				
			||||||
 | 
					    "BaseWsUrl": ""
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
const config = {
 | 
					const config = {
 | 
				
			||||||
    baseApiUrl:  `${(window as any).globalConfig.BaseApiUrl}/api`,
 | 
					    baseApiUrl: `${(window as any).globalConfig.BaseApiUrl}/api`,
 | 
				
			||||||
    baseWsUrl: `${(window as any).globalConfig.BaseWsUrl}/api`
 | 
					    baseWsUrl: `${(window as any).globalConfig.BaseWsUrl || `${location.protocol == 'https:' ? 'wss:' : 'ws:'}//${location.host}`}/api`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default config
 | 
					export default config
 | 
				
			||||||
@@ -2,6 +2,8 @@ import request from './request'
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
    login: (param: any) => request.request('POST', '/sys/accounts/login', param, null),
 | 
					    login: (param: any) => request.request('POST', '/sys/accounts/login', param, null),
 | 
				
			||||||
 | 
					    changePwd: (param: any) => request.request('POST', '/sys/accounts/change-pwd', param, null),
 | 
				
			||||||
 | 
					    getPublicKey: () => request.request('GET', '/common/public-key', null, null),
 | 
				
			||||||
    captcha: () => request.request('GET', '/sys/captcha', null, null),
 | 
					    captcha: () => request.request('GET', '/sys/captcha', null, null),
 | 
				
			||||||
    logout: (param: any) => request.request('POST', '/sys/accounts/logout/{token}', param, null),
 | 
					    logout: (param: any) => request.request('POST', '/sys/accounts/logout/{token}', param, null),
 | 
				
			||||||
    getMenuRoute: (param: any) => request.request('Get', '/sys/resources/account', param, null)
 | 
					    getMenuRoute: (param: any) => request.request('Get', '/sys/resources/account', param, null)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										36
									
								
								mayfly_go_web/src/common/rsa.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								mayfly_go_web/src/common/rsa.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					import openApi from './openApi';
 | 
				
			||||||
 | 
					import JSEncrypt from 'jsencrypt'
 | 
				
			||||||
 | 
					import { notBlank } from './assert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var encryptor: any = null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function getRsaPublicKey() {
 | 
				
			||||||
 | 
					    let publicKey = sessionStorage.getItem('RsaPublicKey')
 | 
				
			||||||
 | 
					    if (publicKey) {
 | 
				
			||||||
 | 
					        return publicKey
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    publicKey = await openApi.getPublicKey() as string
 | 
				
			||||||
 | 
					    sessionStorage.setItem('RsaPublicKey', publicKey)
 | 
				
			||||||
 | 
					    return publicKey
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 公钥加密指定值
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @param value value
 | 
				
			||||||
 | 
					 * @returns 加密后的值
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export async function RsaEncrypt(value: any) {
 | 
				
			||||||
 | 
					    // 不存在则返回空值
 | 
				
			||||||
 | 
					    if (!value) {
 | 
				
			||||||
 | 
					        return ""
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (encryptor != null) {
 | 
				
			||||||
 | 
					        return encryptor.encrypt(value)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    encryptor = new JSEncrypt()
 | 
				
			||||||
 | 
					    const publicKey = await getRsaPublicKey() as string;
 | 
				
			||||||
 | 
					    notBlank(publicKey, "获取公钥失败")
 | 
				
			||||||
 | 
					    encryptor.setPublicKey(publicKey)//设置公钥
 | 
				
			||||||
 | 
					    return encryptor.encrypt(value)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -10,6 +10,7 @@ export const imports = {
 | 
				
			|||||||
    "ResourceList": () => import('@/views/system/resource'),
 | 
					    "ResourceList": () => import('@/views/system/resource'),
 | 
				
			||||||
    "RoleList": () => import('@/views/system/role'),
 | 
					    "RoleList": () => import('@/views/system/role'),
 | 
				
			||||||
    "AccountList": () => import('@/views/system/account'),
 | 
					    "AccountList": () => import('@/views/system/account'),
 | 
				
			||||||
 | 
					    "SyslogList": () => import('@/views/system/syslog/SyslogList.vue'),
 | 
				
			||||||
    // project
 | 
					    // project
 | 
				
			||||||
    "ProjectList": () => import('@/views/ops/project/ProjectList.vue'),
 | 
					    "ProjectList": () => import('@/views/ops/project/ProjectList.vue'),
 | 
				
			||||||
    // db
 | 
					    // db
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -52,6 +52,7 @@ export interface ThemeConfigState {
 | 
				
			|||||||
        terminalBackground: string;
 | 
					        terminalBackground: string;
 | 
				
			||||||
        terminalCursor: string;
 | 
					        terminalCursor: string;
 | 
				
			||||||
        terminalFontSize: number;
 | 
					        terminalFontSize: number;
 | 
				
			||||||
 | 
					        terminalFontWeight: string;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -113,6 +113,7 @@ const themeConfigModule: Module<ThemeConfigState, RootStateTypes> = {
 | 
				
			|||||||
            // ssh终端cursor色
 | 
					            // ssh终端cursor色
 | 
				
			||||||
            terminalCursor: '#268F81',
 | 
					            terminalCursor: '#268F81',
 | 
				
			||||||
            terminalFontSize: 15,
 | 
					            terminalFontSize: 15,
 | 
				
			||||||
 | 
					            terminalFontWeight: 'normal',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* 后端控制路由
 | 
					            /* 后端控制路由
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -239,16 +239,6 @@
 | 
				
			|||||||
	color: set-color(primary);
 | 
						color: set-color(primary);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Switch 开关
 | 
					 | 
				
			||||||
------------------------------- */
 | 
					 | 
				
			||||||
.el-switch.is-checked .el-switch__core {
 | 
					 | 
				
			||||||
	border-color: set-color(primary);
 | 
					 | 
				
			||||||
	background-color: set-color(primary);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
.el-switch__label.is-active {
 | 
					 | 
				
			||||||
	color: set-color(primary);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/* Slider 滑块
 | 
					/* Slider 滑块
 | 
				
			||||||
------------------------------- */
 | 
					------------------------------- */
 | 
				
			||||||
.el-slider__bar {
 | 
					.el-slider__bar {
 | 
				
			||||||
@@ -957,12 +947,6 @@
 | 
				
			|||||||
.el-select-dropdown .el-scrollbar__wrap {
 | 
					.el-select-dropdown .el-scrollbar__wrap {
 | 
				
			||||||
	overflow-x: scroll !important;
 | 
						overflow-x: scroll !important;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
.el-select-dropdown__wrap {
 | 
					 | 
				
			||||||
	max-height: 274px !important; /*修复Select 选择器高度问题*/
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
.el-cascader-menu__wrap.el-scrollbar__wrap {
 | 
					 | 
				
			||||||
	height: 204px !important; /*修复Cascader 级联选择器高度问题*/
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Drawer 抽屉
 | 
					/* Drawer 抽屉
 | 
				
			||||||
------------------------------- */
 | 
					------------------------------- */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,7 +30,7 @@ import { useStore } from '@/store/index.ts';
 | 
				
			|||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'layoutBreadcrumbSearch',
 | 
					    name: 'layoutBreadcrumbSearch',
 | 
				
			||||||
    setup() {
 | 
					    setup() {
 | 
				
			||||||
        const layoutMenuAutocompleteRef = ref();
 | 
					        const layoutMenuAutocompleteRef: any = ref(null);
 | 
				
			||||||
        const store = useStore();
 | 
					        const store = useStore();
 | 
				
			||||||
        const router = useRouter();
 | 
					        const router = useRouter();
 | 
				
			||||||
        const state: any = reactive({
 | 
					        const state: any = reactive({
 | 
				
			||||||
@@ -44,7 +44,9 @@ export default defineComponent({
 | 
				
			|||||||
            state.isShowSearch = true;
 | 
					            state.isShowSearch = true;
 | 
				
			||||||
            initTageView();
 | 
					            initTageView();
 | 
				
			||||||
            nextTick(() => {
 | 
					            nextTick(() => {
 | 
				
			||||||
                layoutMenuAutocompleteRef.value.focus();
 | 
					                setTimeout(() => {
 | 
				
			||||||
 | 
					                    layoutMenuAutocompleteRef.value.focus();
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        // 搜索弹窗关闭
 | 
					        // 搜索弹窗关闭
 | 
				
			||||||
@@ -68,7 +70,6 @@ export default defineComponent({
 | 
				
			|||||||
        // 初始化菜单数据
 | 
					        // 初始化菜单数据
 | 
				
			||||||
        const initTageView = () => {
 | 
					        const initTageView = () => {
 | 
				
			||||||
            if (state.tagsViewList.length > 0) return false;
 | 
					            if (state.tagsViewList.length > 0) return false;
 | 
				
			||||||
            console.log(getRoutes(store.state.routesList.routesList));
 | 
					 | 
				
			||||||
            getRoutes(store.state.routesList.routesList).map((v: any) => {
 | 
					            getRoutes(store.state.routesList.routesList).map((v: any) => {
 | 
				
			||||||
                if (!v.meta.isHide) {
 | 
					                if (!v.meta.isHide) {
 | 
				
			||||||
                    state.tagsViewList.push({ ...v });
 | 
					                    state.tagsViewList.push({ ...v });
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,7 +40,7 @@
 | 
				
			|||||||
                        </el-input-number>
 | 
					                        </el-input-number>
 | 
				
			||||||
                    </div>
 | 
					                    </div>
 | 
				
			||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
                <!-- <div class="layout-breadcrumb-seting-bar-flex mt15">
 | 
					                <div class="layout-breadcrumb-seting-bar-flex mt15">
 | 
				
			||||||
                    <div class="layout-breadcrumb-seting-bar-flex-label">字体粗细</div>
 | 
					                    <div class="layout-breadcrumb-seting-bar-flex-label">字体粗细</div>
 | 
				
			||||||
                    <div class="layout-breadcrumb-seting-bar-flex-value">
 | 
					                    <div class="layout-breadcrumb-seting-bar-flex-value">
 | 
				
			||||||
                        <el-select @change="setLocalThemeConfig" v-model="getThemeConfig.terminalFontWeight" size="small" style="width: 90px">
 | 
					                        <el-select @change="setLocalThemeConfig" v-model="getThemeConfig.terminalFontWeight" size="small" style="width: 90px">
 | 
				
			||||||
@@ -48,7 +48,7 @@
 | 
				
			|||||||
                            <el-option label="bold" value="bold"> </el-option>
 | 
					                            <el-option label="bold" value="bold"> </el-option>
 | 
				
			||||||
                        </el-select>
 | 
					                        </el-select>
 | 
				
			||||||
                    </div>
 | 
					                    </div>
 | 
				
			||||||
                </div> -->
 | 
					                </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <!-- 全局主题 -->
 | 
					                <!-- 全局主题 -->
 | 
				
			||||||
                <el-divider content-position="left">全局主题</el-divider>
 | 
					                <el-divider content-position="left">全局主题</el-divider>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,11 +14,11 @@
 | 
				
			|||||||
                </el-dropdown-menu>
 | 
					                </el-dropdown-menu>
 | 
				
			||||||
            </template>
 | 
					            </template>
 | 
				
			||||||
        </el-dropdown>
 | 
					        </el-dropdown>
 | 
				
			||||||
        <div class="layout-navbars-breadcrumb-user-icon" @click="onSearchClick">
 | 
					        <!-- <div class="layout-navbars-breadcrumb-user-icon" @click="onSearchClick">
 | 
				
			||||||
            <el-icon title="菜单搜索">
 | 
					            <el-icon title="菜单搜索">
 | 
				
			||||||
                <search />
 | 
					                <search />
 | 
				
			||||||
            </el-icon>
 | 
					            </el-icon>
 | 
				
			||||||
        </div>
 | 
					        </div> -->
 | 
				
			||||||
        <div class="layout-navbars-breadcrumb-user-icon" @click="onLayoutSetingClick">
 | 
					        <div class="layout-navbars-breadcrumb-user-icon" @click="onLayoutSetingClick">
 | 
				
			||||||
            <el-icon title="布局设置">
 | 
					            <el-icon title="布局设置">
 | 
				
			||||||
                <setting />
 | 
					                <setting />
 | 
				
			||||||
@@ -28,7 +28,7 @@
 | 
				
			|||||||
            <el-popover
 | 
					            <el-popover
 | 
				
			||||||
                placement="bottom"
 | 
					                placement="bottom"
 | 
				
			||||||
                trigger="click"
 | 
					                trigger="click"
 | 
				
			||||||
                v-model:visible="isShowUserNewsPopover"
 | 
					                :visible="isShowUserNewsPopover"
 | 
				
			||||||
                :width="300"
 | 
					                :width="300"
 | 
				
			||||||
                popper-class="el-popover-pupop-user-news"
 | 
					                popper-class="el-popover-pupop-user-news"
 | 
				
			||||||
            >
 | 
					            >
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,54 +1,75 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-content-form" size="large">
 | 
					    <div>
 | 
				
			||||||
        <el-form-item prop="username">
 | 
					        <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-content-form" size="large">
 | 
				
			||||||
            <el-input type="text" placeholder="请输入用户名" prefix-icon="user" v-model="loginForm.username" clearable autocomplete="off">
 | 
					            <el-form-item prop="username">
 | 
				
			||||||
            </el-input>
 | 
					                <el-input type="text" placeholder="请输入用户名" prefix-icon="user" v-model="loginForm.username" clearable autocomplete="off">
 | 
				
			||||||
        </el-form-item>
 | 
					                </el-input>
 | 
				
			||||||
        <el-form-item prop="password">
 | 
					            </el-form-item>
 | 
				
			||||||
            <el-input
 | 
					            <el-form-item prop="password">
 | 
				
			||||||
                type="password"
 | 
					                <el-input type="password" placeholder="请输入密码" prefix-icon="lock" v-model="loginForm.password" autocomplete="off" show-password>
 | 
				
			||||||
                placeholder="请输入密码"
 | 
					                </el-input>
 | 
				
			||||||
                prefix-icon="lock"
 | 
					            </el-form-item>
 | 
				
			||||||
                v-model="loginForm.password"
 | 
					            <el-form-item prop="captcha">
 | 
				
			||||||
                autocomplete="off"
 | 
					                <el-row :gutter="15">
 | 
				
			||||||
                show-password
 | 
					                    <el-col :span="16">
 | 
				
			||||||
            >
 | 
					                        <el-input
 | 
				
			||||||
            </el-input>
 | 
					                            type="text"
 | 
				
			||||||
        </el-form-item>
 | 
					                            maxlength="6"
 | 
				
			||||||
        <el-form-item prop="captcha">
 | 
					                            placeholder="请输入验证码"
 | 
				
			||||||
            <el-row :gutter="15">
 | 
					                            prefix-icon="position"
 | 
				
			||||||
                <el-col :span="16">
 | 
					                            v-model="loginForm.captcha"
 | 
				
			||||||
 | 
					                            clearable
 | 
				
			||||||
 | 
					                            autocomplete="off"
 | 
				
			||||||
 | 
					                            @keyup.enter="login"
 | 
				
			||||||
 | 
					                        ></el-input>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="8">
 | 
				
			||||||
 | 
					                        <div class="login-content-code">
 | 
				
			||||||
 | 
					                            <img
 | 
				
			||||||
 | 
					                                class="login-content-code-img"
 | 
				
			||||||
 | 
					                                @click="getCaptcha"
 | 
				
			||||||
 | 
					                                width="130px"
 | 
				
			||||||
 | 
					                                height="40px"
 | 
				
			||||||
 | 
					                                :src="captchaImage"
 | 
				
			||||||
 | 
					                                style="cursor: pointer"
 | 
				
			||||||
 | 
					                            />
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-row>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item>
 | 
				
			||||||
 | 
					                <el-button type="primary" class="login-content-submit" round @click="login" :loading="loading.signIn">
 | 
				
			||||||
 | 
					                    <span>登 录</span>
 | 
				
			||||||
 | 
					                </el-button>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					        </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog title="修改密码" v-model="changePwdDialog.visible" :close-on-click-modal="false" width="450px" :destroy-on-close="true">
 | 
				
			||||||
 | 
					            <el-form :model="changePwdDialog.form" :rules="changePwdDialog.rules" ref="changePwdFormRef" label-width="65px">
 | 
				
			||||||
 | 
					                <el-form-item prop="username" label="用户名" required>
 | 
				
			||||||
 | 
					                    <el-input v-model.trim="changePwdDialog.form.username" disabled></el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="oldPassword" label="旧密码" required>
 | 
				
			||||||
 | 
					                    <el-input v-model.trim="changePwdDialog.form.oldPassword" autocomplete="new-password" type="password"></el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="newPassword" label="新密码" required>
 | 
				
			||||||
                    <el-input
 | 
					                    <el-input
 | 
				
			||||||
                        type="text"
 | 
					                        v-model.trim="changePwdDialog.form.newPassword"
 | 
				
			||||||
                        maxlength="6"
 | 
					                        placeholder="须为8位以上且包含字⺟⼤⼩写+数字+特殊符号"
 | 
				
			||||||
                        placeholder="请输入验证码"
 | 
					                        type="password"
 | 
				
			||||||
                        prefix-icon="position"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                        v-model="loginForm.captcha"
 | 
					 | 
				
			||||||
                        clearable
 | 
					 | 
				
			||||||
                        autocomplete="off"
 | 
					 | 
				
			||||||
                        @keyup.enter="login"
 | 
					 | 
				
			||||||
                    ></el-input>
 | 
					                    ></el-input>
 | 
				
			||||||
                </el-col>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-col :span="8">
 | 
					            </el-form>
 | 
				
			||||||
                    <div class="login-content-code">
 | 
					
 | 
				
			||||||
                        <img
 | 
					            <template #footer>
 | 
				
			||||||
                            class="login-content-code-img"
 | 
					                <div class="dialog-footer">
 | 
				
			||||||
                            @click="getCaptcha"
 | 
					                    <el-button @click="cancelChangePwd">取 消</el-button>
 | 
				
			||||||
                            width="130px"
 | 
					                    <el-button @click="changePwd" type="primary" :loading="loading.changePwd">确 定</el-button>
 | 
				
			||||||
                            height="40px"
 | 
					                </div>
 | 
				
			||||||
                            :src="captchaImage"
 | 
					            </template>
 | 
				
			||||||
                            style="cursor: pointer"
 | 
					        </el-dialog>
 | 
				
			||||||
                        />
 | 
					    </div>
 | 
				
			||||||
                    </div>
 | 
					 | 
				
			||||||
                </el-col>
 | 
					 | 
				
			||||||
            </el-row>
 | 
					 | 
				
			||||||
        </el-form-item>
 | 
					 | 
				
			||||||
        <el-form-item>
 | 
					 | 
				
			||||||
            <el-button type="primary" class="login-content-submit" round @click="login" :loading="loading.signIn">
 | 
					 | 
				
			||||||
                <span>登 录</span>
 | 
					 | 
				
			||||||
            </el-button>
 | 
					 | 
				
			||||||
        </el-form-item>
 | 
					 | 
				
			||||||
    </el-form>
 | 
					 | 
				
			||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
@@ -60,7 +81,9 @@ import { useStore } from '@/store/index.ts';
 | 
				
			|||||||
import { setSession } from '@/common/utils/storage.ts';
 | 
					import { setSession } from '@/common/utils/storage.ts';
 | 
				
			||||||
import { formatAxis } from '@/common/utils/formatTime.ts';
 | 
					import { formatAxis } from '@/common/utils/formatTime.ts';
 | 
				
			||||||
import openApi from '@/common/openApi';
 | 
					import openApi from '@/common/openApi';
 | 
				
			||||||
 | 
					import { RsaEncrypt } from '@/common/rsa';
 | 
				
			||||||
import { letterAvatar } from '@/common/utils/string';
 | 
					import { letterAvatar } from '@/common/utils/string';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'AccountLogin',
 | 
					    name: 'AccountLogin',
 | 
				
			||||||
    setup() {
 | 
					    setup() {
 | 
				
			||||||
@@ -68,6 +91,8 @@ export default defineComponent({
 | 
				
			|||||||
        const route = useRoute();
 | 
					        const route = useRoute();
 | 
				
			||||||
        const router = useRouter();
 | 
					        const router = useRouter();
 | 
				
			||||||
        const loginFormRef: any = ref(null);
 | 
					        const loginFormRef: any = ref(null);
 | 
				
			||||||
 | 
					        const changePwdFormRef: any = ref(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            captchaImage: '',
 | 
					            captchaImage: '',
 | 
				
			||||||
            loginForm: {
 | 
					            loginForm: {
 | 
				
			||||||
@@ -76,6 +101,24 @@ export default defineComponent({
 | 
				
			|||||||
                captcha: '',
 | 
					                captcha: '',
 | 
				
			||||||
                cid: '',
 | 
					                cid: '',
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            changePwdDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                form: {
 | 
				
			||||||
 | 
					                    username: '',
 | 
				
			||||||
 | 
					                    oldPassword: '',
 | 
				
			||||||
 | 
					                    newPassword: '',
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                rules: {
 | 
				
			||||||
 | 
					                    newPassword: [
 | 
				
			||||||
 | 
					                        { required: true, message: '请输入新密码', trigger: 'blur' },
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[`~!@#$%^&*()_+<>?:"{},.\/\\;'[\]])[A-Za-z\d`~!@#$%^&*()_+<>?:"{},.\/\\;'[\]]{8,}$/,
 | 
				
			||||||
 | 
					                            message: '须为8位以上且包含字⺟⼤⼩写+数字+特殊符号',
 | 
				
			||||||
 | 
					                            trigger: 'blur',
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                    ],
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
 | 
					                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
 | 
				
			||||||
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
 | 
					                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
 | 
				
			||||||
@@ -83,10 +126,13 @@ export default defineComponent({
 | 
				
			|||||||
            },
 | 
					            },
 | 
				
			||||||
            loading: {
 | 
					            loading: {
 | 
				
			||||||
                signIn: false,
 | 
					                signIn: false,
 | 
				
			||||||
 | 
					                changePwd: false,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        onMounted(() => {
 | 
					        onMounted(() => {
 | 
				
			||||||
 | 
					            // 移除公钥, 方便后续重新获取
 | 
				
			||||||
 | 
					            sessionStorage.removeItem('RsaPublicKey')
 | 
				
			||||||
            getCaptcha();
 | 
					            getCaptcha();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -116,15 +162,26 @@ export default defineComponent({
 | 
				
			|||||||
        const onSignIn = async () => {
 | 
					        const onSignIn = async () => {
 | 
				
			||||||
            state.loading.signIn = true;
 | 
					            state.loading.signIn = true;
 | 
				
			||||||
            let loginRes;
 | 
					            let loginRes;
 | 
				
			||||||
 | 
					            const originPwd = state.loginForm.password;
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                loginRes = await openApi.login(state.loginForm);
 | 
					                const loginReq = { ...state.loginForm };
 | 
				
			||||||
                // // 存储 token 到浏览器缓存
 | 
					                loginReq.password = await RsaEncrypt(originPwd);
 | 
				
			||||||
 | 
					                loginRes = await openApi.login(loginReq);
 | 
				
			||||||
 | 
					                // 存储 token 到浏览器缓存
 | 
				
			||||||
                setSession('token', loginRes.token);
 | 
					                setSession('token', loginRes.token);
 | 
				
			||||||
                setSession('menus', loginRes.menus);
 | 
					                setSession('menus', loginRes.menus);
 | 
				
			||||||
            } catch (e) {
 | 
					            } catch (e: any) {
 | 
				
			||||||
                state.loading.signIn = false;
 | 
					                state.loading.signIn = false;
 | 
				
			||||||
                state.loginForm.captcha = '';
 | 
					                state.loginForm.captcha = '';
 | 
				
			||||||
                getCaptcha();
 | 
					                // 密码强度不足
 | 
				
			||||||
 | 
					                if (e.code && e.code == 401) {
 | 
				
			||||||
 | 
					                    state.changePwdDialog.form.username = state.loginForm.username;
 | 
				
			||||||
 | 
					                    state.changePwdDialog.form.oldPassword = originPwd;
 | 
				
			||||||
 | 
					                    state.changePwdDialog.form.newPassword = '';
 | 
				
			||||||
 | 
					                    state.changePwdDialog.visible = true;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    getCaptcha();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            // 用户信息
 | 
					            // 用户信息
 | 
				
			||||||
@@ -174,11 +231,44 @@ export default defineComponent({
 | 
				
			|||||||
            }, 300);
 | 
					            }, 300);
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const changePwd = () => {
 | 
				
			||||||
 | 
					            changePwdFormRef.value.validate(async (valid: boolean) => {
 | 
				
			||||||
 | 
					                if (!valid) {
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                try {
 | 
				
			||||||
 | 
					                    state.loading.changePwd = true;
 | 
				
			||||||
 | 
					                    const form = state.changePwdDialog.form;
 | 
				
			||||||
 | 
					                    const changePwdReq: any = { ...form };
 | 
				
			||||||
 | 
					                    changePwdReq.oldPassword = await RsaEncrypt(form.oldPassword);
 | 
				
			||||||
 | 
					                    changePwdReq.newPassword = await RsaEncrypt(form.newPassword);
 | 
				
			||||||
 | 
					                    await openApi.changePwd(changePwdReq);
 | 
				
			||||||
 | 
					                    ElMessage.success('密码修改成功, 新密码已填充至登录密码框');
 | 
				
			||||||
 | 
					                    state.loginForm.password = state.changePwdDialog.form.newPassword;
 | 
				
			||||||
 | 
					                    state.changePwdDialog.visible = false;
 | 
				
			||||||
 | 
					                    getCaptcha();
 | 
				
			||||||
 | 
					                } finally {
 | 
				
			||||||
 | 
					                    state.loading.changePwd = false;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const cancelChangePwd = () => {
 | 
				
			||||||
 | 
					            state.changePwdDialog.visible = false;
 | 
				
			||||||
 | 
					            state.changePwdDialog.form.newPassword = '';
 | 
				
			||||||
 | 
					            state.changePwdDialog.form.oldPassword = '';
 | 
				
			||||||
 | 
					            state.changePwdDialog.form.username = '';
 | 
				
			||||||
 | 
					            getCaptcha();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            getCaptcha,
 | 
					            getCaptcha,
 | 
				
			||||||
            currentTime,
 | 
					            currentTime,
 | 
				
			||||||
            loginFormRef,
 | 
					            loginFormRef,
 | 
				
			||||||
 | 
					            changePwdFormRef,
 | 
				
			||||||
            login,
 | 
					            login,
 | 
				
			||||||
 | 
					            changePwd,
 | 
				
			||||||
 | 
					            cancelChangePwd,
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="35%">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
 | 
				
			||||||
            <el-form :model="form" ref="dbForm" :rules="rules" label-width="85px">
 | 
					            <el-form :model="form" ref="dbForm" :rules="rules" label-width="95px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目:" required>
 | 
					                <el-form-item prop="projectId" label="项目:" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
                        <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
					                        <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
				
			||||||
@@ -19,13 +19,17 @@
 | 
				
			|||||||
                <el-form-item prop="type" label="类型:" required>
 | 
					                <el-form-item prop="type" label="类型:" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
 | 
					                    <el-select style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
 | 
				
			||||||
                        <el-option key="item.id" label="mysql" value="mysql"> </el-option>
 | 
					                        <el-option key="item.id" label="mysql" value="mysql"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option key="item.id" label="postgres" value="postgres"> </el-option>
 | 
				
			||||||
                    </el-select>
 | 
					                    </el-select>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="host" label="host:" required>
 | 
					                <el-form-item prop="host" label="host:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
 | 
					                    <el-col :span="18">
 | 
				
			||||||
                </el-form-item>
 | 
					                        <el-input v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
 | 
				
			||||||
                <el-form-item prop="port" label="port:" required>
 | 
					                    </el-col>
 | 
				
			||||||
                    <el-input type="number" v-model.trim="form.port" placeholder="请输入端口"></el-input>
 | 
					                    <el-col style="text-align: center" :span="1">:</el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="5">
 | 
				
			||||||
 | 
					                        <el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="username" label="用户名:" required>
 | 
					                <el-form-item prop="username" label="用户名:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
					                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
				
			||||||
@@ -37,31 +41,57 @@
 | 
				
			|||||||
                        v-model.trim="form.password"
 | 
					                        v-model.trim="form.password"
 | 
				
			||||||
                        placeholder="请输入密码,修改操作可不填"
 | 
					                        placeholder="请输入密码,修改操作可不填"
 | 
				
			||||||
                        autocomplete="new-password"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                    ></el-input>
 | 
					                    >
 | 
				
			||||||
 | 
					                        <template v-if="form.id && form.id != 0" #suffix>
 | 
				
			||||||
 | 
					                            <el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
 | 
				
			||||||
 | 
					                                <template #reference>
 | 
				
			||||||
 | 
					                                    <el-link @click="getDbPwd" :underline="false" type="primary" class="mr5">原密码</el-link>
 | 
				
			||||||
 | 
					                                </template>
 | 
				
			||||||
 | 
					                            </el-popover>
 | 
				
			||||||
 | 
					                        </template>
 | 
				
			||||||
 | 
					                    </el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="params" label="连接参数:">
 | 
				
			||||||
 | 
					                    <el-input v-model="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="database" label="数据库名:" required>
 | 
					                <el-form-item prop="database" label="数据库名:" required>
 | 
				
			||||||
                    <el-tag
 | 
					                    <el-col :span="19">
 | 
				
			||||||
                        v-for="db in databaseList"
 | 
					                        <el-select
 | 
				
			||||||
                        :key="db"
 | 
					                            @change="changeDatabase"
 | 
				
			||||||
                        class="ml5 mt5"
 | 
					                            v-model="databaseList"
 | 
				
			||||||
                        type="success"
 | 
					                            multiple
 | 
				
			||||||
                        effect="plain"
 | 
					                            collapse-tags
 | 
				
			||||||
                        closable
 | 
					                            collapse-tags-tooltip
 | 
				
			||||||
                        :disable-transitions="false"
 | 
					                            filterable
 | 
				
			||||||
                        @close="handleClose(db)"
 | 
					                            allow-create
 | 
				
			||||||
                    >
 | 
					                            placeholder="请确保数据库实例信息填写完整后获取库名"
 | 
				
			||||||
                        {{ db }}
 | 
					                            style="width: 100%"
 | 
				
			||||||
                    </el-tag>
 | 
					                        >
 | 
				
			||||||
                    <el-input
 | 
					                            <el-option v-for="db in allDatabases" :key="db" :label="db" :value="db" />
 | 
				
			||||||
                        v-if="inputDbVisible"
 | 
					                        </el-select>
 | 
				
			||||||
                        ref="InputDbRef"
 | 
					                    </el-col>
 | 
				
			||||||
                        v-model="inputDbValue"
 | 
					                    <el-col style="text-align: center" :span="1"><el-divider direction="vertical" border-style="dashed" /></el-col>
 | 
				
			||||||
                        style="width: 120px; margin-left: 5px; margin-top: 5px"
 | 
					                    <el-col :span="4">
 | 
				
			||||||
                        size="small"
 | 
					                        <el-link @click="getAllDatabase" :underline="false" type="success">获取库名</el-link>
 | 
				
			||||||
                        @keyup.enter="handleInputDbConfirm"
 | 
					                    </el-col>
 | 
				
			||||||
                        @blur="handleInputDbConfirm"
 | 
					                </el-form-item>
 | 
				
			||||||
                    />
 | 
					
 | 
				
			||||||
                    <el-button v-else class="ml5 mt5" size="small" @click="showInputDb"> + 添加数据库 </el-button>
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -76,12 +106,13 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
import { toRefs, reactive, nextTick, watch, defineComponent, ref } from 'vue';
 | 
					import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { dbApi } from './api';
 | 
					import { dbApi } from './api';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
 | 
					import { machineApi } from '../machine/api.ts';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
import type { ElInput } from 'element-plus';
 | 
					 | 
				
			||||||
import { notBlank } from '@/common/assert';
 | 
					import { notBlank } from '@/common/assert';
 | 
				
			||||||
 | 
					import { RsaEncrypt } from '@/common/rsa';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'DbEdit',
 | 
					    name: 'DbEdit',
 | 
				
			||||||
@@ -101,27 +132,31 @@ export default defineComponent({
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    setup(props: any, { emit }) {
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
        const dbForm: any = ref(null);
 | 
					        const dbForm: any = ref(null);
 | 
				
			||||||
        const InputDbRef = ref<InstanceType<typeof ElInput>>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
            envs: [],
 | 
					            envs: [],
 | 
				
			||||||
 | 
					            allDatabases: [] as any,
 | 
				
			||||||
            databaseList: [] as any,
 | 
					            databaseList: [] as any,
 | 
				
			||||||
            inputDbVisible: false,
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            inputDbValue: '',
 | 
					 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
                port: 3306,
 | 
					                port: 3306,
 | 
				
			||||||
                username: null,
 | 
					                username: null,
 | 
				
			||||||
                password: null,
 | 
					                password: null,
 | 
				
			||||||
 | 
					                params: null,
 | 
				
			||||||
                database: '',
 | 
					                database: '',
 | 
				
			||||||
                project: null,
 | 
					                project: null,
 | 
				
			||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                envId: null,
 | 
					                envId: null,
 | 
				
			||||||
                env: null,
 | 
					                env: null,
 | 
				
			||||||
 | 
					                enableSshTunnel: null,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            // 原密码
 | 
				
			||||||
 | 
					            pwd: '',
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                projectId: [
 | 
					                projectId: [
 | 
				
			||||||
@@ -155,14 +190,7 @@ export default defineComponent({
 | 
				
			|||||||
                host: [
 | 
					                host: [
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        required: true,
 | 
					                        required: true,
 | 
				
			||||||
                        message: '请输入主机ip',
 | 
					                        message: '请输入主机ip和port',
 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ],
 | 
					 | 
				
			||||||
                port: [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        required: true,
 | 
					 | 
				
			||||||
                        message: '请输入端口',
 | 
					 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
@@ -173,13 +201,6 @@ export default defineComponent({
 | 
				
			|||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                // password: [
 | 
					 | 
				
			||||||
                //     {
 | 
					 | 
				
			||||||
                //         required: true,
 | 
					 | 
				
			||||||
                //         message: '请输入密码',
 | 
					 | 
				
			||||||
                //         trigger: ['change', 'blur'],
 | 
					 | 
				
			||||||
                //     },
 | 
					 | 
				
			||||||
                // ],
 | 
					 | 
				
			||||||
                database: [
 | 
					                database: [
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        required: true,
 | 
					                        required: true,
 | 
				
			||||||
@@ -191,6 +212,10 @@ export default defineComponent({
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, (newValue) => {
 | 
					        watch(props, (newValue) => {
 | 
				
			||||||
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.db) {
 | 
					            if (newValue.db) {
 | 
				
			||||||
                getEnvs(newValue.db.projectId);
 | 
					                getEnvs(newValue.db.projectId);
 | 
				
			||||||
@@ -199,33 +224,12 @@ export default defineComponent({
 | 
				
			|||||||
                state.databaseList = newValue.db.database.split(' ');
 | 
					                state.databaseList = newValue.db.database.split(' ');
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.envs = [];
 | 
					                state.envs = [];
 | 
				
			||||||
                state.form = { port: 3306 } as any;
 | 
					                state.form = { port: 3306, enableSshTunnel: -1 } as any;
 | 
				
			||||||
                state.databaseList = [];
 | 
					                state.databaseList = [];
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const handleClose = (db: string) => {
 | 
					 | 
				
			||||||
            state.databaseList.splice(state.databaseList.indexOf(db), 1);
 | 
					 | 
				
			||||||
            changeDatabase();
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const showInputDb = () => {
 | 
					 | 
				
			||||||
            state.inputDbVisible = true;
 | 
					 | 
				
			||||||
            nextTick(() => {
 | 
					 | 
				
			||||||
                InputDbRef.value!.input!.focus();
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const handleInputDbConfirm = () => {
 | 
					 | 
				
			||||||
            if (state.inputDbValue) {
 | 
					 | 
				
			||||||
                state.databaseList.push(state.inputDbValue);
 | 
					 | 
				
			||||||
                changeDatabase();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            state.inputDbVisible = false;
 | 
					 | 
				
			||||||
            state.inputDbValue = '';
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 改变表单中的数据库字段,方便表单错误提示。如全部删光,可提示请添加数据库
 | 
					         * 改变表单中的数据库字段,方便表单错误提示。如全部删光,可提示请添加数据库
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
@@ -233,6 +237,13 @@ export default defineComponent({
 | 
				
			|||||||
            state.form.database = state.databaseList.length == 0 ? '' : state.databaseList.join(' ');
 | 
					            state.form.database = state.databaseList.length == 0 ? '' : state.databaseList.join(' ');
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getEnvs = async (projectId: any) => {
 | 
					        const getEnvs = async (projectId: any) => {
 | 
				
			||||||
            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
					            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -257,14 +268,26 @@ export default defineComponent({
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getAllDatabase = async () => {
 | 
				
			||||||
 | 
					            const reqForm = { ...state.form };
 | 
				
			||||||
 | 
					            reqForm.password = await RsaEncrypt(reqForm.password);
 | 
				
			||||||
 | 
					            state.allDatabases = await dbApi.getAllDatabase.request(reqForm);
 | 
				
			||||||
 | 
					            ElMessage.success('获取成功, 请选择需要管理操作的数据库')
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getDbPwd = async () => {
 | 
				
			||||||
 | 
					            state.pwd = await dbApi.getDbPwd.request({ id: state.form.id });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const btnOk = async () => {
 | 
					        const btnOk = async () => {
 | 
				
			||||||
            if (!state.form.id) {
 | 
					            if (!state.form.id) {
 | 
				
			||||||
                notBlank(state.form.password, '新增操作,密码不可为空');
 | 
					                notBlank(state.form.password, '新增操作,密码不可为空');
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            dbForm.value.validate((valid: boolean) => {
 | 
					            dbForm.value.validate(async (valid: boolean) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    state.form.port = Number.parseInt(state.form.port as any);
 | 
					                    const reqForm = { ...state.form };
 | 
				
			||||||
                    dbApi.saveDb.request(state.form).then(() => {
 | 
					                    reqForm.password = await RsaEncrypt(reqForm.password);
 | 
				
			||||||
 | 
					                    dbApi.saveDb.request(reqForm).then(() => {
 | 
				
			||||||
                        ElMessage.success('保存成功');
 | 
					                        ElMessage.success('保存成功');
 | 
				
			||||||
                        emit('val-change', state.form);
 | 
					                        emit('val-change', state.form);
 | 
				
			||||||
                        state.btnLoading = true;
 | 
					                        state.btnLoading = true;
 | 
				
			||||||
@@ -282,9 +305,8 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const resetInputDb = () => {
 | 
					        const resetInputDb = () => {
 | 
				
			||||||
            state.inputDbVisible = false;
 | 
					 | 
				
			||||||
            state.databaseList = [];
 | 
					            state.databaseList = [];
 | 
				
			||||||
            state.inputDbValue = '';
 | 
					            state.allDatabases = [];
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const cancel = () => {
 | 
					        const cancel = () => {
 | 
				
			||||||
@@ -298,10 +320,10 @@ export default defineComponent({
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            dbForm,
 | 
					            dbForm,
 | 
				
			||||||
            InputDbRef,
 | 
					            getAllDatabase,
 | 
				
			||||||
            handleClose,
 | 
					            getDbPwd,
 | 
				
			||||||
            showInputDb,
 | 
					            changeDatabase,
 | 
				
			||||||
            handleInputDbConfirm,
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
            changeEnv,
 | 
					            changeEnv,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,15 +20,15 @@
 | 
				
			|||||||
                        </el-radio>
 | 
					                        </el-radio>
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column prop="project" label="项目" min-width="100"></el-table-column>
 | 
					                <el-table-column prop="project" label="项目" min-width="100" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
                <el-table-column prop="env" label="环境" min-width="100"></el-table-column>
 | 
					                <el-table-column prop="env" label="环境" min-width="100"></el-table-column>
 | 
				
			||||||
                <el-table-column prop="name" label="名称" min-width="200"></el-table-column>
 | 
					                <el-table-column prop="name" label="名称" min-width="160" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
                <el-table-column min-width="160" label="host:port">
 | 
					                <el-table-column min-width="170" label="host:port" show-overflow-tooltip>
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        {{ `${scope.row.host}:${scope.row.port}` }}
 | 
					                        {{ `${scope.row.host}:${scope.row.port}` }}
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column prop="type" label="类型" min-width="80"></el-table-column>
 | 
					                <el-table-column prop="type" label="类型" min-width="90"></el-table-column>
 | 
				
			||||||
                <el-table-column prop="database" label="数据库" min-width="160">
 | 
					                <el-table-column prop="database" label="数据库" min-width="160">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        <el-tag
 | 
					                        <el-tag
 | 
				
			||||||
@@ -46,7 +46,7 @@
 | 
				
			|||||||
                <el-table-column prop="username" label="用户名" min-width="100"></el-table-column>
 | 
					                <el-table-column prop="username" label="用户名" min-width="100"></el-table-column>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-table-column min-width="115" prop="creator" label="创建账号"></el-table-column>
 | 
					                <el-table-column min-width="115" prop="creator" label="创建账号"></el-table-column>
 | 
				
			||||||
                <el-table-column min-width="160" prop="createTime" label="创建时间">
 | 
					                <el-table-column min-width="160" prop="createTime" label="创建时间" show-overflow-tooltip>
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
					                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
@@ -72,11 +72,45 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        <el-dialog width="75%" :title="`${db} 表信息`" :before-close="closeTableInfo" v-model="tableInfoDialog.visible">
 | 
					        <el-dialog width="75%" :title="`${db} 表信息`" :before-close="closeTableInfo" v-model="tableInfoDialog.visible">
 | 
				
			||||||
            <el-row class="mb10">
 | 
					            <el-row class="mb10">
 | 
				
			||||||
 | 
					                <el-popover v-model:visible="showDumpInfo" :width="470" placement="right">
 | 
				
			||||||
 | 
					                    <template #reference>
 | 
				
			||||||
 | 
					                        <el-button class="ml5" type="success" size="small" @click="showDumpInfo = !showDumpInfo">导出</el-button>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                    <el-form-item label="导出内容: ">
 | 
				
			||||||
 | 
					                        <el-radio-group v-model="dumpInfo.type">
 | 
				
			||||||
 | 
					                            <el-radio :label="1" size="small">结构</el-radio>
 | 
				
			||||||
 | 
					                            <el-radio :label="2" size="small">数据</el-radio>
 | 
				
			||||||
 | 
					                            <el-radio :label="3" size="small">结构+数据</el-radio>
 | 
				
			||||||
 | 
					                        </el-radio-group>
 | 
				
			||||||
 | 
					                    </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <el-form-item label="导出表: ">
 | 
				
			||||||
 | 
					                        <el-table @selection-change="handleDumpTableSelectionChange" max-height="300" size="small" :data="tableInfoDialog.infos">
 | 
				
			||||||
 | 
					                            <el-table-column type="selection" width="45" />
 | 
				
			||||||
 | 
					                            <el-table-column property="tableName" label="表名" min-width="150" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
 | 
					                            <el-table-column property="tableComment" label="备注" min-width="150" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
 | 
					                        </el-table>
 | 
				
			||||||
 | 
					                    </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div style="text-align: right">
 | 
				
			||||||
 | 
					                        <el-button @click="showDumpInfo = false" size="small">取消</el-button>
 | 
				
			||||||
 | 
					                        <el-button @click="dump(db)" type="success" size="small">确定</el-button>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                </el-popover>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-button type="primary" size="small" @click="tableCreateDialog.visible = true">创建表</el-button>
 | 
					                <el-button type="primary" size="small" @click="tableCreateDialog.visible = true">创建表</el-button>
 | 
				
			||||||
            </el-row>
 | 
					            </el-row>
 | 
				
			||||||
            <el-table border stripe :data="tableInfoDialog.infos" size="small">
 | 
					            <el-table v-loading="tableInfoDialog.loading" border stripe :data="filterTableInfos" size="small">
 | 
				
			||||||
                <el-table-column property="tableName" label="表名" min-width="150" show-overflow-tooltip></el-table-column>
 | 
					                <el-table-column property="tableName" label="表名" min-width="150" show-overflow-tooltip>
 | 
				
			||||||
                <el-table-column property="tableComment" label="备注" min-width="150" show-overflow-tooltip></el-table-column>
 | 
					                    <template #header>
 | 
				
			||||||
 | 
					                        <el-input v-model="tableInfoDialog.tableNameSearch" size="small" placeholder="表名: 输入可过滤" clearable />
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column property="tableComment" label="备注" min-width="150" show-overflow-tooltip>
 | 
				
			||||||
 | 
					                    <template #header>
 | 
				
			||||||
 | 
					                        <el-input v-model="tableInfoDialog.tableCommentSearch" size="small" placeholder="备注: 输入可过滤" clearable />
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column
 | 
					                <el-table-column
 | 
				
			||||||
                    prop="tableRows"
 | 
					                    prop="tableRows"
 | 
				
			||||||
                    label="Rows"
 | 
					                    label="Rows"
 | 
				
			||||||
@@ -198,6 +232,7 @@
 | 
				
			|||||||
                <el-table-column prop="columnName" label="列名" show-overflow-tooltip> </el-table-column>
 | 
					                <el-table-column prop="columnName" label="列名" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
                <el-table-column prop="seqInIndex" label="列序列号" show-overflow-tooltip> </el-table-column>
 | 
					                <el-table-column prop="seqInIndex" label="列序列号" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
                <el-table-column prop="indexType" label="类型"> </el-table-column>
 | 
					                <el-table-column prop="indexType" label="类型"> </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="indexComment" label="备注" min-width="230" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
            </el-table>
 | 
					            </el-table>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -217,7 +252,7 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script lang='ts'>
 | 
					<script lang='ts'>
 | 
				
			||||||
import { toRefs, reactive, onMounted, defineComponent } from 'vue';
 | 
					import { toRefs, reactive, computed, onMounted, defineComponent } from 'vue';
 | 
				
			||||||
import { ElMessage, ElMessageBox } from 'element-plus';
 | 
					import { ElMessage, ElMessageBox } from 'element-plus';
 | 
				
			||||||
import { formatByteSize } from '@/common/utils/format';
 | 
					import { formatByteSize } from '@/common/utils/format';
 | 
				
			||||||
import DbEdit from './DbEdit.vue';
 | 
					import DbEdit from './DbEdit.vue';
 | 
				
			||||||
@@ -226,6 +261,10 @@ import { dbApi } from './api';
 | 
				
			|||||||
import enums from './enums';
 | 
					import enums from './enums';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
import SqlExecBox from './component/SqlExecBox.ts';
 | 
					import SqlExecBox from './component/SqlExecBox.ts';
 | 
				
			||||||
 | 
					import config from '@/common/config';
 | 
				
			||||||
 | 
					import { getSession } from '@/common/utils/storage';
 | 
				
			||||||
 | 
					import { isTrue } from '@/common/assert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'DbList',
 | 
					    name: 'DbList',
 | 
				
			||||||
    components: {
 | 
					    components: {
 | 
				
			||||||
@@ -255,6 +294,13 @@ export default defineComponent({
 | 
				
			|||||||
            },
 | 
					            },
 | 
				
			||||||
            datas: [],
 | 
					            datas: [],
 | 
				
			||||||
            total: 0,
 | 
					            total: 0,
 | 
				
			||||||
 | 
					            showDumpInfo: false,
 | 
				
			||||||
 | 
					            dumpInfo: {
 | 
				
			||||||
 | 
					                id: 0,
 | 
				
			||||||
 | 
					                db: '',
 | 
				
			||||||
 | 
					                type: 3,
 | 
				
			||||||
 | 
					                tables: [],
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            // sql执行记录弹框
 | 
					            // sql执行记录弹框
 | 
				
			||||||
            sqlExecLogDialog: {
 | 
					            sqlExecLogDialog: {
 | 
				
			||||||
                title: '',
 | 
					                title: '',
 | 
				
			||||||
@@ -276,8 +322,11 @@ export default defineComponent({
 | 
				
			|||||||
            },
 | 
					            },
 | 
				
			||||||
            chooseTableName: '',
 | 
					            chooseTableName: '',
 | 
				
			||||||
            tableInfoDialog: {
 | 
					            tableInfoDialog: {
 | 
				
			||||||
 | 
					                loading: false,
 | 
				
			||||||
                visible: false,
 | 
					                visible: false,
 | 
				
			||||||
                infos: [],
 | 
					                infos: [],
 | 
				
			||||||
 | 
					                tableNameSearch: '',
 | 
				
			||||||
 | 
					                tableCommentSearch: '',
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            columnDialog: {
 | 
					            columnDialog: {
 | 
				
			||||||
                visible: false,
 | 
					                visible: false,
 | 
				
			||||||
@@ -303,7 +352,26 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        onMounted(async () => {
 | 
					        onMounted(async () => {
 | 
				
			||||||
            search();
 | 
					            search();
 | 
				
			||||||
            state.projects = (await projectApi.projects.request({ pageNum: 1, pageSize: 100 })).list;
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const filterTableInfos = computed(() => {
 | 
				
			||||||
 | 
					            const infos = state.tableInfoDialog.infos;
 | 
				
			||||||
 | 
					            const tableNameSearch = state.tableInfoDialog.tableNameSearch;
 | 
				
			||||||
 | 
					            const tableCommentSearch = state.tableInfoDialog.tableCommentSearch;
 | 
				
			||||||
 | 
					            if (!tableNameSearch && !tableCommentSearch) {
 | 
				
			||||||
 | 
					                return infos;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return infos.filter((data: any) => {
 | 
				
			||||||
 | 
					                let tnMatch = true;
 | 
				
			||||||
 | 
					                let tcMatch = true;
 | 
				
			||||||
 | 
					                if (tableNameSearch) {
 | 
				
			||||||
 | 
					                    tnMatch = data.tableName.toLowerCase().includes(tableNameSearch.toLowerCase());
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                if (tableCommentSearch) {
 | 
				
			||||||
 | 
					                    tcMatch = data.tableComment.includes(tableCommentSearch);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                return tnMatch && tcMatch;
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const choose = (item: any) => {
 | 
					        const choose = (item: any) => {
 | 
				
			||||||
@@ -330,7 +398,8 @@ export default defineComponent({
 | 
				
			|||||||
            search();
 | 
					            search();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const editDb = (isAdd = false) => {
 | 
					        const editDb = async (isAdd = false) => {
 | 
				
			||||||
 | 
					            state.projects = await projectApi.accountProjects.request(null);
 | 
				
			||||||
            if (isAdd) {
 | 
					            if (isAdd) {
 | 
				
			||||||
                state.dbEditDialog.data = null;
 | 
					                state.dbEditDialog.data = null;
 | 
				
			||||||
                state.dbEditDialog.title = '新增数据库资源';
 | 
					                state.dbEditDialog.title = '新增数据库资源';
 | 
				
			||||||
@@ -391,6 +460,29 @@ export default defineComponent({
 | 
				
			|||||||
            searchSqlExecLog();
 | 
					            searchSqlExecLog();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 选择导出数据库表
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const handleDumpTableSelectionChange = (vals: any) => {
 | 
				
			||||||
 | 
					            state.dumpInfo.tables = vals.map((x: any) => x.tableName);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 数据库信息导出
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const dump = (db: string) => {
 | 
				
			||||||
 | 
					            isTrue(state.dumpInfo.tables.length > 0, '请选择要导出的表');
 | 
				
			||||||
 | 
					            const a = document.createElement('a');
 | 
				
			||||||
 | 
					            a.setAttribute(
 | 
				
			||||||
 | 
					                'href',
 | 
				
			||||||
 | 
					                `${config.baseApiUrl}/dbs/${state.dbId}/dump?db=${db}&type=${state.dumpInfo.type}&tables=${state.dumpInfo.tables.join(
 | 
				
			||||||
 | 
					                    ','
 | 
				
			||||||
 | 
					                )}&token=${getSession('token')}`
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					            a.click();
 | 
				
			||||||
 | 
					            state.showDumpInfo = false;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const onShowRollbackSql = async (sqlExecLog: any) => {
 | 
					        const onShowRollbackSql = async (sqlExecLog: any) => {
 | 
				
			||||||
            const columns = await dbApi.columnMetadata.request({ id: sqlExecLog.dbId, db: sqlExecLog.db, tableName: sqlExecLog.table });
 | 
					            const columns = await dbApi.columnMetadata.request({ id: sqlExecLog.dbId, db: sqlExecLog.db, tableName: sqlExecLog.table });
 | 
				
			||||||
            const primaryKey = columns[0].columnName;
 | 
					            const primaryKey = columns[0].columnName;
 | 
				
			||||||
@@ -434,13 +526,21 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const showTableInfo = async (row: any, db: string) => {
 | 
					        const showTableInfo = async (row: any, db: string) => {
 | 
				
			||||||
            state.tableInfoDialog.infos = await dbApi.tableInfos.request({ id: row.id, db });
 | 
					            state.tableInfoDialog.loading = true;
 | 
				
			||||||
            state.dbId = row.id;
 | 
					 | 
				
			||||||
            state.db = db;
 | 
					 | 
				
			||||||
            state.tableInfoDialog.visible = true;
 | 
					            state.tableInfoDialog.visible = true;
 | 
				
			||||||
 | 
					            try {
 | 
				
			||||||
 | 
					                state.tableInfoDialog.infos = await dbApi.tableInfos.request({ id: row.id, db });
 | 
				
			||||||
 | 
					                state.dbId = row.id;
 | 
				
			||||||
 | 
					                state.db = db;
 | 
				
			||||||
 | 
					            } catch (e) {
 | 
				
			||||||
 | 
					                state.tableInfoDialog.visible = false;
 | 
				
			||||||
 | 
					            } finally {
 | 
				
			||||||
 | 
					                state.tableInfoDialog.loading = false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const closeTableInfo = () => {
 | 
					        const closeTableInfo = () => {
 | 
				
			||||||
 | 
					            state.showDumpInfo = false;
 | 
				
			||||||
            state.tableInfoDialog.visible = false;
 | 
					            state.tableInfoDialog.visible = false;
 | 
				
			||||||
            state.tableInfoDialog.infos = [];
 | 
					            state.tableInfoDialog.infos = [];
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -502,6 +602,7 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
 | 
					            filterTableInfos,
 | 
				
			||||||
            enums,
 | 
					            enums,
 | 
				
			||||||
            search,
 | 
					            search,
 | 
				
			||||||
            choose,
 | 
					            choose,
 | 
				
			||||||
@@ -510,6 +611,8 @@ export default defineComponent({
 | 
				
			|||||||
            valChange,
 | 
					            valChange,
 | 
				
			||||||
            deleteDb,
 | 
					            deleteDb,
 | 
				
			||||||
            onShowSqlExec,
 | 
					            onShowSqlExec,
 | 
				
			||||||
 | 
					            handleDumpTableSelectionChange,
 | 
				
			||||||
 | 
					            dump,
 | 
				
			||||||
            onBeforeCloseSqlExecDialog,
 | 
					            onBeforeCloseSqlExecDialog,
 | 
				
			||||||
            handleSqlExecPageChange,
 | 
					            handleSqlExecPageChange,
 | 
				
			||||||
            searchSqlExecLog,
 | 
					            searchSqlExecLog,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -152,6 +152,10 @@
 | 
				
			|||||||
                        <el-tooltip class="box-item" effect="dark" content="commit" placement="top">
 | 
					                        <el-tooltip class="box-item" effect="dark" content="commit" placement="top">
 | 
				
			||||||
                            <el-link @click="onCommit" class="ml5" type="success" icon="check" :underline="false"></el-link>
 | 
					                            <el-link @click="onCommit" class="ml5" type="success" icon="check" :underline="false"></el-link>
 | 
				
			||||||
                        </el-tooltip>
 | 
					                        </el-tooltip>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        <el-tooltip class="box-item" effect="dark" content="生成insert sql" placement="top">
 | 
				
			||||||
 | 
					                            <el-link @click="onGenerateInsertSql" type="success" class="ml20" :underline="false">gi</el-link>
 | 
				
			||||||
 | 
					                        </el-tooltip>
 | 
				
			||||||
                    </el-row>
 | 
					                    </el-row>
 | 
				
			||||||
                    <el-row class="mt5">
 | 
					                    <el-row class="mt5">
 | 
				
			||||||
                        <el-input
 | 
					                        <el-input
 | 
				
			||||||
@@ -161,9 +165,14 @@
 | 
				
			|||||||
                            size="small"
 | 
					                            size="small"
 | 
				
			||||||
                        >
 | 
					                        >
 | 
				
			||||||
                            <template #prepend>
 | 
					                            <template #prepend>
 | 
				
			||||||
                                <el-popover trigger="click" :width="270" placement="right">
 | 
					                                <el-popover :visible="dt.selectColumnPopoverVisible" :width="320" placement="right">
 | 
				
			||||||
                                    <template #reference>
 | 
					                                    <template #reference>
 | 
				
			||||||
                                        <el-link type="success" :underline="false">选择列</el-link>
 | 
					                                        <el-link
 | 
				
			||||||
 | 
					                                            @click="dt.selectColumnPopoverVisible = !dt.selectColumnPopoverVisible"
 | 
				
			||||||
 | 
					                                            type="success"
 | 
				
			||||||
 | 
					                                            :underline="false"
 | 
				
			||||||
 | 
					                                            >选择列</el-link
 | 
				
			||||||
 | 
					                                        >
 | 
				
			||||||
                                    </template>
 | 
					                                    </template>
 | 
				
			||||||
                                    <el-table
 | 
					                                    <el-table
 | 
				
			||||||
                                        :data="getColumns4Map(dt.name)"
 | 
					                                        :data="getColumns4Map(dt.name)"
 | 
				
			||||||
@@ -174,6 +183,7 @@
 | 
				
			|||||||
                                                onConditionRowClick(event, dt);
 | 
					                                                onConditionRowClick(event, dt);
 | 
				
			||||||
                                            }
 | 
					                                            }
 | 
				
			||||||
                                        "
 | 
					                                        "
 | 
				
			||||||
 | 
					                                        style="cursor: pointer"
 | 
				
			||||||
                                    >
 | 
					                                    >
 | 
				
			||||||
                                        <el-table-column property="columnName" label="列名" show-overflow-tooltip> </el-table-column>
 | 
					                                        <el-table-column property="columnName" label="列名" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
                                        <el-table-column property="columnComment" label="备注" show-overflow-tooltip> </el-table-column>
 | 
					                                        <el-table-column property="columnComment" label="备注" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
@@ -190,7 +200,7 @@
 | 
				
			|||||||
                        @cell-dblclick="cellClick"
 | 
					                        @cell-dblclick="cellClick"
 | 
				
			||||||
                        @sort-change="onTableSortChange"
 | 
					                        @sort-change="onTableSortChange"
 | 
				
			||||||
                        @selection-change="onDataSelectionChange"
 | 
					                        @selection-change="onDataSelectionChange"
 | 
				
			||||||
                        :data="dt.execRes.data"
 | 
					                        :data="dt.datas"
 | 
				
			||||||
                        size="small"
 | 
					                        size="small"
 | 
				
			||||||
                        :max-height="dataTabsTableHeight"
 | 
					                        :max-height="dataTabsTableHeight"
 | 
				
			||||||
                        v-loading="dt.loading"
 | 
					                        v-loading="dt.loading"
 | 
				
			||||||
@@ -200,12 +210,12 @@
 | 
				
			|||||||
                        border
 | 
					                        border
 | 
				
			||||||
                        class="mt5"
 | 
					                        class="mt5"
 | 
				
			||||||
                    >
 | 
					                    >
 | 
				
			||||||
                        <el-table-column v-if="dt.execRes.tableColumn.length > 0" type="selection" width="35" />
 | 
					                        <el-table-column v-if="dt.datas.length > 0" type="selection" width="35" />
 | 
				
			||||||
                        <el-table-column
 | 
					                        <el-table-column
 | 
				
			||||||
                            min-width="100"
 | 
					                            min-width="100"
 | 
				
			||||||
                            :width="flexColumnWidth(item, dt.execRes.data)"
 | 
					                            :width="flexColumnWidth(item, dt.datas)"
 | 
				
			||||||
                            align="center"
 | 
					                            align="center"
 | 
				
			||||||
                            v-for="item in dt.execRes.tableColumn"
 | 
					                            v-for="item in dt.columnNames"
 | 
				
			||||||
                            :key="item"
 | 
					                            :key="item"
 | 
				
			||||||
                            :prop="item"
 | 
					                            :prop="item"
 | 
				
			||||||
                            :label="item"
 | 
					                            :label="item"
 | 
				
			||||||
@@ -215,15 +225,52 @@
 | 
				
			|||||||
                            <template #header>
 | 
					                            <template #header>
 | 
				
			||||||
                                <el-tooltip raw-content placement="top" effect="customized">
 | 
					                                <el-tooltip raw-content placement="top" effect="customized">
 | 
				
			||||||
                                    <template #content> {{ getColumnTip(dt.name, item) }} </template>
 | 
					                                    <template #content> {{ getColumnTip(dt.name, item) }} </template>
 | 
				
			||||||
                                    <!-- <el-icon><question-filled /></el-icon> -->
 | 
					 | 
				
			||||||
                                    {{ item }}
 | 
					                                    {{ item }}
 | 
				
			||||||
                                </el-tooltip>
 | 
					                                </el-tooltip>
 | 
				
			||||||
                            </template>
 | 
					                            </template>
 | 
				
			||||||
                        </el-table-column>
 | 
					                        </el-table-column>
 | 
				
			||||||
                    </el-table>
 | 
					                    </el-table>
 | 
				
			||||||
 | 
					                    <el-row type="flex" class="mt5" justify="center">
 | 
				
			||||||
 | 
					                        <el-pagination
 | 
				
			||||||
 | 
					                            small
 | 
				
			||||||
 | 
					                            :total="dt.count"
 | 
				
			||||||
 | 
					                            @current-change="handlePageChange(dt)"
 | 
				
			||||||
 | 
					                            layout="prev, pager, next, total, jumper"
 | 
				
			||||||
 | 
					                            v-model:current-page="dt.pageNum"
 | 
				
			||||||
 | 
					                            :page-size="defalutLimit"
 | 
				
			||||||
 | 
					                        ></el-pagination>
 | 
				
			||||||
 | 
					                    </el-row>
 | 
				
			||||||
                </el-tab-pane>
 | 
					                </el-tab-pane>
 | 
				
			||||||
            </el-tabs>
 | 
					            </el-tabs>
 | 
				
			||||||
        </el-container>
 | 
					        </el-container>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog v-model="conditionDialog.visible" :title="conditionDialog.title" width="420px">
 | 
				
			||||||
 | 
					            <el-row>
 | 
				
			||||||
 | 
					                <el-col :span="5">
 | 
				
			||||||
 | 
					                    <el-select v-model="conditionDialog.condition">
 | 
				
			||||||
 | 
					                        <el-option label="=" value="="> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="LIKE" value="LIKE"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label=">" value=">"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label=">=" value=">="> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="<" value="<"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="<=" value="<="> </el-option>
 | 
				
			||||||
 | 
					                    </el-select>
 | 
				
			||||||
 | 
					                </el-col>
 | 
				
			||||||
 | 
					                <el-col :span="19">
 | 
				
			||||||
 | 
					                    <el-input v-model="conditionDialog.value" :placeholder="conditionDialog.placeholder" />
 | 
				
			||||||
 | 
					                </el-col>
 | 
				
			||||||
 | 
					            </el-row>
 | 
				
			||||||
 | 
					            <template #footer>
 | 
				
			||||||
 | 
					                <span class="dialog-footer">
 | 
				
			||||||
 | 
					                    <el-button @click="onCancelCondition">取消</el-button>
 | 
				
			||||||
 | 
					                    <el-button type="primary" @click="onConfirmCondition">确定</el-button>
 | 
				
			||||||
 | 
					                </span>
 | 
				
			||||||
 | 
					            </template>
 | 
				
			||||||
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog @close="genSqlDialog.visible = false" v-model="genSqlDialog.visible" title="SQL" width="1000px">
 | 
				
			||||||
 | 
					            <el-input v-model="genSqlDialog.sql" type="textarea" rows="20" />
 | 
				
			||||||
 | 
					        </el-dialog>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -263,15 +310,15 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            token: token,
 | 
					            token: token,
 | 
				
			||||||
            defalutLimit: 25, // 默认查询数量
 | 
					            defalutLimit: 20, // 默认查询数量
 | 
				
			||||||
            dbs: [], // 数据库实例列表
 | 
					            dbs: [], // 数据库实例列表
 | 
				
			||||||
            databaseList: [], // 数据库实例拥有的数据库列表,1数据库实例  -> 多数据库
 | 
					            databaseList: [], // 数据库实例拥有的数据库列表,1数据库实例  -> 多数据库
 | 
				
			||||||
            db: '', // 当前操作的数据库
 | 
					            db: '', // 当前操作的数据库
 | 
				
			||||||
 | 
					            dbType: '',
 | 
				
			||||||
            tables: [],
 | 
					            tables: [],
 | 
				
			||||||
            dbId: null, // 当前选中操作的数据库实例
 | 
					            dbId: null, // 当前选中操作的数据库实例
 | 
				
			||||||
            tableName: '',
 | 
					            tableName: '',
 | 
				
			||||||
            tableMetadata: [],
 | 
					            tableMetadata: [],
 | 
				
			||||||
            columnMetadata: [],
 | 
					 | 
				
			||||||
            sqlName: '', // 当前sql模板名
 | 
					            sqlName: '', // 当前sql模板名
 | 
				
			||||||
            sqlNames: [], // 所有sql模板名
 | 
					            sqlNames: [], // 所有sql模板名
 | 
				
			||||||
            activeName: 'Query',
 | 
					            activeName: 'Query',
 | 
				
			||||||
@@ -304,6 +351,20 @@ export default defineComponent({
 | 
				
			|||||||
                left: '',
 | 
					                left: '',
 | 
				
			||||||
                top: '',
 | 
					                top: '',
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            selectColumnPopoverVisible: false,
 | 
				
			||||||
 | 
					            conditionDialog: {
 | 
				
			||||||
 | 
					                title: '',
 | 
				
			||||||
 | 
					                placeholder: '',
 | 
				
			||||||
 | 
					                columnRow: null,
 | 
				
			||||||
 | 
					                dataTab: null,
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                condition: '=',
 | 
				
			||||||
 | 
					                value: null,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            genSqlDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                sql: '',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            cmOptions: {
 | 
					            cmOptions: {
 | 
				
			||||||
                tabSize: 4,
 | 
					                tabSize: 4,
 | 
				
			||||||
                mode: 'text/x-sql',
 | 
					                mode: 'text/x-sql',
 | 
				
			||||||
@@ -356,7 +417,7 @@ export default defineComponent({
 | 
				
			|||||||
        const setHeight = () => {
 | 
					        const setHeight = () => {
 | 
				
			||||||
            // 默认300px
 | 
					            // 默认300px
 | 
				
			||||||
            codemirror.setSize('auto', `${window.innerHeight - 538}px`);
 | 
					            codemirror.setSize('auto', `${window.innerHeight - 538}px`);
 | 
				
			||||||
            state.dataTabsTableHeight = window.innerHeight - 258;
 | 
					            state.dataTabsTableHeight = window.innerHeight - 258 - 33;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
@@ -457,33 +518,6 @@ export default defineComponent({
 | 
				
			|||||||
                sql: sql.trim(),
 | 
					                sql: sql.trim(),
 | 
				
			||||||
                remark,
 | 
					                remark,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
            // const sqlTrim = sql.trim();
 | 
					 | 
				
			||||||
            // let remark = '';
 | 
					 | 
				
			||||||
            // let canRun = true;
 | 
					 | 
				
			||||||
            // const needRemark = ['update', 'UPDATE', 'delete', 'DELETE', 'INSERT', 'insert'].indexOf(sqlTrim.split(' ')[0]);
 | 
					 | 
				
			||||||
            // if (needRemark) {
 | 
					 | 
				
			||||||
            //     const res: any = await ElMessageBox.prompt('请输入备注', 'Tip', {
 | 
					 | 
				
			||||||
            //         confirmButtonText: '确定',
 | 
					 | 
				
			||||||
            //         cancelButtonText: '取消',
 | 
					 | 
				
			||||||
            //     });
 | 
					 | 
				
			||||||
            //     remark = res.value;
 | 
					 | 
				
			||||||
            //     if (!remark) {
 | 
					 | 
				
			||||||
            //         canRun = false;
 | 
					 | 
				
			||||||
            //     }
 | 
					 | 
				
			||||||
            // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // if (!canRun) {
 | 
					 | 
				
			||||||
            //     return;
 | 
					 | 
				
			||||||
            // }
 | 
					 | 
				
			||||||
            // try {
 | 
					 | 
				
			||||||
            //     state.queryTab.loading = true;
 | 
					 | 
				
			||||||
            //     return await dbApi.sqlExec.request({
 | 
					 | 
				
			||||||
            //         id: state.dbId,
 | 
					 | 
				
			||||||
            //         db: state.db,
 | 
					 | 
				
			||||||
            //         sql: sqlTrim,
 | 
					 | 
				
			||||||
            //         remark,
 | 
					 | 
				
			||||||
            //     });
 | 
					 | 
				
			||||||
            // } catch (e: any) {}
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const removeDataTab = (targetName: string) => {
 | 
					        const removeDataTab = (targetName: string) => {
 | 
				
			||||||
@@ -533,7 +567,7 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // 获取sql文件上传执行url
 | 
					        // 获取sql文件上传执行url
 | 
				
			||||||
        const getUploadSqlFileUrl = () => {
 | 
					        const getUploadSqlFileUrl = () => {
 | 
				
			||||||
            return `${config.baseApiUrl}/dbs/${state.dbId}/exec-sql-file`;
 | 
					            return `${config.baseApiUrl}/dbs/${state.dbId}/exec-sql-file?db=${state.db}`;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const flexColumnWidth = (str: any, tableData: any, flag = 'equal') => {
 | 
					        const flexColumnWidth = (str: any, tableData: any, flag = 'equal') => {
 | 
				
			||||||
@@ -641,7 +675,9 @@ export default defineComponent({
 | 
				
			|||||||
         */
 | 
					         */
 | 
				
			||||||
        const changeDbInstance = (dbId: any) => {
 | 
					        const changeDbInstance = (dbId: any) => {
 | 
				
			||||||
            state.db = '';
 | 
					            state.db = '';
 | 
				
			||||||
            state.databaseList = (state.dbs.find((e: any) => e.id == dbId) as any).database.split(' ');
 | 
					            const dbInfo = state.dbs.find((e: any) => e.id == dbId) as any;
 | 
				
			||||||
 | 
					            state.dbType = dbInfo.type;
 | 
				
			||||||
 | 
					            state.databaseList = dbInfo.database.split(' ');
 | 
				
			||||||
            clearDb();
 | 
					            clearDb();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -673,8 +709,6 @@ export default defineComponent({
 | 
				
			|||||||
            if (tableName == '') {
 | 
					            if (tableName == '') {
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            state.columnMetadata = (await getColumns(tableName)) as any;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (!execSelectSql) {
 | 
					            if (!execSelectSql) {
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -691,40 +725,18 @@ export default defineComponent({
 | 
				
			|||||||
            tab = {
 | 
					            tab = {
 | 
				
			||||||
                label: tableName,
 | 
					                label: tableName,
 | 
				
			||||||
                name: tableName,
 | 
					                name: tableName,
 | 
				
			||||||
                execRes: {
 | 
					                datas: [],
 | 
				
			||||||
                    tableColumn: [],
 | 
					                columnNames: [],
 | 
				
			||||||
                    data: [],
 | 
					                pageNum: 1,
 | 
				
			||||||
                },
 | 
					                count: 0,
 | 
				
			||||||
                querySql: getDefaultSelectSql(tableName),
 | 
					                selectColumnPopoverVisible: false,
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					            tab.columnNames = await getColumnNames(tableName);
 | 
				
			||||||
            state.dataTabs[tableName] = tab;
 | 
					            state.dataTabs[tableName] = tab;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            state.dataTabs[tableName].execRes.tableColumn = [];
 | 
					 | 
				
			||||||
            state.dataTabs[tableName].execRes.data = [];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            onRefresh(tableName);
 | 
					            onRefresh(tableName);
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					 | 
				
			||||||
         * 获取默认查询语句
 | 
					 | 
				
			||||||
         */
 | 
					 | 
				
			||||||
        const getDefaultSelectSql = (tableName: string, where: string = '', orderBy: string = '') => {
 | 
					 | 
				
			||||||
            return `SELECT * FROM \`${tableName}\` ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''} LIMIT ${state.defalutLimit}`;
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const selectByCondition = async (tableName: string, condition: string) => {
 | 
					 | 
				
			||||||
            notEmpty(condition, '条件不能为空');
 | 
					 | 
				
			||||||
            state.dataTabs[tableName].loading = true;
 | 
					 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                const colAndData: any = await runSql(getDefaultSelectSql(tableName, condition));
 | 
					 | 
				
			||||||
                state.dataTabs[tableName].execRes.tableColumn = colAndData.colNames;
 | 
					 | 
				
			||||||
                state.dataTabs[tableName].execRes.data = colAndData.res;
 | 
					 | 
				
			||||||
                state.dataTabs[tableName].loading = false;
 | 
					 | 
				
			||||||
            } catch (err) {
 | 
					 | 
				
			||||||
                state.dataTabs[tableName].loading = false;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 获取表的所有列信息
 | 
					         * 获取表的所有列信息
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
@@ -748,38 +760,117 @@ export default defineComponent({
 | 
				
			|||||||
            return tableMap.get(tableName);
 | 
					            return tableMap.get(tableName);
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getColumnNames = async (tableName: string) => {
 | 
				
			||||||
 | 
					            const columns = await getColumns(tableName);
 | 
				
			||||||
 | 
					            return columns.map((t: any) => t.columnName);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 条件查询,点击列信息后显示输入对应的值
 | 
					         * 条件查询,点击列信息后显示输入对应的值
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        const onConditionRowClick = (event: any, dataTab: any) => {
 | 
					        const onConditionRowClick = (event: any, dataTab: any) => {
 | 
				
			||||||
 | 
					            dataTab.selectColumnPopoverVisible = false;
 | 
				
			||||||
            const row = event[0];
 | 
					            const row = event[0];
 | 
				
			||||||
            ElMessageBox.prompt(`请输入 [${row.columnName}] 的值`, '查询条件', {
 | 
					            state.conditionDialog.title = `请输入 [${row.columnName}] 的值`;
 | 
				
			||||||
                confirmButtonText: '确定',
 | 
					            state.conditionDialog.placeholder = `${row.columnType}  ${row.columnComment}`;
 | 
				
			||||||
                cancelButtonText: '取消',
 | 
					            state.conditionDialog.columnRow = row;
 | 
				
			||||||
                inputPlaceholder: `${row.columnType}  ${row.columnComment}`,
 | 
					            state.conditionDialog.dataTab = dataTab;
 | 
				
			||||||
            })
 | 
					            state.conditionDialog.visible = true;
 | 
				
			||||||
                .then(({ value }) => {
 | 
					        };
 | 
				
			||||||
                    if (!value) {
 | 
					
 | 
				
			||||||
                        value = '';
 | 
					        // 确认条件
 | 
				
			||||||
                    }
 | 
					        const onConfirmCondition = () => {
 | 
				
			||||||
                    let condition = dataTab.condition;
 | 
					            const conditionDialog = state.conditionDialog;
 | 
				
			||||||
                    if (condition) {
 | 
					            const dataTab = state.conditionDialog.dataTab as any;
 | 
				
			||||||
                        condition += ` AND `;
 | 
					            let condition = dataTab.condition;
 | 
				
			||||||
                    }
 | 
					            if (condition) {
 | 
				
			||||||
                    condition += `${row.columnName} = `;
 | 
					                condition += ` AND `;
 | 
				
			||||||
                    dataTab.condition = condition + wrapColumnValue(row, value);
 | 
					            }
 | 
				
			||||||
                })
 | 
					            const row = conditionDialog.columnRow as any;
 | 
				
			||||||
                .catch(() => {});
 | 
					            condition += `${row.columnName} ${conditionDialog.condition} `;
 | 
				
			||||||
 | 
					            dataTab.condition = condition + wrapColumnValue(row, conditionDialog.value);
 | 
				
			||||||
 | 
					            onCancelCondition();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onCancelCondition = () => {
 | 
				
			||||||
 | 
					            state.conditionDialog.visible = false;
 | 
				
			||||||
 | 
					            state.conditionDialog.title = ``;
 | 
				
			||||||
 | 
					            state.conditionDialog.placeholder = ``;
 | 
				
			||||||
 | 
					            state.conditionDialog.value = null;
 | 
				
			||||||
 | 
					            state.conditionDialog.columnRow = null;
 | 
				
			||||||
 | 
					            state.conditionDialog.dataTab = null;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const onRefresh = async (tableName: string) => {
 | 
					        const onRefresh = async (tableName: string) => {
 | 
				
			||||||
 | 
					            const dataTab = state.dataTabs[tableName];
 | 
				
			||||||
            // 查询条件置空
 | 
					            // 查询条件置空
 | 
				
			||||||
            state.dataTabs[tableName].condition = '';
 | 
					            dataTab.condition = '';
 | 
				
			||||||
            state.dataTabs[tableName].loading = true;
 | 
					            dataTab.pageNum = 1;
 | 
				
			||||||
            const colAndData: any = await runSql(state.dataTabs[tableName].querySql);
 | 
					            setDataTabDatas(dataTab);
 | 
				
			||||||
            state.dataTabs[tableName].execRes.tableColumn = colAndData.colNames;
 | 
					        };
 | 
				
			||||||
            state.dataTabs[tableName].execRes.data = colAndData.res;
 | 
					
 | 
				
			||||||
            state.dataTabs[tableName].loading = false;
 | 
					        /**
 | 
				
			||||||
 | 
					         * 数据tab修改页数
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const handlePageChange = async (dataTab: any) => {
 | 
				
			||||||
 | 
					            setDataTabDatas(dataTab);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 根据条件查询数据
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const selectByCondition = async (tableName: string, condition: string) => {
 | 
				
			||||||
 | 
					            notEmpty(condition, '条件不能为空');
 | 
				
			||||||
 | 
					            const dataTab = state.dataTabs[tableName];
 | 
				
			||||||
 | 
					            dataTab.pageNum = 1;
 | 
				
			||||||
 | 
					            setDataTabDatas(dataTab);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 设置data tab的表数据
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const setDataTabDatas = async (dataTab: any) => {
 | 
				
			||||||
 | 
					            dataTab.loading = true;
 | 
				
			||||||
 | 
					            try {
 | 
				
			||||||
 | 
					                dataTab.count = await getTableCount(dataTab.name, dataTab.condition);
 | 
				
			||||||
 | 
					                if (dataTab.count > 0) {
 | 
				
			||||||
 | 
					                    const colAndData: any = await runSql(getDefaultSelectSql(dataTab.name, dataTab.condition, dataTab.orderBy, dataTab.pageNum));
 | 
				
			||||||
 | 
					                    dataTab.datas = colAndData.res;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    dataTab.datas = [];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            } finally {
 | 
				
			||||||
 | 
					                dataTab.loading = false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 获取表的统计数量
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const getTableCount = async (tableName: string, condition: string = '') => {
 | 
				
			||||||
 | 
					            const countRes = await runSql(getDefaultCountSql(tableName, condition));
 | 
				
			||||||
 | 
					            return countRes.res[0].count;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 获取默认查询语句
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const getDefaultSelectSql = (tableName: string, where: string = '', orderBy: string = '', pageNum: number = 1) => {
 | 
				
			||||||
 | 
					            const baseSql = `SELECT * FROM ${tableName} ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''}`;
 | 
				
			||||||
 | 
					            if (state.dbType == 'mysql') {
 | 
				
			||||||
 | 
					                return `${baseSql} LIMIT ${(pageNum - 1) * state.defalutLimit}, ${state.defalutLimit};`;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (state.dbType == 'postgres') {
 | 
				
			||||||
 | 
					                return `${baseSql} OFFSET ${(pageNum - 1) * state.defalutLimit} LIMIT ${state.defalutLimit};`;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return baseSql;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 获取默认查询统计语句
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const getDefaultCountSql = (tableName: string, where: string = '') => {
 | 
				
			||||||
 | 
					            return `SELECT COUNT(*) count FROM ${tableName} ${where ? 'WHERE ' + where : ''}`;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
@@ -801,7 +892,8 @@ export default defineComponent({
 | 
				
			|||||||
            const tableName = state.activeName;
 | 
					            const tableName = state.activeName;
 | 
				
			||||||
            const sortType = sort.order == 'descending' ? 'DESC' : 'ASC';
 | 
					            const sortType = sort.order == 'descending' ? 'DESC' : 'ASC';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            state.dataTabs[state.activeName].querySql = getDefaultSelectSql(tableName, '', `ORDER BY \`${sort.prop}\` ${sortType}`);
 | 
					            const orderBy = `ORDER BY ${sort.prop} ${sortType}`;
 | 
				
			||||||
 | 
					            state.dataTabs[state.activeName].orderBy = orderBy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            onRefresh(tableName);
 | 
					            onRefresh(tableName);
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -892,7 +984,6 @@ export default defineComponent({
 | 
				
			|||||||
            state.tableName = '';
 | 
					            state.tableName = '';
 | 
				
			||||||
            state.nowTableName = '';
 | 
					            state.nowTableName = '';
 | 
				
			||||||
            state.tableMetadata = [];
 | 
					            state.tableMetadata = [];
 | 
				
			||||||
            state.columnMetadata = [];
 | 
					 | 
				
			||||||
            state.dataTabs = {};
 | 
					            state.dataTabs = {};
 | 
				
			||||||
            setCodermirrorValue('');
 | 
					            setCodermirrorValue('');
 | 
				
			||||||
            state.sqlNames = [];
 | 
					            state.sqlNames = [];
 | 
				
			||||||
@@ -927,10 +1018,7 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            promptExeSql(sql, null, () => {
 | 
					            promptExeSql(sql, null, () => {
 | 
				
			||||||
                if (!queryTab) {
 | 
					                if (!queryTab) {
 | 
				
			||||||
                    state.dataTabs[state.activeName].execRes.data = state.dataTabs[state.activeName].execRes.data.filter(
 | 
					                    onRefresh(state.activeName);
 | 
				
			||||||
                        (d: any) => !(deleteDatas.findIndex((x: any) => x[primaryKeyColumnName] == d[primaryKeyColumnName]) != -1)
 | 
					 | 
				
			||||||
                    );
 | 
					 | 
				
			||||||
                    state.dataTabs[state.activeName].selectionDatas = [];
 | 
					 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    state.queryTab.execRes.data = state.queryTab.execRes.data.filter(
 | 
					                    state.queryTab.execRes.data = state.queryTab.execRes.data.filter(
 | 
				
			||||||
                        (d: any) => !(deleteDatas.findIndex((x: any) => x[primaryKeyColumnName] == d[primaryKeyColumnName]) != -1)
 | 
					                        (d: any) => !(deleteDatas.findIndex((x: any) => x[primaryKeyColumnName] == d[primaryKeyColumnName]) != -1)
 | 
				
			||||||
@@ -940,6 +1028,38 @@ export default defineComponent({
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onGenerateInsertSql = async () => {
 | 
				
			||||||
 | 
					            const queryTab = isQueryTab();
 | 
				
			||||||
 | 
					            const datas = queryTab ? state.queryTab.selectionDatas : state.dataTabs[state.activeName].selectionDatas;
 | 
				
			||||||
 | 
					            isTrue(datas && datas.length > 0, '请先选择要生成insert语句的数据');
 | 
				
			||||||
 | 
					            const tableName = state.nowTableName;
 | 
				
			||||||
 | 
					            const columns: any = await getColumns(tableName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const sqls = [];
 | 
				
			||||||
 | 
					            for (let data of datas) {
 | 
				
			||||||
 | 
					                let colNames = [];
 | 
				
			||||||
 | 
					                let values = [];
 | 
				
			||||||
 | 
					                for (let column of columns) {
 | 
				
			||||||
 | 
					                    const colName = column.columnName;
 | 
				
			||||||
 | 
					                    colNames.push(colName);
 | 
				
			||||||
 | 
					                    values.push(wrapValueByType(data[colName]));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                sqls.push(`INSERT INTO ${tableName} (${colNames.join(', ')}) VALUES(${values.join(', ')})`);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            state.genSqlDialog.sql = sqls.join(';\n') + ';';
 | 
				
			||||||
 | 
					            state.genSqlDialog.visible = true;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const wrapValueByType = (val: any) => {
 | 
				
			||||||
 | 
					            if (val == null) {
 | 
				
			||||||
 | 
					                return 'NULL';
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (typeof val == 'number') {
 | 
				
			||||||
 | 
					                return val;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return `'${val}'`;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 是否为查询tab
 | 
					         * 是否为查询tab
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
@@ -955,7 +1075,7 @@ export default defineComponent({
 | 
				
			|||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            // 转为字符串比较,可能存在数字等
 | 
					            // 转为字符串比较,可能存在数字等
 | 
				
			||||||
            let text = row[property] + '';
 | 
					            let text = (row[property] ? row[property] : '') + '';
 | 
				
			||||||
            let div = cell.children[0];
 | 
					            let div = cell.children[0];
 | 
				
			||||||
            if (div) {
 | 
					            if (div) {
 | 
				
			||||||
                let input = document.createElement('input');
 | 
					                let input = document.createElement('input');
 | 
				
			||||||
@@ -1098,6 +1218,8 @@ export default defineComponent({
 | 
				
			|||||||
            getColumnTip,
 | 
					            getColumnTip,
 | 
				
			||||||
            getColumns4Map,
 | 
					            getColumns4Map,
 | 
				
			||||||
            onConditionRowClick,
 | 
					            onConditionRowClick,
 | 
				
			||||||
 | 
					            onConfirmCondition,
 | 
				
			||||||
 | 
					            onCancelCondition,
 | 
				
			||||||
            changeSqlTemplate,
 | 
					            changeSqlTemplate,
 | 
				
			||||||
            deleteSql,
 | 
					            deleteSql,
 | 
				
			||||||
            saveSql,
 | 
					            saveSql,
 | 
				
			||||||
@@ -1107,12 +1229,14 @@ export default defineComponent({
 | 
				
			|||||||
            formatSql,
 | 
					            formatSql,
 | 
				
			||||||
            onBeforeChange,
 | 
					            onBeforeChange,
 | 
				
			||||||
            onRefresh,
 | 
					            onRefresh,
 | 
				
			||||||
 | 
					            handlePageChange,
 | 
				
			||||||
            selectByCondition,
 | 
					            selectByCondition,
 | 
				
			||||||
            onCommit,
 | 
					            onCommit,
 | 
				
			||||||
            addRow,
 | 
					            addRow,
 | 
				
			||||||
            onDataSelectionChange,
 | 
					            onDataSelectionChange,
 | 
				
			||||||
            onDeleteData,
 | 
					            onDeleteData,
 | 
				
			||||||
            onTableSortChange,
 | 
					            onTableSortChange,
 | 
				
			||||||
 | 
					            onGenerateInsertSql,
 | 
				
			||||||
            showExecBtns,
 | 
					            showExecBtns,
 | 
				
			||||||
            closeExecBtns,
 | 
					            closeExecBtns,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,10 @@ export const dbApi = {
 | 
				
			|||||||
    // 获取权限列表
 | 
					    // 获取权限列表
 | 
				
			||||||
    dbs: Api.create("/dbs", 'get'),
 | 
					    dbs: Api.create("/dbs", 'get'),
 | 
				
			||||||
    saveDb: Api.create("/dbs", 'post'),
 | 
					    saveDb: Api.create("/dbs", 'post'),
 | 
				
			||||||
 | 
					    getAllDatabase: Api.create("/dbs/databases", 'post'),
 | 
				
			||||||
 | 
					    getDbPwd: Api.create("/dbs/{id}/pwd", 'get'),
 | 
				
			||||||
    deleteDb: Api.create("/dbs/{id}", 'delete'),
 | 
					    deleteDb: Api.create("/dbs/{id}", 'delete'),
 | 
				
			||||||
 | 
					    dumpDb: Api.create("/dbs/{id}/dump", 'post'),
 | 
				
			||||||
    tableInfos: Api.create("/dbs/{id}/t-infos", 'get'),
 | 
					    tableInfos: Api.create("/dbs/{id}/t-infos", 'get'),
 | 
				
			||||||
    tableIndex: Api.create("/dbs/{id}/t-index", 'get'),
 | 
					    tableIndex: Api.create("/dbs/{id}/t-index", 'get'),
 | 
				
			||||||
    tableDdl: Api.create("/dbs/{id}/t-create-ddl", 'get'),
 | 
					    tableDdl: Api.create("/dbs/{id}/t-create-ddl", 'get'),
 | 
				
			||||||
@@ -21,5 +24,5 @@ export const dbApi = {
 | 
				
			|||||||
    getSqlNames: Api.create("/dbs/{id}/sql-names", 'get'),
 | 
					    getSqlNames: Api.create("/dbs/{id}/sql-names", 'get'),
 | 
				
			||||||
    deleteDbSql: Api.create("/dbs/{id}/sql", 'delete'),
 | 
					    deleteDbSql: Api.create("/dbs/{id}/sql", 'delete'),
 | 
				
			||||||
    // 获取数据库sql执行记录
 | 
					    // 获取数据库sql执行记录
 | 
				
			||||||
    getSqlExecs: Api.create("/dbs/{id}/sql-execs", 'get'),
 | 
					    getSqlExecs: Api.create("/dbs/{dbId}/sql-execs", 'get'),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog title="待执行SQL" v-model="dialogVisible" :show-close="false" width="600px">
 | 
					        <el-dialog title="待执行SQL" v-model="dialogVisible" :show-close="false" width="600px">
 | 
				
			||||||
            <codemirror height="350px" class="codesql" ref="cmEditor" language="sql" v-model="sqlValue" :options="cmOptions" />
 | 
					            <codemirror height="350px" class="codesql" ref="cmEditor" language="sql" v-model="sqlValue" :options="cmOptions" />
 | 
				
			||||||
            <el-input v-model="remark" placeholder="请输入执行备注" class="mt5" />
 | 
					            <el-input ref="remarkInputRef" v-model="remark" placeholder="请输入执行备注" class="mt5" />
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
                <span class="dialog-footer">
 | 
					                <span class="dialog-footer">
 | 
				
			||||||
                    <el-button @click="cancel">取 消</el-button>
 | 
					                    <el-button @click="cancel">取 消</el-button>
 | 
				
			||||||
@@ -14,9 +14,9 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
import { toRefs, reactive, defineComponent } from 'vue';
 | 
					import { toRefs, ref, nextTick, reactive, defineComponent } from 'vue';
 | 
				
			||||||
import { dbApi } from '../api';
 | 
					import { dbApi } from '../api';
 | 
				
			||||||
import { ElDialog, ElButton, ElInput, ElMessage } from 'element-plus';
 | 
					import { ElDialog, ElButton, ElInput, ElMessage, InputInstance } from 'element-plus';
 | 
				
			||||||
// import base style
 | 
					// import base style
 | 
				
			||||||
import 'codemirror/lib/codemirror.css';
 | 
					import 'codemirror/lib/codemirror.css';
 | 
				
			||||||
// 引入主题后还需要在 options 中指定主题才会生效
 | 
					// 引入主题后还需要在 options 中指定主题才会生效
 | 
				
			||||||
@@ -50,6 +50,7 @@ export default defineComponent({
 | 
				
			|||||||
        },
 | 
					        },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setup(props: any) {
 | 
					    setup(props: any) {
 | 
				
			||||||
 | 
					        const remarkInputRef = ref<InputInstance>();
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            sqlValue: '',
 | 
					            sqlValue: '',
 | 
				
			||||||
@@ -84,16 +85,22 @@ export default defineComponent({
 | 
				
			|||||||
                ElMessage.error('请输入执行的备注信息');
 | 
					                ElMessage.error('请输入执行的备注信息');
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                state.btnLoading = true;
 | 
					                state.btnLoading = true;
 | 
				
			||||||
                await dbApi.sqlExec.request({
 | 
					                const res = await dbApi.sqlExec.request({
 | 
				
			||||||
                    id: state.dbId,
 | 
					                    id: state.dbId,
 | 
				
			||||||
                    db: state.db,
 | 
					                    db: state.db,
 | 
				
			||||||
                    remark: state.remark,
 | 
					                    remark: state.remark,
 | 
				
			||||||
                    sql: state.sqlValue.trim(),
 | 
					                    sql: state.sqlValue.trim(),
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
                runSuccess = true;
 | 
					                if (parseInt(res.res[0].影响条数) >= 1) {
 | 
				
			||||||
 | 
					                    ElMessage.success('执行成功');
 | 
				
			||||||
 | 
					                    runSuccess = true;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    ElMessage.error('执行失败');
 | 
				
			||||||
 | 
					                    runSuccess = false;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            } catch (e) {
 | 
					            } catch (e) {
 | 
				
			||||||
                runSuccess = false;
 | 
					                runSuccess = false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -127,10 +134,16 @@ export default defineComponent({
 | 
				
			|||||||
            state.dbId = props.dbId;
 | 
					            state.dbId = props.dbId;
 | 
				
			||||||
            state.db = props.db;
 | 
					            state.db = props.db;
 | 
				
			||||||
            state.dialogVisible = true;
 | 
					            state.dialogVisible = true;
 | 
				
			||||||
 | 
					            nextTick(() => {
 | 
				
			||||||
 | 
					                setTimeout(() => {
 | 
				
			||||||
 | 
					                    remarkInputRef.value?.focus();
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
 | 
					            remarkInputRef,
 | 
				
			||||||
            open,
 | 
					            open,
 | 
				
			||||||
            runSql,
 | 
					            runSql,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -57,7 +57,7 @@
 | 
				
			|||||||
            </el-row>
 | 
					            </el-row>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <el-dialog :title="tree.title" v-model="tree.visible" :close-on-click-modal="false" width="680px">
 | 
					        <el-dialog :title="tree.title" v-model="tree.visible" :close-on-click-modal="false" width="50%">
 | 
				
			||||||
            <el-progress
 | 
					            <el-progress
 | 
				
			||||||
                v-if="uploadProgressShow"
 | 
					                v-if="uploadProgressShow"
 | 
				
			||||||
                style="width: 90%; margin-left: 20px"
 | 
					                style="width: 90%; margin-left: 20px"
 | 
				
			||||||
@@ -89,17 +89,19 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                                <template #dropdown>
 | 
					                                <template #dropdown>
 | 
				
			||||||
                                    <el-dropdown-menu>
 | 
					                                    <el-dropdown-menu>
 | 
				
			||||||
                                        <el-dropdown-item v-if="data.type == '-' && data.size < 1 * 1024 * 1024">
 | 
					                                        <el-dropdown-item
 | 
				
			||||||
                                            <el-link
 | 
					                                            @click="getFileContent(tree.folder.id, data.path)"
 | 
				
			||||||
                                                @click.prevent="getFileContent(tree.folder.id, data.path)"
 | 
					                                            v-if="data.type == '-' && data.size < 1 * 1024 * 1024"
 | 
				
			||||||
                                                type="info"
 | 
					                                        >
 | 
				
			||||||
                                                icon="view"
 | 
					                                            <el-link type="info" icon="view" :underline="false">查看</el-link>
 | 
				
			||||||
                                                :underline="false"
 | 
					 | 
				
			||||||
                                            >
 | 
					 | 
				
			||||||
                                                查看
 | 
					 | 
				
			||||||
                                            </el-link>
 | 
					 | 
				
			||||||
                                        </el-dropdown-item>
 | 
					                                        </el-dropdown-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                                        <span v-auth="'machine:file:write'">
 | 
				
			||||||
 | 
					                                            <el-dropdown-item @click="showCreateFileDialog(node, data)" v-if="data.type == 'd'">
 | 
				
			||||||
 | 
					                                                <el-link type="primary" icon="document" :underline="false" style="margin-left: 2px">新建</el-link>
 | 
				
			||||||
 | 
					                                            </el-dropdown-item>
 | 
				
			||||||
 | 
					                                        </span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                                        <span v-auth="'machine:file:upload'">
 | 
					                                        <span v-auth="'machine:file:upload'">
 | 
				
			||||||
                                            <el-dropdown-item v-if="data.type == 'd'">
 | 
					                                            <el-dropdown-item v-if="data.type == 'd'">
 | 
				
			||||||
                                                <el-upload
 | 
					                                                <el-upload
 | 
				
			||||||
@@ -112,34 +114,20 @@
 | 
				
			|||||||
                                                    name="file"
 | 
					                                                    name="file"
 | 
				
			||||||
                                                    style="display: inline-block; margin-left: 2px"
 | 
					                                                    style="display: inline-block; margin-left: 2px"
 | 
				
			||||||
                                                >
 | 
					                                                >
 | 
				
			||||||
                                                    <el-link icon="upload" :underline="false"> 上传 </el-link>
 | 
					                                                    <el-link icon="upload" :underline="false">上传</el-link>
 | 
				
			||||||
                                                </el-upload>
 | 
					                                                </el-upload>
 | 
				
			||||||
                                            </el-dropdown-item>
 | 
					                                            </el-dropdown-item>
 | 
				
			||||||
                                        </span>
 | 
					                                        </span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                                        <span v-auth="'machine:file:write'">
 | 
					                                        <span v-auth="'machine:file:write'">
 | 
				
			||||||
                                            <el-dropdown-item v-if="data.type == '-'">
 | 
					                                            <el-dropdown-item @click="downloadFile(node, data)" v-if="data.type == '-'">
 | 
				
			||||||
                                                <el-link
 | 
					                                                <el-link type="primary" icon="download" :underline="false" style="margin-left: 2px">下载</el-link>
 | 
				
			||||||
                                                    @click.prevent="downloadFile(node, data)"
 | 
					 | 
				
			||||||
                                                    type="primary"
 | 
					 | 
				
			||||||
                                                    icon="download"
 | 
					 | 
				
			||||||
                                                    :underline="false"
 | 
					 | 
				
			||||||
                                                    style="margin-left: 2px"
 | 
					 | 
				
			||||||
                                                    >下载</el-link
 | 
					 | 
				
			||||||
                                                >
 | 
					 | 
				
			||||||
                                            </el-dropdown-item>
 | 
					                                            </el-dropdown-item>
 | 
				
			||||||
                                        </span>
 | 
					                                        </span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                                        <span v-auth="'machine:file:rm'">
 | 
					                                        <span v-auth="'machine:file:rm'">
 | 
				
			||||||
                                            <el-dropdown-item v-if="!dontOperate(data)">
 | 
					                                            <el-dropdown-item @click="deleteFile(node, data)" v-if="!dontOperate(data)">
 | 
				
			||||||
                                                <el-link
 | 
					                                                <el-link type="danger" icon="delete" :underline="false" style="margin-left: 2px">删除</el-link>
 | 
				
			||||||
                                                    @click.prevent="deleteFile(node, data)"
 | 
					 | 
				
			||||||
                                                    type="danger"
 | 
					 | 
				
			||||||
                                                    icon="delete"
 | 
					 | 
				
			||||||
                                                    :underline="false"
 | 
					 | 
				
			||||||
                                                    style="margin-left: 2px"
 | 
					 | 
				
			||||||
                                                    >删除
 | 
					 | 
				
			||||||
                                                </el-link>
 | 
					 | 
				
			||||||
                                            </el-dropdown-item>
 | 
					                                            </el-dropdown-item>
 | 
				
			||||||
                                        </span>
 | 
					                                        </span>
 | 
				
			||||||
                                    </el-dropdown-menu>
 | 
					                                    </el-dropdown-menu>
 | 
				
			||||||
@@ -151,6 +139,35 @@
 | 
				
			|||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog
 | 
				
			||||||
 | 
					            :destroy-on-close="true"
 | 
				
			||||||
 | 
					            title="新建文件"
 | 
				
			||||||
 | 
					            v-model="createFileDialog.visible"
 | 
				
			||||||
 | 
					            :before-close="closeCreateFileDialog"
 | 
				
			||||||
 | 
					            :close-on-click-modal="false"
 | 
				
			||||||
 | 
					            top="5vh"
 | 
				
			||||||
 | 
					            width="400px"
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					            <div>
 | 
				
			||||||
 | 
					                <el-form-item prop="name" label="名称:">
 | 
				
			||||||
 | 
					                    <el-input v-model.trim="createFileDialog.name" placeholder="请输入名称" auto-complete="off"></el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="type" label="类型:">
 | 
				
			||||||
 | 
					                    <el-radio-group v-model="createFileDialog.type">
 | 
				
			||||||
 | 
					                        <el-radio label="d" size="small">文件夹</el-radio>
 | 
				
			||||||
 | 
					                        <el-radio label="-" size="small">文件</el-radio>
 | 
				
			||||||
 | 
					                    </el-radio-group>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <template #footer>
 | 
				
			||||||
 | 
					                <div class="dialog-footer">
 | 
				
			||||||
 | 
					                    <el-button @click="closeCreateFileDialog">关闭</el-button>
 | 
				
			||||||
 | 
					                    <el-button v-auth="'machine:file:write'" type="primary" @click="createFile">确定</el-button>
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
 | 
					            </template>
 | 
				
			||||||
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <el-dialog
 | 
					        <el-dialog
 | 
				
			||||||
            :destroy-on-close="true"
 | 
					            :destroy-on-close="true"
 | 
				
			||||||
            :title="fileContent.dialogTitle"
 | 
					            :title="fileContent.dialogTitle"
 | 
				
			||||||
@@ -182,6 +199,7 @@ import { codemirror } from '@/components/codemirror';
 | 
				
			|||||||
import { getSession } from '@/common/utils/storage';
 | 
					import { getSession } from '@/common/utils/storage';
 | 
				
			||||||
import enums from './enums';
 | 
					import enums from './enums';
 | 
				
			||||||
import config from '@/common/config';
 | 
					import config from '@/common/config';
 | 
				
			||||||
 | 
					import { isTrue } from '@/common/assert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'FileManage',
 | 
					    name: 'FileManage',
 | 
				
			||||||
@@ -202,26 +220,8 @@ export default defineComponent({
 | 
				
			|||||||
        const fileTree: any = ref(null);
 | 
					        const fileTree: any = ref(null);
 | 
				
			||||||
        const token = getSession('token');
 | 
					        const token = getSession('token');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const cmOptions = {
 | 
					        const folderType = 'd';
 | 
				
			||||||
            tabSize: 2,
 | 
					        const fileType = '-';
 | 
				
			||||||
            mode: 'text/x-sh',
 | 
					 | 
				
			||||||
            theme: 'panda-syntax',
 | 
					 | 
				
			||||||
            line: true,
 | 
					 | 
				
			||||||
            // 开启校验
 | 
					 | 
				
			||||||
            lint: true,
 | 
					 | 
				
			||||||
            gutters: ['CodeMirror-lint-markers'],
 | 
					 | 
				
			||||||
            indentWithTabs: true,
 | 
					 | 
				
			||||||
            smartIndent: true,
 | 
					 | 
				
			||||||
            matchBrackets: true,
 | 
					 | 
				
			||||||
            autofocus: true,
 | 
					 | 
				
			||||||
            styleSelectedText: true,
 | 
					 | 
				
			||||||
            styleActiveLine: true, // 高亮选中行
 | 
					 | 
				
			||||||
            foldGutter: true, // 块槽
 | 
					 | 
				
			||||||
            hintOptions: {
 | 
					 | 
				
			||||||
                // 当匹配只有一项的时候是否自动补全
 | 
					 | 
				
			||||||
                completeSingle: true,
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
@@ -268,6 +268,12 @@ export default defineComponent({
 | 
				
			|||||||
                path: '',
 | 
					                path: '',
 | 
				
			||||||
                type: '',
 | 
					                type: '',
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            createFileDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                name: '',
 | 
				
			||||||
 | 
					                type: folderType,
 | 
				
			||||||
 | 
					                node: null as any,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            file: null as any,
 | 
					            file: null as any,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -290,18 +296,6 @@ export default defineComponent({
 | 
				
			|||||||
            getFiles();
 | 
					            getFiles();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					 | 
				
			||||||
         * tab切换触发事件
 | 
					 | 
				
			||||||
         * @param {Object} tab
 | 
					 | 
				
			||||||
         * @param {Object} event
 | 
					 | 
				
			||||||
         */
 | 
					 | 
				
			||||||
        // handleClick(tab, event) {
 | 
					 | 
				
			||||||
        //   // if (tab.name == 'file-manage') {
 | 
					 | 
				
			||||||
        //   //   this.fileManage.node.childNodes = [];
 | 
					 | 
				
			||||||
        //   //   this.loadNode(this.fileManage.node, this.fileManage.resolve);
 | 
					 | 
				
			||||||
        //   // }
 | 
					 | 
				
			||||||
        // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const add = () => {
 | 
					        const add = () => {
 | 
				
			||||||
            // 往数组头部添加元素
 | 
					            // 往数组头部添加元素
 | 
				
			||||||
            state.fileTable = [{}].concat(state.fileTable);
 | 
					            state.fileTable = [{}].concat(state.fileTable);
 | 
				
			||||||
@@ -329,7 +323,6 @@ export default defineComponent({
 | 
				
			|||||||
                        })
 | 
					                        })
 | 
				
			||||||
                        .then(() => {
 | 
					                        .then(() => {
 | 
				
			||||||
                            getFiles();
 | 
					                            getFiles();
 | 
				
			||||||
                            // state.fileTable.splice(idx, 1);
 | 
					 | 
				
			||||||
                        });
 | 
					                        });
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
@@ -406,7 +399,6 @@ export default defineComponent({
 | 
				
			|||||||
            emit('update:visible', false);
 | 
					            emit('update:visible', false);
 | 
				
			||||||
            emit('update:machineId', null);
 | 
					            emit('update:machineId', null);
 | 
				
			||||||
            emit('cancel');
 | 
					            emit('cancel');
 | 
				
			||||||
            // state.activeName = 'conf-file'
 | 
					 | 
				
			||||||
            state.fileTable = [];
 | 
					            state.fileTable = [];
 | 
				
			||||||
            state.tree.folder = { id: 0 };
 | 
					            state.tree.folder = { id: 0 };
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -431,7 +423,7 @@ export default defineComponent({
 | 
				
			|||||||
                return resolve([
 | 
					                return resolve([
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        name: path,
 | 
					                        name: path,
 | 
				
			||||||
                        type: 'd',
 | 
					                        type: folderType,
 | 
				
			||||||
                        path: path,
 | 
					                        path: path,
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ]);
 | 
					                ]);
 | 
				
			||||||
@@ -453,13 +445,42 @@ export default defineComponent({
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
            for (const file of res) {
 | 
					            for (const file of res) {
 | 
				
			||||||
                const type = file.type;
 | 
					                const type = file.type;
 | 
				
			||||||
                if (type != 'd') {
 | 
					                if (type == fileType) {
 | 
				
			||||||
                    file.leaf = true;
 | 
					                    file.leaf = true;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return resolve(res);
 | 
					            return resolve(res);
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const showCreateFileDialog = (node: any) => {
 | 
				
			||||||
 | 
					            isTrue(node.expanded, '请先点击展开该节点后再创建');
 | 
				
			||||||
 | 
					            state.createFileDialog.node = node;
 | 
				
			||||||
 | 
					            state.createFileDialog.visible = true;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const createFile = async () => {
 | 
				
			||||||
 | 
					            const node = state.createFileDialog.node;
 | 
				
			||||||
 | 
					            console.log(node.data);
 | 
				
			||||||
 | 
					            const name = state.createFileDialog.name;
 | 
				
			||||||
 | 
					            const type = state.createFileDialog.type;
 | 
				
			||||||
 | 
					            const path = node.data.path + '/' + name;
 | 
				
			||||||
 | 
					            await machineApi.createFile.request({
 | 
				
			||||||
 | 
					                machineId: props.machineId,
 | 
				
			||||||
 | 
					                id: state.tree.folder.id,
 | 
				
			||||||
 | 
					                path,
 | 
				
			||||||
 | 
					                type,
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            fileTree.value.append({ name: name, path: path, type: type, leaf: type === fileType, size: 0 }, node);
 | 
				
			||||||
 | 
					            closeCreateFileDialog();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const closeCreateFileDialog = () => {
 | 
				
			||||||
 | 
					            state.createFileDialog.visible = false;
 | 
				
			||||||
 | 
					            state.createFileDialog.node = null;
 | 
				
			||||||
 | 
					            state.createFileDialog.name = '';
 | 
				
			||||||
 | 
					            state.createFileDialog.type = folderType;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const deleteFile = (node: any, data: any) => {
 | 
					        const deleteFile = (node: any, data: any) => {
 | 
				
			||||||
            const file = data.path;
 | 
					            const file = data.path;
 | 
				
			||||||
            ElMessageBox.confirm(`此操作将删除 [${file}], 是否继续?`, '提示', {
 | 
					            ElMessageBox.confirm(`此操作将删除 [${file}], 是否继续?`, '提示', {
 | 
				
			||||||
@@ -486,7 +507,6 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const downloadFile = (node: any, data: any) => {
 | 
					        const downloadFile = (node: any, data: any) => {
 | 
				
			||||||
            const a = document.createElement('a');
 | 
					            const a = document.createElement('a');
 | 
				
			||||||
            // a.setAttribute('target', '_blank')
 | 
					 | 
				
			||||||
            a.setAttribute(
 | 
					            a.setAttribute(
 | 
				
			||||||
                'href',
 | 
					                'href',
 | 
				
			||||||
                `${config.baseApiUrl}/machines/${props.machineId}/files/${state.tree.folder.id}/read?type=1&path=${data.path}&token=${token}`
 | 
					                `${config.baseApiUrl}/machines/${props.machineId}/files/${state.tree.folder.id}/read?type=1&path=${data.path}&token=${token}`
 | 
				
			||||||
@@ -534,7 +554,6 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const beforeUpload = (file: File) => {
 | 
					        const beforeUpload = (file: File) => {
 | 
				
			||||||
            state.file = file;
 | 
					            state.file = file;
 | 
				
			||||||
            // ElMessage.success(`'${file.name}' 上传中,请关注结果通知`);
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        const getFilePath = (data: object, visible: boolean) => {
 | 
					        const getFilePath = (data: object, visible: boolean) => {
 | 
				
			||||||
            if (visible) {
 | 
					            if (visible) {
 | 
				
			||||||
@@ -590,7 +609,6 @@ export default defineComponent({
 | 
				
			|||||||
            fileTree,
 | 
					            fileTree,
 | 
				
			||||||
            enums,
 | 
					            enums,
 | 
				
			||||||
            token,
 | 
					            token,
 | 
				
			||||||
            cmOptions,
 | 
					 | 
				
			||||||
            add,
 | 
					            add,
 | 
				
			||||||
            getFiles,
 | 
					            getFiles,
 | 
				
			||||||
            handlePageChange,
 | 
					            handlePageChange,
 | 
				
			||||||
@@ -601,6 +619,9 @@ export default defineComponent({
 | 
				
			|||||||
            updateContent,
 | 
					            updateContent,
 | 
				
			||||||
            handleClose,
 | 
					            handleClose,
 | 
				
			||||||
            loadNode,
 | 
					            loadNode,
 | 
				
			||||||
 | 
					            showCreateFileDialog,
 | 
				
			||||||
 | 
					            closeCreateFileDialog,
 | 
				
			||||||
 | 
					            createFile,
 | 
				
			||||||
            deleteFile,
 | 
					            deleteFile,
 | 
				
			||||||
            downloadFile,
 | 
					            downloadFile,
 | 
				
			||||||
            getUploadFile,
 | 
					            getUploadFile,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="false" :destroy-on-close="true" :before-close="cancel" width="35%">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="false" :destroy-on-close="true" :before-close="cancel" width="38%">
 | 
				
			||||||
            <el-form :model="form" ref="machineForm" :rules="rules" label-width="85px">
 | 
					            <el-form :model="form" ref="machineForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目:" required>
 | 
					                <el-form-item prop="projectId" label="项目:" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
@@ -11,26 +11,64 @@
 | 
				
			|||||||
                    <el-input v-model.trim="form.name" placeholder="请输入机器别名" auto-complete="off"></el-input>
 | 
					                    <el-input v-model.trim="form.name" placeholder="请输入机器别名" auto-complete="off"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="ip" label="ip:" required>
 | 
					                <el-form-item prop="ip" label="ip:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.ip" placeholder="请输入主机ip" auto-complete="off"></el-input>
 | 
					                    <el-col :span="18">
 | 
				
			||||||
                </el-form-item>
 | 
					                        <el-input v-model.trim="form.ip" placeholder="主机ip" auto-complete="off"></el-input>
 | 
				
			||||||
                <el-form-item prop="port" label="port:" required>
 | 
					                    </el-col>
 | 
				
			||||||
                    <el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
 | 
					                    <el-col style="text-align: center" :span="1">:</el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="5">
 | 
				
			||||||
 | 
					                        <el-input type="number" v-model.number="form.port" placeholder="端口"></el-input>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="username" label="用户名:" required>
 | 
					                <el-form-item prop="username" label="用户名:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
					                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="password" label="密码:">
 | 
					                <el-form-item prop="authMethod" label="认证方式:" required>
 | 
				
			||||||
 | 
					                    <el-select style="width: 100%" v-model="form.authMethod" placeholder="请选择认证方式">
 | 
				
			||||||
 | 
					                        <el-option key="1" label="Password" :value="1"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option key="2" label="PublicKey" :value="2"> </el-option>
 | 
				
			||||||
 | 
					                    </el-select>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item v-if="form.authMethod == 1" prop="password" label="密码:">
 | 
				
			||||||
                    <el-input
 | 
					                    <el-input
 | 
				
			||||||
                        type="password"
 | 
					                        type="password"
 | 
				
			||||||
                        show-password
 | 
					                        show-password
 | 
				
			||||||
                        v-model.trim="form.password"
 | 
					                        v-model.trim="form.password"
 | 
				
			||||||
                        placeholder="请输入密码,修改操作可不填"
 | 
					                        placeholder="请输入密码,修改操作可不填"
 | 
				
			||||||
                        autocomplete="new-password"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                    ></el-input>
 | 
					                    >
 | 
				
			||||||
 | 
					                        <template v-if="form.id && form.id != 0" #suffix>
 | 
				
			||||||
 | 
					                            <el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
 | 
				
			||||||
 | 
					                                <template #reference>
 | 
				
			||||||
 | 
					                                    <el-link @click="getPwd" :underline="false" type="primary" class="mr5">原密码</el-link>
 | 
				
			||||||
 | 
					                                </template>
 | 
				
			||||||
 | 
					                            </el-popover>
 | 
				
			||||||
 | 
					                        </template>
 | 
				
			||||||
 | 
					                    </el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item v-if="form.authMethod == 2" prop="password" label="秘钥:">
 | 
				
			||||||
 | 
					                    <el-input type="textarea" :rows="3" v-model="form.password" placeholder="请将私钥文件内容拷贝至此,修改操作可不填"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="remark" label="备注:">
 | 
					                <el-form-item prop="remark" label="备注:">
 | 
				
			||||||
                    <el-input type="textarea" v-model="form.remark"></el-input>
 | 
					                    <el-input type="textarea" v-model="form.remark"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
@@ -48,6 +86,7 @@ import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			|||||||
import { machineApi } from './api';
 | 
					import { machineApi } from './api';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
import { notBlank } from '@/common/assert';
 | 
					import { notBlank } from '@/common/assert';
 | 
				
			||||||
 | 
					import { RsaEncrypt } from '@/common/rsa';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'MachineEdit',
 | 
					    name: 'MachineEdit',
 | 
				
			||||||
@@ -70,16 +109,21 @@ export default defineComponent({
 | 
				
			|||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                projectName: null,
 | 
					                projectName: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
 | 
					                authMethod: 1,
 | 
				
			||||||
                port: 22,
 | 
					                port: 22,
 | 
				
			||||||
                username: null,
 | 
					                username: '',
 | 
				
			||||||
                password: null,
 | 
					                password: '',
 | 
				
			||||||
                remark: '',
 | 
					                remark: '',
 | 
				
			||||||
 | 
					                enableSshTunnel: null,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            pwd: '',
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                projectId: [
 | 
					                projectId: [
 | 
				
			||||||
@@ -106,14 +150,7 @@ export default defineComponent({
 | 
				
			|||||||
                ip: [
 | 
					                ip: [
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        required: true,
 | 
					                        required: true,
 | 
				
			||||||
                        message: '请输入主机ip',
 | 
					                        message: '请输入主机ip和端口',
 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ],
 | 
					 | 
				
			||||||
                port: [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        required: true,
 | 
					 | 
				
			||||||
                        message: '请输入端口',
 | 
					 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
@@ -124,19 +161,45 @@ export default defineComponent({
 | 
				
			|||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
 | 
					                authMethod: [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        required: true,
 | 
				
			||||||
 | 
					                        message: '请选择认证方式',
 | 
				
			||||||
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, async (newValue) => {
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.machine) {
 | 
					            if (newValue.machine) {
 | 
				
			||||||
                state.form = { ...newValue.machine };
 | 
					                state.form = { ...newValue.machine };
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.form = { port: 22 } as any;
 | 
					                state.form = { port: 22, authMethod: 1 } as any;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachine = (machineId: any) => {
 | 
				
			||||||
 | 
					            return state.sshTunnelMachineList.find((x: any) => x.id == machineId);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getPwd = async () => {
 | 
				
			||||||
 | 
					            state.pwd = await machineApi.getMachinePwd.request({ id: state.form.id });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const changeProject = (projectId: number) => {
 | 
					        const changeProject = (projectId: number) => {
 | 
				
			||||||
            for (let p of state.projects as any) {
 | 
					            for (let p of state.projects as any) {
 | 
				
			||||||
                if (p.id == projectId) {
 | 
					                if (p.id == projectId) {
 | 
				
			||||||
@@ -149,18 +212,29 @@ export default defineComponent({
 | 
				
			|||||||
            if (!state.form.id) {
 | 
					            if (!state.form.id) {
 | 
				
			||||||
                notBlank(state.form.password, '新增操作,密码不可为空');
 | 
					                notBlank(state.form.password, '新增操作,密码不可为空');
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            machineForm.value.validate((valid: boolean) => {
 | 
					            machineForm.value.validate(async (valid: boolean) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    machineApi.saveMachine.request(state.form).then(() => {
 | 
					                    const form: any = state.form;
 | 
				
			||||||
 | 
					                    if (form.enableSshTunnel == 1) {
 | 
				
			||||||
 | 
					                        const tunnelMachine: any = getSshTunnelMachine(form.sshTunnelMachineId);
 | 
				
			||||||
 | 
					                        if (tunnelMachine.ip == form.ip && tunnelMachine.port == form.port) {
 | 
				
			||||||
 | 
					                            ElMessage.error('隧道机器不能与本机器一致');
 | 
				
			||||||
 | 
					                            return;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    const reqForm: any = { ...form };
 | 
				
			||||||
 | 
					                    if (reqForm.authMethod == 1) {
 | 
				
			||||||
 | 
					                        reqForm.password = await RsaEncrypt(state.form.password);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    state.btnLoading = true;
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        await machineApi.saveMachine.request(reqForm);
 | 
				
			||||||
                        ElMessage.success('保存成功');
 | 
					                        ElMessage.success('保存成功');
 | 
				
			||||||
                        emit('val-change', state.form);
 | 
					                        emit('val-change', state.form);
 | 
				
			||||||
                        state.btnLoading = true;
 | 
					 | 
				
			||||||
                        setTimeout(() => {
 | 
					 | 
				
			||||||
                            state.btnLoading = false;
 | 
					 | 
				
			||||||
                        }, 1000);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                        cancel();
 | 
					                        cancel();
 | 
				
			||||||
                    });
 | 
					                    } finally {
 | 
				
			||||||
 | 
					                        state.btnLoading = false;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    ElMessage.error('请正确填写信息');
 | 
					                    ElMessage.error('请正确填写信息');
 | 
				
			||||||
                    return false;
 | 
					                    return false;
 | 
				
			||||||
@@ -176,6 +250,8 @@ export default defineComponent({
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            machineForm,
 | 
					            machineForm,
 | 
				
			||||||
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
 | 
					            getPwd,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,7 +42,7 @@
 | 
				
			|||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip></el-table-column>
 | 
					                <el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
                <el-table-column prop="ip" label="ip:port" min-width="140">
 | 
					                <el-table-column prop="ip" label="ip:port" min-width="150">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        <el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{
 | 
					                        <el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{
 | 
				
			||||||
                            `${scope.row.ip}:${scope.row.port}`
 | 
					                            `${scope.row.ip}:${scope.row.port}`
 | 
				
			||||||
@@ -57,11 +57,10 @@
 | 
				
			|||||||
                            v-model="scope.row.status"
 | 
					                            v-model="scope.row.status"
 | 
				
			||||||
                            :active-value="1"
 | 
					                            :active-value="1"
 | 
				
			||||||
                            :inactive-value="-1"
 | 
					                            :inactive-value="-1"
 | 
				
			||||||
                            active-color="#13ce66"
 | 
					 | 
				
			||||||
                            inactive-color="#ff4949"
 | 
					 | 
				
			||||||
                            inline-prompt
 | 
					                            inline-prompt
 | 
				
			||||||
                            active-text="启用"
 | 
					                            active-text="启用"
 | 
				
			||||||
                            inactive-text="停用"
 | 
					                            inactive-text="停用"
 | 
				
			||||||
 | 
					                            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
 | 
				
			||||||
                            @change="changeStatus(scope.row)"
 | 
					                            @change="changeStatus(scope.row)"
 | 
				
			||||||
                        ></el-switch>
 | 
					                        ></el-switch>
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
@@ -69,11 +68,6 @@
 | 
				
			|||||||
                <el-table-column prop="username" label="用户名" min-width="90"></el-table-column>
 | 
					                <el-table-column prop="username" label="用户名" min-width="90"></el-table-column>
 | 
				
			||||||
                <el-table-column prop="projectName" label="项目" min-width="120"></el-table-column>
 | 
					                <el-table-column prop="projectName" label="项目" min-width="120"></el-table-column>
 | 
				
			||||||
                <el-table-column prop="remark" label="备注" min-width="250" show-overflow-tooltip></el-table-column>
 | 
					                <el-table-column prop="remark" label="备注" min-width="250" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
                <el-table-column prop="ip" label="hasCli" width="70">
 | 
					 | 
				
			||||||
                    <template #default="scope">
 | 
					 | 
				
			||||||
                        {{ `${scope.row.hasCli ? '是' : '否'}` }}
 | 
					 | 
				
			||||||
                    </template>
 | 
					 | 
				
			||||||
                </el-table-column>
 | 
					 | 
				
			||||||
                <el-table-column prop="createTime" label="创建时间" min-width="165">
 | 
					                <el-table-column prop="createTime" label="创建时间" min-width="165">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
					                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
				
			||||||
@@ -233,7 +227,6 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        onMounted(async () => {
 | 
					        onMounted(async () => {
 | 
				
			||||||
            search();
 | 
					            search();
 | 
				
			||||||
            state.projects = await projectApi.accountProjects.request(null);
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const choose = (item: any) => {
 | 
					        const choose = (item: any) => {
 | 
				
			||||||
@@ -256,18 +249,24 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const closeCli = async (row: any) => {
 | 
					        const closeCli = async (row: any) => {
 | 
				
			||||||
 | 
					            await ElMessageBox.confirm(`确定关闭该机器客户端连接?`, '提示', {
 | 
				
			||||||
 | 
					                confirmButtonText: '确定',
 | 
				
			||||||
 | 
					                cancelButtonText: '取消',
 | 
				
			||||||
 | 
					                type: 'warning',
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
            await machineApi.closeCli.request({ id: row.id });
 | 
					            await machineApi.closeCli.request({ id: row.id });
 | 
				
			||||||
            ElMessage.success('关闭成功');
 | 
					            ElMessage.success('关闭成功');
 | 
				
			||||||
            search();
 | 
					            search();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const openFormDialog = (redis: any) => {
 | 
					        const openFormDialog = async (machine: any) => {
 | 
				
			||||||
 | 
					            state.projects = await projectApi.accountProjects.request(null);
 | 
				
			||||||
            let dialogTitle;
 | 
					            let dialogTitle;
 | 
				
			||||||
            if (redis) {
 | 
					            if (machine) {
 | 
				
			||||||
                state.machineEditDialog.data = state.currentData as any;
 | 
					                state.machineEditDialog.data = state.currentData as any;
 | 
				
			||||||
                dialogTitle = '编辑机器';
 | 
					                dialogTitle = '编辑机器';
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.machineEditDialog.data = { port: 22 } as any;
 | 
					                state.machineEditDialog.data = null;
 | 
				
			||||||
                dialogTitle = '添加机器';
 | 
					                dialogTitle = '添加机器';
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,9 +7,9 @@
 | 
				
			|||||||
            :before-close="cancel"
 | 
					            :before-close="cancel"
 | 
				
			||||||
            :show-close="true"
 | 
					            :show-close="true"
 | 
				
			||||||
            :destroy-on-close="true"
 | 
					            :destroy-on-close="true"
 | 
				
			||||||
            width="800px"
 | 
					            width="900px"
 | 
				
			||||||
        >
 | 
					        >
 | 
				
			||||||
            <el-form :model="form" ref="mockDataForm" label-width="70px">
 | 
					            <el-form :model="form" ref="scriptForm" label-width="70px" size="small">
 | 
				
			||||||
                <el-form-item prop="method" label="名称">
 | 
					                <el-form-item prop="method" label="名称">
 | 
				
			||||||
                    <el-input v-model.trim="form.name" placeholder="请输入名称"></el-input>
 | 
					                    <el-input v-model.trim="form.name" placeholder="请输入名称"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
@@ -24,8 +24,23 @@
 | 
				
			|||||||
                    </el-select>
 | 
					                    </el-select>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-form-item prop="params" label="参数">
 | 
					                <el-row style="margin-left: 30px; margin-bottom: 5px">
 | 
				
			||||||
                    <el-input v-model.trim="form.params" placeholder="参数数组json,若无可不填"></el-input>
 | 
					                    <el-button @click="onAddParam" size="small" type="success">新增占位符参数</el-button>
 | 
				
			||||||
 | 
					                </el-row>
 | 
				
			||||||
 | 
					                <el-form-item :key="param" v-for="(param, index) in params" prop="params" :label="`参数${index + 1}`">
 | 
				
			||||||
 | 
					                    <el-row>
 | 
				
			||||||
 | 
					                        <el-col :span="5"><el-input v-model="param.model" placeholder="内容中用{{.model}}替换"></el-input></el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="4"><el-input v-model="param.name" placeholder="字段名"></el-input></el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="4"><el-input v-model="param.placeholder" placeholder="字段说明"></el-input></el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="4">
 | 
				
			||||||
 | 
					                            <el-input v-model="param.options" placeholder="可选值 ,分割"></el-input>
 | 
				
			||||||
 | 
					                        </el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="2"><el-button @click="onDeleteParam(index)" size="small" type="danger">删除</el-button></el-col>
 | 
				
			||||||
 | 
					                    </el-row>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-form-item prop="script" label="内容" id="content">
 | 
					                <el-form-item prop="script" label="内容" id="content">
 | 
				
			||||||
@@ -35,13 +50,12 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
                <div class="dialog-footer">
 | 
					                <div class="dialog-footer">
 | 
				
			||||||
                    <el-button @click="cancel()" :disabled="submitDisabled" size="small">关 闭</el-button>
 | 
					                    <el-button @click="cancel()" :disabled="submitDisabled">关 闭</el-button>
 | 
				
			||||||
                    <el-button
 | 
					                    <el-button
 | 
				
			||||||
                        v-auth="'machine:script:save'"
 | 
					                        v-auth="'machine:script:save'"
 | 
				
			||||||
                        type="primary"
 | 
					                        type="primary"
 | 
				
			||||||
                        :loading="btnLoading"
 | 
					                        :loading="btnLoading"
 | 
				
			||||||
                        @click="btnOk"
 | 
					                        @click="btnOk"
 | 
				
			||||||
                        size="small"
 | 
					 | 
				
			||||||
                        :disabled="submitDisabled"
 | 
					                        :disabled="submitDisabled"
 | 
				
			||||||
                        >保 存</el-button
 | 
					                        >保 存</el-button
 | 
				
			||||||
                    >
 | 
					                    >
 | 
				
			||||||
@@ -84,41 +98,59 @@ export default defineComponent({
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    setup(props: any, { emit }) {
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
        const { isCommon, machineId } = toRefs(props);
 | 
					        const { isCommon, machineId } = toRefs(props);
 | 
				
			||||||
        const mockDataForm: any = ref(null);
 | 
					        const scriptForm: any = ref(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            submitDisabled: false,
 | 
					            submitDisabled: false,
 | 
				
			||||||
 | 
					            params: [] as any,
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: '',
 | 
					                name: '',
 | 
				
			||||||
                machineId: 0,
 | 
					                machineId: 0,
 | 
				
			||||||
                description: '',
 | 
					                description: '',
 | 
				
			||||||
                script: '',
 | 
					                script: '',
 | 
				
			||||||
                params: null,
 | 
					                params: '',
 | 
				
			||||||
                type: null,
 | 
					                type: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, (newValue) => {
 | 
					        watch(props, (newValue) => {
 | 
				
			||||||
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!newValue.visible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            if (newValue.data) {
 | 
					            if (newValue.data) {
 | 
				
			||||||
                state.form = { ...newValue.data };
 | 
					                state.form = { ...newValue.data };
 | 
				
			||||||
 | 
					                if (state.form.params) {
 | 
				
			||||||
 | 
					                    state.params = JSON.parse(state.form.params);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.form = {} as any;
 | 
					                state.form = {} as any;
 | 
				
			||||||
                state.form.script = '';
 | 
					                state.form.script = '';
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onAddParam = () => {
 | 
				
			||||||
 | 
					            state.params.push({ name: '', model: '', placeholder: '' });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onDeleteParam = (idx: number) => {
 | 
				
			||||||
 | 
					            state.params.splice(idx, 1);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const btnOk = () => {
 | 
					        const btnOk = () => {
 | 
				
			||||||
            state.form.machineId = isCommon.value ? 9999999 : (machineId.value as any);
 | 
					            state.form.machineId = isCommon.value ? 9999999 : (machineId.value as any);
 | 
				
			||||||
            console.log('machineid:', machineId);
 | 
					            console.log('machineid:', machineId);
 | 
				
			||||||
            mockDataForm.value.validate((valid: any) => {
 | 
					            scriptForm.value.validate((valid: any) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    notEmpty(state.form.name, '名称不能为空');
 | 
					                    notEmpty(state.form.name, '名称不能为空');
 | 
				
			||||||
                    notEmpty(state.form.description, '描述不能为空');
 | 
					                    notEmpty(state.form.description, '描述不能为空');
 | 
				
			||||||
                    notEmpty(state.form.script, '内容不能为空');
 | 
					                    notEmpty(state.form.script, '内容不能为空');
 | 
				
			||||||
 | 
					                    if (state.params) {
 | 
				
			||||||
 | 
					                        state.form.params = JSON.stringify(state.params);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    machineApi.saveScript.request(state.form).then(
 | 
					                    machineApi.saveScript.request(state.form).then(
 | 
				
			||||||
                        () => {
 | 
					                        () => {
 | 
				
			||||||
                            ElMessage.success('保存成功');
 | 
					                            ElMessage.success('保存成功');
 | 
				
			||||||
@@ -139,12 +171,15 @@ export default defineComponent({
 | 
				
			|||||||
        const cancel = () => {
 | 
					        const cancel = () => {
 | 
				
			||||||
            emit('update:visible', false);
 | 
					            emit('update:visible', false);
 | 
				
			||||||
            emit('cancel');
 | 
					            emit('cancel');
 | 
				
			||||||
 | 
					            state.params = [];
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            enums,
 | 
					            enums,
 | 
				
			||||||
            mockDataForm,
 | 
					            onAddParam,
 | 
				
			||||||
 | 
					            onDeleteParam,
 | 
				
			||||||
 | 
					            scriptForm,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -76,7 +76,24 @@
 | 
				
			|||||||
        <el-dialog title="脚本参数" v-model="scriptParamsDialog.visible" width="400px">
 | 
					        <el-dialog title="脚本参数" v-model="scriptParamsDialog.visible" width="400px">
 | 
				
			||||||
            <el-form ref="paramsForm" :model="scriptParamsDialog.params" label-width="70px" size="small">
 | 
					            <el-form ref="paramsForm" :model="scriptParamsDialog.params" label-width="70px" size="small">
 | 
				
			||||||
                <el-form-item v-for="item in scriptParamsDialog.paramsFormItem" :key="item.name" :prop="item.model" :label="item.name" required>
 | 
					                <el-form-item v-for="item in scriptParamsDialog.paramsFormItem" :key="item.name" :prop="item.model" :label="item.name" required>
 | 
				
			||||||
                    <el-input v-model="scriptParamsDialog.params[item.model]" :placeholder="item.placeholder" autocomplete="off"></el-input>
 | 
					                    <el-input
 | 
				
			||||||
 | 
					                        v-if="!item.options"
 | 
				
			||||||
 | 
					                        v-model="scriptParamsDialog.params[item.model]"
 | 
				
			||||||
 | 
					                        :placeholder="item.placeholder"
 | 
				
			||||||
 | 
					                        autocomplete="off"
 | 
				
			||||||
 | 
					                        clearable
 | 
				
			||||||
 | 
					                    ></el-input>
 | 
				
			||||||
 | 
					                    <el-select
 | 
				
			||||||
 | 
					                        v-else
 | 
				
			||||||
 | 
					                        v-model="scriptParamsDialog.params[item.model]"
 | 
				
			||||||
 | 
					                        :placeholder="item.placeholder"
 | 
				
			||||||
 | 
					                        filterable
 | 
				
			||||||
 | 
					                        autocomplete="off"
 | 
				
			||||||
 | 
					                        clearable
 | 
				
			||||||
 | 
					                        style="width: 100%"
 | 
				
			||||||
 | 
					                    >
 | 
				
			||||||
 | 
					                        <el-option v-for="option in item.options.split(',')" :key="option" :label="option" :value="option" />
 | 
				
			||||||
 | 
					                    </el-select>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
@@ -88,7 +105,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        <el-dialog title="执行结果" v-model="resultDialog.visible" width="50%">
 | 
					        <el-dialog title="执行结果" v-model="resultDialog.visible" width="50%">
 | 
				
			||||||
            <div style="white-space: pre-line; padding: 10px; color: #000000">
 | 
					            <div style="white-space: pre-line; padding: 10px; color: #000000">
 | 
				
			||||||
                <!-- {{ resultDialog.result }} -->
 | 
					 | 
				
			||||||
                <el-input v-model="resultDialog.result" :rows="20" type="textarea" />
 | 
					                <el-input v-model="resultDialog.result" :rows="20" type="textarea" />
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
@@ -97,12 +113,12 @@
 | 
				
			|||||||
            v-if="terminalDialog.visible"
 | 
					            v-if="terminalDialog.visible"
 | 
				
			||||||
            title="终端"
 | 
					            title="终端"
 | 
				
			||||||
            v-model="terminalDialog.visible"
 | 
					            v-model="terminalDialog.visible"
 | 
				
			||||||
            width="70%"
 | 
					            width="80%"
 | 
				
			||||||
            :close-on-click-modal="false"
 | 
					            :close-on-click-modal="false"
 | 
				
			||||||
            :modal="false"
 | 
					            :modal="false"
 | 
				
			||||||
            @close="closeTermnial"
 | 
					            @close="closeTermnial"
 | 
				
			||||||
        >
 | 
					        >
 | 
				
			||||||
            <ssh-terminal ref="terminal" :cmd="terminalDialog.cmd" :machineId="terminalDialog.machineId" height="600px" />
 | 
					            <ssh-terminal ref="terminal" :cmd="terminalDialog.cmd" :machineId="terminalDialog.machineId" height="560px" />
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <script-edit
 | 
					        <script-edit
 | 
				
			||||||
@@ -196,8 +212,10 @@ export default defineComponent({
 | 
				
			|||||||
            // 如果存在参数,则弹窗输入参数后执行
 | 
					            // 如果存在参数,则弹窗输入参数后执行
 | 
				
			||||||
            if (script.params) {
 | 
					            if (script.params) {
 | 
				
			||||||
                state.scriptParamsDialog.paramsFormItem = JSON.parse(script.params);
 | 
					                state.scriptParamsDialog.paramsFormItem = JSON.parse(script.params);
 | 
				
			||||||
                state.scriptParamsDialog.visible = true;
 | 
					                if (state.scriptParamsDialog.paramsFormItem && state.scriptParamsDialog.paramsFormItem.length > 0) {
 | 
				
			||||||
                return;
 | 
					                    state.scriptParamsDialog.visible = true;
 | 
				
			||||||
 | 
					                    return;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            run(script);
 | 
					            run(script);
 | 
				
			||||||
@@ -268,8 +286,6 @@ export default defineComponent({
 | 
				
			|||||||
        const closeTermnial = () => {
 | 
					        const closeTermnial = () => {
 | 
				
			||||||
            state.terminalDialog.visible = false;
 | 
					            state.terminalDialog.visible = false;
 | 
				
			||||||
            state.terminalDialog.machineId = 0;
 | 
					            state.terminalDialog.machineId = 0;
 | 
				
			||||||
            // const t: any = this.$refs['terminal']
 | 
					 | 
				
			||||||
            // t.closeAll()
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
@@ -295,8 +311,6 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const submitSuccess = () => {
 | 
					        const submitSuccess = () => {
 | 
				
			||||||
            // this.delChoose()
 | 
					 | 
				
			||||||
            // this.search()
 | 
					 | 
				
			||||||
            getScripts();
 | 
					            getScripts();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -326,6 +340,7 @@ export default defineComponent({
 | 
				
			|||||||
            context.emit('update:machineId', null);
 | 
					            context.emit('update:machineId', null);
 | 
				
			||||||
            context.emit('cancel');
 | 
					            context.emit('cancel');
 | 
				
			||||||
            state.scriptTable = [];
 | 
					            state.scriptTable = [];
 | 
				
			||||||
 | 
					            state.scriptParamsDialog.paramsFormItem = [];
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@ import { FitAddon } from 'xterm-addon-fit';
 | 
				
			|||||||
import { getSession } from '@/common/utils/storage.ts';
 | 
					import { getSession } from '@/common/utils/storage.ts';
 | 
				
			||||||
import config from '@/common/config';
 | 
					import config from '@/common/config';
 | 
				
			||||||
import { useStore } from '@/store/index.ts';
 | 
					import { useStore } from '@/store/index.ts';
 | 
				
			||||||
import { toRefs, watch, computed, reactive, defineComponent, onMounted, onBeforeUnmount } from 'vue';
 | 
					import { nextTick, toRefs, watch, computed, reactive, defineComponent, onMounted, onBeforeUnmount } from 'vue';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'SshTerminal',
 | 
					    name: 'SshTerminal',
 | 
				
			||||||
@@ -27,22 +27,20 @@ export default defineComponent({
 | 
				
			|||||||
            socket: null as any,
 | 
					            socket: null as any,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const resize = 1;
 | 
				
			||||||
 | 
					        const data = 2;
 | 
				
			||||||
 | 
					        const ping = 3;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, (newValue) => {
 | 
					        watch(props, (newValue) => {
 | 
				
			||||||
            state.machineId = newValue.machineId;
 | 
					            state.machineId = newValue.machineId;
 | 
				
			||||||
            state.cmd = newValue.cmd;
 | 
					            state.cmd = newValue.cmd;
 | 
				
			||||||
            state.height = newValue.height;
 | 
					            state.height = newValue.height;
 | 
				
			||||||
            if (state.machineId) {
 | 
					 | 
				
			||||||
                initSocket();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        onMounted(() => {
 | 
					        onMounted(() => {
 | 
				
			||||||
            state.machineId = props.machineId;
 | 
					            state.machineId = props.machineId;
 | 
				
			||||||
            state.height = props.height;
 | 
					            state.height = props.height;
 | 
				
			||||||
            state.cmd = props.cmd;
 | 
					            state.cmd = props.cmd;
 | 
				
			||||||
            if (state.machineId) {
 | 
					 | 
				
			||||||
                initSocket();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        onBeforeUnmount(() => {
 | 
					        onBeforeUnmount(() => {
 | 
				
			||||||
@@ -56,13 +54,17 @@ export default defineComponent({
 | 
				
			|||||||
            return store.state.themeConfig.themeConfig;
 | 
					            return store.state.themeConfig.themeConfig;
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        nextTick(() => {
 | 
				
			||||||
 | 
					            initXterm();
 | 
				
			||||||
 | 
					            initSocket();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        function initXterm() {
 | 
					        function initXterm() {
 | 
				
			||||||
            const term: any = new Terminal({
 | 
					            const term: any = new Terminal({
 | 
				
			||||||
                fontSize: getThemeConfig.value.terminalFontSize || 15,
 | 
					                fontSize: getThemeConfig.value.terminalFontSize || 15,
 | 
				
			||||||
                // fontWeight: getThemeConfig.value.terminalFontWeight || 'normal',
 | 
					                fontWeight: getThemeConfig.value.terminalFontWeight || 'normal',
 | 
				
			||||||
                fontFamily: 'JetBrainsMono, Consolas, Menlo, Monaco',
 | 
					                fontFamily: 'JetBrainsMono, monaco, Consolas, Lucida Console, monospace',
 | 
				
			||||||
                cursorBlink: true,
 | 
					                cursorBlink: true,
 | 
				
			||||||
                // cursorStyle: 'underline', //光标样式
 | 
					 | 
				
			||||||
                disableStdin: false,
 | 
					                disableStdin: false,
 | 
				
			||||||
                theme: {
 | 
					                theme: {
 | 
				
			||||||
                    foreground: getThemeConfig.value.terminalForeground || '#7e9192', //字体
 | 
					                    foreground: getThemeConfig.value.terminalForeground || '#7e9192', //字体
 | 
				
			||||||
@@ -82,6 +84,14 @@ export default defineComponent({
 | 
				
			|||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    // 窗口大小改变时,触发xterm的resize方法使自适应
 | 
					                    // 窗口大小改变时,触发xterm的resize方法使自适应
 | 
				
			||||||
                    fitAddon.fit();
 | 
					                    fitAddon.fit();
 | 
				
			||||||
 | 
					                    if (state.term) {
 | 
				
			||||||
 | 
					                        state.term.focus();
 | 
				
			||||||
 | 
					                        send({
 | 
				
			||||||
 | 
					                            type: resize,
 | 
				
			||||||
 | 
					                            Cols: parseInt(state.term.cols),
 | 
				
			||||||
 | 
					                            Rows: parseInt(state.term.rows),
 | 
				
			||||||
 | 
					                        });
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                } catch (e) {
 | 
					                } catch (e) {
 | 
				
			||||||
                    console.log(e);
 | 
					                    console.log(e);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
@@ -104,69 +114,52 @@ export default defineComponent({
 | 
				
			|||||||
            term.onData((key: any) => {
 | 
					            term.onData((key: any) => {
 | 
				
			||||||
                sendCmd(key);
 | 
					                sendCmd(key);
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
            // 为解决窗体resize方法才会向后端发送列数和行数,所以页面加载时也要触发此方法
 | 
					 | 
				
			||||||
            send({
 | 
					 | 
				
			||||||
                type: 'resize',
 | 
					 | 
				
			||||||
                Cols: parseInt(term.cols),
 | 
					 | 
				
			||||||
                Rows: parseInt(term.rows),
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
            // 如果有初始要执行的命令,则发送执行命令
 | 
					 | 
				
			||||||
            if (state.cmd) {
 | 
					 | 
				
			||||||
                sendCmd(state.cmd + ' \r');
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let pingInterval: any;
 | 
				
			||||||
        function initSocket() {
 | 
					        function initSocket() {
 | 
				
			||||||
            state.socket = new WebSocket(`${config.baseWsUrl}/machines/${state.machineId}/terminal?token=${getSession('token')}`);
 | 
					            state.socket = new WebSocket(
 | 
				
			||||||
 | 
					                `${config.baseWsUrl}/machines/${state.machineId}/terminal?token=${getSession('token')}&cols=${state.term.cols}&rows=${
 | 
				
			||||||
 | 
					                    state.term.rows
 | 
				
			||||||
 | 
					                }`
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // 监听socket连接
 | 
					            // 监听socket连接
 | 
				
			||||||
            state.socket.onopen = open;
 | 
					            state.socket.onopen = () => {
 | 
				
			||||||
 | 
					                // 如果有初始要执行的命令,则发送执行命令
 | 
				
			||||||
 | 
					                if (state.cmd) {
 | 
				
			||||||
 | 
					                    sendCmd(state.cmd + ' \r');
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                // 开启心跳
 | 
				
			||||||
 | 
					                pingInterval = setInterval(() => {
 | 
				
			||||||
 | 
					                    send({ type: ping, msg: 'ping' });
 | 
				
			||||||
 | 
					                }, 8000);
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // 监听socket错误信息
 | 
					            // 监听socket错误信息
 | 
				
			||||||
            state.socket.onerror = error;
 | 
					            state.socket.onerror = (e: any) => {
 | 
				
			||||||
            // 监听socket消息
 | 
					                console.log('连接错误', e);
 | 
				
			||||||
            state.socket.onmessage = getMessage;
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            state.socket.onclose = () => {
 | 
				
			||||||
 | 
					                if (state.term) {
 | 
				
			||||||
 | 
					                    state.term.writeln('\r\n\x1b[31m提示: 连接已关闭...');
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                if (pingInterval) {
 | 
				
			||||||
 | 
					                    clearInterval(pingInterval);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // 发送socket消息
 | 
					            // 发送socket消息
 | 
				
			||||||
            state.socket.onsend = send;
 | 
					            state.socket.onsend = send;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // 监听socket消息
 | 
				
			||||||
 | 
					            state.socket.onmessage = getMessage;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        function open() {
 | 
					        function getMessage(msg: any) {
 | 
				
			||||||
            console.log('socket连接成功');
 | 
					            // msg.data是真正后端返回的数据
 | 
				
			||||||
            initXterm();
 | 
					            state.term.write(msg.data);
 | 
				
			||||||
            //开启心跳
 | 
					 | 
				
			||||||
            //   this.start();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        function error() {
 | 
					 | 
				
			||||||
            console.log('连接错误');
 | 
					 | 
				
			||||||
            //重连
 | 
					 | 
				
			||||||
            // reconnect();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        function close() {
 | 
					 | 
				
			||||||
            if (state.socket) {
 | 
					 | 
				
			||||||
                state.socket.close();
 | 
					 | 
				
			||||||
                console.log('socket关闭');
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            //重连
 | 
					 | 
				
			||||||
            //   this.reconnect()
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        function getMessage(msg: string) {
 | 
					 | 
				
			||||||
            //   console.log(msg)
 | 
					 | 
				
			||||||
            state.term.write(msg['data']);
 | 
					 | 
				
			||||||
            //msg是返回的数据
 | 
					 | 
				
			||||||
            //   msg = JSON.parse(msg.data);
 | 
					 | 
				
			||||||
            //   this.socket.send("ping");//有事没事ping一下,看看ws还活着没
 | 
					 | 
				
			||||||
            //   //switch用于处理返回的数据,根据返回数据的格式去判断
 | 
					 | 
				
			||||||
            //   switch (msg["operation"]) {
 | 
					 | 
				
			||||||
            //     case "stdout":
 | 
					 | 
				
			||||||
            //       this.term.write(msg["data"]);//这里write也许不是固定的,失败后找后端看一下该怎么往term里面write
 | 
					 | 
				
			||||||
            //       break;
 | 
					 | 
				
			||||||
            //     default:
 | 
					 | 
				
			||||||
            //       console.error("Unexpected message type:", msg);//但是错误是固定的。。。。
 | 
					 | 
				
			||||||
            //   }
 | 
					 | 
				
			||||||
            //收到服务器信息,心跳重置
 | 
					 | 
				
			||||||
            //   this.reset();
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        function send(msg: any) {
 | 
					        function send(msg: any) {
 | 
				
			||||||
@@ -175,11 +168,18 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        function sendCmd(key: any) {
 | 
					        function sendCmd(key: any) {
 | 
				
			||||||
            send({
 | 
					            send({
 | 
				
			||||||
                type: 'cmd',
 | 
					                type: data,
 | 
				
			||||||
                msg: key,
 | 
					                msg: key,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        function close() {
 | 
				
			||||||
 | 
					            if (state.socket) {
 | 
				
			||||||
 | 
					                state.socket.close();
 | 
				
			||||||
 | 
					                console.log('socket关闭');
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        function closeAll() {
 | 
					        function closeAll() {
 | 
				
			||||||
            close();
 | 
					            close();
 | 
				
			||||||
            if (state.term) {
 | 
					            if (state.term) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ import Api from '@/common/Api';
 | 
				
			|||||||
export const machineApi = {
 | 
					export const machineApi = {
 | 
				
			||||||
    // 获取权限列表
 | 
					    // 获取权限列表
 | 
				
			||||||
    list: Api.create("/machines", 'get'),
 | 
					    list: Api.create("/machines", 'get'),
 | 
				
			||||||
 | 
					    getMachinePwd: Api.create("/machines/{id}/pwd", 'get'),
 | 
				
			||||||
    info: Api.create("/machines/{id}/sysinfo", 'get'),
 | 
					    info: Api.create("/machines/{id}/sysinfo", 'get'),
 | 
				
			||||||
    stats: Api.create("/machines/{id}/stats", 'get'),
 | 
					    stats: Api.create("/machines/{id}/stats", 'get'),
 | 
				
			||||||
    process: Api.create("/machines/{id}/process", 'get'),
 | 
					    process: Api.create("/machines/{id}/process", 'get'),
 | 
				
			||||||
@@ -25,6 +26,7 @@ export const machineApi = {
 | 
				
			|||||||
    rmFile: Api.create("/machines/{machineId}/files/{fileId}/remove", 'delete'),
 | 
					    rmFile: Api.create("/machines/{machineId}/files/{fileId}/remove", 'delete'),
 | 
				
			||||||
    uploadFile: Api.create("/machines/{machineId}/files/{fileId}/upload?token={token}", 'post'),
 | 
					    uploadFile: Api.create("/machines/{machineId}/files/{fileId}/upload?token={token}", 'post'),
 | 
				
			||||||
    fileContent: Api.create("/machines/{machineId}/files/{fileId}/read", 'get'),
 | 
					    fileContent: Api.create("/machines/{machineId}/files/{fileId}/read", 'get'),
 | 
				
			||||||
 | 
					    createFile: Api.create("/machines/{machineId}/files/{id}/create-file", 'post'),
 | 
				
			||||||
    // 修改文件内容
 | 
					    // 修改文件内容
 | 
				
			||||||
    updateFileContent: Api.create("/machines/{machineId}/files/{id}/write", 'post'),
 | 
					    updateFileContent: Api.create("/machines/{machineId}/files/{id}/write", 'post'),
 | 
				
			||||||
    // 添加文件or目录
 | 
					    // 添加文件or目录
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,7 @@
 | 
				
			|||||||
                            </el-form-item>
 | 
					                            </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            <el-form-item label="库" label-width="20px">
 | 
					                            <el-form-item label="库" label-width="20px">
 | 
				
			||||||
                                <el-select v-model="database" placeholder="请选择库" @change="changeDatabase">
 | 
					                                <el-select v-model="database" placeholder="请选择库" @change="changeDatabase" filterable>
 | 
				
			||||||
                                    <el-option v-for="item in databases" :key="item.Name" :label="item.Name" :value="item.Name">
 | 
					                                    <el-option v-for="item in databases" :key="item.Name" :label="item.Name" :value="item.Name">
 | 
				
			||||||
                                        <span style="float: left">{{ item.Name }}</span>
 | 
					                                        <span style="float: left">{{ item.Name }}</span>
 | 
				
			||||||
                                        <span style="float: right; color: #8492a6; margin-left: 4px; font-size: 13px">{{
 | 
					                                        <span style="float: right; color: #8492a6; margin-left: 4px; font-size: 13px">{{
 | 
				
			||||||
@@ -26,12 +26,8 @@
 | 
				
			|||||||
                            </el-form-item>
 | 
					                            </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            <el-form-item label="集合" label-width="40px">
 | 
					                            <el-form-item label="集合" label-width="40px">
 | 
				
			||||||
                                <el-select v-model="collection" placeholder="请选择集合" @change="changeCollection">
 | 
					                                <el-select v-model="collection" placeholder="请选择集合" @change="changeCollection" filterable>
 | 
				
			||||||
                                    <el-option v-for="item in collections" :key="item" :label="item" :value="item">
 | 
					                                    <el-option v-for="item in collections" :key="item" :label="item" :value="item">
 | 
				
			||||||
                                        <!-- <span style="float: left">{{ item.uri }}</span>
 | 
					 | 
				
			||||||
                                            <span style="float: right; color: #8492a6; margin-left: 6px; font-size: 13px">{{
 | 
					 | 
				
			||||||
                                                ` [${item.name}]`
 | 
					 | 
				
			||||||
                                            }}</span> -->
 | 
					 | 
				
			||||||
                                    </el-option>
 | 
					                                    </el-option>
 | 
				
			||||||
                                </el-select>
 | 
					                                </el-select>
 | 
				
			||||||
                            </el-form-item>
 | 
					                            </el-form-item>
 | 
				
			||||||
@@ -124,7 +120,7 @@
 | 
				
			|||||||
            </template>
 | 
					            </template>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <el-dialog width="800px" title="json编辑器" v-model="jsoneditorDialog.visible" @close="onCloseJsonEditDialog" :close-on-click-modal="false">
 | 
					        <el-dialog width="70%" title="json编辑器" v-model="jsoneditorDialog.visible" @close="onCloseJsonEditDialog" :close-on-click-modal="false">
 | 
				
			||||||
            <json-edit v-model="jsoneditorDialog.doc" />
 | 
					            <json-edit v-model="jsoneditorDialog.doc" />
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" width="35%" :destroy-on-close="true">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" width="38%" :destroy-on-close="true">
 | 
				
			||||||
            <el-form :model="form" ref="mongoForm" :rules="rules" label-width="65px">
 | 
					            <el-form :model="form" ref="mongoForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目" required>
 | 
					                <el-form-item prop="projectId" label="项目" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
                        <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
					                        <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
				
			||||||
@@ -25,6 +25,24 @@
 | 
				
			|||||||
                        auto-complete="off"
 | 
					                        auto-complete="off"
 | 
				
			||||||
                    ></el-input>
 | 
					                    ></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
@@ -41,6 +59,7 @@
 | 
				
			|||||||
import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
					import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { mongoApi } from './api';
 | 
					import { mongoApi } from './api';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
 | 
					import { machineApi } from '../machine/api.ts';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
@@ -65,10 +84,13 @@ export default defineComponent({
 | 
				
			|||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
            envs: [],
 | 
					            envs: [],
 | 
				
			||||||
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
                uri: null,
 | 
					                uri: null,
 | 
				
			||||||
 | 
					                enableSshTunnel: -1,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
                project: null,
 | 
					                project: null,
 | 
				
			||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                envId: null,
 | 
					                envId: null,
 | 
				
			||||||
@@ -109,6 +131,9 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        watch(props, async (newValue) => {
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.mongo) {
 | 
					            if (newValue.mongo) {
 | 
				
			||||||
                getEnvs(newValue.mongo.projectId);
 | 
					                getEnvs(newValue.mongo.projectId);
 | 
				
			||||||
@@ -117,8 +142,16 @@ export default defineComponent({
 | 
				
			|||||||
                state.envs = [];
 | 
					                state.envs = [];
 | 
				
			||||||
                state.form = { db: 0 } as any;
 | 
					                state.form = { db: 0 } as any;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getEnvs = async (projectId: any) => {
 | 
					        const getEnvs = async (projectId: any) => {
 | 
				
			||||||
            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
					            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -144,9 +177,11 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const btnOk = async () => {
 | 
					        const btnOk = async () => {
 | 
				
			||||||
            mongoForm.value.validate((valid: boolean) => {
 | 
					            mongoForm.value.validate(async (valid: boolean) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    mongoApi.saveMongo.request(state.form).then(() => {
 | 
					                    const reqForm = { ...state.form };
 | 
				
			||||||
 | 
					                    // reqForm.uri = await RsaEncrypt(reqForm.uri);
 | 
				
			||||||
 | 
					                    mongoApi.saveMongo.request(reqForm).then(() => {
 | 
				
			||||||
                        ElMessage.success('保存成功');
 | 
					                        ElMessage.success('保存成功');
 | 
				
			||||||
                        emit('val-change', state.form);
 | 
					                        emit('val-change', state.form);
 | 
				
			||||||
                        state.btnLoading = true;
 | 
					                        state.btnLoading = true;
 | 
				
			||||||
@@ -172,6 +207,7 @@ export default defineComponent({
 | 
				
			|||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            mongoForm,
 | 
					            mongoForm,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
            changeEnv,
 | 
					            changeEnv,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -250,7 +250,6 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        onMounted(async () => {
 | 
					        onMounted(async () => {
 | 
				
			||||||
            search();
 | 
					            search();
 | 
				
			||||||
            state.projects = (await projectApi.projects.request({ pageNum: 1, pageSize: 100 })).list;
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const handlePageChange = (curPage: number) => {
 | 
					        const handlePageChange = (curPage: number) => {
 | 
				
			||||||
@@ -266,12 +265,6 @@ export default defineComponent({
 | 
				
			|||||||
            state.currentData = item;
 | 
					            state.currentData = item;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // connect() {
 | 
					 | 
				
			||||||
        //   Req.post('/open/redis/connect', this.form, res => {
 | 
					 | 
				
			||||||
        //     this.redisInfo = res
 | 
					 | 
				
			||||||
        //   })
 | 
					 | 
				
			||||||
        // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const showDatabases = async (id: number) => {
 | 
					        const showDatabases = async (id: number) => {
 | 
				
			||||||
            state.databaseDialog.data = (await mongoApi.databases.request({ id })).Databases;
 | 
					            state.databaseDialog.data = (await mongoApi.databases.request({ id })).Databases;
 | 
				
			||||||
            state.databaseDialog.title = `数据库列表`;
 | 
					            state.databaseDialog.title = `数据库列表`;
 | 
				
			||||||
@@ -371,21 +364,14 @@ export default defineComponent({
 | 
				
			|||||||
            } catch (err) {}
 | 
					            } catch (err) {}
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // const info = (redis: any) => {
 | 
					 | 
				
			||||||
        //     redisApi.redisInfo.request({ id: redis.id }).then((res: any) => {
 | 
					 | 
				
			||||||
        //         state.infoDialog.info = res;
 | 
					 | 
				
			||||||
        //         state.infoDialog.title = `'${redis.host}' info`;
 | 
					 | 
				
			||||||
        //         state.infoDialog.visible = true;
 | 
					 | 
				
			||||||
        //     });
 | 
					 | 
				
			||||||
        // };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const search = async () => {
 | 
					        const search = async () => {
 | 
				
			||||||
            const res = await mongoApi.mongoList.request(state.query);
 | 
					            const res = await mongoApi.mongoList.request(state.query);
 | 
				
			||||||
            state.list = res.list;
 | 
					            state.list = res.list;
 | 
				
			||||||
            state.total = res.total;
 | 
					            state.total = res.total;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const editMongo = (isAdd = false) => {
 | 
					        const editMongo = async (isAdd = false) => {
 | 
				
			||||||
 | 
					            state.projects = await projectApi.accountProjects.request(null);
 | 
				
			||||||
            if (isAdd) {
 | 
					            if (isAdd) {
 | 
				
			||||||
                state.mongoEditDialog.data = null;
 | 
					                state.mongoEditDialog.data = null;
 | 
				
			||||||
                state.mongoEditDialog.title = '新增mongo';
 | 
					                state.mongoEditDialog.title = '新增mongo';
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,7 +25,7 @@
 | 
				
			|||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <el-table :data="projects" @current-change="choose" ref="table" style="width: 100%">
 | 
					            <el-table :data="projects" @current-change="choose" ref="table" style="width: 100%">
 | 
				
			||||||
                <el-table-column label="选择" width="50px">
 | 
					                <el-table-column label="选择" width="55px">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        <el-radio v-model="chooseId" :label="scope.row.id">
 | 
					                        <el-radio v-model="chooseId" :label="scope.row.id">
 | 
				
			||||||
                            <i></i>
 | 
					                            <i></i>
 | 
				
			||||||
@@ -152,7 +152,7 @@
 | 
				
			|||||||
                            :remote-method="getAccount"
 | 
					                            :remote-method="getAccount"
 | 
				
			||||||
                            v-model="showMemDialog.memForm.accountId"
 | 
					                            v-model="showMemDialog.memForm.accountId"
 | 
				
			||||||
                            filterable
 | 
					                            filterable
 | 
				
			||||||
                            placeholder="请选择"
 | 
					                            placeholder="请输入账号模糊搜索并选择"
 | 
				
			||||||
                        >
 | 
					                        >
 | 
				
			||||||
                            <el-option v-for="item in showMemDialog.accounts" :key="item.id" :label="item.username" :value="item.id"> </el-option>
 | 
					                            <el-option v-for="item in showMemDialog.accounts" :key="item.id" :label="item.username" :value="item.id"> </el-option>
 | 
				
			||||||
                        </el-select>
 | 
					                        </el-select>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,313 +0,0 @@
 | 
				
			|||||||
<template>
 | 
					 | 
				
			||||||
    <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :show-close="false" width="750px" :destroy-on-close="true">
 | 
					 | 
				
			||||||
        <el-form label-width="85px">
 | 
					 | 
				
			||||||
            <el-form-item prop="key" label="key:">
 | 
					 | 
				
			||||||
                <el-input :disabled="operationType == 2" v-model="key.key"></el-input>
 | 
					 | 
				
			||||||
            </el-form-item>
 | 
					 | 
				
			||||||
            <el-form-item prop="timed" label="过期时间:">
 | 
					 | 
				
			||||||
                <el-input v-model.number="key.timed" type="number"></el-input>
 | 
					 | 
				
			||||||
            </el-form-item>
 | 
					 | 
				
			||||||
            <el-form-item prop="dataType" label="数据类型:">
 | 
					 | 
				
			||||||
                <el-select :disabled="operationType == 2" style="width: 100%" v-model="key.type" placeholder="请选择数据类型">
 | 
					 | 
				
			||||||
                    <el-option key="string" label="string" value="string"> </el-option>
 | 
					 | 
				
			||||||
                    <el-option key="hash" label="hash" value="hash"> </el-option>
 | 
					 | 
				
			||||||
                    <el-option key="set" label="set" value="set"> </el-option>
 | 
					 | 
				
			||||||
                </el-select>
 | 
					 | 
				
			||||||
            </el-form-item>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <el-form-item v-if="key.type == 'string'" prop="value" label="内容:">
 | 
					 | 
				
			||||||
                <div id="string-value-text" style="width: 100%">
 | 
					 | 
				
			||||||
                    <el-input class="json-text" v-model="string.value" type="textarea" :autosize="{ minRows: 10, maxRows: 20 }"></el-input>
 | 
					 | 
				
			||||||
                    <el-select class="text-type-select" @change="onChangeTextType" v-model="string.type">
 | 
					 | 
				
			||||||
                        <el-option key="text" label="text" value="text"> </el-option>
 | 
					 | 
				
			||||||
                        <el-option key="json" label="json" value="json"> </el-option>
 | 
					 | 
				
			||||||
                    </el-select>
 | 
					 | 
				
			||||||
                </div>
 | 
					 | 
				
			||||||
            </el-form-item>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <span v-if="key.type == 'hash'">
 | 
					 | 
				
			||||||
                <el-button @click="onAddHashValue" icon="plus" size="small" plain class="mt10">添加</el-button>
 | 
					 | 
				
			||||||
                <el-table :data="hash.value" stripe style="width: 100%">
 | 
					 | 
				
			||||||
                    <el-table-column prop="key" label="key" width>
 | 
					 | 
				
			||||||
                        <template #default="scope">
 | 
					 | 
				
			||||||
                            <el-input v-model="scope.row.key" clearable size="small"></el-input>
 | 
					 | 
				
			||||||
                        </template>
 | 
					 | 
				
			||||||
                    </el-table-column>
 | 
					 | 
				
			||||||
                    <el-table-column prop="value" label="value" min-width="200">
 | 
					 | 
				
			||||||
                        <template #default="scope">
 | 
					 | 
				
			||||||
                            <el-input
 | 
					 | 
				
			||||||
                                v-model="scope.row.value"
 | 
					 | 
				
			||||||
                                clearable
 | 
					 | 
				
			||||||
                                type="textarea"
 | 
					 | 
				
			||||||
                                :autosize="{ minRows: 2, maxRows: 10 }"
 | 
					 | 
				
			||||||
                                size="small"
 | 
					 | 
				
			||||||
                            ></el-input>
 | 
					 | 
				
			||||||
                        </template>
 | 
					 | 
				
			||||||
                    </el-table-column>
 | 
					 | 
				
			||||||
                    <el-table-column label="操作" width="90">
 | 
					 | 
				
			||||||
                        <template #default="scope">
 | 
					 | 
				
			||||||
                            <el-button type="danger" @click="hash.value.splice(scope.$index, 1)" icon="delete" size="small" plain>删除</el-button>
 | 
					 | 
				
			||||||
                        </template>
 | 
					 | 
				
			||||||
                    </el-table-column>
 | 
					 | 
				
			||||||
                </el-table>
 | 
					 | 
				
			||||||
            </span>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <span v-if="key.type == 'set'">
 | 
					 | 
				
			||||||
                <el-button @click="onAddSetValue" icon="plus" size="small" plain class="mt10">添加</el-button>
 | 
					 | 
				
			||||||
                <el-table :data="set.value" stripe style="width: 100%">
 | 
					 | 
				
			||||||
                    <el-table-column prop="value" label="value" min-width="200">
 | 
					 | 
				
			||||||
                        <template #default="scope">
 | 
					 | 
				
			||||||
                            <el-input
 | 
					 | 
				
			||||||
                                v-model="scope.row.value"
 | 
					 | 
				
			||||||
                                clearable
 | 
					 | 
				
			||||||
                                type="textarea"
 | 
					 | 
				
			||||||
                                :autosize="{ minRows: 2, maxRows: 10 }"
 | 
					 | 
				
			||||||
                                size="small"
 | 
					 | 
				
			||||||
                            ></el-input>
 | 
					 | 
				
			||||||
                        </template>
 | 
					 | 
				
			||||||
                    </el-table-column>
 | 
					 | 
				
			||||||
                    <el-table-column label="操作" width="90">
 | 
					 | 
				
			||||||
                        <template #default="scope">
 | 
					 | 
				
			||||||
                            <el-button type="danger" @click="set.value.splice(scope.$index, 1)" icon="delete" size="small" plain>删除</el-button>
 | 
					 | 
				
			||||||
                        </template>
 | 
					 | 
				
			||||||
                    </el-table-column>
 | 
					 | 
				
			||||||
                </el-table>
 | 
					 | 
				
			||||||
            </span>
 | 
					 | 
				
			||||||
        </el-form>
 | 
					 | 
				
			||||||
        <template #footer>
 | 
					 | 
				
			||||||
            <div class="dialog-footer">
 | 
					 | 
				
			||||||
                <el-button @click="cancel()">取 消</el-button>
 | 
					 | 
				
			||||||
                <el-button @click="saveValue" type="primary" v-auth="'redis:data:save'">确 定</el-button>
 | 
					 | 
				
			||||||
            </div>
 | 
					 | 
				
			||||||
        </template>
 | 
					 | 
				
			||||||
    </el-dialog>
 | 
					 | 
				
			||||||
</template>
 | 
					 | 
				
			||||||
<script lang="ts">
 | 
					 | 
				
			||||||
import { defineComponent, reactive, watch, toRefs } from 'vue';
 | 
					 | 
				
			||||||
import { redisApi } from './api';
 | 
					 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					 | 
				
			||||||
import { isTrue, notEmpty } from '@/common/assert';
 | 
					 | 
				
			||||||
import { formatJsonString } from '@/common/utils/format';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export default defineComponent({
 | 
					 | 
				
			||||||
    name: 'DateEdit',
 | 
					 | 
				
			||||||
    components: {},
 | 
					 | 
				
			||||||
    props: {
 | 
					 | 
				
			||||||
        visible: {
 | 
					 | 
				
			||||||
            type: Boolean,
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        title: {
 | 
					 | 
				
			||||||
            type: String,
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        redisId: {
 | 
					 | 
				
			||||||
            type: [Number],
 | 
					 | 
				
			||||||
            require: true,
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        keyInfo: {
 | 
					 | 
				
			||||||
            type: [Object],
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        // 操作类型,1:新增,2:修改
 | 
					 | 
				
			||||||
        operationType: {
 | 
					 | 
				
			||||||
            type: [Number],
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        stringValue: {
 | 
					 | 
				
			||||||
            type: [String],
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        setValue: {
 | 
					 | 
				
			||||||
            type: [Array, Object],
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        hashValue: {
 | 
					 | 
				
			||||||
            type: [Array, Object],
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    emits: ['valChange', 'cancel', 'update:visible'],
 | 
					 | 
				
			||||||
    setup(props: any, { emit }) {
 | 
					 | 
				
			||||||
        const state = reactive({
 | 
					 | 
				
			||||||
            dialogVisible: false,
 | 
					 | 
				
			||||||
            operationType: 1,
 | 
					 | 
				
			||||||
            redisId: '',
 | 
					 | 
				
			||||||
            key: {
 | 
					 | 
				
			||||||
                key: '',
 | 
					 | 
				
			||||||
                type: 'string',
 | 
					 | 
				
			||||||
                timed: -1,
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            string: {
 | 
					 | 
				
			||||||
                type: 'text',
 | 
					 | 
				
			||||||
                value: '',
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            hash: {
 | 
					 | 
				
			||||||
                value: [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        key: '',
 | 
					 | 
				
			||||||
                        value: '',
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ],
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            set: {
 | 
					 | 
				
			||||||
                value: [{ value: '' }],
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const cancel = () => {
 | 
					 | 
				
			||||||
            emit('update:visible', false);
 | 
					 | 
				
			||||||
            emit('cancel');
 | 
					 | 
				
			||||||
            setTimeout(() => {
 | 
					 | 
				
			||||||
                state.key = {
 | 
					 | 
				
			||||||
                    key: '',
 | 
					 | 
				
			||||||
                    type: 'string',
 | 
					 | 
				
			||||||
                    timed: -1,
 | 
					 | 
				
			||||||
                };
 | 
					 | 
				
			||||||
                state.string.value = '';
 | 
					 | 
				
			||||||
                state.string.type = 'text';
 | 
					 | 
				
			||||||
                state.hash.value = [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        key: '',
 | 
					 | 
				
			||||||
                        value: '',
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ];
 | 
					 | 
				
			||||||
            }, 500);
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.visible,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                state.dialogVisible = val;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.redisId,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                state.redisId = val;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.operationType,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                state.operationType = val;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.keyInfo,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                if (val) {
 | 
					 | 
				
			||||||
                    state.key = { ...val };
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                deep: true, // 深度监听的参数
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.stringValue,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                if (val) {
 | 
					 | 
				
			||||||
                    state.string.value = val;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                deep: true, // 深度监听的参数
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.setValue,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                if (val) {
 | 
					 | 
				
			||||||
                    state.set.value = val;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                deep: true, // 深度监听的参数
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        watch(
 | 
					 | 
				
			||||||
            () => props.hashValue,
 | 
					 | 
				
			||||||
            (val) => {
 | 
					 | 
				
			||||||
                if (val) {
 | 
					 | 
				
			||||||
                    state.hash.value = val;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                deep: true, // 深度监听的参数
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const saveValue = async () => {
 | 
					 | 
				
			||||||
            notEmpty(state.key.key, 'key不能为空');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (state.key.type == 'string') {
 | 
					 | 
				
			||||||
                notEmpty(state.string.value, 'value不能为空');
 | 
					 | 
				
			||||||
                const sv = { value: formatJsonString(state.string.value, true), id: state.redisId };
 | 
					 | 
				
			||||||
                Object.assign(sv, state.key);
 | 
					 | 
				
			||||||
                await redisApi.saveStringValue.request(sv);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (state.key.type == 'hash') {
 | 
					 | 
				
			||||||
                isTrue(state.hash.value.length > 0, 'hash内容不能为空');
 | 
					 | 
				
			||||||
                const sv = { value: state.hash.value, id: state.redisId };
 | 
					 | 
				
			||||||
                Object.assign(sv, state.key);
 | 
					 | 
				
			||||||
                await redisApi.saveHashValue.request(sv);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (state.key.type == 'set') {
 | 
					 | 
				
			||||||
                isTrue(state.set.value.length > 0, 'set内容不能为空');
 | 
					 | 
				
			||||||
                const sv = { value: state.set.value.map((x) => x.value), id: state.redisId };
 | 
					 | 
				
			||||||
                Object.assign(sv, state.key);
 | 
					 | 
				
			||||||
                await redisApi.saveSetValue.request(sv);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            ElMessage.success('数据保存成功');
 | 
					 | 
				
			||||||
            cancel();
 | 
					 | 
				
			||||||
            emit('valChange');
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const onAddHashValue = () => {
 | 
					 | 
				
			||||||
            state.hash.value.push({ key: '', value: '' });
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const onAddSetValue = () => {
 | 
					 | 
				
			||||||
            state.set.value.push({ value: '' });
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // 更改文本类型
 | 
					 | 
				
			||||||
        const onChangeTextType = (val: string) => {
 | 
					 | 
				
			||||||
            if (val == 'json') {
 | 
					 | 
				
			||||||
                state.string.value = formatJsonString(state.string.value, false);
 | 
					 | 
				
			||||||
                return;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if (val == 'text') {
 | 
					 | 
				
			||||||
                state.string.value = formatJsonString(state.string.value, true);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return {
 | 
					 | 
				
			||||||
            ...toRefs(state),
 | 
					 | 
				
			||||||
            saveValue,
 | 
					 | 
				
			||||||
            cancel,
 | 
					 | 
				
			||||||
            onAddHashValue,
 | 
					 | 
				
			||||||
            onAddSetValue,
 | 
					 | 
				
			||||||
            onChangeTextType,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
</script>
 | 
					 | 
				
			||||||
<style lang="scss">
 | 
					 | 
				
			||||||
#string-value-text {
 | 
					 | 
				
			||||||
    flex-grow: 1;
 | 
					 | 
				
			||||||
    display: flex;
 | 
					 | 
				
			||||||
    position: relative;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    .text-type-select {
 | 
					 | 
				
			||||||
        position: absolute;
 | 
					 | 
				
			||||||
        z-index: 2;
 | 
					 | 
				
			||||||
        right: 10px;
 | 
					 | 
				
			||||||
        top: 10px;
 | 
					 | 
				
			||||||
        max-width: 70px;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
</style>
 | 
					 | 
				
			||||||
@@ -23,7 +23,7 @@
 | 
				
			|||||||
                        <el-form class="search-form" label-position="right" :inline="true" label-width="60px">
 | 
					                        <el-form class="search-form" label-position="right" :inline="true" label-width="60px">
 | 
				
			||||||
                            <el-form-item label="key" label-width="40px">
 | 
					                            <el-form-item label="key" label-width="40px">
 | 
				
			||||||
                                <el-input
 | 
					                                <el-input
 | 
				
			||||||
                                    placeholder="支持*模糊key"
 | 
					                                    placeholder="match 支持*模糊key"
 | 
				
			||||||
                                    style="width: 240px"
 | 
					                                    style="width: 240px"
 | 
				
			||||||
                                    v-model="scanParam.match"
 | 
					                                    v-model="scanParam.match"
 | 
				
			||||||
                                    @clear="clear()"
 | 
					                                    @clear="clear()"
 | 
				
			||||||
@@ -31,12 +31,19 @@
 | 
				
			|||||||
                                ></el-input>
 | 
					                                ></el-input>
 | 
				
			||||||
                            </el-form-item>
 | 
					                            </el-form-item>
 | 
				
			||||||
                            <el-form-item label="count" label-width="60px">
 | 
					                            <el-form-item label="count" label-width="60px">
 | 
				
			||||||
                                <el-input placeholder="count" style="width: 62px" v-model="scanParam.count"></el-input>
 | 
					                                <el-input placeholder="count" style="width: 62px" v-model.number="scanParam.count"></el-input>
 | 
				
			||||||
                            </el-form-item>
 | 
					                            </el-form-item>
 | 
				
			||||||
                            <el-form-item>
 | 
					                            <el-form-item>
 | 
				
			||||||
                                <el-button @click="searchKey()" type="success" icon="search" plain></el-button>
 | 
					                                <el-button @click="searchKey()" type="success" icon="search" plain></el-button>
 | 
				
			||||||
                                <el-button @click="scan()" icon="bottom" plain>scan</el-button>
 | 
					                                <el-button @click="scan()" icon="bottom" plain>scan</el-button>
 | 
				
			||||||
                                <el-button type="primary" icon="plus" @click="onAddData(false)" plain></el-button>
 | 
					                                <el-popover placement="right" :width="200" trigger="click">
 | 
				
			||||||
 | 
					                                    <template #reference>
 | 
				
			||||||
 | 
					                                        <el-button type="primary" icon="plus" plain></el-button>
 | 
				
			||||||
 | 
					                                    </template>
 | 
				
			||||||
 | 
					                                    <el-tag @click="onAddData('string')" :color="getTypeColor('string')" style="cursor: pointer">string</el-tag>
 | 
				
			||||||
 | 
					                                    <el-tag @click="onAddData('hash')" :color="getTypeColor('hash')" class="ml5" style="cursor: pointer">hash</el-tag>
 | 
				
			||||||
 | 
					                                    <el-tag @click="onAddData('set')" :color="getTypeColor('set')" class="ml5" style="cursor: pointer">set</el-tag>
 | 
				
			||||||
 | 
					                                </el-popover>
 | 
				
			||||||
                            </el-form-item>
 | 
					                            </el-form-item>
 | 
				
			||||||
                            <div style="float: right">
 | 
					                            <div style="float: right">
 | 
				
			||||||
                                <span>keys: {{ dbsize }}</span>
 | 
					                                <span>keys: {{ dbsize }}</span>
 | 
				
			||||||
@@ -69,17 +76,32 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        <div style="text-align: center; margin-top: 10px"></div>
 | 
					        <div style="text-align: center; margin-top: 10px"></div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <!-- <value-dialog v-model:visible="valueDialog.visible" :keyValue="valueDialog.value" /> -->
 | 
					        <hash-value
 | 
				
			||||||
 | 
					            v-model:visible="hashValueDialog.visible"
 | 
				
			||||||
 | 
					            :operationType="dataEdit.operationType"
 | 
				
			||||||
 | 
					            :title="dataEdit.title"
 | 
				
			||||||
 | 
					            :keyInfo="dataEdit.keyInfo"
 | 
				
			||||||
 | 
					            :redisId="scanParam.id"
 | 
				
			||||||
 | 
					            @cancel="onCancelDataEdit"
 | 
				
			||||||
 | 
					            @valChange="searchKey"
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <data-edit
 | 
					        <string-value
 | 
				
			||||||
            v-model:visible="dataEdit.visible"
 | 
					            v-model:visible="stringValueDialog.visible"
 | 
				
			||||||
 | 
					            :operationType="dataEdit.operationType"
 | 
				
			||||||
 | 
					            :title="dataEdit.title"
 | 
				
			||||||
 | 
					            :keyInfo="dataEdit.keyInfo"
 | 
				
			||||||
 | 
					            :redisId="scanParam.id"
 | 
				
			||||||
 | 
					            @cancel="onCancelDataEdit"
 | 
				
			||||||
 | 
					            @valChange="searchKey"
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <set-value
 | 
				
			||||||
 | 
					            v-model:visible="setValueDialog.visible"
 | 
				
			||||||
            :title="dataEdit.title"
 | 
					            :title="dataEdit.title"
 | 
				
			||||||
            :keyInfo="dataEdit.keyInfo"
 | 
					            :keyInfo="dataEdit.keyInfo"
 | 
				
			||||||
            :redisId="scanParam.id"
 | 
					            :redisId="scanParam.id"
 | 
				
			||||||
            :operationType="dataEdit.operationType"
 | 
					            :operationType="dataEdit.operationType"
 | 
				
			||||||
            :stringValue="dataEdit.stringValue"
 | 
					 | 
				
			||||||
            :setValue="dataEdit.setValue"
 | 
					 | 
				
			||||||
            :hashValue="dataEdit.hashValue"
 | 
					 | 
				
			||||||
            @valChange="searchKey"
 | 
					            @valChange="searchKey"
 | 
				
			||||||
            @cancel="onCancelDataEdit"
 | 
					            @cancel="onCancelDataEdit"
 | 
				
			||||||
        />
 | 
					        />
 | 
				
			||||||
@@ -91,34 +113,31 @@ import { redisApi } from './api';
 | 
				
			|||||||
import { toRefs, reactive, defineComponent } from 'vue';
 | 
					import { toRefs, reactive, defineComponent } from 'vue';
 | 
				
			||||||
import { ElMessage, ElMessageBox } from 'element-plus';
 | 
					import { ElMessage, ElMessageBox } from 'element-plus';
 | 
				
			||||||
import ProjectEnvSelect from '../component/ProjectEnvSelect.vue';
 | 
					import ProjectEnvSelect from '../component/ProjectEnvSelect.vue';
 | 
				
			||||||
import DataEdit from './DataEdit.vue';
 | 
					import HashValue from './HashValue.vue';
 | 
				
			||||||
import { isTrue, notNull } from '@/common/assert';
 | 
					import StringValue from './StringValue.vue';
 | 
				
			||||||
 | 
					import SetValue from './SetValue.vue';
 | 
				
			||||||
 | 
					import { isTrue, notBlank, notNull } from '@/common/assert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'DataOperation',
 | 
					    name: 'DataOperation',
 | 
				
			||||||
    components: {
 | 
					    components: {
 | 
				
			||||||
        DataEdit,
 | 
					        StringValue,
 | 
				
			||||||
 | 
					        HashValue,
 | 
				
			||||||
 | 
					        SetValue,
 | 
				
			||||||
        ProjectEnvSelect,
 | 
					        ProjectEnvSelect,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setup() {
 | 
					    setup() {
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            loading: false,
 | 
					            loading: false,
 | 
				
			||||||
            cluster: 0,
 | 
					 | 
				
			||||||
            redisList: [],
 | 
					            redisList: [],
 | 
				
			||||||
            query: {
 | 
					            query: {
 | 
				
			||||||
                envId: 0,
 | 
					                envId: 0,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            scanParam: {
 | 
					            scanParam: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                cluster: 0,
 | 
					 | 
				
			||||||
                match: null,
 | 
					                match: null,
 | 
				
			||||||
                count: 10,
 | 
					                count: 10,
 | 
				
			||||||
                cursor: 0,
 | 
					                cursor: {},
 | 
				
			||||||
                prevCursor: null,
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            valueDialog: {
 | 
					 | 
				
			||||||
                visible: false,
 | 
					 | 
				
			||||||
                value: {},
 | 
					 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            dataEdit: {
 | 
					            dataEdit: {
 | 
				
			||||||
                visible: false,
 | 
					                visible: false,
 | 
				
			||||||
@@ -129,9 +148,15 @@ export default defineComponent({
 | 
				
			|||||||
                    timed: -1,
 | 
					                    timed: -1,
 | 
				
			||||||
                    key: '',
 | 
					                    key: '',
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                stringValue: '',
 | 
					            },
 | 
				
			||||||
                hashValue: [{ key: '', value: '' }],
 | 
					            hashValueDialog: {
 | 
				
			||||||
                setValue: [{ value: '' }],
 | 
					                visible: false,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            stringValueDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            setValueDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            keys: [],
 | 
					            keys: [],
 | 
				
			||||||
            dbsize: 0,
 | 
					            dbsize: 0,
 | 
				
			||||||
@@ -151,31 +176,38 @@ export default defineComponent({
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const changeRedis = () => {
 | 
					        const changeRedis = (id: number) => {
 | 
				
			||||||
            resetScanParam();
 | 
					            resetScanParam(id);
 | 
				
			||||||
            state.keys = [];
 | 
					            state.keys = [];
 | 
				
			||||||
            state.dbsize = 0;
 | 
					            state.dbsize = 0;
 | 
				
			||||||
            searchKey();
 | 
					            searchKey();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const scan = () => {
 | 
					        const scan = async () => {
 | 
				
			||||||
            isTrue(state.scanParam.id != null, '请先选择redis');
 | 
					            isTrue(state.scanParam.id != null, '请先选择redis');
 | 
				
			||||||
            isTrue(state.scanParam.count < 20001, 'count不能超过20000');
 | 
					            notBlank(state.scanParam.count, 'count不能为空');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const match = state.scanParam.match;
 | 
				
			||||||
 | 
					            if (!match || match == '*') {
 | 
				
			||||||
 | 
					                isTrue(state.scanParam.count <= 200, 'match为空或者*时, count不能超过200');
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                isTrue(state.scanParam.count <= 20000, 'count不能超过20000');
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            state.loading = true;
 | 
					            state.loading = true;
 | 
				
			||||||
            state.scanParam.cluster = state.cluster == 0 ? 0 : 1;
 | 
					            try {
 | 
				
			||||||
 | 
					                const res = await redisApi.scan.request(state.scanParam);
 | 
				
			||||||
            redisApi.scan.request(state.scanParam).then((res) => {
 | 
					 | 
				
			||||||
                state.keys = res.keys;
 | 
					                state.keys = res.keys;
 | 
				
			||||||
                state.dbsize = res.dbSize;
 | 
					                state.dbsize = res.dbSize;
 | 
				
			||||||
                state.scanParam.cursor = res.cursor;
 | 
					                state.scanParam.cursor = res.cursor;
 | 
				
			||||||
 | 
					            } finally {
 | 
				
			||||||
                state.loading = false;
 | 
					                state.loading = false;
 | 
				
			||||||
            });
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const searchKey = () => {
 | 
					        const searchKey = async () => {
 | 
				
			||||||
            state.scanParam.cursor = 0;
 | 
					            state.scanParam.cursor = {};
 | 
				
			||||||
            scan();
 | 
					            await scan();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const clearRedis = () => {
 | 
					        const clearRedis = () => {
 | 
				
			||||||
@@ -193,96 +225,82 @@ export default defineComponent({
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const resetScanParam = () => {
 | 
					        const resetScanParam = (id: number = 0) => {
 | 
				
			||||||
            state.scanParam.match = null;
 | 
					 | 
				
			||||||
            state.scanParam.cursor = 0;
 | 
					 | 
				
			||||||
            state.scanParam.count = 10;
 | 
					            state.scanParam.count = 10;
 | 
				
			||||||
 | 
					            if (id != 0) {
 | 
				
			||||||
 | 
					                const redis: any = state.redisList.find((x: any) => x.id == id);
 | 
				
			||||||
 | 
					                // 集群模式count设小点,因为后端会从所有master节点scan一遍然后合并结果
 | 
				
			||||||
 | 
					                if (redis && redis.mode == 'cluster') {
 | 
				
			||||||
 | 
					                    state.scanParam.count = 5;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            state.scanParam.match = null;
 | 
				
			||||||
 | 
					            state.scanParam.cursor = {};
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getValue = async (row: any) => {
 | 
					        const getValue = async (row: any) => {
 | 
				
			||||||
            const type = row.type;
 | 
					            const type = row.type;
 | 
				
			||||||
            const key = row.key;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            let res: any;
 | 
					 | 
				
			||||||
            const id = state.cluster == 0 ? state.scanParam.id : state.cluster;
 | 
					 | 
				
			||||||
            const reqParam = {
 | 
					 | 
				
			||||||
                cluster: state.cluster,
 | 
					 | 
				
			||||||
                key: row.key,
 | 
					 | 
				
			||||||
                id,
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
            switch (type) {
 | 
					 | 
				
			||||||
                case 'string':
 | 
					 | 
				
			||||||
                    res = await redisApi.getStringValue.request(reqParam);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case 'hash':
 | 
					 | 
				
			||||||
                    res = await redisApi.getHashValue.request(reqParam);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case 'set':
 | 
					 | 
				
			||||||
                    res = await redisApi.getSetValue.request(reqParam);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                default:
 | 
					 | 
				
			||||||
                    res = null;
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            notNull(res, '暂不支持该类型数据查看');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (type == 'string') {
 | 
					 | 
				
			||||||
                state.dataEdit.stringValue = res;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if (type == 'set') {
 | 
					 | 
				
			||||||
                state.dataEdit.setValue = res.map((x: any) => {
 | 
					 | 
				
			||||||
                    return {
 | 
					 | 
				
			||||||
                        value: x,
 | 
					 | 
				
			||||||
                    };
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if (type == 'hash') {
 | 
					 | 
				
			||||||
                const hash = [];
 | 
					 | 
				
			||||||
                //遍历key和value
 | 
					 | 
				
			||||||
                const keys = Object.keys(res);
 | 
					 | 
				
			||||||
                for (let i = 0; i < keys.length; i++) {
 | 
					 | 
				
			||||||
                    const key = keys[i];
 | 
					 | 
				
			||||||
                    const value = res[key];
 | 
					 | 
				
			||||||
                    hash.push({
 | 
					 | 
				
			||||||
                        key,
 | 
					 | 
				
			||||||
                        value,
 | 
					 | 
				
			||||||
                    });
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                state.dataEdit.hashValue = hash;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            state.dataEdit.keyInfo.type = type;
 | 
					            state.dataEdit.keyInfo.type = type;
 | 
				
			||||||
            state.dataEdit.keyInfo.timed = row.ttl;
 | 
					            state.dataEdit.keyInfo.timed = row.ttl;
 | 
				
			||||||
            state.dataEdit.keyInfo.key = key;
 | 
					            state.dataEdit.keyInfo.key = row.key;
 | 
				
			||||||
            state.dataEdit.operationType = 2;
 | 
					            state.dataEdit.operationType = 2;
 | 
				
			||||||
            state.dataEdit.title = '修改数据';
 | 
					            state.dataEdit.title = '查看数据';
 | 
				
			||||||
            state.dataEdit.visible = true;
 | 
					
 | 
				
			||||||
 | 
					            if (type == 'hash') {
 | 
				
			||||||
 | 
					                state.hashValueDialog.visible = true;
 | 
				
			||||||
 | 
					            } else if (type == 'string') {
 | 
				
			||||||
 | 
					                state.stringValueDialog.visible = true;
 | 
				
			||||||
 | 
					            } else if (type == 'set') {
 | 
				
			||||||
 | 
					                state.setValueDialog.visible = true;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                ElMessage.warning('暂不支持该类型');
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onAddData = (type: string) => {
 | 
				
			||||||
 | 
					            notNull(state.scanParam.id, '请先选择redis');
 | 
				
			||||||
 | 
					            state.dataEdit.operationType = 1;
 | 
				
			||||||
 | 
					            state.dataEdit.title = '新增数据';
 | 
				
			||||||
 | 
					            state.dataEdit.keyInfo.type = type;
 | 
				
			||||||
 | 
					            state.dataEdit.keyInfo.timed = -1;
 | 
				
			||||||
 | 
					            if (type == 'hash') {
 | 
				
			||||||
 | 
					                state.hashValueDialog.visible = true;
 | 
				
			||||||
 | 
					            } else if (type == 'string') {
 | 
				
			||||||
 | 
					                state.stringValueDialog.visible = true;
 | 
				
			||||||
 | 
					            } else if (type == 'set') {
 | 
				
			||||||
 | 
					                state.setValueDialog.visible = true;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                ElMessage.warning('暂不支持该类型');
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onCancelDataEdit = () => {
 | 
				
			||||||
 | 
					            state.dataEdit.keyInfo = {} as any;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const del = (key: string) => {
 | 
					        const del = (key: string) => {
 | 
				
			||||||
            ElMessageBox.confirm(`此操作将删除对应的key , 是否继续?`, '提示', {
 | 
					            ElMessageBox.confirm(`确定删除[ ${key} ] 该key?`, '提示', {
 | 
				
			||||||
                confirmButtonText: '确定',
 | 
					                confirmButtonText: '确定',
 | 
				
			||||||
                cancelButtonText: '取消',
 | 
					                cancelButtonText: '取消',
 | 
				
			||||||
                type: 'warning',
 | 
					                type: 'warning',
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
                .then(() => {
 | 
					                .then(() => {
 | 
				
			||||||
                    let id = state.cluster == 0 ? state.scanParam.id : state.cluster;
 | 
					 | 
				
			||||||
                    redisApi.delKey
 | 
					                    redisApi.delKey
 | 
				
			||||||
                        .request({
 | 
					                        .request({
 | 
				
			||||||
                            cluster: state.cluster,
 | 
					 | 
				
			||||||
                            key,
 | 
					                            key,
 | 
				
			||||||
                            id,
 | 
					                            id: state.scanParam.id,
 | 
				
			||||||
                        })
 | 
					                        })
 | 
				
			||||||
                        .then(() => {
 | 
					                        .then(() => {
 | 
				
			||||||
                            ElMessage.success('删除成功!');
 | 
					                            ElMessage.success('删除成功!');
 | 
				
			||||||
                            scan();
 | 
					                            searchKey();
 | 
				
			||||||
                        });
 | 
					                        });
 | 
				
			||||||
                })
 | 
					                })
 | 
				
			||||||
                .catch(() => {});
 | 
					                .catch(() => {});
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const ttlConveter = (ttl: any) => {
 | 
					        const ttlConveter = (ttl: any) => {
 | 
				
			||||||
            if (ttl == -1) {
 | 
					            if (ttl == -1 || ttl == 0) {
 | 
				
			||||||
                return '永久';
 | 
					                return '永久';
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (!ttl) {
 | 
					            if (!ttl) {
 | 
				
			||||||
@@ -329,20 +347,6 @@ export default defineComponent({
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const onAddData = () => {
 | 
					 | 
				
			||||||
            notNull(state.scanParam.id, '请先选择redis');
 | 
					 | 
				
			||||||
            state.dataEdit.operationType = 1;
 | 
					 | 
				
			||||||
            state.dataEdit.title = '新增数据';
 | 
					 | 
				
			||||||
            state.dataEdit.visible = true;
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const onCancelDataEdit = () => {
 | 
					 | 
				
			||||||
            state.dataEdit.keyInfo = {} as any;
 | 
					 | 
				
			||||||
            state.dataEdit.stringValue = '';
 | 
					 | 
				
			||||||
            state.dataEdit.setValue = [];
 | 
					 | 
				
			||||||
            state.dataEdit.hashValue = [];
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            changeProjectEnv,
 | 
					            changeProjectEnv,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										254
									
								
								mayfly_go_web/src/views/ops/redis/HashValue.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										254
									
								
								mayfly_go_web/src/views/ops/redis/HashValue.vue
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,254 @@
 | 
				
			|||||||
 | 
					<template>
 | 
				
			||||||
 | 
					    <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" width="800px" :destroy-on-close="true">
 | 
				
			||||||
 | 
					        <el-form label-width="85px">
 | 
				
			||||||
 | 
					            <el-form-item prop="key" label="key:">
 | 
				
			||||||
 | 
					                <el-input :disabled="operationType == 2" v-model="key.key"></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item prop="timed" label="过期时间:">
 | 
				
			||||||
 | 
					                <el-input v-model.number="key.timed" type="number"></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item prop="dataType" label="数据类型:">
 | 
				
			||||||
 | 
					                <el-input v-model="key.type" disabled></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <el-row class="mt10">
 | 
				
			||||||
 | 
					                <el-form label-position="right" :inline="true">
 | 
				
			||||||
 | 
					                    <el-form-item label="field" label-width="40px" v-if="operationType == 2">
 | 
				
			||||||
 | 
					                        <el-input placeholder="支持*模糊field" style="width: 140px" v-model="scanParam.match" clearable size="small"></el-input>
 | 
				
			||||||
 | 
					                    </el-form-item>
 | 
				
			||||||
 | 
					                    <el-form-item label="count" v-if="operationType == 2">
 | 
				
			||||||
 | 
					                        <el-input placeholder="count" style="width: 62px" v-model.number="scanParam.count" size="small"></el-input>
 | 
				
			||||||
 | 
					                    </el-form-item>
 | 
				
			||||||
 | 
					                    <el-form-item>
 | 
				
			||||||
 | 
					                        <el-button v-if="operationType == 2" @click="reHscan()" type="success" icon="search" plain size="small"></el-button>
 | 
				
			||||||
 | 
					                        <el-button v-if="operationType == 2" @click="hscan()" icon="bottom" plain size="small">scan</el-button>
 | 
				
			||||||
 | 
					                        <el-button @click="onAddHashValue" icon="plus" size="small" plain>添加</el-button>
 | 
				
			||||||
 | 
					                    </el-form-item>
 | 
				
			||||||
 | 
					                    <div v-if="operationType == 2" class="mt10" style="float: right">
 | 
				
			||||||
 | 
					                        <span>fieldSize: {{ keySize }}</span>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                </el-form>
 | 
				
			||||||
 | 
					            </el-row>
 | 
				
			||||||
 | 
					            <el-table :data="hashValues" stripe style="width: 100%">
 | 
				
			||||||
 | 
					                <el-table-column prop="field" label="field" width>
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-input v-model="scope.row.field" clearable size="small"></el-input>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="value" label="value" min-width="200">
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-input v-model="scope.row.value" clearable type="textarea" :autosize="{ minRows: 2, maxRows: 10 }" size="small"></el-input>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column label="操作" width="120">
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-button v-if="operationType == 2" type="success" @click="hset(scope.row)" icon="check" size="small" plain></el-button>
 | 
				
			||||||
 | 
					                        <el-button type="danger" @click="hdel(scope.row.field, scope.$index)" icon="delete" size="small" plain></el-button>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					            </el-table>
 | 
				
			||||||
 | 
					        </el-form>
 | 
				
			||||||
 | 
					        <template #footer v-if="operationType == 1">
 | 
				
			||||||
 | 
					            <div class="dialog-footer">
 | 
				
			||||||
 | 
					                <el-button @click="cancel()">取 消</el-button>
 | 
				
			||||||
 | 
					                <el-button @click="saveValue" type="primary" v-auth="'redis:data:save'">确 定</el-button>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </template>
 | 
				
			||||||
 | 
					    </el-dialog>
 | 
				
			||||||
 | 
					</template>
 | 
				
			||||||
 | 
					<script lang="ts">
 | 
				
			||||||
 | 
					import { defineComponent, reactive, watch, toRefs } from 'vue';
 | 
				
			||||||
 | 
					import { redisApi } from './api';
 | 
				
			||||||
 | 
					import { ElMessage, ElMessageBox } from 'element-plus';
 | 
				
			||||||
 | 
					import { isTrue, notEmpty } from '@/common/assert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default defineComponent({
 | 
				
			||||||
 | 
					    name: 'HashValue',
 | 
				
			||||||
 | 
					    components: {},
 | 
				
			||||||
 | 
					    props: {
 | 
				
			||||||
 | 
					        visible: {
 | 
				
			||||||
 | 
					            type: Boolean,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        title: {
 | 
				
			||||||
 | 
					            type: String,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        // 操作类型,1:新增,2:修改
 | 
				
			||||||
 | 
					        operationType: {
 | 
				
			||||||
 | 
					            type: [Number],
 | 
				
			||||||
 | 
					            require: true,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        redisId: {
 | 
				
			||||||
 | 
					            type: [Number],
 | 
				
			||||||
 | 
					            require: true,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        keyInfo: {
 | 
				
			||||||
 | 
					            type: [Object],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        hashValue: {
 | 
				
			||||||
 | 
					            type: [Array, Object],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    emits: ['valChange', 'cancel', 'update:visible'],
 | 
				
			||||||
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
 | 
					        const state = reactive({
 | 
				
			||||||
 | 
					            dialogVisible: false,
 | 
				
			||||||
 | 
					            operationType: 1,
 | 
				
			||||||
 | 
					            redisId: 0,
 | 
				
			||||||
 | 
					            key: {
 | 
				
			||||||
 | 
					                key: '',
 | 
				
			||||||
 | 
					                type: 'hash',
 | 
				
			||||||
 | 
					                timed: -1,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            scanParam: {
 | 
				
			||||||
 | 
					                key: '',
 | 
				
			||||||
 | 
					                id: 0,
 | 
				
			||||||
 | 
					                cursor: 0,
 | 
				
			||||||
 | 
					                match: '',
 | 
				
			||||||
 | 
					                count: 10,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            keySize: 0,
 | 
				
			||||||
 | 
					            hashValues: [
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    field: '',
 | 
				
			||||||
 | 
					                    value: '',
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const cancel = () => {
 | 
				
			||||||
 | 
					            emit('update:visible', false);
 | 
				
			||||||
 | 
					            emit('cancel');
 | 
				
			||||||
 | 
					            setTimeout(() => {
 | 
				
			||||||
 | 
					                state.hashValues = [];
 | 
				
			||||||
 | 
					                state.key = {} as any;
 | 
				
			||||||
 | 
					            }, 500);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
 | 
					            const visible = newValue.visible;
 | 
				
			||||||
 | 
					            state.redisId = newValue.redisId;
 | 
				
			||||||
 | 
					            state.key = newValue.keyInfo;
 | 
				
			||||||
 | 
					            state.operationType = newValue.operationType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (visible && state.operationType == 2) {
 | 
				
			||||||
 | 
					                state.scanParam.id = props.redisId;
 | 
				
			||||||
 | 
					                state.scanParam.key = state.key.key;
 | 
				
			||||||
 | 
					                await reHscan();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            state.dialogVisible = visible;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const reHscan = async () => {
 | 
				
			||||||
 | 
					            state.scanParam.id = state.redisId;
 | 
				
			||||||
 | 
					            state.scanParam.cursor = 0;
 | 
				
			||||||
 | 
					            hscan();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const hscan = async () => {
 | 
				
			||||||
 | 
					            const match = state.scanParam.match;
 | 
				
			||||||
 | 
					            if (!match || match == '' || match == '*') {
 | 
				
			||||||
 | 
					                if (state.scanParam.count > 100) {
 | 
				
			||||||
 | 
					                    ElMessage.error('match为空或者*时, count不能超过100');
 | 
				
			||||||
 | 
					                    return;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (state.scanParam.count > 1000) {
 | 
				
			||||||
 | 
					                    ElMessage.error('count不能超过1000');
 | 
				
			||||||
 | 
					                    return;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const scanRes = await redisApi.hscan.request(state.scanParam);
 | 
				
			||||||
 | 
					            state.scanParam.cursor = scanRes.cursor;
 | 
				
			||||||
 | 
					            state.keySize = scanRes.keySize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const keys = scanRes.keys;
 | 
				
			||||||
 | 
					            const hashValue = [];
 | 
				
			||||||
 | 
					            const fieldCount = keys.length / 2;
 | 
				
			||||||
 | 
					            let nextFieldIndex = 0;
 | 
				
			||||||
 | 
					            for (let i = 0; i < fieldCount; i++) {
 | 
				
			||||||
 | 
					                hashValue.push({ field: keys[nextFieldIndex++], value: keys[nextFieldIndex++] });
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            state.hashValues = hashValue;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const hdel = async (field: any, index: any) => {
 | 
				
			||||||
 | 
					            // 如果是新增操作,则直接数组移除即可
 | 
				
			||||||
 | 
					            if (state.operationType == 1) {
 | 
				
			||||||
 | 
					                state.hashValues.splice(index, 1);
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            await ElMessageBox.confirm(`确定删除[${field}]?`, '提示', {
 | 
				
			||||||
 | 
					                confirmButtonText: '确定',
 | 
				
			||||||
 | 
					                cancelButtonText: '取消',
 | 
				
			||||||
 | 
					                type: 'warning',
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            await redisApi.hdel.request({
 | 
				
			||||||
 | 
					                id: state.redisId,
 | 
				
			||||||
 | 
					                key: state.key.key,
 | 
				
			||||||
 | 
					                field,
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            ElMessage.success('删除成功');
 | 
				
			||||||
 | 
					            reHscan();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const hset = async (row: any) => {
 | 
				
			||||||
 | 
					            await redisApi.saveHashValue.request({
 | 
				
			||||||
 | 
					                id: state.redisId,
 | 
				
			||||||
 | 
					                key: state.key.key,
 | 
				
			||||||
 | 
					                timed: state.key.timed,
 | 
				
			||||||
 | 
					                value: [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        field: row.field,
 | 
				
			||||||
 | 
					                        value: row.value,
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            ElMessage.success('保存成功');
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onAddHashValue = () => {
 | 
				
			||||||
 | 
					            state.hashValues.unshift({ field: '', value: '' });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const saveValue = async () => {
 | 
				
			||||||
 | 
					            notEmpty(state.key.key, 'key不能为空');
 | 
				
			||||||
 | 
					            isTrue(state.hashValues.length > 0, 'hash内容不能为空');
 | 
				
			||||||
 | 
					            const sv = { value: state.hashValues, id: state.redisId };
 | 
				
			||||||
 | 
					            Object.assign(sv, state.key);
 | 
				
			||||||
 | 
					            await redisApi.saveHashValue.request(sv);
 | 
				
			||||||
 | 
					            ElMessage.success('保存成功');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            cancel();
 | 
				
			||||||
 | 
					            emit('valChange');
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            ...toRefs(state),
 | 
				
			||||||
 | 
					            reHscan,
 | 
				
			||||||
 | 
					            hscan,
 | 
				
			||||||
 | 
					            cancel,
 | 
				
			||||||
 | 
					            hdel,
 | 
				
			||||||
 | 
					            hset,
 | 
				
			||||||
 | 
					            onAddHashValue,
 | 
				
			||||||
 | 
					            saveValue,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					<style lang="scss">
 | 
				
			||||||
 | 
					#string-value-text {
 | 
				
			||||||
 | 
					    flex-grow: 1;
 | 
				
			||||||
 | 
					    display: flex;
 | 
				
			||||||
 | 
					    position: relative;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    .text-type-select {
 | 
				
			||||||
 | 
					        position: absolute;
 | 
				
			||||||
 | 
					        z-index: 2;
 | 
				
			||||||
 | 
					        right: 10px;
 | 
				
			||||||
 | 
					        top: 10px;
 | 
				
			||||||
 | 
					        max-width: 70px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					</style>
 | 
				
			||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="35%">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
 | 
				
			||||||
            <el-form :model="form" ref="redisForm" :rules="rules" label-width="85px">
 | 
					            <el-form :model="form" ref="redisForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目:" required>
 | 
					                <el-form-item prop="projectId" label="项目:" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
@@ -13,27 +13,66 @@
 | 
				
			|||||||
                        <el-option v-for="item in envs" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
					                        <el-option v-for="item in envs" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
				
			||||||
                    </el-select>
 | 
					                    </el-select>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="mode" label="mode:" required>
 | 
				
			||||||
 | 
					                    <el-select style="width: 100%" v-model="form.mode" placeholder="请选择模式">
 | 
				
			||||||
 | 
					                        <el-option label="standalone" value="standalone"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="cluster" value="cluster"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="sentinel" value="sentinel"> </el-option>
 | 
				
			||||||
 | 
					                    </el-select>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="host" label="host:" required>
 | 
					                <el-form-item prop="host" label="host:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.host" placeholder="请输入host:port" auto-complete="off"></el-input>
 | 
					                    <el-input
 | 
				
			||||||
 | 
					                        v-model.trim="form.host"
 | 
				
			||||||
 | 
					                        placeholder="请输入host:port;sentinel模式为: mastername=sentinelhost:port,若集群或哨兵需设多个节点可使用','分割"
 | 
				
			||||||
 | 
					                        auto-complete="off"
 | 
				
			||||||
 | 
					                        type="textarea"
 | 
				
			||||||
 | 
					                    ></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="password" label="密码:">
 | 
					                <el-form-item prop="password" label="密码:">
 | 
				
			||||||
                    <el-input
 | 
					                    <el-input
 | 
				
			||||||
                        type="password"
 | 
					                        type="password"
 | 
				
			||||||
                        show-password
 | 
					                        show-password
 | 
				
			||||||
                        v-model.trim="form.password"
 | 
					                        v-model.trim="form.password"
 | 
				
			||||||
                        placeholder="请输入密码"
 | 
					                        placeholder="请输入密码, 修改操作可不填"
 | 
				
			||||||
                        autocomplete="new-password"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                    ></el-input>
 | 
					                        ><template v-if="form.id && form.id != 0" #suffix>
 | 
				
			||||||
 | 
					                            <el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
 | 
				
			||||||
 | 
					                                <template #reference>
 | 
				
			||||||
 | 
					                                    <el-link @click="getPwd" :underline="false" type="primary" class="mr5">原密码</el-link>
 | 
				
			||||||
 | 
					                                </template>
 | 
				
			||||||
 | 
					                            </el-popover>
 | 
				
			||||||
 | 
					                        </template></el-input
 | 
				
			||||||
 | 
					                    >
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="db" label="库号:" required>
 | 
					                <el-form-item prop="db" label="库号:" required>
 | 
				
			||||||
                    <el-input v-model.number="form.db" placeholder="请输入库号"></el-input>
 | 
					                    <el-input v-model.number="form.db" placeholder="请输入库号"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="remark" label="备注:">
 | 
				
			||||||
 | 
					                    <el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
                <div class="dialog-footer">
 | 
					                <div class="dialog-footer">
 | 
				
			||||||
                    <el-button @click="cancel()">取 消</el-button>
 | 
					                    <el-button @click="cancel()">取 消</el-button>
 | 
				
			||||||
                     <el-button type="primary" :loading="btnLoading" @click="btnOk">确 定</el-button>
 | 
					                    <el-button type="primary" :loading="btnLoading" @click="btnOk">确 定</el-button>
 | 
				
			||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
            </template>
 | 
					            </template>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
@@ -44,7 +83,9 @@
 | 
				
			|||||||
import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
					import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { redisApi } from './api';
 | 
					import { redisApi } from './api';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
 | 
					import { machineApi } from '../machine/api.ts';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
 | 
					import { RsaEncrypt } from '@/common/rsa';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'RedisEdit',
 | 
					    name: 'RedisEdit',
 | 
				
			||||||
@@ -68,16 +109,22 @@ export default defineComponent({
 | 
				
			|||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
            envs: [],
 | 
					            envs: [],
 | 
				
			||||||
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
                host: null,
 | 
					                mode: 'standalone',
 | 
				
			||||||
 | 
					                host: '',
 | 
				
			||||||
                password: null,
 | 
					                password: null,
 | 
				
			||||||
                project: null,
 | 
					                project: null,
 | 
				
			||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                envId: null,
 | 
					                envId: null,
 | 
				
			||||||
                env: null,
 | 
					                env: null,
 | 
				
			||||||
 | 
					                remark: '',
 | 
				
			||||||
 | 
					                enableSshTunnel: null,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            pwd: '',
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                projectId: [
 | 
					                projectId: [
 | 
				
			||||||
@@ -108,25 +155,47 @@ export default defineComponent({
 | 
				
			|||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
 | 
					                mode: [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        required: true,
 | 
				
			||||||
 | 
					                        message: '请输入模式',
 | 
				
			||||||
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, async (newValue) => {
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.redis) {
 | 
					            if (newValue.redis) {
 | 
				
			||||||
                getEnvs(newValue.redis.projectId);
 | 
					                getEnvs(newValue.redis.projectId);
 | 
				
			||||||
                state.form = { ...newValue.redis };
 | 
					                state.form = { ...newValue.redis };
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.envs = [];
 | 
					                state.envs = [];
 | 
				
			||||||
                state.form = { db: 0 } as any;
 | 
					                state.form = { db: 0, enableSshTunnel: -1 } as any;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getEnvs = async (projectId: any) => {
 | 
					        const getEnvs = async (projectId: any) => {
 | 
				
			||||||
            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
					            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getPwd = async () => {
 | 
				
			||||||
 | 
					            state.pwd = await redisApi.getRedisPwd.request({ id: state.form.id });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const changeProject = (projectId: number) => {
 | 
					        const changeProject = (projectId: number) => {
 | 
				
			||||||
            for (let p of state.projects as any) {
 | 
					            for (let p of state.projects as any) {
 | 
				
			||||||
                if (p.id == projectId) {
 | 
					                if (p.id == projectId) {
 | 
				
			||||||
@@ -148,9 +217,15 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const btnOk = async () => {
 | 
					        const btnOk = async () => {
 | 
				
			||||||
            redisForm.value.validate((valid: boolean) => {
 | 
					            redisForm.value.validate(async (valid: boolean) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    redisApi.saveRedis.request(state.form).then(() => {
 | 
					                    const reqForm = { ...state.form };
 | 
				
			||||||
 | 
					                    if (reqForm.mode == 'sentinel' && reqForm.host.split('=').length != 2) {
 | 
				
			||||||
 | 
					                        ElMessage.error('sentinel模式host需为: mastername=sentinelhost:sentinelport模式');
 | 
				
			||||||
 | 
					                        return;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    reqForm.password = await RsaEncrypt(reqForm.password);
 | 
				
			||||||
 | 
					                    redisApi.saveRedis.request(reqForm).then(() => {
 | 
				
			||||||
                        ElMessage.success('保存成功');
 | 
					                        ElMessage.success('保存成功');
 | 
				
			||||||
                        emit('val-change', state.form);
 | 
					                        emit('val-change', state.form);
 | 
				
			||||||
                        state.btnLoading = true;
 | 
					                        state.btnLoading = true;
 | 
				
			||||||
@@ -175,6 +250,8 @@ export default defineComponent({
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            redisForm,
 | 
					            redisForm,
 | 
				
			||||||
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
 | 
					            getPwd,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
            changeEnv,
 | 
					            changeEnv,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,16 +5,12 @@
 | 
				
			|||||||
            <el-button type="primary" icon="edit" :disabled="currentId == null" @click="editRedis(false)" plain>编辑</el-button>
 | 
					            <el-button type="primary" icon="edit" :disabled="currentId == null" @click="editRedis(false)" plain>编辑</el-button>
 | 
				
			||||||
            <el-button type="danger" icon="delete" :disabled="currentId == null" @click="deleteRedis" plain>删除</el-button>
 | 
					            <el-button type="danger" icon="delete" :disabled="currentId == null" @click="deleteRedis" plain>删除</el-button>
 | 
				
			||||||
            <div style="float: right">
 | 
					            <div style="float: right">
 | 
				
			||||||
                <!-- <el-input placeholder="host"  style="width: 140px" v-model="query.host" @clear="search" plain clearable></el-input>
 | 
					 | 
				
			||||||
                <el-select v-model="params.clusterId"  clearable placeholder="集群选择">
 | 
					 | 
				
			||||||
                    <el-option v-for="item in clusters" :key="item.id" :value="item.id" :label="item.name"></el-option>
 | 
					 | 
				
			||||||
                </el-select> -->
 | 
					 | 
				
			||||||
                <el-select v-model="query.projectId" placeholder="请选择项目" filterable clearable>
 | 
					                <el-select v-model="query.projectId" placeholder="请选择项目" filterable clearable>
 | 
				
			||||||
                    <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
					                    <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
				
			||||||
                </el-select>
 | 
					                </el-select>
 | 
				
			||||||
                <el-button class="ml5" @click="search" type="success" icon="search"></el-button>
 | 
					                <el-button class="ml5" @click="search" type="success" icon="search"></el-button>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <el-table :data="redisTable" style="width: 100%" @current-change="choose" stripe>
 | 
					            <el-table :data="redisTable" @current-change="choose" stripe>
 | 
				
			||||||
                <el-table-column label="选择" width="60px">
 | 
					                <el-table-column label="选择" width="60px">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        <el-radio v-model="currentId" :label="scope.row.id">
 | 
					                        <el-radio v-model="currentId" :label="scope.row.id">
 | 
				
			||||||
@@ -22,19 +18,29 @@
 | 
				
			|||||||
                        </el-radio>
 | 
					                        </el-radio>
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column prop="project" label="项目" width></el-table-column>
 | 
					                <el-table-column prop="project" label="项目" min-width="100"></el-table-column>
 | 
				
			||||||
                <el-table-column prop="env" label="环境" width></el-table-column>
 | 
					                <el-table-column prop="env" label="环境" min-width="100"></el-table-column>
 | 
				
			||||||
                <el-table-column prop="host" label="host:port" width></el-table-column>
 | 
					                <el-table-column prop="host" label="host:port" min-width="150" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
                <el-table-column prop="createTime" label="创建时间">
 | 
					                <el-table-column prop="mode" label="mode" min-width="100"></el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="remark" label="备注" min-width="120" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="createTime" label="创建时间" min-width="160">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
					                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column prop="creator" label="创建人"></el-table-column>
 | 
					                <el-table-column prop="creator" label="创建人" min-width="100"></el-table-column>
 | 
				
			||||||
                <el-table-column label="操作" width>
 | 
					                <el-table-column label="更多" min-width="130" fixed="right">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        <el-button type="primary" @click="info(scope.row)" icon="tickets" plain size="small">info</el-button>
 | 
					                        <el-link
 | 
				
			||||||
                        <!-- <el-button type="success" @click="manage(scope.row)" :ref="scope.row"  plain>数据管理</el-button> -->
 | 
					                            v-if="scope.row.mode == 'standalone' || scope.row.mode == 'sentinel'"
 | 
				
			||||||
 | 
					                            type="primary"
 | 
				
			||||||
 | 
					                            @click="info(scope.row)"
 | 
				
			||||||
 | 
					                            :underline="false"
 | 
				
			||||||
 | 
					                            >单机信息</el-link
 | 
				
			||||||
 | 
					                        >
 | 
				
			||||||
 | 
					                        <el-link @click="onShowClusterInfo(scope.row)" v-if="scope.row.mode == 'cluster'" type="success" :underline="false"
 | 
				
			||||||
 | 
					                            >集群信息</el-link
 | 
				
			||||||
 | 
					                        >
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
            </el-table>
 | 
					            </el-table>
 | 
				
			||||||
@@ -52,6 +58,84 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        <info v-model:visible="infoDialog.visible" :title="infoDialog.title" :info="infoDialog.info"></info>
 | 
					        <info v-model:visible="infoDialog.visible" :title="infoDialog.title" :info="infoDialog.info"></info>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog width="1000px" title="集群信息" v-model="clusterInfoDialog.visible">
 | 
				
			||||||
 | 
					            <el-input type="textarea" :autosize="{ minRows: 12, maxRows: 12 }" v-model="clusterInfoDialog.info"> </el-input>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <el-divider content-position="left">节点信息</el-divider>
 | 
				
			||||||
 | 
					            <el-table :data="clusterInfoDialog.nodes" stripe size="small" border>
 | 
				
			||||||
 | 
					                <el-table-column prop="nodeId" label="nodeId" min-width="300">
 | 
				
			||||||
 | 
					                    <template #header>
 | 
				
			||||||
 | 
					                        nodeId
 | 
				
			||||||
 | 
					                        <el-tooltip class="box-item" effect="dark" content="节点id" placement="top">
 | 
				
			||||||
 | 
					                            <el-icon><question-filled /></el-icon>
 | 
				
			||||||
 | 
					                        </el-tooltip>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="ip" label="ip" min-width="180">
 | 
				
			||||||
 | 
					                    <template #header>
 | 
				
			||||||
 | 
					                        ip
 | 
				
			||||||
 | 
					                        <el-tooltip
 | 
				
			||||||
 | 
					                            class="box-item"
 | 
				
			||||||
 | 
					                            effect="dark"
 | 
				
			||||||
 | 
					                            content="ip:port1@port2:port1指redis服务器与客户端通信的端口,port2则是集群内部节点间通信的端口"
 | 
				
			||||||
 | 
					                            placement="top"
 | 
				
			||||||
 | 
					                        >
 | 
				
			||||||
 | 
					                            <el-icon><question-filled /></el-icon>
 | 
				
			||||||
 | 
					                        </el-tooltip>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-tag
 | 
				
			||||||
 | 
					                            @click="info({ id: clusterInfoDialog.redisId, ip: scope.row.ip })"
 | 
				
			||||||
 | 
					                            effect="plain"
 | 
				
			||||||
 | 
					                            type="success"
 | 
				
			||||||
 | 
					                            size="small"
 | 
				
			||||||
 | 
					                            style="cursor: pointer"
 | 
				
			||||||
 | 
					                            >{{ scope.row.ip }}</el-tag
 | 
				
			||||||
 | 
					                        >
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="flags" label="flags" min-width="110"></el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="masterSlaveRelation" label="masterSlaveRelation" min-width="300">
 | 
				
			||||||
 | 
					                    <template #header>
 | 
				
			||||||
 | 
					                        masterSlaveRelation
 | 
				
			||||||
 | 
					                        <el-tooltip
 | 
				
			||||||
 | 
					                            class="box-item"
 | 
				
			||||||
 | 
					                            effect="dark"
 | 
				
			||||||
 | 
					                            content="如果节点是slave,并且已知master节点,则为master节点ID;否则为符号'-'"
 | 
				
			||||||
 | 
					                            placement="top"
 | 
				
			||||||
 | 
					                        >
 | 
				
			||||||
 | 
					                            <el-icon><question-filled /></el-icon>
 | 
				
			||||||
 | 
					                        </el-tooltip>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="pingSent" label="pingSent" min-width="130" show-overflow-tooltip>
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        {{ scope.row.pingSent == 0 ? 0 : new Date(parseInt(scope.row.pingSent)).toLocaleString() }}
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="pongRecv" label="pongRecv" min-width="130" show-overflow-tooltip>
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        {{ scope.row.pongRecv == 0 ? 0 : new Date(parseInt(scope.row.pongRecv)).toLocaleString() }}
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="configEpoch" label="configEpoch" min-width="130">
 | 
				
			||||||
 | 
					                    <template #header>
 | 
				
			||||||
 | 
					                        configEpoch
 | 
				
			||||||
 | 
					                        <el-tooltip
 | 
				
			||||||
 | 
					                            class="box-item"
 | 
				
			||||||
 | 
					                            effect="dark"
 | 
				
			||||||
 | 
					                            content="节点的epoch值(如果该节点是从节点,则为其主节点的epoch值)。每当节点发生失败切换时,都会创建一个新的,独特的,递增的epoch。"
 | 
				
			||||||
 | 
					                            placement="top"
 | 
				
			||||||
 | 
					                        >
 | 
				
			||||||
 | 
					                            <el-icon><question-filled /></el-icon>
 | 
				
			||||||
 | 
					                        </el-tooltip>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="linkState" label="linkState" min-width="100"></el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="slot" label="slot" min-width="100"></el-table-column>
 | 
				
			||||||
 | 
					            </el-table>
 | 
				
			||||||
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <redis-edit
 | 
					        <redis-edit
 | 
				
			||||||
            @val-change="valChange"
 | 
					            @val-change="valChange"
 | 
				
			||||||
            :projects="projects"
 | 
					            :projects="projects"
 | 
				
			||||||
@@ -92,6 +176,12 @@ export default defineComponent({
 | 
				
			|||||||
            redisInfo: {
 | 
					            redisInfo: {
 | 
				
			||||||
                url: '',
 | 
					                url: '',
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            clusterInfoDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                redisId: 0,
 | 
				
			||||||
 | 
					                info: '',
 | 
				
			||||||
 | 
					                nodes: [],
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            clusters: [
 | 
					            clusters: [
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: 0,
 | 
					                    id: 0,
 | 
				
			||||||
@@ -118,7 +208,6 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        onMounted(async () => {
 | 
					        onMounted(async () => {
 | 
				
			||||||
            search();
 | 
					            search();
 | 
				
			||||||
            state.projects = (await projectApi.projects.request({ pageNum: 1, pageSize: 100 })).list;
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const handlePageChange = (curPage: number) => {
 | 
					        const handlePageChange = (curPage: number) => {
 | 
				
			||||||
@@ -134,12 +223,6 @@ export default defineComponent({
 | 
				
			|||||||
            state.currentData = item;
 | 
					            state.currentData = item;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // connect() {
 | 
					 | 
				
			||||||
        //   Req.post('/open/redis/connect', this.form, res => {
 | 
					 | 
				
			||||||
        //     this.redisInfo = res
 | 
					 | 
				
			||||||
        //   })
 | 
					 | 
				
			||||||
        // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const deleteRedis = async () => {
 | 
					        const deleteRedis = async () => {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                await ElMessageBox.confirm(`确定删除该redis?`, '提示', {
 | 
					                await ElMessageBox.confirm(`确定删除该redis?`, '提示', {
 | 
				
			||||||
@@ -155,12 +238,23 @@ export default defineComponent({
 | 
				
			|||||||
            } catch (err) {}
 | 
					            } catch (err) {}
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const info = (redis: any) => {
 | 
					        const info = async (redis: any) => {
 | 
				
			||||||
            redisApi.redisInfo.request({ id: redis.id }).then((res: any) => {
 | 
					            var host = redis.host;
 | 
				
			||||||
                state.infoDialog.info = res;
 | 
					            if (redis.ip) {
 | 
				
			||||||
                state.infoDialog.title = `'${redis.host}' info`;
 | 
					                host = redis.ip.split('@')[0];
 | 
				
			||||||
                state.infoDialog.visible = true;
 | 
					            }
 | 
				
			||||||
            });
 | 
					            const res = await redisApi.redisInfo.request({ id: redis.id, host });
 | 
				
			||||||
 | 
					            state.infoDialog.info = res;
 | 
				
			||||||
 | 
					            state.infoDialog.title = `'${host}' info`;
 | 
				
			||||||
 | 
					            state.infoDialog.visible = true;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onShowClusterInfo = async (redis: any) => {
 | 
				
			||||||
 | 
					            const ci = await redisApi.clusterInfo.request({ id: redis.id });
 | 
				
			||||||
 | 
					            state.clusterInfoDialog.info = ci.clusterInfo;
 | 
				
			||||||
 | 
					            state.clusterInfoDialog.nodes = ci.clusterNodes;
 | 
				
			||||||
 | 
					            state.clusterInfoDialog.redisId = redis.id;
 | 
				
			||||||
 | 
					            state.clusterInfoDialog.visible = true;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const search = async () => {
 | 
					        const search = async () => {
 | 
				
			||||||
@@ -169,7 +263,8 @@ export default defineComponent({
 | 
				
			|||||||
            state.total = res.total;
 | 
					            state.total = res.total;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const editRedis = (isAdd = false) => {
 | 
					        const editRedis = async (isAdd = false) => {
 | 
				
			||||||
 | 
					            state.projects = await projectApi.accountProjects.request(null);
 | 
				
			||||||
            if (isAdd) {
 | 
					            if (isAdd) {
 | 
				
			||||||
                state.redisEditDialog.data = null;
 | 
					                state.redisEditDialog.data = null;
 | 
				
			||||||
                state.redisEditDialog.title = '新增redis';
 | 
					                state.redisEditDialog.title = '新增redis';
 | 
				
			||||||
@@ -192,6 +287,7 @@ export default defineComponent({
 | 
				
			|||||||
            handlePageChange,
 | 
					            handlePageChange,
 | 
				
			||||||
            choose,
 | 
					            choose,
 | 
				
			||||||
            info,
 | 
					            info,
 | 
				
			||||||
 | 
					            onShowClusterInfo,
 | 
				
			||||||
            deleteRedis,
 | 
					            deleteRedis,
 | 
				
			||||||
            editRedis,
 | 
					            editRedis,
 | 
				
			||||||
            valChange,
 | 
					            valChange,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										157
									
								
								mayfly_go_web/src/views/ops/redis/SetValue.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								mayfly_go_web/src/views/ops/redis/SetValue.vue
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,157 @@
 | 
				
			|||||||
 | 
					<template>
 | 
				
			||||||
 | 
					    <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" width="800px" :destroy-on-close="true">
 | 
				
			||||||
 | 
					        <el-form label-width="85px">
 | 
				
			||||||
 | 
					            <el-form-item prop="key" label="key:">
 | 
				
			||||||
 | 
					                <el-input :disabled="operationType == 2" v-model="key.key"></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item prop="timed" label="过期时间:">
 | 
				
			||||||
 | 
					                <el-input v-model.number="key.timed" type="number"></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item prop="dataType" label="数据类型:">
 | 
				
			||||||
 | 
					                <el-input v-model="key.type" disabled></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <el-button @click="onAddSetValue" icon="plus" size="small" plain class="mt10">添加</el-button>
 | 
				
			||||||
 | 
					            <el-table :data="value" stripe style="width: 100%">
 | 
				
			||||||
 | 
					                <el-table-column prop="value" label="value" min-width="200">
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-input v-model="scope.row.value" clearable type="textarea" :autosize="{ minRows: 2, maxRows: 10 }" size="small"></el-input>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column label="操作" width="90">
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-button type="danger" @click="set.value.splice(scope.$index, 1)" icon="delete" size="small" plain>删除</el-button>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					            </el-table>
 | 
				
			||||||
 | 
					        </el-form>
 | 
				
			||||||
 | 
					        <template #footer>
 | 
				
			||||||
 | 
					            <div class="dialog-footer">
 | 
				
			||||||
 | 
					                <el-button @click="cancel()">取 消</el-button>
 | 
				
			||||||
 | 
					                <el-button @click="saveValue" type="primary" v-auth="'redis:data:save'">确 定</el-button>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </template>
 | 
				
			||||||
 | 
					    </el-dialog>
 | 
				
			||||||
 | 
					</template>
 | 
				
			||||||
 | 
					<script lang="ts">
 | 
				
			||||||
 | 
					import { defineComponent, reactive, watch, toRefs } from 'vue';
 | 
				
			||||||
 | 
					import { redisApi } from './api';
 | 
				
			||||||
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
 | 
					import { isTrue, notEmpty } from '@/common/assert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default defineComponent({
 | 
				
			||||||
 | 
					    name: 'SetValue',
 | 
				
			||||||
 | 
					    components: {},
 | 
				
			||||||
 | 
					    props: {
 | 
				
			||||||
 | 
					        visible: {
 | 
				
			||||||
 | 
					            type: Boolean,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        title: {
 | 
				
			||||||
 | 
					            type: String,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        redisId: {
 | 
				
			||||||
 | 
					            type: [Number],
 | 
				
			||||||
 | 
					            require: true,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        keyInfo: {
 | 
				
			||||||
 | 
					            type: [Object],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        // 操作类型,1:新增,2:修改
 | 
				
			||||||
 | 
					        operationType: {
 | 
				
			||||||
 | 
					            type: [Number],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        setValue: {
 | 
				
			||||||
 | 
					            type: [Array, Object],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    emits: ['valChange', 'cancel', 'update:visible'],
 | 
				
			||||||
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
 | 
					        const state = reactive({
 | 
				
			||||||
 | 
					            dialogVisible: false,
 | 
				
			||||||
 | 
					            operationType: 1,
 | 
				
			||||||
 | 
					            redisId: '',
 | 
				
			||||||
 | 
					            key: {
 | 
				
			||||||
 | 
					                key: '',
 | 
				
			||||||
 | 
					                type: 'string',
 | 
				
			||||||
 | 
					                timed: -1,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            value: [{ value: '' }],
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const cancel = () => {
 | 
				
			||||||
 | 
					            emit('update:visible', false);
 | 
				
			||||||
 | 
					            emit('cancel');
 | 
				
			||||||
 | 
					            setTimeout(() => {
 | 
				
			||||||
 | 
					                state.key = {
 | 
				
			||||||
 | 
					                    key: '',
 | 
				
			||||||
 | 
					                    type: 'string',
 | 
				
			||||||
 | 
					                    timed: -1,
 | 
				
			||||||
 | 
					                };
 | 
				
			||||||
 | 
					                state.value = [];
 | 
				
			||||||
 | 
					            }, 500);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            state.key = newValue.key;
 | 
				
			||||||
 | 
					            state.redisId = newValue.redisId;
 | 
				
			||||||
 | 
					            state.key = newValue.keyInfo;
 | 
				
			||||||
 | 
					            state.operationType = newValue.operationType;
 | 
				
			||||||
 | 
					            // 如果是查看编辑操作,则获取值
 | 
				
			||||||
 | 
					            if (state.dialogVisible && state.operationType == 2) {
 | 
				
			||||||
 | 
					                getSetValue();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSetValue = async () => {
 | 
				
			||||||
 | 
					            const res = await redisApi.getSetValue.request({
 | 
				
			||||||
 | 
					                id: state.redisId,
 | 
				
			||||||
 | 
					                key: state.key.key,
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            state.value = res.map((x: any) => {
 | 
				
			||||||
 | 
					                return {
 | 
				
			||||||
 | 
					                    value: x,
 | 
				
			||||||
 | 
					                };
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const saveValue = async () => {
 | 
				
			||||||
 | 
					            notEmpty(state.key.key, 'key不能为空');
 | 
				
			||||||
 | 
					            isTrue(state.value.length > 0, 'set内容不能为空');
 | 
				
			||||||
 | 
					            const sv = { value: state.value.map((x) => x.value), id: state.redisId };
 | 
				
			||||||
 | 
					            Object.assign(sv, state.key);
 | 
				
			||||||
 | 
					            await redisApi.saveSetValue.request(sv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            ElMessage.success('数据保存成功');
 | 
				
			||||||
 | 
					            cancel();
 | 
				
			||||||
 | 
					            emit('valChange');
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onAddSetValue = () => {
 | 
				
			||||||
 | 
					            state.value.unshift({ value: '' });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            ...toRefs(state),
 | 
				
			||||||
 | 
					            saveValue,
 | 
				
			||||||
 | 
					            cancel,
 | 
				
			||||||
 | 
					            onAddSetValue,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					<style lang="scss">
 | 
				
			||||||
 | 
					#string-value-text {
 | 
				
			||||||
 | 
					    flex-grow: 1;
 | 
				
			||||||
 | 
					    display: flex;
 | 
				
			||||||
 | 
					    position: relative;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    .text-type-select {
 | 
				
			||||||
 | 
					        position: absolute;
 | 
				
			||||||
 | 
					        z-index: 2;
 | 
				
			||||||
 | 
					        right: 10px;
 | 
				
			||||||
 | 
					        top: 10px;
 | 
				
			||||||
 | 
					        max-width: 70px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					</style>
 | 
				
			||||||
							
								
								
									
										169
									
								
								mayfly_go_web/src/views/ops/redis/StringValue.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										169
									
								
								mayfly_go_web/src/views/ops/redis/StringValue.vue
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,169 @@
 | 
				
			|||||||
 | 
					<template>
 | 
				
			||||||
 | 
					    <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" width="800px" :destroy-on-close="true">
 | 
				
			||||||
 | 
					        <el-form label-width="85px">
 | 
				
			||||||
 | 
					            <el-form-item prop="key" label="key:">
 | 
				
			||||||
 | 
					                <el-input :disabled="operationType == 2" v-model="key.key"></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item prop="timed" label="过期时间:">
 | 
				
			||||||
 | 
					                <el-input v-model.number="key.timed" type="number"></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					            <el-form-item prop="dataType" label="数据类型:">
 | 
				
			||||||
 | 
					                <el-input v-model="key.type" disabled></el-input>
 | 
				
			||||||
 | 
					            </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <div id="string-value-text" style="width: 100%">
 | 
				
			||||||
 | 
					                <el-input class="json-text" v-model="string.value" type="textarea" :autosize="{ minRows: 10, maxRows: 20 }"></el-input>
 | 
				
			||||||
 | 
					                <el-select class="text-type-select" @change="onChangeTextType" v-model="string.type">
 | 
				
			||||||
 | 
					                    <el-option key="text" label="text" value="text"> </el-option>
 | 
				
			||||||
 | 
					                    <el-option key="json" label="json" value="json"> </el-option>
 | 
				
			||||||
 | 
					                </el-select>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </el-form>
 | 
				
			||||||
 | 
					        <template #footer>
 | 
				
			||||||
 | 
					            <div class="dialog-footer">
 | 
				
			||||||
 | 
					                <el-button @click="cancel()">取 消</el-button>
 | 
				
			||||||
 | 
					                <el-button @click="saveValue" type="primary" v-auth="'redis:data:save'">确 定</el-button>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </template>
 | 
				
			||||||
 | 
					    </el-dialog>
 | 
				
			||||||
 | 
					</template>
 | 
				
			||||||
 | 
					<script lang="ts">
 | 
				
			||||||
 | 
					import { defineComponent, reactive, watch, toRefs } from 'vue';
 | 
				
			||||||
 | 
					import { redisApi } from './api';
 | 
				
			||||||
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
 | 
					import { notEmpty } from '@/common/assert';
 | 
				
			||||||
 | 
					import { formatJsonString } from '@/common/utils/format';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default defineComponent({
 | 
				
			||||||
 | 
					    name: 'StringValue',
 | 
				
			||||||
 | 
					    components: {},
 | 
				
			||||||
 | 
					    props: {
 | 
				
			||||||
 | 
					        visible: {
 | 
				
			||||||
 | 
					            type: Boolean,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        title: {
 | 
				
			||||||
 | 
					            type: String,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        redisId: {
 | 
				
			||||||
 | 
					            type: [Number],
 | 
				
			||||||
 | 
					            require: true,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        keyInfo: {
 | 
				
			||||||
 | 
					            type: [Object],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        // 操作类型,1:新增,2:修改
 | 
				
			||||||
 | 
					        operationType: {
 | 
				
			||||||
 | 
					            type: [Number],
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    emits: ['valChange', 'cancel', 'update:visible'],
 | 
				
			||||||
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
 | 
					        const state = reactive({
 | 
				
			||||||
 | 
					            dialogVisible: false,
 | 
				
			||||||
 | 
					            operationType: 1,
 | 
				
			||||||
 | 
					            redisId: '',
 | 
				
			||||||
 | 
					            key: {
 | 
				
			||||||
 | 
					                key: '',
 | 
				
			||||||
 | 
					                type: 'string',
 | 
				
			||||||
 | 
					                timed: -1,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            string: {
 | 
				
			||||||
 | 
					                type: 'text',
 | 
				
			||||||
 | 
					                value: '',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const cancel = () => {
 | 
				
			||||||
 | 
					            emit('update:visible', false);
 | 
				
			||||||
 | 
					            emit('cancel');
 | 
				
			||||||
 | 
					            setTimeout(() => {
 | 
				
			||||||
 | 
					                state.key = {
 | 
				
			||||||
 | 
					                    key: '',
 | 
				
			||||||
 | 
					                    type: 'string',
 | 
				
			||||||
 | 
					                    timed: -1,
 | 
				
			||||||
 | 
					                };
 | 
				
			||||||
 | 
					                state.string.value = '';
 | 
				
			||||||
 | 
					                state.string.type = 'text';
 | 
				
			||||||
 | 
					            }, 500);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        watch(
 | 
				
			||||||
 | 
					            () => props.visible,
 | 
				
			||||||
 | 
					            (val) => {
 | 
				
			||||||
 | 
					                state.dialogVisible = val;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        watch(
 | 
				
			||||||
 | 
					            () => props.redisId,
 | 
				
			||||||
 | 
					            (val) => {
 | 
				
			||||||
 | 
					                state.redisId = val;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            state.key = newValue.key;
 | 
				
			||||||
 | 
					            state.redisId = newValue.redisId;
 | 
				
			||||||
 | 
					            state.key = newValue.keyInfo;
 | 
				
			||||||
 | 
					            state.operationType = newValue.operationType
 | 
				
			||||||
 | 
					            // 如果是查看编辑操作,则获取值
 | 
				
			||||||
 | 
					            if (state.dialogVisible && state.operationType == 2) {
 | 
				
			||||||
 | 
					                getStringValue();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getStringValue = async () => {
 | 
				
			||||||
 | 
					            state.string.value = await redisApi.getStringValue.request({
 | 
				
			||||||
 | 
					                id: state.redisId,
 | 
				
			||||||
 | 
					                key: state.key.key,
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const saveValue = async () => {
 | 
				
			||||||
 | 
					            notEmpty(state.key.key, 'key不能为空');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            notEmpty(state.string.value, 'value不能为空');
 | 
				
			||||||
 | 
					            const sv = { value: formatJsonString(state.string.value, true), id: state.redisId };
 | 
				
			||||||
 | 
					            Object.assign(sv, state.key);
 | 
				
			||||||
 | 
					            await redisApi.saveStringValue.request(sv);
 | 
				
			||||||
 | 
					            ElMessage.success('数据保存成功');
 | 
				
			||||||
 | 
					            cancel();
 | 
				
			||||||
 | 
					            emit('valChange');
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 更改文本类型
 | 
				
			||||||
 | 
					        const onChangeTextType = (val: string) => {
 | 
				
			||||||
 | 
					            if (val == 'json') {
 | 
				
			||||||
 | 
					                state.string.value = formatJsonString(state.string.value, false);
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (val == 'text') {
 | 
				
			||||||
 | 
					                state.string.value = formatJsonString(state.string.value, true);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            ...toRefs(state),
 | 
				
			||||||
 | 
					            saveValue,
 | 
				
			||||||
 | 
					            cancel,
 | 
				
			||||||
 | 
					            onChangeTextType,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					<style lang="scss">
 | 
				
			||||||
 | 
					#string-value-text {
 | 
				
			||||||
 | 
					    flex-grow: 1;
 | 
				
			||||||
 | 
					    display: flex;
 | 
				
			||||||
 | 
					    position: relative;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    .text-type-select {
 | 
				
			||||||
 | 
					        position: absolute;
 | 
				
			||||||
 | 
					        z-index: 2;
 | 
				
			||||||
 | 
					        right: 10px;
 | 
				
			||||||
 | 
					        top: 10px;
 | 
				
			||||||
 | 
					        max-width: 70px;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					</style>
 | 
				
			||||||
@@ -2,14 +2,19 @@ import Api from '@/common/Api';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export const redisApi = {
 | 
					export const redisApi = {
 | 
				
			||||||
    redisList : Api.create("/redis", 'get'),
 | 
					    redisList : Api.create("/redis", 'get'),
 | 
				
			||||||
 | 
					    getRedisPwd: Api.create("/redis/{id}/pwd", 'get'),
 | 
				
			||||||
    redisInfo: Api.create("/redis/{id}/info", 'get'),
 | 
					    redisInfo: Api.create("/redis/{id}/info", 'get'),
 | 
				
			||||||
 | 
					    clusterInfo: Api.create("/redis/{id}/cluster-info", 'get'),
 | 
				
			||||||
    saveRedis: Api.create("/redis", 'post'),
 | 
					    saveRedis: Api.create("/redis", 'post'),
 | 
				
			||||||
    delRedis: Api.create("/redis/{id}", 'delete'),
 | 
					    delRedis: Api.create("/redis/{id}", 'delete'),
 | 
				
			||||||
    // 获取权限列表
 | 
					    // 获取权限列表
 | 
				
			||||||
    scan: Api.create("/redis/{id}/scan/{cursor}/{count}", 'get'),
 | 
					    scan: Api.create("/redis/{id}/scan", 'post'),
 | 
				
			||||||
    getStringValue: Api.create("/redis/{id}/string-value", 'get'),
 | 
					    getStringValue: Api.create("/redis/{id}/string-value", 'get'),
 | 
				
			||||||
    saveStringValue: Api.create("/redis/{id}/string-value", 'post'),
 | 
					    saveStringValue: Api.create("/redis/{id}/string-value", 'post'),
 | 
				
			||||||
    getHashValue: Api.create("/redis/{id}/hash-value", 'get'),
 | 
					    getHashValue: Api.create("/redis/{id}/hash-value", 'get'),
 | 
				
			||||||
 | 
					    hscan: Api.create("/redis/{id}/hscan", 'get'),
 | 
				
			||||||
 | 
					    hget: Api.create("/redis/{id}/hget", 'get'),
 | 
				
			||||||
 | 
					    hdel: Api.create("/redis/{id}/hdel", 'delete'),
 | 
				
			||||||
    saveHashValue: Api.create("/redis/{id}/hash-value", 'post'),
 | 
					    saveHashValue: Api.create("/redis/{id}/hash-value", 'post'),
 | 
				
			||||||
    getSetValue: Api.create("/redis/{id}/set-value", 'get'),
 | 
					    getSetValue: Api.create("/redis/{id}/set-value", 'get'),
 | 
				
			||||||
    saveSetValue: Api.create("/redis/{id}/set-value", 'post'),
 | 
					    saveSetValue: Api.create("/redis/{id}/set-value", 'post'),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@
 | 
				
			|||||||
        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :show-close="false" width="35%" :destroy-on-close="true">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :show-close="false" width="35%" :destroy-on-close="true">
 | 
				
			||||||
            <el-form :model="form" ref="accountForm" :rules="rules" label-width="85px">
 | 
					            <el-form :model="form" ref="accountForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="username" label="用户名:" required>
 | 
					                <el-form-item prop="username" label="用户名:" required>
 | 
				
			||||||
                    <el-input :disabled="edit" v-model.trim="form.username" placeholder="请输入账号用户名" auto-complete="off"></el-input>
 | 
					                    <el-input :disabled="edit" v-model.trim="form.username" placeholder="请输入账号用户名,密码默认与账号名一致" auto-complete="off"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <!-- <el-form-item prop="password" label="密码:" required>
 | 
					                <!-- <el-form-item prop="password" label="密码:" required>
 | 
				
			||||||
                    <el-input type="password" v-model.trim="form.password" placeholder="请输入密码" autocomplete="new-password"></el-input>
 | 
					                    <el-input type="password" v-model.trim="form.password" placeholder="请输入密码" autocomplete="new-password"></el-input>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
    <div class="role-list">
 | 
					    <div class="role-list">
 | 
				
			||||||
        <el-card>
 | 
					        <el-card>
 | 
				
			||||||
            <el-button v-auth="'account:add'" type="primary" icon="plus" @click="editAccount(true)">添加</el-button>
 | 
					            <el-button v-auth="'account:add'" type="primary" icon="plus" @click="editAccount(true)">添加</el-button>
 | 
				
			||||||
            <el-button v-auth="'account:update'" :disabled="chooseId == null" @click="editAccount(false)" type="primary" icon="edit">编辑</el-button>
 | 
					            <!-- <el-button v-auth="'account:update'" :disabled="chooseId == null" @click="editAccount(false)" type="primary" icon="edit">编辑</el-button> -->
 | 
				
			||||||
            <el-button v-auth="'account:saveRoles'" :disabled="chooseId == null" @click="roleEdit()" type="success" icon="setting"
 | 
					            <el-button v-auth="'account:saveRoles'" :disabled="chooseId == null" @click="roleEdit()" type="success" icon="setting"
 | 
				
			||||||
                >角色分配</el-button
 | 
					                >角色分配</el-button
 | 
				
			||||||
            >
 | 
					            >
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,5 +33,5 @@ export const accountApi = {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const logApi = {
 | 
					export const logApi = {
 | 
				
			||||||
    list: Api.create("/sys/logs", "get")
 | 
					    list: Api.create("/syslogs", "get")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,15 +10,14 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            <div style="float: right">
 | 
					            <div style="float: right">
 | 
				
			||||||
                <el-input
 | 
					                <el-input
 | 
				
			||||||
                    placeholder="请输入角色名称!"
 | 
					                    placeholder="请输入角色名称"
 | 
				
			||||||
                    class="mr2"
 | 
					                    class="mr2"
 | 
				
			||||||
                    size="small"
 | 
					                    style="width: 200px"
 | 
				
			||||||
                    style="width: 300px"
 | 
					 | 
				
			||||||
                    v-model="query.name"
 | 
					                    v-model="query.name"
 | 
				
			||||||
                    @clear="search"
 | 
					                    @clear="search"
 | 
				
			||||||
                    clearable
 | 
					                    clearable
 | 
				
			||||||
                ></el-input>
 | 
					                ></el-input>
 | 
				
			||||||
                <el-button @click="search" type="success" icon="search" size="small"></el-button>
 | 
					                <el-button @click="search" type="success" icon="search"></el-button>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <el-table :data="roles" @current-change="choose" ref="table" style="width: 100%">
 | 
					            <el-table :data="roles" @current-change="choose" ref="table" style="width: 100%">
 | 
				
			||||||
                <el-table-column label="选择" width="50px">
 | 
					                <el-table-column label="选择" width="50px">
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										103
									
								
								mayfly_go_web/src/views/system/syslog/SyslogList.vue
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										103
									
								
								mayfly_go_web/src/views/system/syslog/SyslogList.vue
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,103 @@
 | 
				
			|||||||
 | 
					<template>
 | 
				
			||||||
 | 
					    <div class="role-list">
 | 
				
			||||||
 | 
					        <el-card>
 | 
				
			||||||
 | 
					            <div style="float: right">
 | 
				
			||||||
 | 
					                <el-select
 | 
				
			||||||
 | 
					                    remote
 | 
				
			||||||
 | 
					                    :remote-method="getAccount"
 | 
				
			||||||
 | 
					                    v-model="query.creatorId"
 | 
				
			||||||
 | 
					                    filterable
 | 
				
			||||||
 | 
					                    placeholder="请输入并选择账号"
 | 
				
			||||||
 | 
					                    clearable
 | 
				
			||||||
 | 
					                    class="mr5"
 | 
				
			||||||
 | 
					                >
 | 
				
			||||||
 | 
					                    <el-option v-for="item in accounts" :key="item.id" :label="item.username" :value="item.id"> </el-option>
 | 
				
			||||||
 | 
					                </el-select>
 | 
				
			||||||
 | 
					                <el-select v-model="query.type" filterable placeholder="请选择操作结果" clearable class="mr5">
 | 
				
			||||||
 | 
					                    <el-option label="成功" :value="1"> </el-option>
 | 
				
			||||||
 | 
					                    <el-option label="失败" :value="2"> </el-option>
 | 
				
			||||||
 | 
					                </el-select>
 | 
				
			||||||
 | 
					                <el-button @click="search" type="success" icon="search"></el-button>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					            <el-table :data="logs" style="width: 100%">
 | 
				
			||||||
 | 
					                <el-table-column prop="creator" label="操作人" min-width="100" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="createTime" label="操作时间" min-width="160">
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="type" label="结果" min-width="65">
 | 
				
			||||||
 | 
					                    <template #default="scope">
 | 
				
			||||||
 | 
					                        <el-tag v-if="scope.row.type == 1" type="success" size="small">成功</el-tag>
 | 
				
			||||||
 | 
					                        <el-tag v-if="scope.row.type == 2" type="danger" size="small">失败</el-tag>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
 | 
					                </el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="description" label="描述" min-width="160" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                <el-table-column prop="reqParam" label="请求信息" min-width="300" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
 | 
					                <el-table-column prop="resp" label="响应信息" min-width="200" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
 | 
					            </el-table>
 | 
				
			||||||
 | 
					            <el-row style="margin-top: 20px" type="flex" justify="end">
 | 
				
			||||||
 | 
					                <el-pagination
 | 
				
			||||||
 | 
					                    style="text-align: right"
 | 
				
			||||||
 | 
					                    @current-change="handlePageChange"
 | 
				
			||||||
 | 
					                    :total="total"
 | 
				
			||||||
 | 
					                    layout="prev, pager, next, total, jumper"
 | 
				
			||||||
 | 
					                    v-model:current-page="query.pageNum"
 | 
				
			||||||
 | 
					                    :page-size="query.pageSize"
 | 
				
			||||||
 | 
					                ></el-pagination>
 | 
				
			||||||
 | 
					            </el-row>
 | 
				
			||||||
 | 
					        </el-card>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<script lang="ts">
 | 
				
			||||||
 | 
					import { toRefs, reactive, onMounted, defineComponent } from 'vue';
 | 
				
			||||||
 | 
					import { logApi, accountApi } from '../api';
 | 
				
			||||||
 | 
					export default defineComponent({
 | 
				
			||||||
 | 
					    name: 'SyslogList',
 | 
				
			||||||
 | 
					    components: {},
 | 
				
			||||||
 | 
					    setup() {
 | 
				
			||||||
 | 
					        const state = reactive({
 | 
				
			||||||
 | 
					            query: {
 | 
				
			||||||
 | 
					                pageNum: 1,
 | 
				
			||||||
 | 
					                pageSize: 10,
 | 
				
			||||||
 | 
					                name: null,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            total: 0,
 | 
				
			||||||
 | 
					            logs: [],
 | 
				
			||||||
 | 
					            accounts: [],
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        onMounted(() => {
 | 
				
			||||||
 | 
					            search();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const search = async () => {
 | 
				
			||||||
 | 
					            let res = await logApi.list.request(state.query);
 | 
				
			||||||
 | 
					            state.logs = res.list;
 | 
				
			||||||
 | 
					            state.total = res.total;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const handlePageChange = (curPage: number) => {
 | 
				
			||||||
 | 
					            state.query.pageNum = curPage;
 | 
				
			||||||
 | 
					            search();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getAccount = (username: any) => {
 | 
				
			||||||
 | 
					            accountApi.list.request({ username }).then((res) => {
 | 
				
			||||||
 | 
					                state.accounts = res.list;
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            ...toRefs(state),
 | 
				
			||||||
 | 
					            search,
 | 
				
			||||||
 | 
					            handlePageChange,
 | 
				
			||||||
 | 
					            getAccount,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					<style lang="scss">
 | 
				
			||||||
 | 
					</style>
 | 
				
			||||||
@@ -30,7 +30,6 @@ const viteConfig: UserConfig = {
 | 
				
			|||||||
				target: 'http://localhost:8888',
 | 
									target: 'http://localhost:8888',
 | 
				
			||||||
				ws: true,
 | 
									ws: true,
 | 
				
			||||||
				changeOrigin: true,
 | 
									changeOrigin: true,
 | 
				
			||||||
				rewrite: (path) => path.replace(/^\/api/, '/'),
 | 
					 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 
 | 
				
			|||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,7 +1,3 @@
 | 
				
			|||||||
app:
 | 
					 | 
				
			||||||
  name: mayfly-go
 | 
					 | 
				
			||||||
  version: 1.0.0
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
server:
 | 
					server:
 | 
				
			||||||
  # debug release test
 | 
					  # debug release test
 | 
				
			||||||
  model: release
 | 
					  model: release
 | 
				
			||||||
@@ -11,28 +7,14 @@ server:
 | 
				
			|||||||
    enable: false
 | 
					    enable: false
 | 
				
			||||||
    key-file: ./default.key
 | 
					    key-file: ./default.key
 | 
				
			||||||
    cert-file: ./default.pem
 | 
					    cert-file: ./default.pem
 | 
				
			||||||
  # 静态资源
 | 
					 | 
				
			||||||
  static:
 | 
					 | 
				
			||||||
    - relative-path: /assets
 | 
					 | 
				
			||||||
      root: ./static/assets
 | 
					 | 
				
			||||||
  # 静态文件
 | 
					 | 
				
			||||||
  static-file:
 | 
					 | 
				
			||||||
    - relative-path: /
 | 
					 | 
				
			||||||
      filepath: ./static/index.html
 | 
					 | 
				
			||||||
    - relative-path: /favicon.ico
 | 
					 | 
				
			||||||
      filepath: ./static/favicon.ico
 | 
					 | 
				
			||||||
    - relative-path: /config.js
 | 
					 | 
				
			||||||
      filepath: ./static/config.js
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
jwt:
 | 
					jwt:
 | 
				
			||||||
  key: mykey
 | 
					  # jwt key,不设置默认使用随机字符串
 | 
				
			||||||
 | 
					  key: 
 | 
				
			||||||
  # 过期时间单位分钟
 | 
					  # 过期时间单位分钟
 | 
				
			||||||
  expire-time: 1440
 | 
					  expire-time: 1440
 | 
				
			||||||
 | 
					# 资源密码aes加密key
 | 
				
			||||||
redis:
 | 
					aes:
 | 
				
			||||||
  host: 127.0.0.1
 | 
					  key: 1111111111111111
 | 
				
			||||||
  port: 6379
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
mysql:
 | 
					mysql:
 | 
				
			||||||
  host: localhost:3306
 | 
					  host: localhost:3306
 | 
				
			||||||
  username: root
 | 
					  username: root
 | 
				
			||||||
@@ -40,7 +22,6 @@ mysql:
 | 
				
			|||||||
  db-name: mayfly-go
 | 
					  db-name: mayfly-go
 | 
				
			||||||
  config: charset=utf8&loc=Local&parseTime=true
 | 
					  config: charset=utf8&loc=Local&parseTime=true
 | 
				
			||||||
  max-idle-conns: 5
 | 
					  max-idle-conns: 5
 | 
				
			||||||
 | 
					 | 
				
			||||||
log:
 | 
					log:
 | 
				
			||||||
   # 日志等级, trace, debug, info, warn, error, fatal
 | 
					   # 日志等级, trace, debug, info, warn, error, fatal
 | 
				
			||||||
  level: info
 | 
					  level: info
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,37 +1,40 @@
 | 
				
			|||||||
module mayfly-go
 | 
					module mayfly-go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.17
 | 
					go 1.18
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/dgrijalva/jwt-go v3.2.0+incompatible // jwt
 | 
						github.com/gin-gonic/gin v1.8.1
 | 
				
			||||||
	github.com/gin-gonic/gin v1.7.7
 | 
						github.com/go-redis/redis/v8 v8.11.5
 | 
				
			||||||
	github.com/go-redis/redis v6.15.9+incompatible
 | 
						github.com/go-sql-driver/mysql v1.6.0
 | 
				
			||||||
 | 
						github.com/golang-jwt/jwt/v4 v4.4.2
 | 
				
			||||||
	github.com/gorilla/websocket v1.5.0
 | 
						github.com/gorilla/websocket v1.5.0
 | 
				
			||||||
 | 
						github.com/lib/pq v1.10.6
 | 
				
			||||||
	github.com/mojocn/base64Captcha v1.3.5 // 验证码
 | 
						github.com/mojocn/base64Captcha v1.3.5 // 验证码
 | 
				
			||||||
	github.com/pkg/sftp v1.13.4
 | 
						github.com/pkg/sftp v1.13.5
 | 
				
			||||||
	github.com/robfig/cron/v3 v3.0.1 // 定时任务
 | 
						github.com/robfig/cron/v3 v3.0.1 // 定时任务
 | 
				
			||||||
	github.com/sirupsen/logrus v1.8.1
 | 
						github.com/sirupsen/logrus v1.9.0
 | 
				
			||||||
	github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
 | 
						github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
 | 
				
			||||||
	go.mongodb.org/mongo-driver v1.9.1 // mongo
 | 
						go.mongodb.org/mongo-driver v1.9.1 // mongo
 | 
				
			||||||
	golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // ssh
 | 
						golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // ssh
 | 
				
			||||||
	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 | 
						gopkg.in/yaml.v3 v3.0.1
 | 
				
			||||||
	// gorm
 | 
						// gorm
 | 
				
			||||||
	gorm.io/driver/mysql v1.3.4
 | 
						gorm.io/driver/mysql v1.3.5
 | 
				
			||||||
	gorm.io/gorm v1.23.5
 | 
						gorm.io/gorm v1.23.8
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
 | 
						github.com/cespare/xxhash/v2 v2.1.2 // indirect
 | 
				
			||||||
 | 
						github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 | 
				
			||||||
	github.com/gin-contrib/sse v0.1.0 // indirect
 | 
						github.com/gin-contrib/sse v0.1.0 // indirect
 | 
				
			||||||
	github.com/go-playground/locales v0.14.0 // indirect
 | 
						github.com/go-playground/locales v0.14.0 // indirect
 | 
				
			||||||
	github.com/go-playground/universal-translator v0.18.0 // indirect
 | 
						github.com/go-playground/universal-translator v0.18.0 // indirect
 | 
				
			||||||
	github.com/go-playground/validator/v10 v10.10.1 // indirect
 | 
						github.com/go-playground/validator/v10 v10.10.1 // indirect
 | 
				
			||||||
	github.com/go-sql-driver/mysql v1.6.0 // indirect
 | 
					 | 
				
			||||||
	github.com/go-stack/stack v1.8.0 // indirect
 | 
						github.com/go-stack/stack v1.8.0 // indirect
 | 
				
			||||||
 | 
						github.com/goccy/go-json v0.9.7 // indirect
 | 
				
			||||||
	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
 | 
						github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
 | 
				
			||||||
	github.com/golang/protobuf v1.5.2 // indirect
 | 
					 | 
				
			||||||
	github.com/golang/snappy v0.0.1 // indirect
 | 
						github.com/golang/snappy v0.0.1 // indirect
 | 
				
			||||||
	github.com/jinzhu/inflection v1.0.0 // indirect
 | 
						github.com/jinzhu/inflection v1.0.0 // indirect
 | 
				
			||||||
	github.com/jinzhu/now v1.1.4 // indirect
 | 
						github.com/jinzhu/now v1.1.5 // indirect
 | 
				
			||||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
						github.com/json-iterator/go v1.1.12 // indirect
 | 
				
			||||||
	github.com/klauspost/compress v1.13.6 // indirect
 | 
						github.com/klauspost/compress v1.13.6 // indirect
 | 
				
			||||||
	github.com/kr/fs v0.1.0 // indirect
 | 
						github.com/kr/fs v0.1.0 // indirect
 | 
				
			||||||
@@ -39,8 +42,7 @@ require (
 | 
				
			|||||||
	github.com/mattn/go-isatty v0.0.14 // indirect
 | 
						github.com/mattn/go-isatty v0.0.14 // indirect
 | 
				
			||||||
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
						github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
				
			||||||
	github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
						github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
				
			||||||
	github.com/onsi/ginkgo v1.16.5 // indirect
 | 
						github.com/pelletier/go-toml/v2 v2.0.1 // indirect
 | 
				
			||||||
	github.com/onsi/gomega v1.18.1 // indirect
 | 
					 | 
				
			||||||
	github.com/pkg/errors v0.9.1 // indirect
 | 
						github.com/pkg/errors v0.9.1 // indirect
 | 
				
			||||||
	github.com/ugorji/go/codec v1.2.7 // indirect
 | 
						github.com/ugorji/go/codec v1.2.7 // indirect
 | 
				
			||||||
	github.com/xdg-go/pbkdf2 v1.0.0 // indirect
 | 
						github.com/xdg-go/pbkdf2 v1.0.0 // indirect
 | 
				
			||||||
@@ -48,9 +50,11 @@ require (
 | 
				
			|||||||
	github.com/xdg-go/stringprep v1.0.2 // indirect
 | 
						github.com/xdg-go/stringprep v1.0.2 // indirect
 | 
				
			||||||
	github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
 | 
						github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
 | 
				
			||||||
	golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
 | 
						golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
 | 
				
			||||||
 | 
						golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
 | 
				
			||||||
	golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
 | 
						golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
 | 
						golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
 | 
				
			||||||
	golang.org/x/text v0.3.7 // indirect
 | 
						golang.org/x/text v0.3.7 // indirect
 | 
				
			||||||
	google.golang.org/protobuf v1.27.1 // indirect
 | 
						golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
 | 
				
			||||||
 | 
						google.golang.org/protobuf v1.28.0 // indirect
 | 
				
			||||||
	gopkg.in/yaml.v2 v2.4.0 // indirect
 | 
						gopkg.in/yaml.v2 v2.4.0 // indirect
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,16 +2,25 @@ package initialize
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	common_index_router "mayfly-go/internal/common/router"
 | 
						"io/fs"
 | 
				
			||||||
 | 
						common_router "mayfly-go/internal/common/router"
 | 
				
			||||||
	devops_router "mayfly-go/internal/devops/router"
 | 
						devops_router "mayfly-go/internal/devops/router"
 | 
				
			||||||
	sys_router "mayfly-go/internal/sys/router"
 | 
						sys_router "mayfly-go/internal/sys/router"
 | 
				
			||||||
	"mayfly-go/pkg/config"
 | 
						"mayfly-go/pkg/config"
 | 
				
			||||||
	"mayfly-go/pkg/middleware"
 | 
						"mayfly-go/pkg/middleware"
 | 
				
			||||||
 | 
						"mayfly-go/static"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func WrapStaticHandler(h http.Handler) gin.HandlerFunc {
 | 
				
			||||||
 | 
						return func(c *gin.Context) {
 | 
				
			||||||
 | 
							c.Writer.Header().Set("Cache-Control", `public, max-age=31536000`)
 | 
				
			||||||
 | 
							h.ServeHTTP(c.Writer, c.Request)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func InitRouter() *gin.Engine {
 | 
					func InitRouter() *gin.Engine {
 | 
				
			||||||
	// server配置
 | 
						// server配置
 | 
				
			||||||
	serverConfig := config.Conf.Server
 | 
						serverConfig := config.Conf.Server
 | 
				
			||||||
@@ -25,12 +34,21 @@ func InitRouter() *gin.Engine {
 | 
				
			|||||||
		g.JSON(http.StatusNotFound, gin.H{"code": 404, "msg": fmt.Sprintf("not found '%s:%s'", g.Request.Method, g.Request.URL.Path)})
 | 
							g.JSON(http.StatusNotFound, gin.H{"code": 404, "msg": fmt.Sprintf("not found '%s:%s'", g.Request.Method, g.Request.URL.Path)})
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 使用embed打包静态资源至二进制文件中
 | 
				
			||||||
 | 
						fsys, _ := fs.Sub(static.Static, "static")
 | 
				
			||||||
 | 
						fileServer := http.FileServer(http.FS(fsys))
 | 
				
			||||||
 | 
						handler := WrapStaticHandler(fileServer)
 | 
				
			||||||
 | 
						router.GET("/", handler)
 | 
				
			||||||
 | 
						router.GET("/favicon.ico", handler)
 | 
				
			||||||
 | 
						router.GET("/config.js", handler)
 | 
				
			||||||
 | 
						// 所有/assets/**开头的都是静态资源文件
 | 
				
			||||||
 | 
						router.GET("/assets/*file", handler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 设置静态资源
 | 
						// 设置静态资源
 | 
				
			||||||
	if staticConfs := serverConfig.Static; staticConfs != nil {
 | 
						if staticConfs := serverConfig.Static; staticConfs != nil {
 | 
				
			||||||
		for _, scs := range *staticConfs {
 | 
							for _, scs := range *staticConfs {
 | 
				
			||||||
			router.Static(scs.RelativePath, scs.Root)
 | 
								router.Static(scs.RelativePath, scs.Root)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// 设置静态文件
 | 
						// 设置静态文件
 | 
				
			||||||
	if staticFileConfs := serverConfig.StaticFile; staticFileConfs != nil {
 | 
						if staticFileConfs := serverConfig.StaticFile; staticFileConfs != nil {
 | 
				
			||||||
@@ -38,6 +56,7 @@ func InitRouter() *gin.Engine {
 | 
				
			|||||||
			router.StaticFile(sfs.RelativePath, sfs.Filepath)
 | 
								router.StaticFile(sfs.RelativePath, sfs.Filepath)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 是否允许跨域
 | 
						// 是否允许跨域
 | 
				
			||||||
	if serverConfig.Cors {
 | 
						if serverConfig.Cors {
 | 
				
			||||||
		router.Use(middleware.Cors())
 | 
							router.Use(middleware.Cors())
 | 
				
			||||||
@@ -46,13 +65,15 @@ func InitRouter() *gin.Engine {
 | 
				
			|||||||
	// 设置路由组
 | 
						// 设置路由组
 | 
				
			||||||
	api := router.Group("/api")
 | 
						api := router.Group("/api")
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		common_index_router.InitIndexRouter(api)
 | 
							common_router.InitIndexRouter(api)
 | 
				
			||||||
 | 
							common_router.InitCommonRouter(api)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		sys_router.InitCaptchaRouter(api)
 | 
							sys_router.InitCaptchaRouter(api)
 | 
				
			||||||
		sys_router.InitAccountRouter(api) // 注册account路由
 | 
							sys_router.InitAccountRouter(api) // 注册account路由
 | 
				
			||||||
		sys_router.InitResourceRouter(api)
 | 
							sys_router.InitResourceRouter(api)
 | 
				
			||||||
		sys_router.InitRoleRouter(api)
 | 
							sys_router.InitRoleRouter(api)
 | 
				
			||||||
		sys_router.InitSystemRouter(api)
 | 
							sys_router.InitSystemRouter(api)
 | 
				
			||||||
 | 
							sys_router.InitSyslogRouter(api)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		devops_router.InitProjectRouter(api)
 | 
							devops_router.InitProjectRouter(api)
 | 
				
			||||||
		devops_router.InitDbRouter(api)
 | 
							devops_router.InitDbRouter(api)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								server/initialize/savelog.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								server/initialize/savelog.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					package initialize
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						sys_application "mayfly-go/internal/sys/application"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/ctx"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func InitSaveLogFunc() ctx.SaveLogFunc {
 | 
				
			||||||
 | 
						return sys_application.SyslogApp.SaveFromReq
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								server/internal/common/api/common.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								server/internal/common/api/common.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					package api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/ctx"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Common struct {
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (i *Common) RasPublicKey(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						publicKeyStr, err := utils.GetRsaPublicKey()
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "rsa生成公私钥失败")
 | 
				
			||||||
 | 
						rc.ResData = publicKeyStr
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								server/internal/common/router/common.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								server/internal/common/router/common.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					package router
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/api"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/ctx"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func InitCommonRouter(router *gin.RouterGroup) {
 | 
				
			||||||
 | 
						common := router.Group("common")
 | 
				
			||||||
 | 
						c := &api.Common{}
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							// 获取公钥
 | 
				
			||||||
 | 
							common.GET("public-key", func(g *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(g).
 | 
				
			||||||
 | 
									WithNeedToken(false).
 | 
				
			||||||
 | 
									Handle(c.RasPublicKey)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										35
									
								
								server/internal/common/utils/pwd.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								server/internal/common/utils/pwd.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/config"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 使用config.yml的aes.key进行密码加密
 | 
				
			||||||
 | 
					func PwdAesEncrypt(password string) string {
 | 
				
			||||||
 | 
						if password == "" {
 | 
				
			||||||
 | 
							return ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						aes := config.Conf.Aes
 | 
				
			||||||
 | 
						if aes == nil {
 | 
				
			||||||
 | 
							return password
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						encryptPwd, err := aes.EncryptBase64([]byte(password))
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "密码加密失败: %s")
 | 
				
			||||||
 | 
						return encryptPwd
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 使用config.yml的aes.key进行密码解密
 | 
				
			||||||
 | 
					func PwdAesDecrypt(encryptPwd string) string {
 | 
				
			||||||
 | 
						if encryptPwd == "" {
 | 
				
			||||||
 | 
							return ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						aes := config.Conf.Aes
 | 
				
			||||||
 | 
						if aes == nil {
 | 
				
			||||||
 | 
							return encryptPwd
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						decryptPwd, err := aes.DecryptBase64(encryptPwd)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "密码解密失败: %s")
 | 
				
			||||||
 | 
						// 解密后的密码
 | 
				
			||||||
 | 
						return string(decryptPwd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								server/internal/constant/constant.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								server/internal/constant/constant.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					package constant
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						MachineConnExpireTime = 60 * time.Minute
 | 
				
			||||||
 | 
						DbConnExpireTime      = 45 * time.Minute
 | 
				
			||||||
 | 
						RedisConnExpireTime   = 30 * time.Minute
 | 
				
			||||||
 | 
						MongoConnExpireTime   = 30 * time.Minute
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/****  开发测试使用   ****/
 | 
				
			||||||
 | 
					// MachineConnExpireTime = 4 * time.Minute
 | 
				
			||||||
 | 
					// DbConnExpireTime      = 2 * time.Minute
 | 
				
			||||||
 | 
					// RedisConnExpireTime   = 2 * time.Minute
 | 
				
			||||||
 | 
					// MongoConnExpireTime   = 2 * time.Minute
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
@@ -2,7 +2,7 @@ package api
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io"
 | 
				
			||||||
	"mayfly-go/internal/devops/api/form"
 | 
						"mayfly-go/internal/devops/api/form"
 | 
				
			||||||
	"mayfly-go/internal/devops/api/vo"
 | 
						"mayfly-go/internal/devops/api/vo"
 | 
				
			||||||
	"mayfly-go/internal/devops/application"
 | 
						"mayfly-go/internal/devops/application"
 | 
				
			||||||
@@ -16,8 +16,10 @@ import (
 | 
				
			|||||||
	"mayfly-go/pkg/ws"
 | 
						"mayfly-go/pkg/ws"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
 | 
						"github.com/xwb1989/sqlparser"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Db struct {
 | 
					type Db struct {
 | 
				
			||||||
@@ -27,6 +29,8 @@ type Db struct {
 | 
				
			|||||||
	ProjectApp   application.Project
 | 
						ProjectApp   application.Project
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const DEFAULT_COLUMN_SIZE = 500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// @router /api/dbs [get]
 | 
					// @router /api/dbs [get]
 | 
				
			||||||
func (d *Db) Dbs(rc *ctx.ReqCtx) {
 | 
					func (d *Db) Dbs(rc *ctx.ReqCtx) {
 | 
				
			||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
@@ -41,12 +45,48 @@ func (d *Db) Save(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	form := &form.DbForm{}
 | 
						form := &form.DbForm{}
 | 
				
			||||||
	ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
						ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						db := new(entity.Db)
 | 
				
			||||||
 | 
						utils.Copy(db, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 密码解密,并使用解密后的赋值
 | 
				
			||||||
 | 
						originPwd, err := utils.DefaultRsaDecrypt(form.Password, true)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
						db.Password = originPwd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 密码脱敏记录日志
 | 
				
			||||||
 | 
						form.Password = "****"
 | 
				
			||||||
	rc.ReqParam = form
 | 
						rc.ReqParam = form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						db.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
 | 
						d.DbApp.Save(db)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
				
			||||||
 | 
					func (d *Db) GetDbPwd(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						dbId := GetDbId(rc.GinCtx)
 | 
				
			||||||
 | 
						dbEntity := d.DbApp.GetById(dbId, "Password")
 | 
				
			||||||
 | 
						dbEntity.PwdDecrypt()
 | 
				
			||||||
 | 
						rc.ResData = dbEntity.Password
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库实例的所有数据库名
 | 
				
			||||||
 | 
					func (d *Db) GetDatabaseNames(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						form := &form.DbForm{}
 | 
				
			||||||
 | 
						ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	db := new(entity.Db)
 | 
						db := new(entity.Db)
 | 
				
			||||||
	utils.Copy(db, form)
 | 
						utils.Copy(db, form)
 | 
				
			||||||
	db.SetBaseInfo(rc.LoginAccount)
 | 
					
 | 
				
			||||||
	d.DbApp.Save(db)
 | 
						// 密码解密,并使用解密后的赋值
 | 
				
			||||||
 | 
						originPwd, err := utils.DefaultRsaDecrypt(form.Password, true)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
						db.Password = originPwd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果id不为空,并且密码为空则从数据库查询
 | 
				
			||||||
 | 
						if form.Id != 0 && db.Password == "" {
 | 
				
			||||||
 | 
							db = d.DbApp.GetById(form.Id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						rc.ResData = d.DbApp.GetDatabases(db)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *Db) DeleteDb(rc *ctx.ReqCtx) {
 | 
					func (d *Db) DeleteDb(rc *ctx.ReqCtx) {
 | 
				
			||||||
@@ -57,19 +97,19 @@ func (d *Db) DeleteDb(rc *ctx.ReqCtx) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *Db) TableInfos(rc *ctx.ReqCtx) {
 | 
					func (d *Db) TableInfos(rc *ctx.ReqCtx) {
 | 
				
			||||||
	rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetTableInfos()
 | 
						rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableInfos()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *Db) TableIndex(rc *ctx.ReqCtx) {
 | 
					func (d *Db) TableIndex(rc *ctx.ReqCtx) {
 | 
				
			||||||
	tn := rc.GinCtx.Query("tableName")
 | 
						tn := rc.GinCtx.Query("tableName")
 | 
				
			||||||
	biz.NotEmpty(tn, "tableName不能为空")
 | 
						biz.NotEmpty(tn, "tableName不能为空")
 | 
				
			||||||
	rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetTableIndex(tn)
 | 
						rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableIndex(tn)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *Db) GetCreateTableDdl(rc *ctx.ReqCtx) {
 | 
					func (d *Db) GetCreateTableDdl(rc *ctx.ReqCtx) {
 | 
				
			||||||
	tn := rc.GinCtx.Query("tableName")
 | 
						tn := rc.GinCtx.Query("tableName")
 | 
				
			||||||
	biz.NotEmpty(tn, "tableName不能为空")
 | 
						biz.NotEmpty(tn, "tableName不能为空")
 | 
				
			||||||
	rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetCreateTableDdl(tn)
 | 
						rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetCreateTableDdl(tn)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *Db) ExecSql(rc *ctx.ReqCtx) {
 | 
					func (d *Db) ExecSql(rc *ctx.ReqCtx) {
 | 
				
			||||||
@@ -91,7 +131,7 @@ func (d *Db) ExecSql(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	rc.ReqParam = fmt.Sprintf("db: %d:%s | sql: %s", id, db, sql)
 | 
						rc.ReqParam = fmt.Sprintf("db: %d:%s | sql: %s", id, db, sql)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	biz.NotEmpty(sql, "sql不能为空")
 | 
						biz.NotEmpty(sql, "sql不能为空")
 | 
				
			||||||
	if strings.HasPrefix(sql, "SELECT") || strings.HasPrefix(sql, "select") || strings.HasPrefix(sql, "show") {
 | 
						if strings.HasPrefix(sql, "SELECT") || strings.HasPrefix(sql, "select") || strings.HasPrefix(sql, "show") || strings.HasPrefix(sql, "explain") {
 | 
				
			||||||
		colNames, res, err := dbInstance.SelectData(sql)
 | 
							colNames, res, err := dbInstance.SelectData(sql)
 | 
				
			||||||
		biz.ErrIsNilAppendErr(err, "查询失败: %s")
 | 
							biz.ErrIsNilAppendErr(err, "查询失败: %s")
 | 
				
			||||||
		colAndRes := make(map[string]interface{})
 | 
							colAndRes := make(map[string]interface{})
 | 
				
			||||||
@@ -128,14 +168,12 @@ func (d *Db) ExecSqlFile(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	fileheader, err := g.FormFile("file")
 | 
						fileheader, err := g.FormFile("file")
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "读取sql文件失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "读取sql文件失败: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 读取sql文件并根据;切割sql语句
 | 
					 | 
				
			||||||
	file, _ := fileheader.Open()
 | 
						file, _ := fileheader.Open()
 | 
				
			||||||
	filename := fileheader.Filename
 | 
						filename := fileheader.Filename
 | 
				
			||||||
	bytes, _ := ioutil.ReadAll(file)
 | 
					 | 
				
			||||||
	sqlContent := string(bytes)
 | 
					 | 
				
			||||||
	sqls := strings.Split(sqlContent, ";")
 | 
					 | 
				
			||||||
	dbId, db := GetIdAndDb(g)
 | 
						dbId, db := GetIdAndDb(g)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rc.ReqParam = fmt.Sprintf("dbId: %d, db: %s, filename: %s", dbId, db, filename)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		db := d.DbApp.GetDbInstance(dbId, db)
 | 
							db := d.DbApp.GetDbInstance(dbId, db)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -153,12 +191,14 @@ func (d *Db) ExecSqlFile(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, db.ProjectId), "%s")
 | 
							biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, db.ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for _, sql := range sqls {
 | 
							tokens := sqlparser.NewTokenizer(file)
 | 
				
			||||||
			sql = strings.Trim(sql, " ")
 | 
							for {
 | 
				
			||||||
			if sql == "" || sql == "\n" {
 | 
								stmt, err := sqlparser.ParseNext(tokens)
 | 
				
			||||||
				continue
 | 
								if err == io.EOF {
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			_, err := db.Exec(sql)
 | 
								sql := sqlparser.String(stmt)
 | 
				
			||||||
 | 
								_, err = db.Exec(sql)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrMsg("sql脚本执行失败", fmt.Sprintf("[%s]%s执行失败: [%s]", filename, dbInfo, err.Error())))
 | 
									d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrMsg("sql脚本执行失败", fmt.Sprintf("[%s]%s执行失败: [%s]", filename, dbInfo, err.Error())))
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
@@ -168,11 +208,103 @@ func (d *Db) ExecSqlFile(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	}()
 | 
						}()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 数据库dump
 | 
				
			||||||
 | 
					func (d *Db) DumpSql(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						g := rc.GinCtx
 | 
				
			||||||
 | 
						dbId, db := GetIdAndDb(g)
 | 
				
			||||||
 | 
						dumpType := g.Query("type")
 | 
				
			||||||
 | 
						tablesStr := g.Query("tables")
 | 
				
			||||||
 | 
						biz.NotEmpty(tablesStr, "请选择要导出的表")
 | 
				
			||||||
 | 
						tables := strings.Split(tablesStr, ",")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 是否需要导出表结构
 | 
				
			||||||
 | 
						needStruct := dumpType == "1" || dumpType == "3"
 | 
				
			||||||
 | 
						// 是否需要导出数据
 | 
				
			||||||
 | 
						needData := dumpType == "2" || dumpType == "3"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbInstance := d.DbApp.GetDbInstance(dbId, db)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbInstance.ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						now := time.Now()
 | 
				
			||||||
 | 
						filename := fmt.Sprintf("%s.%s.sql", db, now.Format("200601021504"))
 | 
				
			||||||
 | 
						g.Header("Content-Type", "application/octet-stream")
 | 
				
			||||||
 | 
						g.Header("Content-Disposition", "attachment; filename="+filename)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						writer := g.Writer
 | 
				
			||||||
 | 
						writer.WriteString("-- ----------------------------")
 | 
				
			||||||
 | 
						writer.WriteString("\n-- 导出平台: mayfly-go")
 | 
				
			||||||
 | 
						writer.WriteString(fmt.Sprintf("\n-- 导出时间: %s ", now.Format("2006-01-02 15:04:05")))
 | 
				
			||||||
 | 
						writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", db))
 | 
				
			||||||
 | 
						writer.WriteString("\n-- ----------------------------\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbmeta := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta()
 | 
				
			||||||
 | 
						for _, table := range tables {
 | 
				
			||||||
 | 
							if needStruct {
 | 
				
			||||||
 | 
								writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
 | 
				
			||||||
 | 
								writer.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS `%s`;\n", table))
 | 
				
			||||||
 | 
								writer.WriteString(dbmeta.GetCreateTableDdl(table)[0]["Create Table"].(string) + ";\n")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if !needData {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表记录: %s \n-- ----------------------------\n", table))
 | 
				
			||||||
 | 
							writer.WriteString("BEGIN;\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							countSql := fmt.Sprintf("SELECT COUNT(*) count FROM %s", table)
 | 
				
			||||||
 | 
							_, countRes, _ := dbInstance.SelectData(countSql)
 | 
				
			||||||
 | 
							// 查询出所有列信息总数,手动分页获取所有数据
 | 
				
			||||||
 | 
							maCount := int(countRes[0]["count"].(int64))
 | 
				
			||||||
 | 
							// 计算需要查询的页数
 | 
				
			||||||
 | 
							pageNum := maCount / DEFAULT_COLUMN_SIZE
 | 
				
			||||||
 | 
							if maCount%DEFAULT_COLUMN_SIZE > 0 {
 | 
				
			||||||
 | 
								pageNum++
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var sqlTmp string
 | 
				
			||||||
 | 
							switch dbInstance.Type {
 | 
				
			||||||
 | 
							case "mysql":
 | 
				
			||||||
 | 
								sqlTmp = "SELECT * FROM %s LIMIT %d, %d"
 | 
				
			||||||
 | 
							case "postgres":
 | 
				
			||||||
 | 
								sqlTmp = "SELECT * FROM %s OFFSET %d LIMIT %d"
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for index := 0; index < pageNum; index++ {
 | 
				
			||||||
 | 
								sql := fmt.Sprintf(sqlTmp, table, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
 | 
				
			||||||
 | 
								columns, result, _ := dbInstance.SelectData(sql)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								insertSql := "INSERT INTO `%s` VALUES (%s);\n"
 | 
				
			||||||
 | 
								for _, res := range result {
 | 
				
			||||||
 | 
									var values []string
 | 
				
			||||||
 | 
									for _, column := range columns {
 | 
				
			||||||
 | 
										value := res[column]
 | 
				
			||||||
 | 
										if value == nil {
 | 
				
			||||||
 | 
											values = append(values, "NULL")
 | 
				
			||||||
 | 
											continue
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										strValue, ok := value.(string)
 | 
				
			||||||
 | 
										if ok {
 | 
				
			||||||
 | 
											values = append(values, fmt.Sprintf("%#v", strValue))
 | 
				
			||||||
 | 
										} else {
 | 
				
			||||||
 | 
											values = append(values, utils.ToString(value))
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									writer.WriteString(fmt.Sprintf(insertSql, table, strings.Join(values, ", ")))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							writer.WriteString("COMMIT;\n")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						rc.NoRes = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rc.ReqParam = fmt.Sprintf("dbId: %d, db: %s, tables: %s, dumpType: %s", dbId, db, tablesStr, dumpType)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// @router /api/db/:dbId/t-metadata [get]
 | 
					// @router /api/db/:dbId/t-metadata [get]
 | 
				
			||||||
func (d *Db) TableMA(rc *ctx.ReqCtx) {
 | 
					func (d *Db) TableMA(rc *ctx.ReqCtx) {
 | 
				
			||||||
	dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
 | 
						dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbi.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbi.ProjectId), "%s")
 | 
				
			||||||
	rc.ResData = dbi.GetTableMetedatas()
 | 
						rc.ResData = dbi.GetMeta().GetTables()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// @router /api/db/:dbId/c-metadata [get]
 | 
					// @router /api/db/:dbId/c-metadata [get]
 | 
				
			||||||
@@ -183,16 +315,17 @@ func (d *Db) ColumnMA(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
 | 
						dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbi.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbi.ProjectId), "%s")
 | 
				
			||||||
	rc.ResData = dbi.GetColumnMetadatas(tn)
 | 
						rc.ResData = dbi.GetMeta().GetColumns(tn)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// @router /api/db/:dbId/hint-tables [get]
 | 
					// @router /api/db/:dbId/hint-tables [get]
 | 
				
			||||||
func (d *Db) HintTables(rc *ctx.ReqCtx) {
 | 
					func (d *Db) HintTables(rc *ctx.ReqCtx) {
 | 
				
			||||||
	dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
 | 
						dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbi.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbi.ProjectId), "%s")
 | 
				
			||||||
	// 获取所有表
 | 
					 | 
				
			||||||
	tables := dbi.GetTableMetedatas()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dm := dbi.GetMeta()
 | 
				
			||||||
 | 
						// 获取所有表
 | 
				
			||||||
 | 
						tables := dm.GetTables()
 | 
				
			||||||
	tableNames := make([]string, 0)
 | 
						tableNames := make([]string, 0)
 | 
				
			||||||
	for _, v := range tables {
 | 
						for _, v := range tables {
 | 
				
			||||||
		tableNames = append(tableNames, v["tableName"].(string))
 | 
							tableNames = append(tableNames, v["tableName"].(string))
 | 
				
			||||||
@@ -207,7 +340,7 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 获取所有表下的所有列信息
 | 
						// 获取所有表下的所有列信息
 | 
				
			||||||
	columnMds := dbi.GetColumnMetadatas(tableNames...)
 | 
						columnMds := dm.GetColumns(tableNames...)
 | 
				
			||||||
	for _, v := range columnMds {
 | 
						for _, v := range columnMds {
 | 
				
			||||||
		tName := v["tableName"].(string)
 | 
							tName := v["tableName"].(string)
 | 
				
			||||||
		if res[tName] == nil {
 | 
							if res[tName] == nil {
 | 
				
			||||||
@@ -217,7 +350,7 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) {
 | 
				
			|||||||
		columnName := fmt.Sprintf("%s  [%s]", v["columnName"], v["columnType"])
 | 
							columnName := fmt.Sprintf("%s  [%s]", v["columnName"], v["columnType"])
 | 
				
			||||||
		comment := v["columnComment"]
 | 
							comment := v["columnComment"]
 | 
				
			||||||
		// 如果字段备注不为空,则加上备注信息
 | 
							// 如果字段备注不为空,则加上备注信息
 | 
				
			||||||
		if comment != "" {
 | 
							if comment != nil && comment != "" {
 | 
				
			||||||
			columnName = fmt.Sprintf("%s[%s]", columnName, comment)
 | 
								columnName = fmt.Sprintf("%s[%s]", columnName, comment)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,11 +8,22 @@ type DbForm struct {
 | 
				
			|||||||
	Port      int    `binding:"required" json:"port"`
 | 
						Port      int    `binding:"required" json:"port"`
 | 
				
			||||||
	Username  string `binding:"required" json:"username"`
 | 
						Username  string `binding:"required" json:"username"`
 | 
				
			||||||
	Password  string `json:"password"`
 | 
						Password  string `json:"password"`
 | 
				
			||||||
	Database  string `binding:"required" json:"database"`
 | 
						Params    string `json:"params"`
 | 
				
			||||||
 | 
						Database  string `json:"database"`
 | 
				
			||||||
	ProjectId uint64 `binding:"required" json:"projectId"`
 | 
						ProjectId uint64 `binding:"required" json:"projectId"`
 | 
				
			||||||
	Project   string `json:"project"`
 | 
						Project   string `json:"project"`
 | 
				
			||||||
	Env       string `json:"env"`
 | 
						Env       string `json:"env"`
 | 
				
			||||||
	EnvId     uint64 `binding:"required" json:"envId"`
 | 
						EnvId     uint64 `binding:"required" json:"envId"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type DbSqlSaveForm struct {
 | 
				
			||||||
 | 
						Name string
 | 
				
			||||||
 | 
						Sql  string `binding:"required"`
 | 
				
			||||||
 | 
						Type int    `binding:"required"`
 | 
				
			||||||
 | 
						Db   string `binding:"required"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 数据库SQL执行表单
 | 
					// 数据库SQL执行表单
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,52 +0,0 @@
 | 
				
			|||||||
package form
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type MachineForm struct {
 | 
					 | 
				
			||||||
	Id          uint64 `json:"id"`
 | 
					 | 
				
			||||||
	ProjectId   uint64 `json:"projectId"`
 | 
					 | 
				
			||||||
	ProjectName string `json:"projectName"`
 | 
					 | 
				
			||||||
	Name        string `json:"name" binding:"required"`
 | 
					 | 
				
			||||||
	// IP地址
 | 
					 | 
				
			||||||
	Ip string `json:"ip" binding:"required"`
 | 
					 | 
				
			||||||
	// 用户名
 | 
					 | 
				
			||||||
	Username string `json:"username" binding:"required"`
 | 
					 | 
				
			||||||
	Password string `json:"password"`
 | 
					 | 
				
			||||||
	// 端口号
 | 
					 | 
				
			||||||
	Port   int    `json:"port" binding:"required"`
 | 
					 | 
				
			||||||
	Remark string `json:"remark"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type MachineRunForm struct {
 | 
					 | 
				
			||||||
	MachineId int64  `binding:"required"`
 | 
					 | 
				
			||||||
	Cmd       string `binding:"required"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type MachineFileForm struct {
 | 
					 | 
				
			||||||
	Id        uint64
 | 
					 | 
				
			||||||
	Name      string `binding:"required"`
 | 
					 | 
				
			||||||
	MachineId uint64 `binding:"required"`
 | 
					 | 
				
			||||||
	Type      int    `binding:"required"`
 | 
					 | 
				
			||||||
	Path      string `binding:"required"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type MachineScriptForm struct {
 | 
					 | 
				
			||||||
	Id          uint64
 | 
					 | 
				
			||||||
	Name        string `binding:"required"`
 | 
					 | 
				
			||||||
	MachineId   uint64 `binding:"required"`
 | 
					 | 
				
			||||||
	Type        int    `binding:"required"`
 | 
					 | 
				
			||||||
	Description string `binding:"required"`
 | 
					 | 
				
			||||||
	Params      string
 | 
					 | 
				
			||||||
	Script      string `binding:"required"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type DbSqlSaveForm struct {
 | 
					 | 
				
			||||||
	Name string
 | 
					 | 
				
			||||||
	Sql  string `binding:"required"`
 | 
					 | 
				
			||||||
	Type int    `binding:"required"`
 | 
					 | 
				
			||||||
	Db   string `binding:"required"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type MachineFileUpdateForm struct {
 | 
					 | 
				
			||||||
	Content string `binding:"required"`
 | 
					 | 
				
			||||||
	Id      uint64 `binding:"required"`
 | 
					 | 
				
			||||||
	Path    string `binding:"required"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										50
									
								
								server/internal/devops/api/form/machine.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								server/internal/devops/api/form/machine.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					package form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MachineForm struct {
 | 
				
			||||||
 | 
						Id                 uint64 `json:"id"`
 | 
				
			||||||
 | 
						ProjectId          uint64 `json:"projectId"`
 | 
				
			||||||
 | 
						ProjectName        string `json:"projectName"`
 | 
				
			||||||
 | 
						Name               string `json:"name" binding:"required"`
 | 
				
			||||||
 | 
						Ip                 string `json:"ip" binding:"required"`       // IP地址
 | 
				
			||||||
 | 
						Username           string `json:"username" binding:"required"` // 用户名
 | 
				
			||||||
 | 
						AuthMethod         int8   `json:"authMethod" binding:"required"`
 | 
				
			||||||
 | 
						Password           string `json:"password"`
 | 
				
			||||||
 | 
						Port               int    `json:"port" binding:"required"` // 端口号
 | 
				
			||||||
 | 
						Remark             string `json:"remark"`
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MachineRunForm struct {
 | 
				
			||||||
 | 
						MachineId int64  `binding:"required"`
 | 
				
			||||||
 | 
						Cmd       string `binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MachineFileForm struct {
 | 
				
			||||||
 | 
						Id        uint64
 | 
				
			||||||
 | 
						Name      string `binding:"required"`
 | 
				
			||||||
 | 
						MachineId uint64 `binding:"required"`
 | 
				
			||||||
 | 
						Type      int    `binding:"required"`
 | 
				
			||||||
 | 
						Path      string `binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MachineScriptForm struct {
 | 
				
			||||||
 | 
						Id          uint64
 | 
				
			||||||
 | 
						Name        string `binding:"required"`
 | 
				
			||||||
 | 
						MachineId   uint64 `binding:"required"`
 | 
				
			||||||
 | 
						Type        int    `binding:"required"`
 | 
				
			||||||
 | 
						Description string `binding:"required"`
 | 
				
			||||||
 | 
						Params      string
 | 
				
			||||||
 | 
						Script      string `binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MachineCreateFileForm struct {
 | 
				
			||||||
 | 
						Path string `binding:"required"`
 | 
				
			||||||
 | 
						Type string `binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MachineFileUpdateForm struct {
 | 
				
			||||||
 | 
						Content string `binding:"required"`
 | 
				
			||||||
 | 
						Id      uint64 `binding:"required"`
 | 
				
			||||||
 | 
						Path    string `binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,13 +1,15 @@
 | 
				
			|||||||
package form
 | 
					package form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Mongo struct {
 | 
					type Mongo struct {
 | 
				
			||||||
	Id        uint64
 | 
						Id                 uint64
 | 
				
			||||||
	Uri       string `binding:"required" json:"uri"`
 | 
						Uri                string `binding:"required" json:"uri"`
 | 
				
			||||||
	Name      string `binding:"required" json:"name"`
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
	ProjectId uint64 `binding:"required" json:"projectId"`
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	Project   string `json:"project"`
 | 
						Name               string `binding:"required" json:"name"`
 | 
				
			||||||
	Env       string `json:"env"`
 | 
						ProjectId          uint64 `binding:"required" json:"projectId"`
 | 
				
			||||||
	EnvId     uint64 `binding:"required" json:"envId"`
 | 
						Project            string `json:"project"`
 | 
				
			||||||
 | 
						Env                string `json:"env"`
 | 
				
			||||||
 | 
						EnvId              uint64 `binding:"required" json:"envId"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MongoCommand struct {
 | 
					type MongoCommand struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,18 @@
 | 
				
			|||||||
package form
 | 
					package form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Redis struct {
 | 
					type Redis struct {
 | 
				
			||||||
	Id        uint64
 | 
						Id                 uint64
 | 
				
			||||||
	Host      string `binding:"required" json:"host"`
 | 
						Host               string `binding:"required" json:"host"`
 | 
				
			||||||
	Password  string `json:"password"`
 | 
						Password           string `json:"password"`
 | 
				
			||||||
	Db        int    `json:"db"`
 | 
						Mode               string `json:"mode"`
 | 
				
			||||||
	ProjectId uint64 `binding:"required" json:"projectId"`
 | 
						Db                 int    `json:"db"`
 | 
				
			||||||
	Project   string `json:"project"`
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
	Env       string `json:"env"`
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	EnvId     uint64 `binding:"required" json:"envId"`
 | 
						ProjectId          uint64 `binding:"required" json:"projectId"`
 | 
				
			||||||
 | 
						Project            string `json:"project"`
 | 
				
			||||||
 | 
						Env                string `json:"env"`
 | 
				
			||||||
 | 
						EnvId              uint64 `binding:"required" json:"envId"`
 | 
				
			||||||
 | 
						Remark             string `json:"remark"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type KeyInfo struct {
 | 
					type KeyInfo struct {
 | 
				
			||||||
@@ -30,3 +34,9 @@ type SetValue struct {
 | 
				
			|||||||
	KeyInfo
 | 
						KeyInfo
 | 
				
			||||||
	Value []interface{} `binding:"required" json:"value"`
 | 
						Value []interface{} `binding:"required" json:"value"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type RedisScanForm struct {
 | 
				
			||||||
 | 
						Cursor map[string]uint64 `json:"cursor"`
 | 
				
			||||||
 | 
						Match  string            `json:"match"`
 | 
				
			||||||
 | 
						Count  int64             `json:"count"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,11 +54,30 @@ func (m *Machine) SaveMachine(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	machineForm := new(form.MachineForm)
 | 
						machineForm := new(form.MachineForm)
 | 
				
			||||||
	ginx.BindJsonAndValid(g, machineForm)
 | 
						ginx.BindJsonAndValid(g, machineForm)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	entity := new(entity.Machine)
 | 
						me := new(entity.Machine)
 | 
				
			||||||
	utils.Copy(entity, machineForm)
 | 
						utils.Copy(me, machineForm)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	entity.SetBaseInfo(rc.LoginAccount)
 | 
						if me.AuthMethod == entity.MachineAuthMethodPassword {
 | 
				
			||||||
	m.MachineApp.Save(entity)
 | 
							// 密码解密,并使用解密后的赋值
 | 
				
			||||||
 | 
							originPwd, err := utils.DefaultRsaDecrypt(machineForm.Password, true)
 | 
				
			||||||
 | 
							biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
							me.Password = originPwd
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 密码脱敏记录日志
 | 
				
			||||||
 | 
						machineForm.Password = "****"
 | 
				
			||||||
 | 
						rc.ReqParam = machineForm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						me.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
 | 
						m.MachineApp.Save(me)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取机器实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
				
			||||||
 | 
					func (m *Machine) GetMachinePwd(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						mid := GetMachineId(rc.GinCtx)
 | 
				
			||||||
 | 
						me := m.MachineApp.GetById(mid, "Password")
 | 
				
			||||||
 | 
						me.PwdDecrypt()
 | 
				
			||||||
 | 
						rc.ResData = me.Password
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *Machine) ChangeStatus(rc *ctx.ReqCtx) {
 | 
					func (m *Machine) ChangeStatus(rc *ctx.ReqCtx) {
 | 
				
			||||||
@@ -141,21 +160,16 @@ func (m *Machine) WsSSH(g *gin.Context) {
 | 
				
			|||||||
		panic(biz.NewBizErr("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
 | 
							panic(biz.NewBizErr("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cols := ginx.QueryInt(g, "cols", 80)
 | 
					 | 
				
			||||||
	rows := ginx.QueryInt(g, "rows", 40)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	cli := m.MachineApp.GetCli(GetMachineId(g))
 | 
						cli := m.MachineApp.GetCli(GetMachineId(g))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(m.ProjectApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(m.ProjectApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sws, err := machine.NewLogicSshWsSession(cols, rows, cli, wsConn)
 | 
						cols := ginx.QueryInt(g, "cols", 80)
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "\033[1;31m连接失败:%s\033[0m")
 | 
						rows := ginx.QueryInt(g, "rows", 40)
 | 
				
			||||||
	defer sws.Close()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	quitChan := make(chan bool, 3)
 | 
						mts, err := machine.NewTerminalSession(utils.RandString(16), wsConn, cli, rows, cols)
 | 
				
			||||||
	sws.Start(quitChan)
 | 
						biz.ErrIsNilAppendErr(err, "\033[1;31m连接失败: %s\033[0m")
 | 
				
			||||||
	go sws.Wait(quitChan)
 | 
						mts.Start()
 | 
				
			||||||
 | 
						defer mts.Stop()
 | 
				
			||||||
	<-quitChan
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetMachineId(g *gin.Context) uint64 {
 | 
					func GetMachineId(g *gin.Context) uint64 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,6 +60,22 @@ func (m *MachineFile) DeleteFile(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/***      sftp相关操作      */
 | 
					/***      sftp相关操作      */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *MachineFile) CreateFile(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						g := rc.GinCtx
 | 
				
			||||||
 | 
						fid := GetMachineFileId(g)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						form := new(form.MachineCreateFileForm)
 | 
				
			||||||
 | 
						ginx.BindJsonAndValid(g, form)
 | 
				
			||||||
 | 
						path := form.Path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if form.Type == dir {
 | 
				
			||||||
 | 
							m.MachineFileApp.MkDir(fid, form.Path)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							m.MachineFileApp.CreateFile(fid, form.Path)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						rc.ReqParam = fmt.Sprintf("path: %s, type: %s", path, form.Type)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *MachineFile) ReadFileContent(rc *ctx.ReqCtx) {
 | 
					func (m *MachineFile) ReadFileContent(rc *ctx.ReqCtx) {
 | 
				
			||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
	fid := GetMachineFileId(g)
 | 
						fid := GetMachineFileId(g)
 | 
				
			||||||
@@ -104,6 +120,7 @@ func (m *MachineFile) GetDirEntry(rc *ctx.ReqCtx) {
 | 
				
			|||||||
			Size: fi.Size(),
 | 
								Size: fi.Size(),
 | 
				
			||||||
			Path: readPath + fi.Name(),
 | 
								Path: readPath + fi.Name(),
 | 
				
			||||||
			Type: getFileType(fi.Mode()),
 | 
								Type: getFileType(fi.Mode()),
 | 
				
			||||||
 | 
								Mode: fi.Mode().String(),
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	rc.ResData = fisVO
 | 
						rc.ResData = fisVO
 | 
				
			||||||
@@ -155,7 +172,6 @@ func (m *MachineFile) UploadFile(rc *ctx.ReqCtx) {
 | 
				
			|||||||
func (m *MachineFile) RemoveFile(rc *ctx.ReqCtx) {
 | 
					func (m *MachineFile) RemoveFile(rc *ctx.ReqCtx) {
 | 
				
			||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
	fid := GetMachineFileId(g)
 | 
						fid := GetMachineFileId(g)
 | 
				
			||||||
	// mid := GetMachineId(g)
 | 
					 | 
				
			||||||
	path := g.Query("path")
 | 
						path := g.Query("path")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	m.MachineFileApp.RemoveFile(fid, path)
 | 
						m.MachineFileApp.RemoveFile(fid, path)
 | 
				
			||||||
@@ -167,7 +183,10 @@ func getFileType(fm fs.FileMode) string {
 | 
				
			|||||||
	if fm.IsDir() {
 | 
						if fm.IsDir() {
 | 
				
			||||||
		return dir
 | 
							return dir
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return file
 | 
						if fm.IsRegular() {
 | 
				
			||||||
 | 
							return file
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return dir
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetMachineFileId(g *gin.Context) uint64 {
 | 
					func GetMachineFileId(g *gin.Context) uint64 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,6 +38,7 @@ func (m *Mongo) Save(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	mongo := new(entity.Mongo)
 | 
						mongo := new(entity.Mongo)
 | 
				
			||||||
	utils.Copy(mongo, form)
 | 
						utils.Copy(mongo, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mongo.SetBaseInfo(rc.LoginAccount)
 | 
						mongo.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
	m.MongoApp.Save(mongo)
 | 
						m.MongoApp.Save(mongo)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,7 +43,8 @@ func (p *Project) GetProjects(rc *ctx.ReqCtx) {
 | 
				
			|||||||
func (p *Project) SaveProject(rc *ctx.ReqCtx) {
 | 
					func (p *Project) SaveProject(rc *ctx.ReqCtx) {
 | 
				
			||||||
	project := &entity.Project{}
 | 
						project := &entity.Project{}
 | 
				
			||||||
	ginx.BindJsonAndValid(rc.GinCtx, project)
 | 
						ginx.BindJsonAndValid(rc.GinCtx, project)
 | 
				
			||||||
	rc.ReqParam = project
 | 
					
 | 
				
			||||||
 | 
						rc.ReqParam = fmt.Sprintf("projectId: %d, projectName: %s, remark: %s", project.Id, project.Name, project.Remark)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	project.SetBaseInfo(rc.LoginAccount)
 | 
						project.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
	p.ProjectApp.SaveProject(project)
 | 
						p.ProjectApp.SaveProject(project)
 | 
				
			||||||
@@ -81,7 +82,8 @@ func (p *Project) GetProjectMembers(rc *ctx.ReqCtx) {
 | 
				
			|||||||
func (p *Project) SaveProjectMember(rc *ctx.ReqCtx) {
 | 
					func (p *Project) SaveProjectMember(rc *ctx.ReqCtx) {
 | 
				
			||||||
	projectMem := &entity.ProjectMember{}
 | 
						projectMem := &entity.ProjectMember{}
 | 
				
			||||||
	ginx.BindJsonAndValid(rc.GinCtx, projectMem)
 | 
						ginx.BindJsonAndValid(rc.GinCtx, projectMem)
 | 
				
			||||||
	rc.ReqParam = projectMem
 | 
					
 | 
				
			||||||
 | 
						rc.ReqParam = fmt.Sprintf("projectId: %d, username: %s", projectMem.ProjectId, projectMem.Username)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 校验账号,并赋值username
 | 
						// 校验账号,并赋值username
 | 
				
			||||||
	account := &sys_entity.Account{}
 | 
						account := &sys_entity.Account{}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package api
 | 
					package api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"mayfly-go/internal/devops/api/form"
 | 
						"mayfly-go/internal/devops/api/form"
 | 
				
			||||||
	"mayfly-go/internal/devops/api/vo"
 | 
						"mayfly-go/internal/devops/api/vo"
 | 
				
			||||||
	"mayfly-go/internal/devops/application"
 | 
						"mayfly-go/internal/devops/application"
 | 
				
			||||||
@@ -11,7 +12,10 @@ import (
 | 
				
			|||||||
	"mayfly-go/pkg/utils"
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/go-redis/redis/v8"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Redis struct {
 | 
					type Redis struct {
 | 
				
			||||||
@@ -32,20 +36,69 @@ func (r *Redis) Save(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	form := &form.Redis{}
 | 
						form := &form.Redis{}
 | 
				
			||||||
	ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
						ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rc.ReqParam = form
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	redis := new(entity.Redis)
 | 
						redis := new(entity.Redis)
 | 
				
			||||||
	utils.Copy(redis, form)
 | 
						utils.Copy(redis, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 密码解密,并使用解密后的赋值
 | 
				
			||||||
 | 
						originPwd, err := utils.DefaultRsaDecrypt(redis.Password, true)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
						redis.Password = originPwd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 密码脱敏记录日志
 | 
				
			||||||
 | 
						form.Password = "****"
 | 
				
			||||||
 | 
						rc.ReqParam = form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	redis.SetBaseInfo(rc.LoginAccount)
 | 
						redis.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
	r.RedisApp.Save(redis)
 | 
						r.RedisApp.Save(redis)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取redis实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
				
			||||||
 | 
					func (r *Redis) GetRedisPwd(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
 | 
				
			||||||
 | 
						re := r.RedisApp.GetById(rid, "Password")
 | 
				
			||||||
 | 
						re.PwdDecrypt()
 | 
				
			||||||
 | 
						rc.ResData = re.Password
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) DeleteRedis(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) DeleteRedis(rc *ctx.ReqCtx) {
 | 
				
			||||||
	r.RedisApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
 | 
						r.RedisApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) RedisInfo(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) RedisInfo(rc *ctx.ReqCtx) {
 | 
				
			||||||
	res, _ := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(rc.GinCtx, "id"))).Cli.Info().Result()
 | 
						ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var res string
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx := context.Background()
 | 
				
			||||||
 | 
						if ri.Mode == "" || ri.Mode == entity.RedisModeStandalone {
 | 
				
			||||||
 | 
							res, err = ri.Cli.Info(ctx).Result()
 | 
				
			||||||
 | 
						} else if ri.Mode == entity.RedisModeCluster {
 | 
				
			||||||
 | 
							host := rc.GinCtx.Query("host")
 | 
				
			||||||
 | 
							biz.NotEmpty(host, "集群模式host信息不能为空")
 | 
				
			||||||
 | 
							clusterClient := ri.ClusterCli
 | 
				
			||||||
 | 
							var redisClient *redis.Client
 | 
				
			||||||
 | 
							// 遍历集群的master节点找到该redis client
 | 
				
			||||||
 | 
							clusterClient.ForEachMaster(ctx, func(ctx context.Context, client *redis.Client) error {
 | 
				
			||||||
 | 
								if host == client.Options().Addr {
 | 
				
			||||||
 | 
									redisClient = client
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
							if redisClient == nil {
 | 
				
			||||||
 | 
								// 遍历集群的slave节点找到该redis client
 | 
				
			||||||
 | 
								clusterClient.ForEachSlave(ctx, func(ctx context.Context, client *redis.Client) error {
 | 
				
			||||||
 | 
									if host == client.Options().Addr {
 | 
				
			||||||
 | 
										redisClient = client
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							biz.NotNil(redisClient, "该实例不在该集群中")
 | 
				
			||||||
 | 
							res, err = redisClient.Info(ctx).Result()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "获取redis info失败: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	datas := strings.Split(res, "\r\n")
 | 
						datas := strings.Split(res, "\r\n")
 | 
				
			||||||
	i := 0
 | 
						i := 0
 | 
				
			||||||
@@ -81,43 +134,125 @@ func (r *Redis) RedisInfo(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	rc.ResData = parseMap
 | 
						rc.ResData = parseMap
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) ClusterInfo(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						g := rc.GinCtx
 | 
				
			||||||
 | 
						ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
				
			||||||
 | 
						biz.IsEquals(ri.Mode, entity.RedisModeCluster, "非集群模式")
 | 
				
			||||||
 | 
						info, _ := ri.ClusterCli.ClusterInfo(context.Background()).Result()
 | 
				
			||||||
 | 
						nodesStr, _ := ri.ClusterCli.ClusterNodes(context.Background()).Result()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						nodesRes := make([]map[string]string, 0)
 | 
				
			||||||
 | 
						nodes := strings.Split(nodesStr, "\n")
 | 
				
			||||||
 | 
						for _, node := range nodes {
 | 
				
			||||||
 | 
							if node == "" {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							nodeInfos := strings.Split(node, " ")
 | 
				
			||||||
 | 
							node := make(map[string]string)
 | 
				
			||||||
 | 
							node["nodeId"] = nodeInfos[0]
 | 
				
			||||||
 | 
							// ip:port1@port2:port1指redis服务器与客户端通信的端口,port2则是集群内部节点间通信的端口
 | 
				
			||||||
 | 
							node["ip"] = nodeInfos[1]
 | 
				
			||||||
 | 
							node["flags"] = nodeInfos[2]
 | 
				
			||||||
 | 
							// 如果节点是slave,并且已知master节点,则为master节点ID;否则为符号"-"
 | 
				
			||||||
 | 
							node["masterSlaveRelation"] = nodeInfos[3]
 | 
				
			||||||
 | 
							// 最近一次发送ping的时间,这个时间是一个unix毫秒时间戳,0代表没有发送过
 | 
				
			||||||
 | 
							node["pingSent"] = nodeInfos[4]
 | 
				
			||||||
 | 
							// 最近一次收到pong的时间,使用unix时间戳表示
 | 
				
			||||||
 | 
							node["pongRecv"] = nodeInfos[5]
 | 
				
			||||||
 | 
							// 节点的epoch值(如果该节点是从节点,则为其主节点的epoch值)。每当节点发生失败切换时,都会创建一个新的,独特的,递增的epoch。
 | 
				
			||||||
 | 
							// 如果多个节点竞争同一个哈希槽时,epoch值更高的节点会抢夺到
 | 
				
			||||||
 | 
							node["configEpoch"] = nodeInfos[6]
 | 
				
			||||||
 | 
							// node-to-node集群总线使用的链接的状态,我们使用这个链接与集群中其他节点进行通信.值可以是 connected 和 disconnected
 | 
				
			||||||
 | 
							node["linkState"] = nodeInfos[7]
 | 
				
			||||||
 | 
							// slave节点没有插槽信息
 | 
				
			||||||
 | 
							if len(nodeInfos) > 8 {
 | 
				
			||||||
 | 
								// slot:master节点第9位为哈希槽值或者一个哈希槽范围,代表当前节点可以提供服务的所有哈希槽值。如果只是一个值,那就是只有一个槽会被使用。
 | 
				
			||||||
 | 
								// 如果是一个范围,这个值表示为起始槽-结束槽,节点将处理包括起始槽和结束槽在内的所有哈希槽。
 | 
				
			||||||
 | 
								node["slot"] = nodeInfos[8]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							nodesRes = append(nodesRes, node)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						rc.ResData = map[string]interface{}{
 | 
				
			||||||
 | 
							"clusterInfo":  info,
 | 
				
			||||||
 | 
							"clusterNodes": nodesRes,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// scan获取redis的key列表信息
 | 
					// scan获取redis的key列表信息
 | 
				
			||||||
func (r *Redis) Scan(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) Scan(rc *ctx.ReqCtx) {
 | 
				
			||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
 | 
					 | 
				
			||||||
	ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
						ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	keys, cursor := ri.Scan(uint64(ginx.PathParamInt(g, "cursor")), g.Query("match"), int64(ginx.PathParamInt(g, "count")))
 | 
						form := &form.RedisScanForm{}
 | 
				
			||||||
 | 
						ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var keyInfoSplit []string
 | 
						cmd := ri.GetCmdable()
 | 
				
			||||||
	if len(keys) > 0 {
 | 
						ctx := context.Background()
 | 
				
			||||||
		keyInfoLua := `
 | 
					 | 
				
			||||||
			local result = {}
 | 
					 | 
				
			||||||
			-- KEYS[1]为第1个参数,lua数组下标从1开始
 | 
					 | 
				
			||||||
			local ttl = redis.call('ttl', KEYS[1]);
 | 
					 | 
				
			||||||
			local keyType = redis.call('type', KEYS[1]);
 | 
					 | 
				
			||||||
			for i = 1, #KEYS do
 | 
					 | 
				
			||||||
				local ttl = redis.call('ttl', KEYS[i]);
 | 
					 | 
				
			||||||
				local keyType = redis.call('type', KEYS[i]);
 | 
					 | 
				
			||||||
				table.insert(result, string.format("%d,%s", ttl, keyType['ok']));
 | 
					 | 
				
			||||||
			end;
 | 
					 | 
				
			||||||
			return table.concat(result, ".");`
 | 
					 | 
				
			||||||
		// 通过lua获取 ttl,type.ttl2,type2格式,以便下面切割获取ttl和type。避免多次调用ttl和type函数
 | 
					 | 
				
			||||||
		keyInfos, _ := ri.Cli.Eval(keyInfoLua, keys).Result()
 | 
					 | 
				
			||||||
		keyInfoSplit = strings.Split(keyInfos.(string), ".")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	kis := make([]*vo.KeyInfo, 0)
 | 
						kis := make([]*vo.KeyInfo, 0)
 | 
				
			||||||
	for i, k := range keys {
 | 
						var cursorRes map[string]uint64 = make(map[string]uint64)
 | 
				
			||||||
		ttlType := strings.Split(keyInfoSplit[i], ",")
 | 
					
 | 
				
			||||||
		ttl, _ := strconv.Atoi(ttlType[0])
 | 
						if ri.Mode == "" || ri.Mode == entity.RedisModeStandalone || ri.Mode == entity.RedisModeSentinel {
 | 
				
			||||||
		ki := &vo.KeyInfo{Key: k, Type: ttlType[1], Ttl: int64(ttl)}
 | 
							redisAddr := ri.Cli.Options().Addr
 | 
				
			||||||
		kis = append(kis, ki)
 | 
							keys, cursor := ri.Scan(form.Cursor[redisAddr], form.Match, form.Count)
 | 
				
			||||||
 | 
							cursorRes[redisAddr] = cursor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var keyInfoSplit []string
 | 
				
			||||||
 | 
							if len(keys) > 0 {
 | 
				
			||||||
 | 
								keyInfosLua := `local result = {}
 | 
				
			||||||
 | 
												-- KEYS[1]为第1个参数,lua数组下标从1开始
 | 
				
			||||||
 | 
												for i = 1, #KEYS do
 | 
				
			||||||
 | 
													local ttl = redis.call('ttl', KEYS[i]);
 | 
				
			||||||
 | 
													local keyType = redis.call('type', KEYS[i]);
 | 
				
			||||||
 | 
													table.insert(result, string.format("%d,%s", ttl, keyType['ok']));
 | 
				
			||||||
 | 
												end;
 | 
				
			||||||
 | 
												return table.concat(result, ".");`
 | 
				
			||||||
 | 
								// 通过lua获取 ttl,type.ttl2,type2格式,以便下面切割获取ttl和type。避免多次调用ttl和type函数
 | 
				
			||||||
 | 
								keyInfos, err := cmd.Eval(ctx, keyInfosLua, keys).Result()
 | 
				
			||||||
 | 
								biz.ErrIsNilAppendErr(err, "执行lua脚本获取key信息失败: %s")
 | 
				
			||||||
 | 
								keyInfoSplit = strings.Split(keyInfos.(string), ".")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for i, k := range keys {
 | 
				
			||||||
 | 
								ttlType := strings.Split(keyInfoSplit[i], ",")
 | 
				
			||||||
 | 
								ttl, _ := strconv.Atoi(ttlType[0])
 | 
				
			||||||
 | 
								ki := &vo.KeyInfo{Key: k, Type: ttlType[1], Ttl: int64(ttl)}
 | 
				
			||||||
 | 
								kis = append(kis, ki)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if ri.Mode == entity.RedisModeCluster {
 | 
				
			||||||
 | 
							var keys []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							mu := &sync.Mutex{}
 | 
				
			||||||
 | 
							// 遍历所有master节点,并执行scan命令,合并keys
 | 
				
			||||||
 | 
							ri.ClusterCli.ForEachMaster(ctx, func(ctx context.Context, client *redis.Client) error {
 | 
				
			||||||
 | 
								redisAddr := client.Options().Addr
 | 
				
			||||||
 | 
								ks, cursor, _ := client.Scan(ctx, form.Cursor[redisAddr], form.Match, form.Count).Result()
 | 
				
			||||||
 | 
								// 遍历节点的内部回调函数使用异步调用,如不加锁会导致集合并发错误
 | 
				
			||||||
 | 
								mu.Lock()
 | 
				
			||||||
 | 
								cursorRes[redisAddr] = cursor
 | 
				
			||||||
 | 
								keys = append(keys, ks...)
 | 
				
			||||||
 | 
								mu.Unlock()
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 因为redis集群模式执行lua脚本key必须位于同一slot中,故单机获取的方式不适合
 | 
				
			||||||
 | 
							// 使用lua获取key的ttl以及类型,减少网络调用
 | 
				
			||||||
 | 
							keyInfoLua := `local ttl = redis.call('ttl', KEYS[1]);
 | 
				
			||||||
 | 
										   local keyType = redis.call('type', KEYS[1]);
 | 
				
			||||||
 | 
										   return string.format("%d,%s", ttl, keyType['ok'])`
 | 
				
			||||||
 | 
							for _, k := range keys {
 | 
				
			||||||
 | 
								keyInfo, err := cmd.Eval(ctx, keyInfoLua, []string{k}).Result()
 | 
				
			||||||
 | 
								biz.ErrIsNilAppendErr(err, "执行lua脚本获取key信息失败: %s")
 | 
				
			||||||
 | 
								ttlType := strings.Split(keyInfo.(string), ",")
 | 
				
			||||||
 | 
								ttl, _ := strconv.Atoi(ttlType[0])
 | 
				
			||||||
 | 
								ki := &vo.KeyInfo{Key: k, Type: ttlType[1], Ttl: int64(ttl)}
 | 
				
			||||||
 | 
								kis = append(kis, ki)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	size, _ := ri.Cli.DBSize().Result()
 | 
						size, _ := cmd.DBSize(context.TODO()).Result()
 | 
				
			||||||
	rc.ResData = &vo.Keys{Cursor: cursor, Keys: kis, DbSize: size}
 | 
						rc.ResData = &vo.Keys{Cursor: cursorRes, Keys: kis, DbSize: size}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) DeleteKey(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) DeleteKey(rc *ctx.ReqCtx) {
 | 
				
			||||||
@@ -129,7 +264,7 @@ func (r *Redis) DeleteKey(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rc.ReqParam = key
 | 
						rc.ReqParam = key
 | 
				
			||||||
	ri.Cli.Del(key)
 | 
						ri.GetCmdable().Del(context.Background(), key)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) {
 | 
					func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) {
 | 
				
			||||||
@@ -145,18 +280,11 @@ func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (r *Redis) GetStringValue(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) GetStringValue(rc *ctx.ReqCtx) {
 | 
				
			||||||
	ri, key := r.checkKey(rc)
 | 
						ri, key := r.checkKey(rc)
 | 
				
			||||||
	str, err := ri.Cli.Get(key).Result()
 | 
						str, err := ri.GetCmdable().Get(context.TODO(), key).Result()
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "获取字符串值失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "获取字符串值失败: %s")
 | 
				
			||||||
	rc.ResData = str
 | 
						rc.ResData = str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) GetHashValue(rc *ctx.ReqCtx) {
 | 
					 | 
				
			||||||
	ri, key := r.checkKey(rc)
 | 
					 | 
				
			||||||
	res, err := ri.Cli.HGetAll(key).Result()
 | 
					 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "获取hash值失败: %s")
 | 
					 | 
				
			||||||
	rc.ResData = res
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (r *Redis) SetStringValue(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) SetStringValue(rc *ctx.ReqCtx) {
 | 
				
			||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
	keyValue := new(form.StringValue)
 | 
						keyValue := new(form.StringValue)
 | 
				
			||||||
@@ -165,11 +293,50 @@ func (r *Redis) SetStringValue(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
						ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	str, err := ri.Cli.Set(keyValue.Key, keyValue.Value, time.Second*time.Duration(keyValue.Timed)).Result()
 | 
						str, err := ri.GetCmdable().Set(context.TODO(), keyValue.Key, keyValue.Value, time.Second*time.Duration(keyValue.Timed)).Result()
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "保存字符串值失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "保存字符串值失败: %s")
 | 
				
			||||||
	rc.ResData = str
 | 
						rc.ResData = str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) Hscan(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						ri, key := r.checkKey(rc)
 | 
				
			||||||
 | 
						g := rc.GinCtx
 | 
				
			||||||
 | 
						count := ginx.QueryInt(g, "count", 10)
 | 
				
			||||||
 | 
						match := g.Query("match")
 | 
				
			||||||
 | 
						cursor := ginx.QueryInt(g, "cursor", 0)
 | 
				
			||||||
 | 
						contextTodo := context.TODO()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmdable := ri.GetCmdable()
 | 
				
			||||||
 | 
						keys, nextCursor, err := cmdable.HScan(contextTodo, key, uint64(cursor), match, int64(count)).Result()
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "hcan err: %s")
 | 
				
			||||||
 | 
						keySize, err := cmdable.HLen(contextTodo, key).Result()
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "hlen err: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rc.ResData = map[string]interface{}{
 | 
				
			||||||
 | 
							"keys":    keys,
 | 
				
			||||||
 | 
							"cursor":  nextCursor,
 | 
				
			||||||
 | 
							"keySize": keySize,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) Hdel(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						ri, key := r.checkKey(rc)
 | 
				
			||||||
 | 
						field := rc.GinCtx.Query("field")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						delRes, err := ri.GetCmdable().HDel(context.TODO(), key, field).Result()
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "hdel err: %s")
 | 
				
			||||||
 | 
						rc.ResData = delRes
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) Hget(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						ri, key := r.checkKey(rc)
 | 
				
			||||||
 | 
						field := rc.GinCtx.Query("field")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res, err := ri.GetCmdable().HGet(context.TODO(), key, field).Result()
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "hget err: %s")
 | 
				
			||||||
 | 
						rc.ResData = res
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) SetHashValue(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) SetHashValue(rc *ctx.ReqCtx) {
 | 
				
			||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
	hashValue := new(form.HashValue)
 | 
						hashValue := new(form.HashValue)
 | 
				
			||||||
@@ -178,21 +345,21 @@ func (r *Redis) SetHashValue(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
						ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmd := ri.GetCmdable()
 | 
				
			||||||
	key := hashValue.Key
 | 
						key := hashValue.Key
 | 
				
			||||||
	// 简单处理->先删除,后新增
 | 
						contextTodo := context.TODO()
 | 
				
			||||||
	ri.Cli.Del(key)
 | 
					 | 
				
			||||||
	for _, v := range hashValue.Value {
 | 
						for _, v := range hashValue.Value {
 | 
				
			||||||
		res := ri.Cli.HSet(key, v["key"].(string), v["value"])
 | 
							res := cmd.HSet(contextTodo, key, v["field"].(string), v["value"])
 | 
				
			||||||
		biz.ErrIsNilAppendErr(res.Err(), "保存hash值失败: %s")
 | 
							biz.ErrIsNilAppendErr(res.Err(), "保存hash值失败: %s")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if hashValue.Timed != -1 {
 | 
						if hashValue.Timed != 0 && hashValue.Timed != -1 {
 | 
				
			||||||
		ri.Cli.Expire(key, time.Second*time.Duration(hashValue.Timed))
 | 
							cmd.Expire(context.TODO(), key, time.Second*time.Duration(hashValue.Timed))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) GetSetValue(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) GetSetValue(rc *ctx.ReqCtx) {
 | 
				
			||||||
	ri, key := r.checkKey(rc)
 | 
						ri, key := r.checkKey(rc)
 | 
				
			||||||
	res, err := ri.Cli.SMembers(key).Result()
 | 
						res, err := ri.GetCmdable().SMembers(context.TODO(), key).Result()
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "获取set值失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "获取set值失败: %s")
 | 
				
			||||||
	rc.ResData = res
 | 
						rc.ResData = res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -204,13 +371,14 @@ func (r *Redis) SetSetValue(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
						ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
						biz.ErrIsNilAppendErr(r.ProjectApp.CanAccess(rc.LoginAccount.Id, ri.ProjectId), "%s")
 | 
				
			||||||
 | 
						cmd := ri.GetCmdable()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	key := keyvalue.Key
 | 
						key := keyvalue.Key
 | 
				
			||||||
	// 简单处理->先删除,后新增
 | 
						// 简单处理->先删除,后新增
 | 
				
			||||||
	ri.Cli.Del(key)
 | 
						cmd.Del(context.TODO(), key)
 | 
				
			||||||
	ri.Cli.SAdd(key, keyvalue.Value...)
 | 
						cmd.SAdd(context.TODO(), key, keyvalue.Value...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if keyvalue.Timed != -1 {
 | 
						if keyvalue.Timed != -1 {
 | 
				
			||||||
		ri.Cli.Expire(key, time.Second*time.Duration(keyvalue.Timed))
 | 
							cmd.Expire(context.TODO(), key, time.Second*time.Duration(keyvalue.Timed))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,7 @@ type SelectDataDbVO struct {
 | 
				
			|||||||
	Host       *string    `json:"host"`
 | 
						Host       *string    `json:"host"`
 | 
				
			||||||
	Port       *int       `json:"port"`
 | 
						Port       *int       `json:"port"`
 | 
				
			||||||
	Type       *string    `json:"type"`
 | 
						Type       *string    `json:"type"`
 | 
				
			||||||
 | 
						Params     *string    `json:"params"`
 | 
				
			||||||
	Database   *string    `json:"database"`
 | 
						Database   *string    `json:"database"`
 | 
				
			||||||
	Username   *string    `json:"username"`
 | 
						Username   *string    `json:"username"`
 | 
				
			||||||
	ProjectId  *int64     `json:"projectId"`
 | 
						ProjectId  *int64     `json:"projectId"`
 | 
				
			||||||
@@ -18,4 +19,7 @@ type SelectDataDbVO struct {
 | 
				
			|||||||
	CreateTime *time.Time `json:"createTime"`
 | 
						CreateTime *time.Time `json:"createTime"`
 | 
				
			||||||
	Creator    *string    `json:"creator"`
 | 
						Creator    *string    `json:"creator"`
 | 
				
			||||||
	CreatorId  *int64     `json:"creatorId"`
 | 
						CreatorId  *int64     `json:"creatorId"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnableSshTunnel    *int8   `json:"enableSshTunnel"`
 | 
				
			||||||
 | 
						SshTunnelMachineId *uint64 `json:"sshTunnelMachineId"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,21 +5,25 @@ import "time"
 | 
				
			|||||||
type Redis struct {
 | 
					type Redis struct {
 | 
				
			||||||
	Id *int64 `json:"id"`
 | 
						Id *int64 `json:"id"`
 | 
				
			||||||
	// Name       *string    `json:"name"`
 | 
						// Name       *string    `json:"name"`
 | 
				
			||||||
	Host       *string    `json:"host"`
 | 
						Host               *string    `json:"host"`
 | 
				
			||||||
	Db         int        `json:"db"`
 | 
						Db                 int        `json:"db"`
 | 
				
			||||||
	ProjectId  *int64     `json:"projectId"`
 | 
						ProjectId          *int64     `json:"projectId"`
 | 
				
			||||||
	Project    *string    `json:"project"`
 | 
						Project            *string    `json:"project"`
 | 
				
			||||||
	Env        *string    `json:"env"`
 | 
						Mode               *string    `json:"mode"`
 | 
				
			||||||
	EnvId      *int64     `json:"envId"`
 | 
						EnableSshTunnel    *int8      `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
	CreateTime *time.Time `json:"createTime"`
 | 
						SshTunnelMachineId *uint64    `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	Creator    *string    `json:"creator"`
 | 
						Remark             *string    `json:"remark"`
 | 
				
			||||||
	CreatorId  *int64     `json:"creatorId"`
 | 
						Env                *string    `json:"env"`
 | 
				
			||||||
 | 
						EnvId              *int64     `json:"envId"`
 | 
				
			||||||
 | 
						CreateTime         *time.Time `json:"createTime"`
 | 
				
			||||||
 | 
						Creator            *string    `json:"creator"`
 | 
				
			||||||
 | 
						CreatorId          *int64     `json:"creatorId"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Keys struct {
 | 
					type Keys struct {
 | 
				
			||||||
	Cursor uint64     `json:"cursor"`
 | 
						Cursor map[string]uint64 `json:"cursor"`
 | 
				
			||||||
	Keys   []*KeyInfo `json:"keys"`
 | 
						Keys   []*KeyInfo        `json:"keys"`
 | 
				
			||||||
	DbSize int64      `json:"dbSize"`
 | 
						DbSize int64             `json:"dbSize"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type KeyInfo struct {
 | 
					type KeyInfo struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,22 +15,25 @@ type AccountVO struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type MachineVO struct {
 | 
					type MachineVO struct {
 | 
				
			||||||
	//models.BaseModel
 | 
						//models.BaseModel
 | 
				
			||||||
	Id          *uint64    `json:"id"`
 | 
						Id                 *uint64    `json:"id"`
 | 
				
			||||||
	ProjectId   uint64     `json:"projectId"`
 | 
						ProjectId          uint64     `json:"projectId"`
 | 
				
			||||||
	ProjectName string     `json:"projectName"`
 | 
						ProjectName        string     `json:"projectName"`
 | 
				
			||||||
	Name        *string    `json:"name"`
 | 
						Name               *string    `json:"name"`
 | 
				
			||||||
	Username    *string    `json:"username"`
 | 
						Username           *string    `json:"username"`
 | 
				
			||||||
	Ip          *string    `json:"ip"`
 | 
						Ip                 *string    `json:"ip"`
 | 
				
			||||||
	Port        *int       `json:"port"`
 | 
						Port               *int       `json:"port"`
 | 
				
			||||||
	Status      *int8      `json:"status"`
 | 
						AuthMethod         *int8      `json:"authMethod"`
 | 
				
			||||||
	CreateTime  *time.Time `json:"createTime"`
 | 
						Status             *int8      `json:"status"`
 | 
				
			||||||
	Creator     *string    `json:"creator"`
 | 
						EnableSshTunnel    *int8      `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
	CreatorId   *int64     `json:"creatorId"`
 | 
						SshTunnelMachineId *uint64    `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	UpdateTime  *time.Time `json:"updateTime"`
 | 
						CreateTime         *time.Time `json:"createTime"`
 | 
				
			||||||
	Modifier    *string    `json:"modifier"`
 | 
						Creator            *string    `json:"creator"`
 | 
				
			||||||
	ModifierId  *int64     `json:"modifierId"`
 | 
						CreatorId          *int64     `json:"creatorId"`
 | 
				
			||||||
	HasCli      bool       `json:"hasCli" gorm:"-"`
 | 
						UpdateTime         *time.Time `json:"updateTime"`
 | 
				
			||||||
	Remark      *string    `json:"remark"`
 | 
						Modifier           *string    `json:"modifier"`
 | 
				
			||||||
 | 
						ModifierId         *int64     `json:"modifierId"`
 | 
				
			||||||
 | 
						HasCli             bool       `json:"hasCli" gorm:"-"`
 | 
				
			||||||
 | 
						Remark             *string    `json:"remark"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MachineScriptVO struct {
 | 
					type MachineScriptVO struct {
 | 
				
			||||||
@@ -56,6 +59,7 @@ type MachineFileInfo struct {
 | 
				
			|||||||
	Path string `json:"path"`
 | 
						Path string `json:"path"`
 | 
				
			||||||
	Size int64  `json:"size"`
 | 
						Size int64  `json:"size"`
 | 
				
			||||||
	Type string `json:"type"`
 | 
						Type string `json:"type"`
 | 
				
			||||||
 | 
						Mode string `json:"mode"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type RoleVO struct {
 | 
					type RoleVO struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,22 +1,29 @@
 | 
				
			|||||||
package application
 | 
					package application
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"database/sql"
 | 
						"database/sql"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/infrastructure/machine"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
	"mayfly-go/pkg/utils"
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/go-sql-driver/mysql"
 | 
				
			||||||
 | 
						"github.com/lib/pq"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Db interface {
 | 
					type Db interface {
 | 
				
			||||||
@@ -40,6 +47,9 @@ type Db interface {
 | 
				
			|||||||
	// @param id 数据库实例id
 | 
						// @param id 数据库实例id
 | 
				
			||||||
	// @param db 数据库
 | 
						// @param db 数据库
 | 
				
			||||||
	GetDbInstance(id uint64, db string) *DbInstance
 | 
						GetDbInstance(id uint64, db string) *DbInstance
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取数据库实例的所有数据库列表
 | 
				
			||||||
 | 
						GetDatabases(entity *entity.Db) []string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type dbAppImpl struct {
 | 
					type dbAppImpl struct {
 | 
				
			||||||
@@ -73,10 +83,11 @@ func (d *dbAppImpl) GetById(id uint64, cols ...string) *entity.Db {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
					func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			||||||
	// 默认tcp连接
 | 
						// 默认tcp连接
 | 
				
			||||||
	dbEntity.Network = "tcp"
 | 
						dbEntity.Network = dbEntity.GetNetwork()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 测试连接
 | 
						// 测试连接
 | 
				
			||||||
	if dbEntity.Password != "" {
 | 
						if dbEntity.Password != "" {
 | 
				
			||||||
		TestConnection(*dbEntity)
 | 
							TestConnection(dbEntity)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 查找是否存在该库
 | 
						// 查找是否存在该库
 | 
				
			||||||
@@ -86,6 +97,7 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			|||||||
	if dbEntity.Id == 0 {
 | 
						if dbEntity.Id == 0 {
 | 
				
			||||||
		biz.NotEmpty(dbEntity.Password, "密码不能为空")
 | 
							biz.NotEmpty(dbEntity.Password, "密码不能为空")
 | 
				
			||||||
		biz.IsTrue(err != nil, "该数据库实例已存在")
 | 
							biz.IsTrue(err != nil, "该数据库实例已存在")
 | 
				
			||||||
 | 
							dbEntity.PwdEncrypt()
 | 
				
			||||||
		d.dbRepo.Insert(dbEntity)
 | 
							d.dbRepo.Insert(dbEntity)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -100,6 +112,8 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var oldDbs []interface{}
 | 
						var oldDbs []interface{}
 | 
				
			||||||
	for _, v := range strings.Split(old.Database, " ") {
 | 
						for _, v := range strings.Split(old.Database, " ") {
 | 
				
			||||||
 | 
							// 关闭数据库连接
 | 
				
			||||||
 | 
							CloseDb(dbEntity.Id, v)
 | 
				
			||||||
		oldDbs = append(oldDbs, v)
 | 
							oldDbs = append(oldDbs, v)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -112,14 +126,12 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			|||||||
		return i1.(string) == i2.(string)
 | 
							return i1.(string) == i2.(string)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	for _, v := range delDb {
 | 
						for _, v := range delDb {
 | 
				
			||||||
		// 先关闭数据库连接
 | 
					 | 
				
			||||||
		CloseDb(dbEntity.Id, v.(string))
 | 
					 | 
				
			||||||
		// 删除该库关联的所有sql记录
 | 
							// 删除该库关联的所有sql记录
 | 
				
			||||||
		d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
 | 
							d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbEntity.PwdEncrypt()
 | 
				
			||||||
	d.dbRepo.Update(dbEntity)
 | 
						d.dbRepo.Update(dbEntity)
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *dbAppImpl) Delete(id uint64) {
 | 
					func (d *dbAppImpl) Delete(id uint64) {
 | 
				
			||||||
@@ -134,11 +146,35 @@ func (d *dbAppImpl) Delete(id uint64) {
 | 
				
			|||||||
	d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
 | 
						d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *dbAppImpl) GetDatabases(ed *entity.Db) []string {
 | 
				
			||||||
 | 
						ed.Network = ed.GetNetwork()
 | 
				
			||||||
 | 
						databases := make([]string, 0)
 | 
				
			||||||
 | 
						var dbConn *sql.DB
 | 
				
			||||||
 | 
						var metaDb string
 | 
				
			||||||
 | 
						var getDatabasesSql string
 | 
				
			||||||
 | 
						if ed.Type == entity.DbTypeMysql {
 | 
				
			||||||
 | 
							metaDb = "information_schema"
 | 
				
			||||||
 | 
							getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							metaDb = "postgres"
 | 
				
			||||||
 | 
							getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbConn, err := GetDbConn(ed, metaDb)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
 | 
				
			||||||
 | 
						defer dbConn.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
 | 
				
			||||||
 | 
						for _, re := range res {
 | 
				
			||||||
 | 
							databases = append(databases, re["dbname"].(string))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return databases
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var mutex sync.Mutex
 | 
					var mutex sync.Mutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
					func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			||||||
	mutex.Lock()
 | 
					 | 
				
			||||||
	defer mutex.Unlock()
 | 
					 | 
				
			||||||
	// Id不为0,则为需要缓存
 | 
						// Id不为0,则为需要缓存
 | 
				
			||||||
	needCache := id != 0
 | 
						needCache := id != 0
 | 
				
			||||||
	if needCache {
 | 
						if needCache {
 | 
				
			||||||
@@ -147,20 +183,23 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			|||||||
			return load.(*DbInstance)
 | 
								return load.(*DbInstance)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						mutex.Lock()
 | 
				
			||||||
 | 
						defer mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	d := da.GetById(id)
 | 
						d := da.GetById(id)
 | 
				
			||||||
 | 
						// 密码解密
 | 
				
			||||||
 | 
						d.PwdDecrypt()
 | 
				
			||||||
	biz.NotNil(d, "数据库信息不存在")
 | 
						biz.NotNil(d, "数据库信息不存在")
 | 
				
			||||||
	biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
 | 
						biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
 | 
				
			||||||
	global.Log.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 将数据库替换为要访问的数据库,原本数据库为空格拼接的所有库
 | 
						cacheKey := GetDbCacheKey(id, db)
 | 
				
			||||||
	d.Database = db
 | 
						dbi := &DbInstance{Id: cacheKey, Type: d.Type, ProjectId: d.ProjectId, sshTunnelMachineId: d.SshTunnelMachineId}
 | 
				
			||||||
	DB, err := sql.Open(d.Type, getDsn(d))
 | 
					
 | 
				
			||||||
	biz.ErrIsNil(err, fmt.Sprintf("Open %s failed, err:%v\n", d.Type, err))
 | 
						DB, err := GetDbConn(d, db)
 | 
				
			||||||
	perr := DB.Ping()
 | 
						if err != nil {
 | 
				
			||||||
	if perr != nil {
 | 
							dbi.Close()
 | 
				
			||||||
		global.Log.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
 | 
							global.Log.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
 | 
				
			||||||
		panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", perr.Error())))
 | 
							panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", err.Error())))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 最大连接周期,超过时间的连接就close
 | 
						// 最大连接周期,超过时间的连接就close
 | 
				
			||||||
@@ -170,8 +209,8 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			|||||||
	// 设置闲置连接数
 | 
						// 设置闲置连接数
 | 
				
			||||||
	DB.SetMaxIdleConns(1)
 | 
						DB.SetMaxIdleConns(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cacheKey := GetDbCacheKey(id, db)
 | 
						dbi.db = DB
 | 
				
			||||||
	dbi := &DbInstance{Id: cacheKey, Type: d.Type, ProjectId: d.ProjectId, db: DB}
 | 
						global.Log.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
 | 
				
			||||||
	if needCache {
 | 
						if needCache {
 | 
				
			||||||
		dbCache.Put(cacheKey, dbi)
 | 
							dbCache.Put(cacheKey, dbi)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -180,14 +219,30 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
//------------------------------------------------------------------------------
 | 
					//------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 客户端连接缓存,30分钟内没有访问则会被关闭, key为数据库实例id:数据库
 | 
					// 单次最大查询数据集
 | 
				
			||||||
var dbCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
 | 
					const Max_Rows = 2000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 客户端连接缓存,指定时间内没有访问则会被关闭, key为数据库实例id:数据库
 | 
				
			||||||
 | 
					var dbCache = cache.NewTimedCache(constant.DbConnExpireTime, 5*time.Second).
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
						OnEvicted(func(key interface{}, value interface{}) {
 | 
				
			||||||
		global.Log.Info(fmt.Sprintf("删除db连接缓存 id = %s", key))
 | 
							global.Log.Info(fmt.Sprintf("删除db连接缓存 id = %s", key))
 | 
				
			||||||
		value.(*DbInstance).Close()
 | 
							value.(*DbInstance).Close()
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						machine.AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
 | 
							// 遍历所有db连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := dbCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*DbInstance).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetDbCacheKey(dbId uint64, db string) string {
 | 
					func GetDbCacheKey(dbId uint64, db string) string {
 | 
				
			||||||
	return fmt.Sprintf("%d:%s", dbId, db)
 | 
						return fmt.Sprintf("%d:%s", dbId, db)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -199,40 +254,45 @@ func GetDbInstanceByCache(id string) *DbInstance {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestConnection(d entity.Db) {
 | 
					func TestConnection(d *entity.Db) {
 | 
				
			||||||
	// 验证第一个库是否可以连接即可
 | 
						// 验证第一个库是否可以连接即可
 | 
				
			||||||
	d.Database = strings.Split(d.Database, " ")[0]
 | 
						DB, err := GetDbConn(d, strings.Split(d.Database, " ")[0])
 | 
				
			||||||
	DB, err := sql.Open(d.Type, getDsn(&d))
 | 
						biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
 | 
				
			||||||
	biz.ErrIsNil(err, "Open %s failed, err:%v\n", d.Type, err)
 | 
					 | 
				
			||||||
	defer DB.Close()
 | 
						defer DB.Close()
 | 
				
			||||||
	perr := DB.Ping()
 | 
					 | 
				
			||||||
	biz.ErrIsNilAppendErr(perr, "数据库连接失败: %s")
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// db实例
 | 
					// 获取数据库连接
 | 
				
			||||||
type DbInstance struct {
 | 
					func GetDbConn(d *entity.Db, db string) (*sql.DB, error) {
 | 
				
			||||||
	Id        string
 | 
						// SSH Conect
 | 
				
			||||||
	Type      string
 | 
						if d.EnableSshTunnel == 1 && d.SshTunnelMachineId != 0 {
 | 
				
			||||||
	ProjectId uint64
 | 
							sshTunnelMachine := MachineApp.GetSshTunnelMachine(d.SshTunnelMachineId)
 | 
				
			||||||
	db        *sql.DB
 | 
							if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
 | 
								mysql.RegisterDialContext(d.Network, func(ctx context.Context, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
									return sshTunnelMachine.GetDialConn("tcp", addr)
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							} else if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
 | 
								_, err := pq.DialOpen(&PqSqlDialer{sshTunnelMachine: sshTunnelMachine}, getDsn(d, db))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(biz.NewBizErr(fmt.Sprintf("postgres隧道连接失败: %s", err.Error())))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						DB, err := sql.Open(d.Type, getDsn(d, db))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = DB.Ping()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							DB.Close()
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return DB, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 执行查询语句
 | 
					func SelectDataByDb(db *sql.DB, selectSql string) ([]string, []map[string]interface{}, error) {
 | 
				
			||||||
// 依次返回 列名数组,结果map,错误
 | 
						rows, err := db.Query(selectSql)
 | 
				
			||||||
func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interface{}, error) {
 | 
					 | 
				
			||||||
	execSql = strings.Trim(execSql, " ")
 | 
					 | 
				
			||||||
	isSelect := strings.HasPrefix(execSql, "SELECT") || strings.HasPrefix(execSql, "select")
 | 
					 | 
				
			||||||
	isShow := strings.HasPrefix(execSql, "show")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if !isSelect && !isShow {
 | 
					 | 
				
			||||||
		return nil, nil, errors.New("该sql非查询语句")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	// 没加limit,则默认限制50条
 | 
					 | 
				
			||||||
	if isSelect && !strings.Contains(execSql, "limit") && !strings.Contains(execSql, "LIMIT") {
 | 
					 | 
				
			||||||
		execSql = execSql + " LIMIT 50"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rows, err := d.db.Query(execSql)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, nil, err
 | 
							return nil, nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -258,7 +318,11 @@ func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interfac
 | 
				
			|||||||
	colNames := make([]string, 0)
 | 
						colNames := make([]string, 0)
 | 
				
			||||||
	// 是否第一次遍历,列名数组只需第一次遍历时加入
 | 
						// 是否第一次遍历,列名数组只需第一次遍历时加入
 | 
				
			||||||
	isFirst := true
 | 
						isFirst := true
 | 
				
			||||||
 | 
						rowNum := 0
 | 
				
			||||||
	for rows.Next() {
 | 
						for rows.Next() {
 | 
				
			||||||
 | 
							rowNum++
 | 
				
			||||||
 | 
							biz.IsTrue(rowNum <= Max_Rows, "结果集 > 2000, 请完善条件或分页信息")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 不Scan也会导致等待,该链接实际处于未工作的状态,然后也会导致连接数迅速达到最大
 | 
							// 不Scan也会导致等待,该链接实际处于未工作的状态,然后也会导致连接数迅速达到最大
 | 
				
			||||||
		err := rows.Scan(scans...)
 | 
							err := rows.Scan(scans...)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
@@ -272,14 +336,14 @@ func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interfac
 | 
				
			|||||||
			colName := colType.Name()
 | 
								colName := colType.Name()
 | 
				
			||||||
			// 字段类型名
 | 
								// 字段类型名
 | 
				
			||||||
			colScanType := colType.ScanType().Name()
 | 
								colScanType := colType.ScanType().Name()
 | 
				
			||||||
 | 
								// 如果是第一行,则将列名加入到列信息中,由于map是无序的,所有需要返回列名的有序数组
 | 
				
			||||||
			// 如果是密码字段,则脱敏显示
 | 
					 | 
				
			||||||
			if colName == "password" {
 | 
					 | 
				
			||||||
				v = []byte("******")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if isFirst {
 | 
								if isFirst {
 | 
				
			||||||
				colNames = append(colNames, colName)
 | 
									colNames = append(colNames, colName)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								if v == nil {
 | 
				
			||||||
 | 
									rowData[colName] = nil
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			// 这里把[]byte数据转成string
 | 
								// 这里把[]byte数据转成string
 | 
				
			||||||
			stringV := string(v)
 | 
								stringV := string(v)
 | 
				
			||||||
			if stringV == "" {
 | 
								if stringV == "" {
 | 
				
			||||||
@@ -318,6 +382,45 @@ func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interfac
 | 
				
			|||||||
	return colNames, result, nil
 | 
						return colNames, result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PqSqlDialer struct {
 | 
				
			||||||
 | 
						sshTunnelMachine *machine.SshTunnelMachine
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pd *PqSqlDialer) Dial(network, address string) (net.Conn, error) {
 | 
				
			||||||
 | 
						if sshConn, err := pd.sshTunnelMachine.GetDialConn("tcp", address); err == nil {
 | 
				
			||||||
 | 
							// 将ssh conn包装,否则redis内部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
 | 
				
			||||||
 | 
							return &utils.WrapSshConn{Conn: sshConn}, nil
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (pd *PqSqlDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
 | 
				
			||||||
 | 
						return pd.Dial(network, address)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// db实例
 | 
				
			||||||
 | 
					type DbInstance struct {
 | 
				
			||||||
 | 
						Id                 string
 | 
				
			||||||
 | 
						Type               string
 | 
				
			||||||
 | 
						ProjectId          uint64
 | 
				
			||||||
 | 
						db                 *sql.DB
 | 
				
			||||||
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 执行查询语句
 | 
				
			||||||
 | 
					// 依次返回 列名数组,结果map,错误
 | 
				
			||||||
 | 
					func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interface{}, error) {
 | 
				
			||||||
 | 
						execSql = strings.Trim(execSql, " ")
 | 
				
			||||||
 | 
						isSelect := strings.HasPrefix(execSql, "SELECT") || strings.HasPrefix(execSql, "select")
 | 
				
			||||||
 | 
						isShow := strings.HasPrefix(execSql, "show")
 | 
				
			||||||
 | 
						isExplain := strings.HasPrefix(execSql, "explain")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !isSelect && !isShow && !isExplain {
 | 
				
			||||||
 | 
							return nil, nil, errors.New("该sql非查询语句")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return SelectDataByDb(d.db, execSql)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 执行 update, insert, delete,建表等sql
 | 
					// 执行 update, insert, delete,建表等sql
 | 
				
			||||||
// 返回影响条数和错误
 | 
					// 返回影响条数和错误
 | 
				
			||||||
func (d *DbInstance) Exec(sql string) (int64, error) {
 | 
					func (d *DbInstance) Exec(sql string) (int64, error) {
 | 
				
			||||||
@@ -328,15 +431,45 @@ func (d *DbInstance) Exec(sql string) (int64, error) {
 | 
				
			|||||||
	return res.RowsAffected()
 | 
						return res.RowsAffected()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库元信息实现接口
 | 
				
			||||||
 | 
					func (di *DbInstance) GetMeta() DbMetadata {
 | 
				
			||||||
 | 
						dbType := di.Type
 | 
				
			||||||
 | 
						if dbType == entity.DbTypeMysql {
 | 
				
			||||||
 | 
							return &MysqlMetadata{di: di}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if dbType == entity.DbTypePostgres {
 | 
				
			||||||
 | 
							return &PgsqlMetadata{di: di}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 关闭连接
 | 
					// 关闭连接
 | 
				
			||||||
func (d *DbInstance) Close() {
 | 
					func (d *DbInstance) Close() {
 | 
				
			||||||
	d.db.Close()
 | 
						if d.db != nil {
 | 
				
			||||||
 | 
							if err := d.db.Close(); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭数据库实例[%s]连接失败: %s", d.Id, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							d.db = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取dataSourceName
 | 
					// 获取dataSourceName
 | 
				
			||||||
func getDsn(d *entity.Db) string {
 | 
					func getDsn(d *entity.Db, db string) string {
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						var dsn string
 | 
				
			||||||
		return fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, d.Database)
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
 | 
							dsn = fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, db)
 | 
				
			||||||
 | 
							if d.Params != "" {
 | 
				
			||||||
 | 
								dsn = fmt.Sprintf("%s&%s", dsn, d.Params)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return dsn
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
 | 
							dsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, db)
 | 
				
			||||||
 | 
							if d.Params != "" {
 | 
				
			||||||
 | 
								dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return dsn
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return ""
 | 
						return ""
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -346,49 +479,72 @@ func CloseDb(dbId uint64, db string) {
 | 
				
			|||||||
	dbCache.Delete(GetDbCacheKey(dbId, db))
 | 
						dbCache.Delete(GetDbCacheKey(dbId, db))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//-----------------------------------元数据-------------------------------------------
 | 
					// -----------------------------------元数据-------------------------------------------
 | 
				
			||||||
 | 
					// 数据库元信息接口(表、列等元信息)
 | 
				
			||||||
 | 
					type DbMetadata interface {
 | 
				
			||||||
 | 
						// 获取表基础元信息, 如表名等
 | 
				
			||||||
 | 
						GetTables() []map[string]interface{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取列元信息, 如列名等
 | 
				
			||||||
 | 
						GetColumns(tableNames ...string) []map[string]interface{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取表主键字段名,默认第一个字段
 | 
				
			||||||
 | 
						GetPrimaryKey(tablename string) string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取表信息,比GetTables获取更详细的表信息
 | 
				
			||||||
 | 
						GetTableInfos() []map[string]interface{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取表索引信息
 | 
				
			||||||
 | 
						GetTableIndex(tableName string) []map[string]interface{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取建表ddl
 | 
				
			||||||
 | 
						GetCreateTableDdl(tableName string) []map[string]interface{}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认每次查询列元信息数量
 | 
				
			||||||
 | 
					const DEFAULT_COLUMN_SIZE = 2000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ---------------------------------- mysql元数据 -----------------------------------
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	// mysql 表信息元数据
 | 
						// mysql 表信息元数据
 | 
				
			||||||
	MYSQL_TABLE_MA = `SELECT table_name tableName, engine, table_comment tableComment, 
 | 
						MYSQL_TABLE_MA = `SELECT table_name tableName, engine, table_comment tableComment, 
 | 
				
			||||||
	create_time createTime from information_schema.tables
 | 
						create_time createTime from information_schema.tables
 | 
				
			||||||
	WHERE table_schema = (SELECT database())`
 | 
						WHERE table_schema = (SELECT database()) LIMIT 2000`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// mysql 表信息
 | 
						// mysql 表信息
 | 
				
			||||||
	MYSQL_TABLE_INFO = `SELECT table_name tableName, table_comment tableComment, table_rows tableRows,
 | 
						MYSQL_TABLE_INFO = `SELECT table_name tableName, table_comment tableComment, table_rows tableRows,
 | 
				
			||||||
	data_length dataLength, index_length indexLength, create_time createTime 
 | 
						data_length dataLength, index_length indexLength, create_time createTime 
 | 
				
			||||||
	FROM information_schema.tables 
 | 
						FROM information_schema.tables 
 | 
				
			||||||
    WHERE table_schema = (SELECT database())`
 | 
					    WHERE table_schema = (SELECT database()) LIMIT 2000`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// mysql 索引信息
 | 
						// mysql 索引信息
 | 
				
			||||||
	MYSQL_INDEX_INFO = `SELECT index_name indexName, column_name columnName, index_type indexType,
 | 
						MYSQL_INDEX_INFO = `SELECT index_name indexName, column_name columnName, index_type indexType,
 | 
				
			||||||
	SEQ_IN_INDEX seqInIndex, INDEX_COMMENT indexComment
 | 
						SEQ_IN_INDEX seqInIndex, INDEX_COMMENT indexComment
 | 
				
			||||||
	FROM information_schema.STATISTICS 
 | 
						FROM information_schema.STATISTICS 
 | 
				
			||||||
    WHERE table_schema = (SELECT database()) AND table_name = '%s'`
 | 
					    WHERE table_schema = (SELECT database()) AND table_name = '%s' LIMIT 500`
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 默认每次查询列元信息数量
 | 
					 | 
				
			||||||
	DEFAULT_COLUMN_SIZE = 2000
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// mysql 列信息元数据
 | 
						// mysql 列信息元数据
 | 
				
			||||||
	MYSQL_COLOUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType,
 | 
						MYSQL_COLUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType,
 | 
				
			||||||
	column_comment columnComment, column_key columnKey, extra, is_nullable nullable from information_schema.columns
 | 
						column_comment columnComment, column_key columnKey, extra, is_nullable nullable from information_schema.columns
 | 
				
			||||||
	WHERE table_name in (%s) AND table_schema = (SELECT database()) ORDER BY tableName, ordinal_position limit %d, %d`
 | 
						WHERE table_name in (%s) AND table_schema = (SELECT database()) ORDER BY tableName, ordinal_position LIMIT %d, %d`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// mysql 列信息元数据总数
 | 
						// mysql 列信息元数据总数
 | 
				
			||||||
	MYSQL_COLOUMN_MA_COUNT = `SELECT COUNT(*) maNum from information_schema.columns
 | 
						MYSQL_COLOUMN_MA_COUNT = `SELECT COUNT(*) maNum from information_schema.columns
 | 
				
			||||||
	WHERE table_name in (%s) AND table_schema = (SELECT database())`
 | 
						WHERE table_name in (%s) AND table_schema = (SELECT database())`
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetTableMetedatas() []map[string]interface{} {
 | 
					type MysqlMetadata struct {
 | 
				
			||||||
	var sql string
 | 
						di *DbInstance
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
					}
 | 
				
			||||||
		sql = MYSQL_TABLE_MA
 | 
					
 | 
				
			||||||
	}
 | 
					// 获取表基础元信息, 如表名等
 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
					func (mm *MysqlMetadata) GetTables() []map[string]interface{} {
 | 
				
			||||||
 | 
						_, res, _ := mm.di.SelectData(MYSQL_TABLE_MA)
 | 
				
			||||||
	return res
 | 
						return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]interface{} {
 | 
					// 获取列元信息, 如列名等
 | 
				
			||||||
 | 
					func (mm *MysqlMetadata) GetColumns(tableNames ...string) []map[string]interface{} {
 | 
				
			||||||
	var sql, tableName string
 | 
						var sql, tableName string
 | 
				
			||||||
	for i := 0; i < len(tableNames); i++ {
 | 
						for i := 0; i < len(tableNames); i++ {
 | 
				
			||||||
		if i != 0 {
 | 
							if i != 0 {
 | 
				
			||||||
@@ -397,61 +553,162 @@ func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]inter
 | 
				
			|||||||
		tableName = tableName + "'" + tableNames[i] + "'"
 | 
							tableName = tableName + "'" + tableNames[i] + "'"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var countSqlTmp string
 | 
						pageNum := 1
 | 
				
			||||||
	var sqlTmp string
 | 
						// 如果大于一个表,则统计列数并分页获取
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if len(tableNames) > 1 {
 | 
				
			||||||
		countSqlTmp = MYSQL_COLOUMN_MA_COUNT
 | 
							countSql := fmt.Sprintf(MYSQL_COLOUMN_MA_COUNT, tableName)
 | 
				
			||||||
		sqlTmp = MYSQL_COLOUMN_MA
 | 
							_, countRes, _ := mm.di.SelectData(countSql)
 | 
				
			||||||
	}
 | 
							// 查询出所有列信息总数,手动分页获取所有数据
 | 
				
			||||||
 | 
							maCount := int(countRes[0]["maNum"].(int64))
 | 
				
			||||||
	countSql := fmt.Sprintf(countSqlTmp, tableName)
 | 
							// 计算需要查询的页数
 | 
				
			||||||
	_, countRes, _ := d.SelectData(countSql)
 | 
							pageNum = maCount / DEFAULT_COLUMN_SIZE
 | 
				
			||||||
	// 查询出所有列信息总数,手动分页获取所有数据
 | 
							if maCount%DEFAULT_COLUMN_SIZE > 0 {
 | 
				
			||||||
	maCount := int(countRes[0]["maNum"].(int64))
 | 
								pageNum++
 | 
				
			||||||
	// 计算需要查询的页数
 | 
							}
 | 
				
			||||||
	pageNum := maCount / DEFAULT_COLUMN_SIZE
 | 
					 | 
				
			||||||
	if maCount%DEFAULT_COLUMN_SIZE > 0 {
 | 
					 | 
				
			||||||
		pageNum++
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res := make([]map[string]interface{}, 0)
 | 
						res := make([]map[string]interface{}, 0)
 | 
				
			||||||
	for index := 0; index < pageNum; index++ {
 | 
						for index := 0; index < pageNum; index++ {
 | 
				
			||||||
		sql = fmt.Sprintf(sqlTmp, tableName, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
 | 
							sql = fmt.Sprintf(MYSQL_COLUMN_MA, tableName, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
 | 
				
			||||||
		_, result, err := d.SelectData(sql)
 | 
							_, result, err := mm.di.SelectData(sql)
 | 
				
			||||||
		biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
 | 
							biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
 | 
				
			||||||
		res = append(res, result...)
 | 
							res = append(res, result...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return res
 | 
						return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取表的主键,目前默认第一列为主键
 | 
					// 获取表主键字段名,默认第一个字段
 | 
				
			||||||
func (d *DbInstance) GetPrimaryKey(tablename string) string {
 | 
					func (mm *MysqlMetadata) GetPrimaryKey(tablename string) string {
 | 
				
			||||||
	return d.GetColumnMetadatas(tablename)[0]["columnName"].(string)
 | 
						return mm.GetColumns(tablename)[0]["columnName"].(string)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetTableInfos() []map[string]interface{} {
 | 
					// 获取表信息,比GetTableMetedatas获取更详细的表信息
 | 
				
			||||||
	var sql string
 | 
					func (mm *MysqlMetadata) GetTableInfos() []map[string]interface{} {
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						_, res, _ := mm.di.SelectData(MYSQL_TABLE_INFO)
 | 
				
			||||||
		sql = MYSQL_TABLE_INFO
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
					 | 
				
			||||||
	return res
 | 
						return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetTableIndex(tableName string) []map[string]interface{} {
 | 
					// 获取表索引信息
 | 
				
			||||||
	var sql string
 | 
					func (mm *MysqlMetadata) GetTableIndex(tableName string) []map[string]interface{} {
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						_, res, _ := mm.di.SelectData(MYSQL_INDEX_INFO)
 | 
				
			||||||
		sql = fmt.Sprintf(MYSQL_INDEX_INFO, tableName)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
					 | 
				
			||||||
	return res
 | 
						return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetCreateTableDdl(tableName string) []map[string]interface{} {
 | 
					// 获取建表ddl
 | 
				
			||||||
	var sql string
 | 
					func (mm *MysqlMetadata) GetCreateTableDdl(tableName string) []map[string]interface{} {
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						_, res, _ := mm.di.SelectData(fmt.Sprintf("show create table %s ", tableName))
 | 
				
			||||||
		sql = fmt.Sprintf("show create table %s ", tableName)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
					 | 
				
			||||||
	return res
 | 
						return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ---------------------------------- pgsql元数据 -----------------------------------
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						// postgres 表信息元数据
 | 
				
			||||||
 | 
						PGSQL_TABLE_MA = `SELECT obj_description(c.oid) AS "tableComment", c.relname AS "tableName" FROM pg_class c 
 | 
				
			||||||
 | 
						JOIN pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = (select current_schema()) AND c.reltype > 0`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						PGSQL_TABLE_INFO = `SELECT obj_description(c.oid) AS "tableComment", c.relname AS "tableName" FROM pg_class c 
 | 
				
			||||||
 | 
						JOIN pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = (select current_schema()) AND c.reltype > 0`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						PGSQL_INDEX_INFO = `SELECT indexname AS "indexName", indexdef AS "indexComment"
 | 
				
			||||||
 | 
						FROM pg_indexes WHERE schemaname =  (select current_schema()) AND tablename = '%s'`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						PGSQL_COLUMN_MA = `SELECT
 | 
				
			||||||
 | 
							C.relname AS "tableName",
 | 
				
			||||||
 | 
							A.attname AS "columnName",
 | 
				
			||||||
 | 
							concat_ws ( '', t.typname, SUBSTRING ( format_type ( a.atttypid, a.atttypmod ) FROM '\(.*\)' ) ) AS "columnType",
 | 
				
			||||||
 | 
							d.description AS "columnComment" 
 | 
				
			||||||
 | 
						FROM
 | 
				
			||||||
 | 
							pg_attribute a LEFT JOIN pg_description d ON d.objoid = a.attrelid 
 | 
				
			||||||
 | 
							AND d.objsubid = A.attnum
 | 
				
			||||||
 | 
							LEFT JOIN pg_class c ON A.attrelid = c.oid
 | 
				
			||||||
 | 
							LEFT JOIN pg_namespace pn ON c.relnamespace = pn.oid
 | 
				
			||||||
 | 
							LEFT JOIN pg_type t ON a.atttypid = t.oid 
 | 
				
			||||||
 | 
						WHERE
 | 
				
			||||||
 | 
							A.attnum >= 0 
 | 
				
			||||||
 | 
							AND pn.nspname = (select current_schema())
 | 
				
			||||||
 | 
							AND C.relname in (%s)
 | 
				
			||||||
 | 
						ORDER BY
 | 
				
			||||||
 | 
							C.relname DESC,
 | 
				
			||||||
 | 
							A.attnum ASC
 | 
				
			||||||
 | 
						OFFSET %d LIMIT %d	
 | 
				
			||||||
 | 
						`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						PGSQL_COLUMN_MA_COUNT = `SELECT COUNT(*) "maNum"
 | 
				
			||||||
 | 
						FROM
 | 
				
			||||||
 | 
							pg_attribute a LEFT JOIN pg_description d ON d.objoid = a.attrelid 
 | 
				
			||||||
 | 
							AND d.objsubid = A.attnum
 | 
				
			||||||
 | 
							LEFT JOIN pg_class c ON A.attrelid = c.oid
 | 
				
			||||||
 | 
							LEFT JOIN pg_namespace pn ON c.relnamespace = pn.oid
 | 
				
			||||||
 | 
							LEFT JOIN pg_type t ON a.atttypid = t.oid 
 | 
				
			||||||
 | 
						WHERE
 | 
				
			||||||
 | 
							A.attnum >= 0 
 | 
				
			||||||
 | 
							AND pn.nspname = (select current_schema())
 | 
				
			||||||
 | 
							AND C.relname in (%s)
 | 
				
			||||||
 | 
						`
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PgsqlMetadata struct {
 | 
				
			||||||
 | 
						di *DbInstance
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取表基础元信息, 如表名等
 | 
				
			||||||
 | 
					func (pm *PgsqlMetadata) GetTables() []map[string]interface{} {
 | 
				
			||||||
 | 
						_, res, _ := pm.di.SelectData(PGSQL_TABLE_MA)
 | 
				
			||||||
 | 
						return res
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取列元信息, 如列名等
 | 
				
			||||||
 | 
					func (pm *PgsqlMetadata) GetColumns(tableNames ...string) []map[string]interface{} {
 | 
				
			||||||
 | 
						var sql, tableName string
 | 
				
			||||||
 | 
						for i := 0; i < len(tableNames); i++ {
 | 
				
			||||||
 | 
							if i != 0 {
 | 
				
			||||||
 | 
								tableName = tableName + ", "
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							tableName = tableName + "'" + tableNames[i] + "'"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pageNum := 1
 | 
				
			||||||
 | 
						// 如果大于一个表,则统计列数并分页获取
 | 
				
			||||||
 | 
						if len(tableNames) > 1 {
 | 
				
			||||||
 | 
							countSql := fmt.Sprintf(PGSQL_COLUMN_MA_COUNT, tableName)
 | 
				
			||||||
 | 
							_, countRes, _ := pm.di.SelectData(countSql)
 | 
				
			||||||
 | 
							// 查询出所有列信息总数,手动分页获取所有数据
 | 
				
			||||||
 | 
							maCount := int(countRes[0]["maNum"].(int64))
 | 
				
			||||||
 | 
							// 计算需要查询的页数
 | 
				
			||||||
 | 
							pageNum = maCount / DEFAULT_COLUMN_SIZE
 | 
				
			||||||
 | 
							if maCount%DEFAULT_COLUMN_SIZE > 0 {
 | 
				
			||||||
 | 
								pageNum++
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res := make([]map[string]interface{}, 0)
 | 
				
			||||||
 | 
						for index := 0; index < pageNum; index++ {
 | 
				
			||||||
 | 
							sql = fmt.Sprintf(PGSQL_COLUMN_MA, tableName, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
 | 
				
			||||||
 | 
							_, result, err := pm.di.SelectData(sql)
 | 
				
			||||||
 | 
							biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
 | 
				
			||||||
 | 
							res = append(res, result...)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return res
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取表主键字段名,默认第一个字段
 | 
				
			||||||
 | 
					func (pm *PgsqlMetadata) GetPrimaryKey(tablename string) string {
 | 
				
			||||||
 | 
						return pm.GetColumns(tablename)[0]["columnName"].(string)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取表信息,比GetTables获取更详细的表信息
 | 
				
			||||||
 | 
					func (pm *PgsqlMetadata) GetTableInfos() []map[string]interface{} {
 | 
				
			||||||
 | 
						_, res, _ := pm.di.SelectData(PGSQL_TABLE_INFO)
 | 
				
			||||||
 | 
						return res
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取表索引信息
 | 
				
			||||||
 | 
					func (pm *PgsqlMetadata) GetTableIndex(tableName string) []map[string]interface{} {
 | 
				
			||||||
 | 
						_, res, _ := pm.di.SelectData(PGSQL_INDEX_INFO)
 | 
				
			||||||
 | 
						return res
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取建表ddl
 | 
				
			||||||
 | 
					func (mm *PgsqlMetadata) GetCreateTableDdl(tableName string) []map[string]interface{} {
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ package application
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
@@ -89,7 +90,7 @@ func doUpdate(update *sqlparser.Update, dbInstance *DbInstance, dbSqlExec *entit
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 获取表主键列名,排除使用别名
 | 
						// 获取表主键列名,排除使用别名
 | 
				
			||||||
	primaryKey := dbInstance.GetPrimaryKey(tableName)
 | 
						primaryKey := dbInstance.GetMeta().GetPrimaryKey(tableName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	updateColumnsAndPrimaryKey := strings.Join(updateColumns, ",") + "," + primaryKey
 | 
						updateColumnsAndPrimaryKey := strings.Join(updateColumns, ",") + "," + primaryKey
 | 
				
			||||||
	// 查询要更新字段数据的旧值,以及主键值
 | 
						// 查询要更新字段数据的旧值,以及主键值
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,6 +32,9 @@ type Machine interface {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// 获取机器连接
 | 
						// 获取机器连接
 | 
				
			||||||
	GetCli(id uint64) *machine.Cli
 | 
						GetCli(id uint64) *machine.Cli
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取ssh隧道机器连接
 | 
				
			||||||
 | 
						GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type machineAppImpl struct {
 | 
					type machineAppImpl struct {
 | 
				
			||||||
@@ -53,7 +56,7 @@ func (m *machineAppImpl) Count(condition *entity.Machine) int64 {
 | 
				
			|||||||
func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
					func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
				
			||||||
	// ’修改机器信息且密码不为空‘ or ‘新增’需要测试是否可连接
 | 
						// ’修改机器信息且密码不为空‘ or ‘新增’需要测试是否可连接
 | 
				
			||||||
	if (me.Id != 0 && me.Password != "") || me.Id == 0 {
 | 
						if (me.Id != 0 && me.Password != "") || me.Id == 0 {
 | 
				
			||||||
		biz.ErrIsNilAppendErr(machine.TestConn(me), "该机器无法连接: %s")
 | 
							biz.ErrIsNilAppendErr(machine.TestConn(*me, func(u uint64) *entity.Machine { return m.GetById(u) }), "该机器无法连接: %s")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
 | 
						oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
 | 
				
			||||||
@@ -66,11 +69,13 @@ func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		// 关闭连接
 | 
							// 关闭连接
 | 
				
			||||||
		machine.DeleteCli(me.Id)
 | 
							machine.DeleteCli(me.Id)
 | 
				
			||||||
 | 
							me.PwdEncrypt()
 | 
				
			||||||
		m.machineRepo.UpdateById(me)
 | 
							m.machineRepo.UpdateById(me)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		biz.IsTrue(err != nil, "该机器信息已存在")
 | 
							biz.IsTrue(err != nil, "该机器信息已存在")
 | 
				
			||||||
		// 新增机器,默认启用状态
 | 
							// 新增机器,默认启用状态
 | 
				
			||||||
		me.Status = entity.MachineStatusEnable
 | 
							me.Status = entity.MachineStatusEnable
 | 
				
			||||||
 | 
							me.PwdEncrypt()
 | 
				
			||||||
		m.machineRepo.Create(me)
 | 
							m.machineRepo.Create(me)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -120,9 +125,21 @@ func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
 | 
				
			|||||||
func (m *machineAppImpl) GetCli(id uint64) *machine.Cli {
 | 
					func (m *machineAppImpl) GetCli(id uint64) *machine.Cli {
 | 
				
			||||||
	cli, err := machine.GetCli(id, func(machineId uint64) *entity.Machine {
 | 
						cli, err := machine.GetCli(id, func(machineId uint64) *entity.Machine {
 | 
				
			||||||
		machine := m.GetById(machineId)
 | 
							machine := m.GetById(machineId)
 | 
				
			||||||
 | 
							machine.PwdDecrypt()
 | 
				
			||||||
		biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
 | 
							biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
 | 
				
			||||||
		return machine
 | 
							return machine
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
 | 
						biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
 | 
				
			||||||
	return cli
 | 
						return cli
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *machineAppImpl) GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine {
 | 
				
			||||||
 | 
						sshTunnel, err := machine.GetSshTunnelMachine(id, func(machineId uint64) *entity.Machine {
 | 
				
			||||||
 | 
							machine := m.GetById(machineId)
 | 
				
			||||||
 | 
							machine.PwdDecrypt()
 | 
				
			||||||
 | 
							biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
 | 
				
			||||||
 | 
							return machine
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "获取ssh隧道连接失败: %s")
 | 
				
			||||||
 | 
						return sshTunnel
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package application
 | 
					package application
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"io/fs"
 | 
						"io/fs"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
@@ -30,6 +31,12 @@ type MachineFile interface {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	/**  sftp 相关操作 **/
 | 
						/**  sftp 相关操作 **/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 创建目录
 | 
				
			||||||
 | 
						MkDir(fid uint64, path string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 创建文件
 | 
				
			||||||
 | 
						CreateFile(fid uint64, path string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 读取目录
 | 
						// 读取目录
 | 
				
			||||||
	ReadDir(fid uint64, path string) []fs.FileInfo
 | 
						ReadDir(fid uint64, path string) []fs.FileInfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -100,6 +107,25 @@ func (m *machineFileAppImpl) ReadDir(fid uint64, path string) []fs.FileInfo {
 | 
				
			|||||||
	return fis
 | 
						return fis
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *machineFileAppImpl) MkDir(fid uint64, path string) {
 | 
				
			||||||
 | 
						path, machineId := m.checkAndReturnPathMid(fid, path)
 | 
				
			||||||
 | 
						if !strings.HasSuffix(path, "/") {
 | 
				
			||||||
 | 
							path = path + "/"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sftpCli := m.getSftpCli(machineId)
 | 
				
			||||||
 | 
						err := sftpCli.Mkdir(path)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "创建目录失败: %s")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *machineFileAppImpl) CreateFile(fid uint64, path string) {
 | 
				
			||||||
 | 
						path, machineId := m.checkAndReturnPathMid(fid, path)
 | 
				
			||||||
 | 
						sftpCli := m.getSftpCli(machineId)
 | 
				
			||||||
 | 
						file, err := sftpCli.Create(path)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
 | 
				
			||||||
 | 
						defer file.Close()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) *sftp.File {
 | 
					func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) *sftp.File {
 | 
				
			||||||
	path, machineId := m.checkAndReturnPathMid(fileId, path)
 | 
						path, machineId := m.checkAndReturnPathMid(fileId, path)
 | 
				
			||||||
	sftpCli := m.getSftpCli(machineId)
 | 
						sftpCli := m.getSftpCli(machineId)
 | 
				
			||||||
@@ -148,6 +174,11 @@ func (m *machineFileAppImpl) RemoveFile(fileId uint64, path string) {
 | 
				
			|||||||
	fi, _ := file.Stat()
 | 
						fi, _ := file.Stat()
 | 
				
			||||||
	if fi.IsDir() {
 | 
						if fi.IsDir() {
 | 
				
			||||||
		err = sftpCli.RemoveDirectory(path)
 | 
							err = sftpCli.RemoveDirectory(path)
 | 
				
			||||||
 | 
							// 如果文件夹有内容会删除失败,则使用rm -rf命令删除
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								MachineApp.GetCli(machineId).Run(fmt.Sprintf("rm -rf %s", path))
 | 
				
			||||||
 | 
								err = nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		err = sftpCli.Remove(path)
 | 
							err = sftpCli.Remove(path)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,13 +2,17 @@ package application
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/infrastructure/machine"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo"
 | 
						"go.mongodb.org/mongo-driver/mongo"
 | 
				
			||||||
@@ -80,36 +84,50 @@ func (d *mongoAppImpl) Save(m *entity.Mongo) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *mongoAppImpl) GetMongoCli(id uint64) *mongo.Client {
 | 
					func (d *mongoAppImpl) GetMongoCli(id uint64) *mongo.Client {
 | 
				
			||||||
	cli, err := GetMongoCli(id, func(u uint64) string {
 | 
						mongoInstance, err := GetMongoInstance(id, func(u uint64) *entity.Mongo {
 | 
				
			||||||
		mongo := d.GetById(id)
 | 
							mongo := d.GetById(u)
 | 
				
			||||||
		biz.NotNil(mongo, "mongo信息不存在")
 | 
							biz.NotNil(mongo, "mongo信息不存在")
 | 
				
			||||||
		return mongo.Uri
 | 
							return mongo
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "连接mongo失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "连接mongo失败: %s")
 | 
				
			||||||
	return cli
 | 
						return mongoInstance.Cli
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// -----------------------------------------------------------
 | 
					// -----------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//mongo客户端连接缓存,30分钟内没有访问则会被关闭
 | 
					//mongo客户端连接缓存,指定时间内没有访问则会被关闭
 | 
				
			||||||
var mongoCliCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
 | 
					var mongoCliCache = cache.NewTimedCache(constant.MongoConnExpireTime, 5*time.Second).
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
						OnEvicted(func(key interface{}, value interface{}) {
 | 
				
			||||||
		global.Log.Info("关闭mongo连接: id = ", key)
 | 
							global.Log.Info("删除mongo连接缓存: id = ", key)
 | 
				
			||||||
		value.(*mongo.Client).Disconnect(context.TODO())
 | 
							value.(*MongoInstance).Close()
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetMongoCli(mongoId uint64, getMongoUri func(uint64) string) (*mongo.Client, error) {
 | 
					func init() {
 | 
				
			||||||
	cli, err := mongoCliCache.ComputeIfAbsent(mongoId, func(key interface{}) (interface{}, error) {
 | 
						machine.AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
		c, err := connect(getMongoUri(mongoId))
 | 
							// 遍历所有mongo连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := mongoCliCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*MongoInstance).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取mongo的连接实例
 | 
				
			||||||
 | 
					func GetMongoInstance(mongoId uint64, getMongoEntity func(uint64) *entity.Mongo) (*MongoInstance, error) {
 | 
				
			||||||
 | 
						mi, err := mongoCliCache.ComputeIfAbsent(mongoId, func(_ interface{}) (interface{}, error) {
 | 
				
			||||||
 | 
							c, err := connect(getMongoEntity(mongoId))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return c, nil
 | 
							return c, nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cli != nil {
 | 
						if mi != nil {
 | 
				
			||||||
		return cli.(*mongo.Client), err
 | 
							return mi.(*MongoInstance), err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil, err
 | 
						return nil, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -118,16 +136,61 @@ func DeleteMongoCache(mongoId uint64) {
 | 
				
			|||||||
	mongoCliCache.Delete(mongoId)
 | 
						mongoCliCache.Delete(mongoId)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MongoInstance struct {
 | 
				
			||||||
 | 
						Id                 uint64
 | 
				
			||||||
 | 
						ProjectId          uint64
 | 
				
			||||||
 | 
						Cli                *mongo.Client
 | 
				
			||||||
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (mi *MongoInstance) Close() {
 | 
				
			||||||
 | 
						if mi.Cli != nil {
 | 
				
			||||||
 | 
							if err := mi.Cli.Disconnect(context.Background()); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭mongo实例[%d]连接失败: %s", mi.Id, err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mi.Cli = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 连接mongo,并返回client
 | 
					// 连接mongo,并返回client
 | 
				
			||||||
func connect(uri string) (*mongo.Client, error) {
 | 
					func connect(me *entity.Mongo) (*MongoInstance, error) {
 | 
				
			||||||
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
 | 
						ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 | 
				
			||||||
	defer cancel()
 | 
						defer cancel()
 | 
				
			||||||
	client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetMaxPoolSize(2))
 | 
					
 | 
				
			||||||
 | 
						mongoInstance := &MongoInstance{Id: me.Id, ProjectId: me.ProjectId}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mongoOptions := options.Client().ApplyURI(me.Uri).
 | 
				
			||||||
 | 
							SetMaxPoolSize(1)
 | 
				
			||||||
 | 
						// 启用ssh隧道则连接隧道机器
 | 
				
			||||||
 | 
						if me.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							mongoInstance.sshTunnelMachineId = me.SshTunnelMachineId
 | 
				
			||||||
 | 
							mongoOptions.SetDialer(&MongoSshDialer{machineId: me.SshTunnelMachineId})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						client, err := mongo.Connect(ctx, mongoOptions)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							mongoInstance.Close()
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err = client.Ping(context.TODO(), nil); err != nil {
 | 
						if err = client.Ping(context.TODO(), nil); err != nil {
 | 
				
			||||||
 | 
							mongoInstance.Close()
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						global.Log.Infof("连接mongo: %s", me.Uri)
 | 
				
			||||||
 | 
						mongoInstance.Cli = client
 | 
				
			||||||
 | 
						return mongoInstance, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MongoSshDialer struct {
 | 
				
			||||||
 | 
						machineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (sd *MongoSshDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
 | 
				
			||||||
 | 
						if sshConn, err := MachineApp.GetSshTunnelMachine(sd.machineId).GetDialConn(network, address); err == nil {
 | 
				
			||||||
 | 
							// 将ssh conn包装,否则内部部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
 | 
				
			||||||
 | 
							return &utils.WrapSshConn{Conn: sshConn}, nil
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return client, err
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,17 +1,23 @@
 | 
				
			|||||||
package application
 | 
					package application
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/infrastructure/machine"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-redis/redis"
 | 
						"github.com/go-redis/redis/v8"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Redis interface {
 | 
					type Redis interface {
 | 
				
			||||||
@@ -63,7 +69,10 @@ func (r *redisAppImpl) GetRedisBy(condition *entity.Redis, cols ...string) error
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *redisAppImpl) Save(re *entity.Redis) {
 | 
					func (r *redisAppImpl) Save(re *entity.Redis) {
 | 
				
			||||||
	TestRedisConnection(re)
 | 
						// ’修改信息且密码不为空‘ or ‘新增’需要测试是否可连接
 | 
				
			||||||
 | 
						if (re.Id != 0 && re.Password != "") || re.Id == 0 {
 | 
				
			||||||
 | 
							TestRedisConnection(re)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 查找是否存在该库
 | 
						// 查找是否存在该库
 | 
				
			||||||
	oldRedis := &entity.Redis{Host: re.Host, Db: re.Db}
 | 
						oldRedis := &entity.Redis{Host: re.Host, Db: re.Db}
 | 
				
			||||||
@@ -71,6 +80,7 @@ func (r *redisAppImpl) Save(re *entity.Redis) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if re.Id == 0 {
 | 
						if re.Id == 0 {
 | 
				
			||||||
		biz.IsTrue(err != nil, "该库已存在")
 | 
							biz.IsTrue(err != nil, "该库已存在")
 | 
				
			||||||
 | 
							re.PwdEncrypt()
 | 
				
			||||||
		r.redisRepo.Insert(re)
 | 
							r.redisRepo.Insert(re)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// 如果存在该库,则校验修改的库是否为该库
 | 
							// 如果存在该库,则校验修改的库是否为该库
 | 
				
			||||||
@@ -79,6 +89,7 @@ func (r *redisAppImpl) Save(re *entity.Redis) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		// 先关闭数据库连接
 | 
							// 先关闭数据库连接
 | 
				
			||||||
		CloseRedis(re.Id)
 | 
							CloseRedis(re.Id)
 | 
				
			||||||
 | 
							re.PwdEncrypt()
 | 
				
			||||||
		r.redisRepo.Update(re)
 | 
							r.redisRepo.Update(re)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -101,61 +112,200 @@ func (r *redisAppImpl) GetRedisInstance(id uint64) *RedisInstance {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	// 缓存不存在,则回调获取redis信息
 | 
						// 缓存不存在,则回调获取redis信息
 | 
				
			||||||
	re := r.GetById(id)
 | 
						re := r.GetById(id)
 | 
				
			||||||
 | 
						re.PwdDecrypt()
 | 
				
			||||||
	biz.NotNil(re, "redis信息不存在")
 | 
						biz.NotNil(re, "redis信息不存在")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						redisMode := re.Mode
 | 
				
			||||||
 | 
						var ri *RedisInstance
 | 
				
			||||||
 | 
						if redisMode == "" || redisMode == entity.RedisModeStandalone {
 | 
				
			||||||
 | 
							ri = getRedisCient(re)
 | 
				
			||||||
 | 
							// 测试连接
 | 
				
			||||||
 | 
							_, e := ri.Cli.Ping(context.Background()).Result()
 | 
				
			||||||
 | 
							if e != nil {
 | 
				
			||||||
 | 
								ri.Close()
 | 
				
			||||||
 | 
								panic(biz.NewBizErr(fmt.Sprintf("redis连接失败: %s", e.Error())))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if redisMode == entity.RedisModeCluster {
 | 
				
			||||||
 | 
							ri = getRedisClusterClient(re)
 | 
				
			||||||
 | 
							// 测试连接
 | 
				
			||||||
 | 
							_, e := ri.ClusterCli.Ping(context.Background()).Result()
 | 
				
			||||||
 | 
							if e != nil {
 | 
				
			||||||
 | 
								ri.Close()
 | 
				
			||||||
 | 
								panic(biz.NewBizErr(fmt.Sprintf("redis集群连接失败: %s", e.Error())))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if redisMode == entity.RedisModeSentinel {
 | 
				
			||||||
 | 
							ri = getRedisSentinelCient(re)
 | 
				
			||||||
 | 
							// 测试连接
 | 
				
			||||||
 | 
							_, e := ri.Cli.Ping(context.Background()).Result()
 | 
				
			||||||
 | 
							if e != nil {
 | 
				
			||||||
 | 
								ri.Close()
 | 
				
			||||||
 | 
								panic(biz.NewBizErr(fmt.Sprintf("redis sentinel连接失败: %s", e.Error())))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	global.Log.Infof("连接redis: %s", re.Host)
 | 
						global.Log.Infof("连接redis: %s", re.Host)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	rcli := redis.NewClient(&redis.Options{
 | 
					 | 
				
			||||||
		Addr:     re.Host,
 | 
					 | 
				
			||||||
		Password: re.Password, // no password set
 | 
					 | 
				
			||||||
		DB:       re.Db,       // use default DB
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	// 测试连接
 | 
					 | 
				
			||||||
	_, e := rcli.Ping().Result()
 | 
					 | 
				
			||||||
	biz.ErrIsNilAppendErr(e, "redis连接失败: %s")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	ri := &RedisInstance{Id: id, ProjectId: re.ProjectId, Cli: rcli}
 | 
					 | 
				
			||||||
	if needCache {
 | 
						if needCache {
 | 
				
			||||||
		redisCache.Put(re.Id, ri)
 | 
							redisCache.Put(re.Id, ri)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return ri
 | 
						return ri
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getRedisCient(re *entity.Redis) *RedisInstance {
 | 
				
			||||||
 | 
						ri := &RedisInstance{Id: re.Id, ProjectId: re.ProjectId, Mode: re.Mode}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						redisOptions := &redis.Options{
 | 
				
			||||||
 | 
							Addr:         re.Host,
 | 
				
			||||||
 | 
							Password:     re.Password, // no password set
 | 
				
			||||||
 | 
							DB:           re.Db,       // use default DB
 | 
				
			||||||
 | 
							DialTimeout:  8 * time.Second,
 | 
				
			||||||
 | 
							ReadTimeout:  -1, // Disable timeouts, because SSH does not support deadlines.
 | 
				
			||||||
 | 
							WriteTimeout: -1,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if re.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							ri.sshTunnelMachineId = re.SshTunnelMachineId
 | 
				
			||||||
 | 
							redisOptions.Dialer = getRedisDialer(re.SshTunnelMachineId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ri.Cli = redis.NewClient(redisOptions)
 | 
				
			||||||
 | 
						return ri
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getRedisClusterClient(re *entity.Redis) *RedisInstance {
 | 
				
			||||||
 | 
						ri := &RedisInstance{Id: re.Id, ProjectId: re.ProjectId, Mode: re.Mode}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						redisClusterOptions := &redis.ClusterOptions{
 | 
				
			||||||
 | 
							Addrs:       strings.Split(re.Host, ","),
 | 
				
			||||||
 | 
							Password:    re.Password,
 | 
				
			||||||
 | 
							DialTimeout: 8 * time.Second,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if re.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							ri.sshTunnelMachineId = re.SshTunnelMachineId
 | 
				
			||||||
 | 
							redisClusterOptions.Dialer = getRedisDialer(re.SshTunnelMachineId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ri.ClusterCli = redis.NewClusterClient(redisClusterOptions)
 | 
				
			||||||
 | 
						return ri
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getRedisSentinelCient(re *entity.Redis) *RedisInstance {
 | 
				
			||||||
 | 
						ri := &RedisInstance{Id: re.Id, ProjectId: re.ProjectId, Mode: re.Mode}
 | 
				
			||||||
 | 
						// sentinel模式host为 masterName=host:port,host:port
 | 
				
			||||||
 | 
						masterNameAndHosts := strings.Split(re.Host, "=")
 | 
				
			||||||
 | 
						sentinelOptions := &redis.FailoverOptions{
 | 
				
			||||||
 | 
							MasterName:    masterNameAndHosts[0],
 | 
				
			||||||
 | 
							SentinelAddrs: strings.Split(masterNameAndHosts[1], ","),
 | 
				
			||||||
 | 
							Password:      re.Password, // no password set
 | 
				
			||||||
 | 
							DB:            re.Db,       // use default DB
 | 
				
			||||||
 | 
							DialTimeout:   8 * time.Second,
 | 
				
			||||||
 | 
							ReadTimeout:   -1, // Disable timeouts, because SSH does not support deadlines.
 | 
				
			||||||
 | 
							WriteTimeout:  -1,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if re.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							ri.sshTunnelMachineId = re.SshTunnelMachineId
 | 
				
			||||||
 | 
							sentinelOptions.Dialer = getRedisDialer(re.SshTunnelMachineId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ri.Cli = redis.NewFailoverClient(sentinelOptions)
 | 
				
			||||||
 | 
						return ri
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getRedisDialer(machineId uint64) func(ctx context.Context, network, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
						sshTunnel := MachineApp.GetSshTunnelMachine(machineId)
 | 
				
			||||||
 | 
						return func(_ context.Context, network, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
							if sshConn, err := sshTunnel.GetDialConn(network, addr); err == nil {
 | 
				
			||||||
 | 
								// 将ssh conn包装,否则redis内部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
 | 
				
			||||||
 | 
								return &utils.WrapSshConn{Conn: sshConn}, nil
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//------------------------------------------------------------------------------
 | 
					//------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// redis客户端连接缓存,30分钟内没有访问则会被关闭
 | 
					// redis客户端连接缓存,指定时间内没有访问则会被关闭
 | 
				
			||||||
var redisCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
 | 
					var redisCache = cache.NewTimedCache(constant.RedisConnExpireTime, 5*time.Second).
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
						OnEvicted(func(key interface{}, value interface{}) {
 | 
				
			||||||
		global.Log.Info(fmt.Sprintf("删除redis连接缓存 id = %d", key))
 | 
							global.Log.Info(fmt.Sprintf("删除redis连接缓存 id = %d", key))
 | 
				
			||||||
		value.(*RedisInstance).Cli.Close()
 | 
							value.(*RedisInstance).Close()
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// redis实例
 | 
					 | 
				
			||||||
type RedisInstance struct {
 | 
					 | 
				
			||||||
	Id        uint64
 | 
					 | 
				
			||||||
	ProjectId uint64
 | 
					 | 
				
			||||||
	Cli       *redis.Client
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 移除redis连接缓存并关闭redis连接
 | 
					// 移除redis连接缓存并关闭redis连接
 | 
				
			||||||
func CloseRedis(id uint64) {
 | 
					func CloseRedis(id uint64) {
 | 
				
			||||||
	redisCache.Delete(id)
 | 
						redisCache.Delete(id)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRedisConnection(re *entity.Redis) {
 | 
					func init() {
 | 
				
			||||||
	rcli := redis.NewClient(&redis.Options{
 | 
						machine.AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
		Addr:     re.Host,
 | 
							// 遍历所有redis连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
		Password: re.Password, // no password set
 | 
							items := redisCache.Items()
 | 
				
			||||||
		DB:       re.Db,       // use default DB
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*RedisInstance).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	defer rcli.Close()
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestRedisConnection(re *entity.Redis) {
 | 
				
			||||||
 | 
						var cmd redis.Cmdable
 | 
				
			||||||
 | 
						if re.Mode == "" || re.Mode == entity.RedisModeStandalone {
 | 
				
			||||||
 | 
							rcli := getRedisCient(re)
 | 
				
			||||||
 | 
							defer rcli.Close()
 | 
				
			||||||
 | 
							cmd = rcli.Cli
 | 
				
			||||||
 | 
						} else if re.Mode == entity.RedisModeCluster {
 | 
				
			||||||
 | 
							ccli := getRedisClusterClient(re)
 | 
				
			||||||
 | 
							defer ccli.Close()
 | 
				
			||||||
 | 
							cmd = ccli.ClusterCli
 | 
				
			||||||
 | 
						} else if re.Mode == entity.RedisModeSentinel {
 | 
				
			||||||
 | 
							rcli := getRedisSentinelCient(re)
 | 
				
			||||||
 | 
							defer rcli.Close()
 | 
				
			||||||
 | 
							cmd = rcli.Cli
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 测试连接
 | 
						// 测试连接
 | 
				
			||||||
	_, e := rcli.Ping().Result()
 | 
						_, e := cmd.Ping(context.Background()).Result()
 | 
				
			||||||
	biz.ErrIsNilAppendErr(e, "Redis连接失败: %s")
 | 
						biz.ErrIsNilAppendErr(e, "Redis连接失败: %s")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// redis实例
 | 
				
			||||||
 | 
					type RedisInstance struct {
 | 
				
			||||||
 | 
						Id                 uint64
 | 
				
			||||||
 | 
						ProjectId          uint64
 | 
				
			||||||
 | 
						Mode               string
 | 
				
			||||||
 | 
						Cli                *redis.Client
 | 
				
			||||||
 | 
						ClusterCli         *redis.ClusterClient
 | 
				
			||||||
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取命令执行接口的具体实现
 | 
				
			||||||
 | 
					func (r *RedisInstance) GetCmdable() redis.Cmdable {
 | 
				
			||||||
 | 
						redisMode := r.Mode
 | 
				
			||||||
 | 
						if redisMode == "" || redisMode == entity.RedisModeStandalone || r.Mode == entity.RedisModeSentinel {
 | 
				
			||||||
 | 
							return r.Cli
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if redisMode == entity.RedisModeCluster {
 | 
				
			||||||
 | 
							return r.ClusterCli
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *RedisInstance) Scan(cursor uint64, match string, count int64) ([]string, uint64) {
 | 
					func (r *RedisInstance) Scan(cursor uint64, match string, count int64) ([]string, uint64) {
 | 
				
			||||||
	keys, newcursor, err := r.Cli.Scan(cursor, match, count).Result()
 | 
						keys, newcursor, err := r.GetCmdable().Scan(context.Background(), cursor, match, count).Result()
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "scan失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "scan失败: %s")
 | 
				
			||||||
	return keys, newcursor
 | 
						return keys, newcursor
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *RedisInstance) Close() {
 | 
				
			||||||
 | 
						if r.Mode == entity.RedisModeStandalone || r.Mode == entity.RedisModeSentinel {
 | 
				
			||||||
 | 
							if err := r.Cli.Close(); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭redis单机实例[%d]连接失败: %s", r.Id, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.Cli = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if r.Mode == entity.RedisModeCluster {
 | 
				
			||||||
 | 
							if err := r.ClusterCli.Close(); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭redis集群实例[%d]连接失败: %s", r.Id, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.ClusterCli = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
package entity
 | 
					package entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/utils"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -15,8 +17,40 @@ type Db struct {
 | 
				
			|||||||
	Username  string `orm:"column(username)" json:"username"`
 | 
						Username  string `orm:"column(username)" json:"username"`
 | 
				
			||||||
	Password  string `orm:"column(password)" json:"-"`
 | 
						Password  string `orm:"column(password)" json:"-"`
 | 
				
			||||||
	Database  string `orm:"column(database)" json:"database"`
 | 
						Database  string `orm:"column(database)" json:"database"`
 | 
				
			||||||
 | 
						Params    string `json:"params"`
 | 
				
			||||||
	ProjectId uint64
 | 
						ProjectId uint64
 | 
				
			||||||
	Project   string
 | 
						Project   string
 | 
				
			||||||
	EnvId     uint64
 | 
						EnvId     uint64
 | 
				
			||||||
	Env       string
 | 
						Env       string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"`        // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial
 | 
				
			||||||
 | 
					func (d *Db) GetNetwork() string {
 | 
				
			||||||
 | 
						network := d.Network
 | 
				
			||||||
 | 
						if d.EnableSshTunnel == 0 || d.EnableSshTunnel == -1 {
 | 
				
			||||||
 | 
							if network == "" {
 | 
				
			||||||
 | 
								return "tcp"
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								return network
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *Db) PwdEncrypt() {
 | 
				
			||||||
 | 
						// 密码替换为加密后的密码
 | 
				
			||||||
 | 
						d.Password = utils.PwdAesEncrypt(d.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *Db) PwdDecrypt() {
 | 
				
			||||||
 | 
						// 密码替换为解密后的密码
 | 
				
			||||||
 | 
						d.Password = utils.PwdAesDecrypt(d.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						DbTypeMysql    = "mysql"
 | 
				
			||||||
 | 
						DbTypePostgres = "postgres"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,23 +1,39 @@
 | 
				
			|||||||
package entity
 | 
					package entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/utils"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Machine struct {
 | 
					type Machine struct {
 | 
				
			||||||
	model.Model
 | 
						model.Model
 | 
				
			||||||
	ProjectId   uint64 `json:"projectId"`
 | 
						ProjectId          uint64 `json:"projectId"`
 | 
				
			||||||
	ProjectName string `json:"projectName"`
 | 
						ProjectName        string `json:"projectName"`
 | 
				
			||||||
	Name        string `json:"name"`
 | 
						Name               string `json:"name"`
 | 
				
			||||||
	Ip          string `json:"ip"`       // IP地址
 | 
						Ip                 string `json:"ip"`         // IP地址
 | 
				
			||||||
	Username    string `json:"username"` // 用户名
 | 
						Username           string `json:"username"`   // 用户名
 | 
				
			||||||
	Password    string `json:"-"`
 | 
						AuthMethod         int8   `json:"authMethod"` // 授权认证方式
 | 
				
			||||||
	Port        int    `json:"port"`   // 端口号
 | 
						Password           string `json:"-"`
 | 
				
			||||||
	Status      int8   `json:"status"` // 状态 1:启用;2:停用
 | 
						Port               int    `json:"port"`               // 端口号
 | 
				
			||||||
	Remark      string `json:"remark"` // 备注
 | 
						Status             int8   `json:"status"`             // 状态 1:启用;2:停用
 | 
				
			||||||
 | 
						Remark             string `json:"remark"`             // 备注
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	MachineStatusEnable  int8 = 1  // 启用状态
 | 
						MachineStatusEnable        int8 = 1  // 启用状态
 | 
				
			||||||
	MachineStatusDisable int8 = -1 // 禁用状态
 | 
						MachineStatusDisable       int8 = -1 // 禁用状态
 | 
				
			||||||
 | 
						MachineAuthMethodPassword  int8 = 1  // 密码登录
 | 
				
			||||||
 | 
						MachineAuthMethodPublicKey int8 = 2  // 公钥免密登录
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *Machine) PwdEncrypt() {
 | 
				
			||||||
 | 
						// 密码替换为加密后的密码
 | 
				
			||||||
 | 
						m.Password = utils.PwdAesEncrypt(m.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *Machine) PwdDecrypt() {
 | 
				
			||||||
 | 
						// 密码替换为解密后的密码
 | 
				
			||||||
 | 
						m.Password = utils.PwdAesDecrypt(m.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,10 +5,12 @@ import "mayfly-go/pkg/model"
 | 
				
			|||||||
type Mongo struct {
 | 
					type Mongo struct {
 | 
				
			||||||
	model.Model
 | 
						model.Model
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Name      string `orm:"column(name)" json:"name"`
 | 
						Name               string `orm:"column(name)" json:"name"`
 | 
				
			||||||
	Uri       string `orm:"column(uri)" json:"uri"`
 | 
						Uri                string `orm:"column(uri)" json:"uri"`
 | 
				
			||||||
	ProjectId uint64 `json:"projectId"`
 | 
						EnableSshTunnel    int8   `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"`        // 是否启用ssh隧道
 | 
				
			||||||
	Project   string `json:"project"`
 | 
						SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	EnvId     uint64 `json:"envId"`
 | 
						ProjectId          uint64 `json:"projectId"`
 | 
				
			||||||
	Env       string `json:"env"`
 | 
						Project            string `json:"project"`
 | 
				
			||||||
 | 
						EnvId              uint64 `json:"envId"`
 | 
				
			||||||
 | 
						Env                string `json:"env"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,17 +1,38 @@
 | 
				
			|||||||
package entity
 | 
					package entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/utils"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Redis struct {
 | 
					type Redis struct {
 | 
				
			||||||
	model.Model
 | 
						model.Model
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Host      string `orm:"column(host)" json:"host"`
 | 
						Host               string `orm:"column(host)" json:"host"`
 | 
				
			||||||
	Password  string `orm:"column(password)" json:"-"`
 | 
						Mode               string `json:"mode"`
 | 
				
			||||||
	Db        int    `orm:"column(database)" json:"db"`
 | 
						Password           string `orm:"column(password)" json:"-"`
 | 
				
			||||||
	ProjectId uint64
 | 
						Db                 int    `orm:"column(database)" json:"db"`
 | 
				
			||||||
	Project   string
 | 
						EnableSshTunnel    int8   `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"`        // 是否启用ssh隧道
 | 
				
			||||||
	EnvId     uint64
 | 
						SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	Env       string
 | 
						Remark             string
 | 
				
			||||||
 | 
						ProjectId          uint64
 | 
				
			||||||
 | 
						Project            string
 | 
				
			||||||
 | 
						EnvId              uint64
 | 
				
			||||||
 | 
						Env                string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						RedisModeStandalone = "standalone"
 | 
				
			||||||
 | 
						RedisModeCluster    = "cluster"
 | 
				
			||||||
 | 
						RedisModeSentinel   = "sentinel"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) PwdEncrypt() {
 | 
				
			||||||
 | 
						// 密码替换为加密后的密码
 | 
				
			||||||
 | 
						r.Password = utils.PwdAesEncrypt(r.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) PwdDecrypt() {
 | 
				
			||||||
 | 
						// 密码替换为解密后的密码
 | 
				
			||||||
 | 
						r.Password = utils.PwdAesDecrypt(r.Password)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ package machine
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
@@ -18,62 +19,12 @@ import (
 | 
				
			|||||||
// 客户端信息
 | 
					// 客户端信息
 | 
				
			||||||
type Cli struct {
 | 
					type Cli struct {
 | 
				
			||||||
	machine *entity.Machine
 | 
						machine *entity.Machine
 | 
				
			||||||
	// ssh客户端
 | 
					 | 
				
			||||||
	client *ssh.Client
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sftpClient *sftp.Client
 | 
						client     *ssh.Client  // ssh客户端
 | 
				
			||||||
}
 | 
						sftpClient *sftp.Client // sftp客户端
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 机器客户端连接缓存,45分钟内没有访问则会被关闭
 | 
						enableSshTunnel    int8
 | 
				
			||||||
var cliCache = cache.NewTimedCache(45*time.Minute, 5*time.Second).
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
					 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
					 | 
				
			||||||
		value.(*Cli).Close()
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 是否存在指定id的客户端连接
 | 
					 | 
				
			||||||
func HasCli(machineId uint64) bool {
 | 
					 | 
				
			||||||
	if _, ok := cliCache.Get(machineId); ok {
 | 
					 | 
				
			||||||
		return true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return false
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 删除指定机器客户端,并关闭客户端连接
 | 
					 | 
				
			||||||
func DeleteCli(id uint64) {
 | 
					 | 
				
			||||||
	cliCache.Delete(id)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建
 | 
					 | 
				
			||||||
func GetCli(machineId uint64, getMachine func(uint64) *entity.Machine) (*Cli, error) {
 | 
					 | 
				
			||||||
	cli, err := cliCache.ComputeIfAbsent(machineId, func(key interface{}) (interface{}, error) {
 | 
					 | 
				
			||||||
		c, err := newClient(getMachine(machineId))
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return nil, err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		return c, nil
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if cli != nil {
 | 
					 | 
				
			||||||
		return cli.(*Cli), err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return nil, err
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//根据机器信息创建客户端对象
 | 
					 | 
				
			||||||
func newClient(machine *entity.Machine) (*Cli, error) {
 | 
					 | 
				
			||||||
	if machine == nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("机器不存在")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	global.Log.Infof("[%s]机器连接:%s:%d", machine.Name, machine.Ip, machine.Port)
 | 
					 | 
				
			||||||
	cli := new(Cli)
 | 
					 | 
				
			||||||
	cli.machine = machine
 | 
					 | 
				
			||||||
	err := cli.connect()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return cli, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//连接
 | 
					//连接
 | 
				
			||||||
@@ -83,16 +34,7 @@ func (c *Cli) connect() error {
 | 
				
			|||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	m := c.machine
 | 
						m := c.machine
 | 
				
			||||||
	config := ssh.ClientConfig{
 | 
						sshClient, err := GetSshClient(m)
 | 
				
			||||||
		User: m.Username,
 | 
					 | 
				
			||||||
		Auth: []ssh.AuthMethod{ssh.Password(m.Password)},
 | 
					 | 
				
			||||||
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 | 
					 | 
				
			||||||
			return nil
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		Timeout: 5 * time.Second,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
 | 
					 | 
				
			||||||
	sshClient, err := ssh.Dial("tcp", addr, &config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -100,26 +42,7 @@ func (c *Cli) connect() error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 测试连接
 | 
					// 关闭client并从缓存中移除,如果使用隧道则也关闭
 | 
				
			||||||
func TestConn(m *entity.Machine) error {
 | 
					 | 
				
			||||||
	config := ssh.ClientConfig{
 | 
					 | 
				
			||||||
		User: m.Username,
 | 
					 | 
				
			||||||
		Auth: []ssh.AuthMethod{ssh.Password(m.Password)},
 | 
					 | 
				
			||||||
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 | 
					 | 
				
			||||||
			return nil
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		Timeout: 5 * time.Second,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
 | 
					 | 
				
			||||||
	sshClient, err := ssh.Dial("tcp", addr, &config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer sshClient.Close()
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 关闭client和并从缓存中移除
 | 
					 | 
				
			||||||
func (c *Cli) Close() {
 | 
					func (c *Cli) Close() {
 | 
				
			||||||
	m := c.machine
 | 
						m := c.machine
 | 
				
			||||||
	global.Log.Info(fmt.Sprintf("关闭机器客户端连接-> id: %d, name: %s, ip: %s", m.Id, m.Name, m.Ip))
 | 
						global.Log.Info(fmt.Sprintf("关闭机器客户端连接-> id: %d, name: %s, ip: %s", m.Id, m.Name, m.Ip))
 | 
				
			||||||
@@ -131,6 +54,9 @@ func (c *Cli) Close() {
 | 
				
			|||||||
		c.sftpClient.Close()
 | 
							c.sftpClient.Close()
 | 
				
			||||||
		c.sftpClient = nil
 | 
							c.sftpClient = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if c.enableSshTunnel == 1 {
 | 
				
			||||||
 | 
							CloseSshTunnelMachine(c.sshTunnelMachineId, c.machine.Id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取sftp client
 | 
					// 获取sftp client
 | 
				
			||||||
@@ -184,3 +110,144 @@ func (c *Cli) Run(shell string) (*string, error) {
 | 
				
			|||||||
func (c *Cli) GetMachine() *entity.Machine {
 | 
					func (c *Cli) GetMachine() *entity.Machine {
 | 
				
			||||||
	return c.machine
 | 
						return c.machine
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 机器客户端连接缓存,指定时间内没有访问则会被关闭
 | 
				
			||||||
 | 
					var cliCache = cache.NewTimedCache(constant.MachineConnExpireTime, 5*time.Second).
 | 
				
			||||||
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
 | 
						OnEvicted(func(_, value interface{}) {
 | 
				
			||||||
 | 
							value.(*Cli).Close()
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
 | 
							// 遍历所有机器连接实例,若存在机器连接实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := cliCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*Cli).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 是否存在指定id的客户端连接
 | 
				
			||||||
 | 
					func HasCli(machineId uint64) bool {
 | 
				
			||||||
 | 
						if _, ok := cliCache.Get(machineId); ok {
 | 
				
			||||||
 | 
							return true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return false
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 删除指定机器客户端,并关闭客户端连接
 | 
				
			||||||
 | 
					func DeleteCli(id uint64) {
 | 
				
			||||||
 | 
						cliCache.Delete(id)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建
 | 
				
			||||||
 | 
					func GetCli(machineId uint64, getMachine func(uint64) *entity.Machine) (*Cli, error) {
 | 
				
			||||||
 | 
						cli, err := cliCache.ComputeIfAbsent(machineId, func(_ interface{}) (interface{}, error) {
 | 
				
			||||||
 | 
							me := getMachine(machineId)
 | 
				
			||||||
 | 
							err := IfUseSshTunnelChangeIpPort(me, getMachine)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, fmt.Errorf("ssh隧道连接失败: %s", err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							c, err := newClient(me)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								CloseSshTunnelMachine(me.SshTunnelMachineId, me.Id)
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							c.enableSshTunnel = me.EnableSshTunnel
 | 
				
			||||||
 | 
							c.sshTunnelMachineId = me.SshTunnelMachineId
 | 
				
			||||||
 | 
							return c, nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if cli != nil {
 | 
				
			||||||
 | 
							return cli.(*Cli), err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 测试连接,使用传值的方式,而非引用。因为如果使用了ssh隧道,则ip和端口会变为本地映射地址与端口
 | 
				
			||||||
 | 
					func TestConn(me entity.Machine, getSshTunnelMachine func(uint64) *entity.Machine) error {
 | 
				
			||||||
 | 
						originId := me.Id
 | 
				
			||||||
 | 
						if originId == 0 {
 | 
				
			||||||
 | 
							// 随机设置一个ip,如果使用了隧道则用于临时保存隧道
 | 
				
			||||||
 | 
							me.Id = uint64(time.Now().Nanosecond())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := IfUseSshTunnelChangeIpPort(&me, getSshTunnelMachine)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "ssh隧道连接失败: %s")
 | 
				
			||||||
 | 
						if me.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							defer CloseSshTunnelMachine(me.SshTunnelMachineId, me.Id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sshClient, err := GetSshClient(&me)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer sshClient.Close()
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 如果使用了ssh隧道,则修改机器ip port为暴露的ip port
 | 
				
			||||||
 | 
					func IfUseSshTunnelChangeIpPort(me *entity.Machine, getMachine func(uint64) *entity.Machine) error {
 | 
				
			||||||
 | 
						if me.EnableSshTunnel != 1 {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sshTunnelMachine, err := GetSshTunnelMachine(me.SshTunnelMachineId, func(u uint64) *entity.Machine {
 | 
				
			||||||
 | 
							return getMachine(u)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						exposeIp, exposePort, err := sshTunnelMachine.OpenSshTunnel(me.Id, me.Ip, me.Port)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 修改机器ip地址
 | 
				
			||||||
 | 
						me.Ip = exposeIp
 | 
				
			||||||
 | 
						me.Port = exposePort
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetSshClient(m *entity.Machine) (*ssh.Client, error) {
 | 
				
			||||||
 | 
						config := ssh.ClientConfig{
 | 
				
			||||||
 | 
							User: m.Username,
 | 
				
			||||||
 | 
							HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							Timeout: 5 * time.Second,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if m.AuthMethod == entity.MachineAuthMethodPassword {
 | 
				
			||||||
 | 
							config.Auth = []ssh.AuthMethod{ssh.Password(m.Password)}
 | 
				
			||||||
 | 
						} else if m.AuthMethod == entity.MachineAuthMethodPublicKey {
 | 
				
			||||||
 | 
							if signer, err := ssh.ParsePrivateKey([]byte(m.Password)); err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								config.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
 | 
				
			||||||
 | 
						sshClient, err := ssh.Dial("tcp", addr, &config)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return sshClient, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//根据机器信息创建客户端对象
 | 
				
			||||||
 | 
					func newClient(machine *entity.Machine) (*Cli, error) {
 | 
				
			||||||
 | 
						if machine == nil {
 | 
				
			||||||
 | 
							return nil, errors.New("机器不存在")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						global.Log.Infof("[%s]机器连接:%s:%d", machine.Name, machine.Ip, machine.Port)
 | 
				
			||||||
 | 
						cli := new(Cli)
 | 
				
			||||||
 | 
						cli.machine = machine
 | 
				
			||||||
 | 
						err := cli.connect()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return cli, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,19 +0,0 @@
 | 
				
			|||||||
package machine
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const StatsShell = `
 | 
					 | 
				
			||||||
cat /proc/uptime
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
/bin/hostname -f
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
cat /proc/loadavg
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
cat /proc/meminfo
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
df -B1
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
/sbin/ip -o addr
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
/bin/cat /proc/net/dev
 | 
					 | 
				
			||||||
echo '-----'
 | 
					 | 
				
			||||||
top -b -n 1 | grep Cpu
 | 
					 | 
				
			||||||
`
 | 
					 | 
				
			||||||
							
								
								
									
										242
									
								
								server/internal/devops/infrastructure/machine/sshtunnel.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										242
									
								
								server/internal/devops/infrastructure/machine/sshtunnel.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,242 @@
 | 
				
			|||||||
 | 
					package machine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/scheduler"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/crypto/ssh"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						sshTunnelMachines map[uint64]*SshTunnelMachine = make(map[uint64]*SshTunnelMachine)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mutex sync.Mutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 所有检测ssh隧道机器是否被使用的函数
 | 
				
			||||||
 | 
						checkSshTunnelMachineHasUseFuncs []CheckSshTunnelMachineHasUseFunc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 是否开启检查ssh隧道机器是否被使用,只有使用到了隧道机器才启用
 | 
				
			||||||
 | 
						startCheckSshTunnelHasUse bool = false
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 检查ssh隧道机器是否有被使用
 | 
				
			||||||
 | 
					type CheckSshTunnelMachineHasUseFunc func(uint64) bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func startCheckUse() {
 | 
				
			||||||
 | 
						global.Log.Info("开启定时检测ssh隧道机器是否还有被使用")
 | 
				
			||||||
 | 
						// 每十分钟检查一次隧道机器是否还有被使用
 | 
				
			||||||
 | 
						scheduler.AddFun("@every 10m", func() {
 | 
				
			||||||
 | 
							if !mutex.TryLock() {
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							defer mutex.Unlock()
 | 
				
			||||||
 | 
							// 遍历隧道机器,都未被使用将会被关闭
 | 
				
			||||||
 | 
							for mid, sshTunnelMachine := range sshTunnelMachines {
 | 
				
			||||||
 | 
								global.Log.Debugf("开始定时检查ssh隧道机器[%d]是否还有被使用...", mid)
 | 
				
			||||||
 | 
								hasUse := false
 | 
				
			||||||
 | 
								for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs {
 | 
				
			||||||
 | 
									// 如果一个在使用则返回不关闭,不继续后续检查
 | 
				
			||||||
 | 
									if checkUseFunc(mid) {
 | 
				
			||||||
 | 
										hasUse = true
 | 
				
			||||||
 | 
										break
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if !hasUse {
 | 
				
			||||||
 | 
									// 都未被使用,则关闭
 | 
				
			||||||
 | 
									sshTunnelMachine.Close()
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 添加ssh隧道机器检测是否使用函数
 | 
				
			||||||
 | 
					func AddCheckSshTunnelMachineUseFunc(checkFunc CheckSshTunnelMachineHasUseFunc) {
 | 
				
			||||||
 | 
						if checkSshTunnelMachineHasUseFuncs == nil {
 | 
				
			||||||
 | 
							checkSshTunnelMachineHasUseFuncs = make([]CheckSshTunnelMachineHasUseFunc, 0)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						checkSshTunnelMachineHasUseFuncs = append(checkSshTunnelMachineHasUseFuncs, checkFunc)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ssh隧道机器
 | 
				
			||||||
 | 
					type SshTunnelMachine struct {
 | 
				
			||||||
 | 
						machineId uint64 // 隧道机器id
 | 
				
			||||||
 | 
						SshClient *ssh.Client
 | 
				
			||||||
 | 
						mutex     sync.Mutex
 | 
				
			||||||
 | 
						tunnels   map[uint64]*Tunnel // 机器id -> 隧道
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (stm *SshTunnelMachine) OpenSshTunnel(id uint64, ip string, port int) (exposedIp string, exposedPort int, err error) {
 | 
				
			||||||
 | 
						stm.mutex.Lock()
 | 
				
			||||||
 | 
						defer stm.mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						localPort, err := utils.GetAvailablePort()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hostname, err := os.Hostname()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// debug
 | 
				
			||||||
 | 
						//hostname = "0.0.0.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						localAddr := fmt.Sprintf("%s:%d", hostname, localPort)
 | 
				
			||||||
 | 
						listener, err := net.Listen("tcp", localAddr)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tunnel := &Tunnel{
 | 
				
			||||||
 | 
							id:         id,
 | 
				
			||||||
 | 
							machineId:  stm.machineId,
 | 
				
			||||||
 | 
							localHost:  hostname,
 | 
				
			||||||
 | 
							localPort:  localPort,
 | 
				
			||||||
 | 
							remoteHost: ip,
 | 
				
			||||||
 | 
							remotePort: port,
 | 
				
			||||||
 | 
							listener:   listener,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						go tunnel.Open(stm.SshClient)
 | 
				
			||||||
 | 
						stm.tunnels[tunnel.id] = tunnel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return tunnel.localHost, tunnel.localPort, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (st *SshTunnelMachine) GetDialConn(network string, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
						st.mutex.Lock()
 | 
				
			||||||
 | 
						defer st.mutex.Unlock()
 | 
				
			||||||
 | 
						return st.SshClient.Dial(network, addr)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (stm *SshTunnelMachine) Close() {
 | 
				
			||||||
 | 
						stm.mutex.Lock()
 | 
				
			||||||
 | 
						defer stm.mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for id, tunnel := range stm.tunnels {
 | 
				
			||||||
 | 
							if tunnel != nil {
 | 
				
			||||||
 | 
								tunnel.Close()
 | 
				
			||||||
 | 
								delete(stm.tunnels, id)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if stm.SshClient != nil {
 | 
				
			||||||
 | 
							global.Log.Infof("ssh隧道机器[%d]未被使用, 关闭隧道...", stm.machineId)
 | 
				
			||||||
 | 
							err := stm.SshClient.Close()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭ssh隧道机器[%d]发生错误: %s", stm.machineId, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						delete(sshTunnelMachines, stm.machineId)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取ssh隧道机器,方便统一管理充当ssh隧道的机器,避免创建多个ssh client
 | 
				
			||||||
 | 
					func GetSshTunnelMachine(machineId uint64, getMachine func(uint64) *entity.Machine) (*SshTunnelMachine, error) {
 | 
				
			||||||
 | 
						sshTunnelMachine := sshTunnelMachines[machineId]
 | 
				
			||||||
 | 
						if sshTunnelMachine != nil {
 | 
				
			||||||
 | 
							return sshTunnelMachine, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mutex.Lock()
 | 
				
			||||||
 | 
						defer mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						me := getMachine(machineId)
 | 
				
			||||||
 | 
						sshClient, err := GetSshClient(me)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sshTunnelMachine = &SshTunnelMachine{SshClient: sshClient, machineId: machineId, tunnels: map[uint64]*Tunnel{}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						global.Log.Infof("初次连接ssh隧道机器[%d][%s:%d]", machineId, me.Ip, me.Port)
 | 
				
			||||||
 | 
						sshTunnelMachines[machineId] = sshTunnelMachine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果实用了隧道机器且还没开始定时检查是否还被实用,则执行定时任务检测隧道是否还被使用
 | 
				
			||||||
 | 
						if !startCheckSshTunnelHasUse {
 | 
				
			||||||
 | 
							startCheckUse()
 | 
				
			||||||
 | 
							startCheckSshTunnelHasUse = true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return sshTunnelMachine, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 关闭ssh隧道机器的指定隧道
 | 
				
			||||||
 | 
					func CloseSshTunnelMachine(machineId uint64, tunnelId uint64) {
 | 
				
			||||||
 | 
						sshTunnelMachine := sshTunnelMachines[machineId]
 | 
				
			||||||
 | 
						if sshTunnelMachine == nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sshTunnelMachine.mutex.Lock()
 | 
				
			||||||
 | 
						defer sshTunnelMachine.mutex.Unlock()
 | 
				
			||||||
 | 
						t := sshTunnelMachine.tunnels[tunnelId]
 | 
				
			||||||
 | 
						if t != nil {
 | 
				
			||||||
 | 
							t.Close()
 | 
				
			||||||
 | 
							delete(sshTunnelMachine.tunnels, tunnelId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Tunnel struct {
 | 
				
			||||||
 | 
						id                uint64 // 唯一标识
 | 
				
			||||||
 | 
						machineId         uint64 // 隧道机器id
 | 
				
			||||||
 | 
						localHost         string // 本地监听地址
 | 
				
			||||||
 | 
						localPort         int    // 本地端口
 | 
				
			||||||
 | 
						remoteHost        string // 远程连接地址
 | 
				
			||||||
 | 
						remotePort        int    // 远程端口
 | 
				
			||||||
 | 
						listener          net.Listener
 | 
				
			||||||
 | 
						localConnections  []net.Conn
 | 
				
			||||||
 | 
						remoteConnections []net.Conn
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Tunnel) Open(sshClient *ssh.Client) {
 | 
				
			||||||
 | 
						localAddr := fmt.Sprintf("%s:%d", r.localHost, r.localPort)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 等待客户端访问 %v", r.id, localAddr)
 | 
				
			||||||
 | 
							localConn, err := r.listener.Accept()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								global.Log.Debugf("隧道 %v 接受连接失败 %v, 退出循环", r.id, err.Error())
 | 
				
			||||||
 | 
								global.Log.Debug("-------------------------------------------------")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.localConnections = append(r.localConnections, localConn)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 新增本地连接 %v", r.id, localConn.RemoteAddr().String())
 | 
				
			||||||
 | 
							remoteAddr := fmt.Sprintf("%s:%d", r.remoteHost, r.remotePort)
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 连接远程地址 %v ...", r.id, remoteAddr)
 | 
				
			||||||
 | 
							remoteConn, err := sshClient.Dial("tcp", remoteAddr)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								global.Log.Debugf("隧道 %v 连接远程地址 %v, 退出循环", r.id, err.Error())
 | 
				
			||||||
 | 
								global.Log.Debug("-------------------------------------------------")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.remoteConnections = append(r.remoteConnections, remoteConn)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 连接远程主机成功", r.id)
 | 
				
			||||||
 | 
							go copyConn(localConn, remoteConn)
 | 
				
			||||||
 | 
							go copyConn(remoteConn, localConn)
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 开始转发数据 [%v]->[%v]", r.id, localAddr, remoteAddr)
 | 
				
			||||||
 | 
							global.Log.Debug("~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Tunnel) Close() {
 | 
				
			||||||
 | 
						for i := range r.localConnections {
 | 
				
			||||||
 | 
							_ = r.localConnections[i].Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r.localConnections = nil
 | 
				
			||||||
 | 
						for i := range r.remoteConnections {
 | 
				
			||||||
 | 
							_ = r.remoteConnections[i].Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r.remoteConnections = nil
 | 
				
			||||||
 | 
						_ = r.listener.Close()
 | 
				
			||||||
 | 
						global.Log.Debugf("隧道 %d 监听器关闭", r.id)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func copyConn(writer, reader net.Conn) {
 | 
				
			||||||
 | 
						_, _ = io.Copy(writer, reader)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -53,6 +53,24 @@ type Stats struct {
 | 
				
			|||||||
	CPU          CPUInfo // or []CPUInfo to get all the cpu-core's stats?
 | 
						CPU          CPUInfo // or []CPUInfo to get all the cpu-core's stats?
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const StatsShell = `
 | 
				
			||||||
 | 
					cat /proc/uptime
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					/bin/hostname -f
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					cat /proc/loadavg
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					cat /proc/meminfo
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					df -B1
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					/sbin/ip -o addr
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					/bin/cat /proc/net/dev
 | 
				
			||||||
 | 
					echo '-----'
 | 
				
			||||||
 | 
					top -b -n 1 | grep Cpu
 | 
				
			||||||
 | 
					`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Cli) GetAllStats() *Stats {
 | 
					func (c *Cli) GetAllStats() *Stats {
 | 
				
			||||||
	res, _ := c.Run(StatsShell)
 | 
						res, _ := c.Run(StatsShell)
 | 
				
			||||||
	infos := strings.Split(*res, "-----")
 | 
						infos := strings.Split(*res, "-----")
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										74
									
								
								server/internal/devops/infrastructure/machine/terminal.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								server/internal/devops/infrastructure/machine/terminal.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
				
			|||||||
 | 
					package machine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/crypto/ssh"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Terminal struct {
 | 
				
			||||||
 | 
						SshSession   *ssh.Session
 | 
				
			||||||
 | 
						StdinPipe    io.WriteCloser
 | 
				
			||||||
 | 
						StdoutReader *bufio.Reader
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 新建机器ssh终端
 | 
				
			||||||
 | 
					func NewTerminal(cli *Cli) (*Terminal, error) {
 | 
				
			||||||
 | 
						sshSession, err := cli.GetSession()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						stdoutPipe, err := sshSession.StdoutPipe()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						stdoutReader := bufio.NewReader(stdoutPipe)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						stdinPipe, err := sshSession.StdinPipe()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						terminal := Terminal{
 | 
				
			||||||
 | 
							SshSession:   sshSession,
 | 
				
			||||||
 | 
							StdinPipe:    stdinPipe,
 | 
				
			||||||
 | 
							StdoutReader: stdoutReader,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &terminal, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Terminal) Write(p []byte) (int, error) {
 | 
				
			||||||
 | 
						return t.StdinPipe.Write(p)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Terminal) ReadRune() (r rune, size int, err error) {
 | 
				
			||||||
 | 
						return t.StdoutReader.ReadRune()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Terminal) Close() error {
 | 
				
			||||||
 | 
						if t.SshSession != nil {
 | 
				
			||||||
 | 
							return t.SshSession.Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Terminal) WindowChange(h int, w int) error {
 | 
				
			||||||
 | 
						return t.SshSession.WindowChange(h, w)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Terminal) RequestPty(term string, h, w int) error {
 | 
				
			||||||
 | 
						modes := ssh.TerminalModes{
 | 
				
			||||||
 | 
							ssh.ECHO:          1,
 | 
				
			||||||
 | 
							ssh.TTY_OP_ISPEED: 14400,
 | 
				
			||||||
 | 
							ssh.TTY_OP_OSPEED: 14400,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return t.SshSession.RequestPty(term, h, w, modes)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Terminal) Shell() error {
 | 
				
			||||||
 | 
						return t.SshSession.Shell()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,175 @@
 | 
				
			|||||||
 | 
					package machine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
						"unicode/utf8"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/gorilla/websocket"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						Resize = 1
 | 
				
			||||||
 | 
						Data   = 2
 | 
				
			||||||
 | 
						Ping   = 3
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type TerminalSession struct {
 | 
				
			||||||
 | 
						ID       string
 | 
				
			||||||
 | 
						wsConn   *websocket.Conn
 | 
				
			||||||
 | 
						terminal *Terminal
 | 
				
			||||||
 | 
						ctx      context.Context
 | 
				
			||||||
 | 
						cancel   context.CancelFunc
 | 
				
			||||||
 | 
						dataChan chan rune
 | 
				
			||||||
 | 
						tick     *time.Ticker
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewTerminalSession(sessionId string, ws *websocket.Conn, cli *Cli, rows, cols int) (*TerminalSession, error) {
 | 
				
			||||||
 | 
						terminal, err := NewTerminal(cli)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = terminal.RequestPty("xterm-256color", rows, cols)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = terminal.Shell()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx, cancel := context.WithCancel(context.Background())
 | 
				
			||||||
 | 
						tick := time.NewTicker(time.Millisecond * time.Duration(60))
 | 
				
			||||||
 | 
						ts := &TerminalSession{
 | 
				
			||||||
 | 
							ID:       sessionId,
 | 
				
			||||||
 | 
							wsConn:   ws,
 | 
				
			||||||
 | 
							terminal: terminal,
 | 
				
			||||||
 | 
							ctx:      ctx,
 | 
				
			||||||
 | 
							cancel:   cancel,
 | 
				
			||||||
 | 
							dataChan: make(chan rune),
 | 
				
			||||||
 | 
							tick:     tick,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return ts, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r TerminalSession) Start() {
 | 
				
			||||||
 | 
						go r.readFormTerminal()
 | 
				
			||||||
 | 
						go r.writeToWebsocket()
 | 
				
			||||||
 | 
						r.receiveWsMsg()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r TerminalSession) Stop() {
 | 
				
			||||||
 | 
						global.Log.Debug("close machine ssh terminal session")
 | 
				
			||||||
 | 
						r.tick.Stop()
 | 
				
			||||||
 | 
						r.cancel()
 | 
				
			||||||
 | 
						if r.wsConn != nil {
 | 
				
			||||||
 | 
							r.wsConn.Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if r.terminal != nil {
 | 
				
			||||||
 | 
							if err := r.terminal.Close(); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭机器ssh终端失败: %s", err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ts TerminalSession) readFormTerminal() {
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case <-ts.ctx.Done():
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								rn, size, err := ts.terminal.ReadRune()
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									if err != io.EOF {
 | 
				
			||||||
 | 
										global.Log.Error("机器ssh终端读取消息失败: ", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if size > 0 {
 | 
				
			||||||
 | 
									ts.dataChan <- rn
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ts TerminalSession) writeToWebsocket() {
 | 
				
			||||||
 | 
						var buf []byte
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case <-ts.ctx.Done():
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							case <-ts.tick.C:
 | 
				
			||||||
 | 
								if len(buf) > 0 {
 | 
				
			||||||
 | 
									s := string(buf)
 | 
				
			||||||
 | 
									if err := WriteMessage(ts.wsConn, s); err != nil {
 | 
				
			||||||
 | 
										global.Log.Error("机器ssh终端发送消息至websocket失败: ", err)
 | 
				
			||||||
 | 
										return
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									buf = []byte{}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							case data := <-ts.dataChan:
 | 
				
			||||||
 | 
								if data != utf8.RuneError {
 | 
				
			||||||
 | 
									p := make([]byte, utf8.RuneLen(data))
 | 
				
			||||||
 | 
									utf8.EncodeRune(p, data)
 | 
				
			||||||
 | 
									buf = append(buf, p...)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									buf = append(buf, []byte("@")...)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type WsMsg struct {
 | 
				
			||||||
 | 
						Type int    `json:"type"`
 | 
				
			||||||
 | 
						Msg  string `json:"msg"`
 | 
				
			||||||
 | 
						Cols int    `json:"cols"`
 | 
				
			||||||
 | 
						Rows int    `json:"rows"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ts *TerminalSession) receiveWsMsg() {
 | 
				
			||||||
 | 
						wsConn := ts.wsConn
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case <-ts.ctx.Done():
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								// read websocket msg
 | 
				
			||||||
 | 
								_, wsData, err := wsConn.ReadMessage()
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									global.Log.Debug("机器ssh终端读取websocket消息失败: ", err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								// 解析消息
 | 
				
			||||||
 | 
								msgObj := WsMsg{}
 | 
				
			||||||
 | 
								if err := json.Unmarshal(wsData, &msgObj); err != nil {
 | 
				
			||||||
 | 
									global.Log.Error("机器ssh终端消息解析失败: ", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								switch msgObj.Type {
 | 
				
			||||||
 | 
								case Resize:
 | 
				
			||||||
 | 
									if msgObj.Cols > 0 && msgObj.Rows > 0 {
 | 
				
			||||||
 | 
										if err := ts.terminal.WindowChange(msgObj.Rows, msgObj.Cols); err != nil {
 | 
				
			||||||
 | 
											global.Log.Error("ssh pty change windows size failed")
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								case Data:
 | 
				
			||||||
 | 
									_, err := ts.terminal.Write([]byte(msgObj.Msg))
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										global.Log.Debug("机器ssh终端写入消息失败: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								case Ping:
 | 
				
			||||||
 | 
									_, err := ts.terminal.SshSession.SendRequest("ping", true, nil)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										WriteMessage(wsConn, "\r\n\033[1;31m提示: 终端连接已断开...\033[0m")
 | 
				
			||||||
 | 
										return
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func WriteMessage(ws *websocket.Conn, msg string) error {
 | 
				
			||||||
 | 
						return ws.WriteMessage(websocket.TextMessage, []byte(msg))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,195 +0,0 @@
 | 
				
			|||||||
package machine
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"encoding/json"
 | 
					 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
					 | 
				
			||||||
	"sync"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/gorilla/websocket"
 | 
					 | 
				
			||||||
	"golang.org/x/crypto/ssh"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type safeBuffer struct {
 | 
					 | 
				
			||||||
	buffer bytes.Buffer
 | 
					 | 
				
			||||||
	mu     sync.Mutex
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (w *safeBuffer) Write(p []byte) (int, error) {
 | 
					 | 
				
			||||||
	w.mu.Lock()
 | 
					 | 
				
			||||||
	defer w.mu.Unlock()
 | 
					 | 
				
			||||||
	return w.buffer.Write(p)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
func (w *safeBuffer) Bytes() []byte {
 | 
					 | 
				
			||||||
	w.mu.Lock()
 | 
					 | 
				
			||||||
	defer w.mu.Unlock()
 | 
					 | 
				
			||||||
	return w.buffer.Bytes()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
func (w *safeBuffer) Reset() {
 | 
					 | 
				
			||||||
	w.mu.Lock()
 | 
					 | 
				
			||||||
	defer w.mu.Unlock()
 | 
					 | 
				
			||||||
	w.buffer.Reset()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const (
 | 
					 | 
				
			||||||
	wsMsgCmd    = "cmd"
 | 
					 | 
				
			||||||
	wsMsgResize = "resize"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type WsMsg struct {
 | 
					 | 
				
			||||||
	Type string `json:"type"`
 | 
					 | 
				
			||||||
	Msg  string `json:"msg"`
 | 
					 | 
				
			||||||
	Cols int    `json:"cols"`
 | 
					 | 
				
			||||||
	Rows int    `json:"rows"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type LogicSshWsSession struct {
 | 
					 | 
				
			||||||
	stdinPipe       io.WriteCloser
 | 
					 | 
				
			||||||
	comboOutput     *safeBuffer //ssh 终端混合输出
 | 
					 | 
				
			||||||
	inputFilterBuff *safeBuffer //用来过滤输入的命令和ssh_filter配置对比的
 | 
					 | 
				
			||||||
	session         *ssh.Session
 | 
					 | 
				
			||||||
	wsConn          *websocket.Conn
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func NewLogicSshWsSession(cols, rows int, cli *Cli, wsConn *websocket.Conn) (*LogicSshWsSession, error) {
 | 
					 | 
				
			||||||
	sshSession, err := cli.GetSession()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	stdinP, err := sshSession.StdinPipe()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	comboWriter := new(safeBuffer)
 | 
					 | 
				
			||||||
	inputBuf := new(safeBuffer)
 | 
					 | 
				
			||||||
	//ssh.stdout and stderr will write output into comboWriter
 | 
					 | 
				
			||||||
	sshSession.Stdout = comboWriter
 | 
					 | 
				
			||||||
	sshSession.Stderr = comboWriter
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	modes := ssh.TerminalModes{
 | 
					 | 
				
			||||||
		ssh.ECHO:          1,     // disable echo
 | 
					 | 
				
			||||||
		ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
 | 
					 | 
				
			||||||
		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	// Request pseudo terminal
 | 
					 | 
				
			||||||
	if err := sshSession.RequestPty("xterm", rows, cols, modes); err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	// Start remote shell
 | 
					 | 
				
			||||||
	if err := sshSession.Shell(); err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return &LogicSshWsSession{
 | 
					 | 
				
			||||||
		stdinPipe:       stdinP,
 | 
					 | 
				
			||||||
		comboOutput:     comboWriter,
 | 
					 | 
				
			||||||
		inputFilterBuff: inputBuf,
 | 
					 | 
				
			||||||
		session:         sshSession,
 | 
					 | 
				
			||||||
		wsConn:          wsConn,
 | 
					 | 
				
			||||||
	}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//Close 关闭
 | 
					 | 
				
			||||||
func (sws *LogicSshWsSession) Close() {
 | 
					 | 
				
			||||||
	if sws.session != nil {
 | 
					 | 
				
			||||||
		sws.session.Close()
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if sws.comboOutput != nil {
 | 
					 | 
				
			||||||
		sws.comboOutput = nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (sws *LogicSshWsSession) Start(quitChan chan bool) {
 | 
					 | 
				
			||||||
	go sws.receiveWsMsg(quitChan)
 | 
					 | 
				
			||||||
	go sws.sendComboOutput(quitChan)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//receiveWsMsg  receive websocket msg do some handling then write into ssh.session.stdin
 | 
					 | 
				
			||||||
func (sws *LogicSshWsSession) receiveWsMsg(exitCh chan bool) {
 | 
					 | 
				
			||||||
	wsConn := sws.wsConn
 | 
					 | 
				
			||||||
	//tells other go routine quit
 | 
					 | 
				
			||||||
	defer setQuit(exitCh)
 | 
					 | 
				
			||||||
	for {
 | 
					 | 
				
			||||||
		select {
 | 
					 | 
				
			||||||
		case <-exitCh:
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		default:
 | 
					 | 
				
			||||||
			//read websocket msg
 | 
					 | 
				
			||||||
			_, wsData, err := wsConn.ReadMessage()
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
 | 
					 | 
				
			||||||
					return
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				global.Log.Error("reading webSocket message failed: ", err)
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			//unmashal bytes into struct
 | 
					 | 
				
			||||||
			msgObj := WsMsg{}
 | 
					 | 
				
			||||||
			if err := json.Unmarshal(wsData, &msgObj); err != nil {
 | 
					 | 
				
			||||||
				global.Log.Error("unmarshal websocket message failed:", err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			switch msgObj.Type {
 | 
					 | 
				
			||||||
			case wsMsgResize:
 | 
					 | 
				
			||||||
				//handle xterm.js size change
 | 
					 | 
				
			||||||
				if msgObj.Cols > 0 && msgObj.Rows > 0 {
 | 
					 | 
				
			||||||
					if err := sws.session.WindowChange(msgObj.Rows, msgObj.Cols); err != nil {
 | 
					 | 
				
			||||||
						global.Log.Error("ssh pty change windows size failed")
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			case wsMsgCmd:
 | 
					 | 
				
			||||||
				sws.sendWebsocketInputCommandToSshSessionStdinPipe([]byte(msgObj.Msg))
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//sendWebsocketInputCommandToSshSessionStdinPipe
 | 
					 | 
				
			||||||
func (sws *LogicSshWsSession) sendWebsocketInputCommandToSshSessionStdinPipe(cmdBytes []byte) {
 | 
					 | 
				
			||||||
	if _, err := sws.stdinPipe.Write(cmdBytes); err != nil {
 | 
					 | 
				
			||||||
		global.Log.Error("ws cmd bytes write to ssh.stdin pipe failed")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (sws *LogicSshWsSession) sendComboOutput(exitCh chan bool) {
 | 
					 | 
				
			||||||
	wsConn := sws.wsConn
 | 
					 | 
				
			||||||
	//todo 优化成一个方法
 | 
					 | 
				
			||||||
	//tells other go routine quit
 | 
					 | 
				
			||||||
	defer setQuit(exitCh)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	//every 120ms write combine output bytes into websocket response
 | 
					 | 
				
			||||||
	tick := time.NewTicker(time.Millisecond * time.Duration(60))
 | 
					 | 
				
			||||||
	//for range time.Tick(120 * time.Millisecond){}
 | 
					 | 
				
			||||||
	defer tick.Stop()
 | 
					 | 
				
			||||||
	for {
 | 
					 | 
				
			||||||
		select {
 | 
					 | 
				
			||||||
		case <-tick.C:
 | 
					 | 
				
			||||||
			if sws.comboOutput == nil {
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			bs := sws.comboOutput.Bytes()
 | 
					 | 
				
			||||||
			if len(bs) > 0 {
 | 
					 | 
				
			||||||
				err := wsConn.WriteMessage(websocket.TextMessage, bs)
 | 
					 | 
				
			||||||
				if err != nil {
 | 
					 | 
				
			||||||
					global.Log.Error("ssh sending combo output to webSocket failed")
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				sws.comboOutput.buffer.Reset()
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		case <-exitCh:
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (sws *LogicSshWsSession) Wait(quitChan chan bool) {
 | 
					 | 
				
			||||||
	if err := sws.session.Wait(); err != nil {
 | 
					 | 
				
			||||||
		setQuit(quitChan)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func setQuit(ch chan bool) {
 | 
					 | 
				
			||||||
	ch <- true
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -51,9 +52,9 @@ func (m *machineRepo) GetById(id uint64, cols ...string) *entity.Machine {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *machineRepo) Create(entity *entity.Machine) {
 | 
					func (m *machineRepo) Create(entity *entity.Machine) {
 | 
				
			||||||
	model.Insert(entity)
 | 
						biz.ErrIsNilAppendErr(model.Insert(entity), "创建机器信息失败: %s")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *machineRepo) UpdateById(entity *entity.Machine) {
 | 
					func (m *machineRepo) UpdateById(entity *entity.Machine) {
 | 
				
			||||||
	model.UpdateById(entity)
 | 
						biz.ErrIsNilAppendErr(model.UpdateById(entity), "更新机器信息失败: %s")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,27 +0,0 @@
 | 
				
			|||||||
package scheduler
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
	SaveMachineMonitor()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func SaveMachineMonitor() {
 | 
					 | 
				
			||||||
	AddFun("@every 60s", func() {
 | 
					 | 
				
			||||||
		// for _, m := range models.GetNeedMonitorMachine() {
 | 
					 | 
				
			||||||
		// 	m := m
 | 
					 | 
				
			||||||
		// 	go func() {
 | 
					 | 
				
			||||||
		// 		cli, err := machine.GetCli(uint64(utils.GetInt4Map(m, "id")))
 | 
					 | 
				
			||||||
		// 		if err != nil {
 | 
					 | 
				
			||||||
		// 			mlog.Log.Error("获取客户端失败:", err.Error())
 | 
					 | 
				
			||||||
		// 			return
 | 
					 | 
				
			||||||
		// 		}
 | 
					 | 
				
			||||||
		// 		mm := cli.GetMonitorInfo()
 | 
					 | 
				
			||||||
		// 		if mm != nil {
 | 
					 | 
				
			||||||
		// 			err := model.Insert(mm)
 | 
					 | 
				
			||||||
		// 			if err != nil {
 | 
					 | 
				
			||||||
		// 				mlog.Log.Error("保存机器监控信息失败: ", err.Error())
 | 
					 | 
				
			||||||
		// 			}
 | 
					 | 
				
			||||||
		// 		}
 | 
					 | 
				
			||||||
		// 	}()
 | 
					 | 
				
			||||||
		// }
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -20,18 +20,27 @@ func InitDbRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		// 获取所有数据库列表
 | 
							// 获取所有数据库列表
 | 
				
			||||||
		db.GET("", func(c *gin.Context) {
 | 
							db.GET("", func(c *gin.Context) {
 | 
				
			||||||
			rc := ctx.NewReqCtxWithGin(c)
 | 
								ctx.NewReqCtxWithGin(c).Handle(d.Dbs)
 | 
				
			||||||
			rc.Handle(d.Dbs)
 | 
					 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		saveDb := ctx.NewLogInfo("保存数据库信息")
 | 
							saveDb := ctx.NewLogInfo("保存数据库信息").WithSave(true)
 | 
				
			||||||
		db.POST("", func(c *gin.Context) {
 | 
							db.POST("", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
				WithLog(saveDb).
 | 
									WithLog(saveDb).
 | 
				
			||||||
				Handle(d.Save)
 | 
									Handle(d.Save)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		deleteDb := ctx.NewLogInfo("删除数据库信息")
 | 
							// 获取数据库实例的所有数据库名
 | 
				
			||||||
 | 
							db.POST("databases", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
 | 
									Handle(d.GetDatabaseNames)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							db.GET(":dbId/pwd", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(d.GetDbPwd)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							deleteDb := ctx.NewLogInfo("删除数据库信息").WithSave(true)
 | 
				
			||||||
		db.DELETE(":dbId", func(c *gin.Context) {
 | 
							db.DELETE(":dbId", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
				WithLog(deleteDb).
 | 
									WithLog(deleteDb).
 | 
				
			||||||
@@ -50,15 +59,24 @@ func InitDbRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(d.GetCreateTableDdl)
 | 
								ctx.NewReqCtxWithGin(c).Handle(d.GetCreateTableDdl)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// db.GET(":dbId/exec-sql", controllers.SelectData)
 | 
							execSqlLog := ctx.NewLogInfo("执行Sql语句")
 | 
				
			||||||
		db.POST(":dbId/exec-sql", func(g *gin.Context) {
 | 
							db.POST(":dbId/exec-sql", func(g *gin.Context) {
 | 
				
			||||||
			rc := ctx.NewReqCtxWithGin(g).WithLog(ctx.NewLogInfo("执行Sql语句"))
 | 
								rc := ctx.NewReqCtxWithGin(g).WithLog(execSqlLog)
 | 
				
			||||||
			rc.Handle(d.ExecSql)
 | 
								rc.Handle(d.ExecSql)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							execSqlFileLog := ctx.NewLogInfo("执行Sql文件").WithSave(true)
 | 
				
			||||||
		db.POST(":dbId/exec-sql-file", func(g *gin.Context) {
 | 
							db.POST(":dbId/exec-sql-file", func(g *gin.Context) {
 | 
				
			||||||
			rc := ctx.NewReqCtxWithGin(g).WithLog(ctx.NewLogInfo("执行Sql文件"))
 | 
								ctx.NewReqCtxWithGin(g).
 | 
				
			||||||
			rc.Handle(d.ExecSqlFile)
 | 
									WithLog(execSqlFileLog).
 | 
				
			||||||
 | 
									Handle(d.ExecSqlFile)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dumpLog := ctx.NewLogInfo("导出sql文件").WithSave(true)
 | 
				
			||||||
 | 
							db.GET(":dbId/dump", func(g *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(g).
 | 
				
			||||||
 | 
									WithLog(dumpLog).
 | 
				
			||||||
 | 
									Handle(d.DumpSql)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		db.GET(":dbId/t-metadata", func(c *gin.Context) {
 | 
							db.GET(":dbId/t-metadata", func(c *gin.Context) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,10 @@ func InitMachineRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(m.Machines)
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.Machines)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							machines.GET(":machineId/pwd", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.GetMachinePwd)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		machines.GET(":machineId/stats", func(c *gin.Context) {
 | 
							machines.GET(":machineId/stats", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).Handle(m.MachineStats)
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.MachineStats)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
@@ -29,7 +33,7 @@ func InitMachineRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 终止进程
 | 
							// 终止进程
 | 
				
			||||||
		killProcessL := ctx.NewLogInfo("终止进程")
 | 
							killProcessL := ctx.NewLogInfo("终止进程").WithSave(true)
 | 
				
			||||||
		killProcessP := ctx.NewPermission("machine:killprocess")
 | 
							killProcessP := ctx.NewPermission("machine:killprocess")
 | 
				
			||||||
		machines.DELETE(":machineId/process", func(c *gin.Context) {
 | 
							machines.DELETE(":machineId/process", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
@@ -38,28 +42,28 @@ func InitMachineRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(m.KillProcess)
 | 
									Handle(m.KillProcess)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		saveMachine := ctx.NewLogInfo("保存机器信息")
 | 
							saveMachine := ctx.NewLogInfo("保存机器信息").WithSave(true)
 | 
				
			||||||
		machines.POST("", func(c *gin.Context) {
 | 
							machines.POST("", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
				WithLog(saveMachine).
 | 
									WithLog(saveMachine).
 | 
				
			||||||
				Handle(m.SaveMachine)
 | 
									Handle(m.SaveMachine)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		changeStatus := ctx.NewLogInfo("调整机器状态")
 | 
							changeStatus := ctx.NewLogInfo("调整机器状态").WithSave(true)
 | 
				
			||||||
		machines.PUT(":machineId/:status", func(c *gin.Context) {
 | 
							machines.PUT(":machineId/:status", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
				WithLog(changeStatus).
 | 
									WithLog(changeStatus).
 | 
				
			||||||
				Handle(m.ChangeStatus)
 | 
									Handle(m.ChangeStatus)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		delMachine := ctx.NewLogInfo("删除机器")
 | 
							delMachine := ctx.NewLogInfo("删除机器").WithSave(true)
 | 
				
			||||||
		machines.DELETE(":machineId", func(c *gin.Context) {
 | 
							machines.DELETE(":machineId", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
				WithLog(delMachine).
 | 
									WithLog(delMachine).
 | 
				
			||||||
				Handle(m.DeleteMachine)
 | 
									Handle(m.DeleteMachine)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		closeCli := ctx.NewLogInfo("关闭机器客户端")
 | 
							closeCli := ctx.NewLogInfo("关闭机器客户端").WithSave(true)
 | 
				
			||||||
		machines.DELETE(":machineId/close-cli", func(c *gin.Context) {
 | 
							machines.DELETE(":machineId/close-cli", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(closeCli).Handle(m.CloseCli)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(closeCli).Handle(m.CloseCli)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,7 +24,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 新增修改机器文件
 | 
							// 新增修改机器文件
 | 
				
			||||||
		addFileConf := ctx.NewLogInfo("新增机器文件配置")
 | 
							addFileConf := ctx.NewLogInfo("新增机器文件配置").WithSave(true)
 | 
				
			||||||
		afcP := ctx.NewPermission("machine:file:add")
 | 
							afcP := ctx.NewPermission("machine:file:add")
 | 
				
			||||||
		machineFile.POST(":machineId/files", func(c *gin.Context) {
 | 
							machineFile.POST(":machineId/files", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(addFileConf).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(addFileConf).
 | 
				
			||||||
@@ -33,7 +33,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 删除机器文件
 | 
							// 删除机器文件
 | 
				
			||||||
		delFileConf := ctx.NewLogInfo("删除机器文件配置")
 | 
							delFileConf := ctx.NewLogInfo("删除机器文件配置").WithSave(true)
 | 
				
			||||||
		dfcP := ctx.NewPermission("machine:file:del")
 | 
							dfcP := ctx.NewPermission("machine:file:del")
 | 
				
			||||||
		machineFile.DELETE(":machineId/files/:fileId", func(c *gin.Context) {
 | 
							machineFile.DELETE(":machineId/files/:fileId", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(delFileConf).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(delFileConf).
 | 
				
			||||||
@@ -41,19 +41,19 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(mf.DeleteFile)
 | 
									Handle(mf.DeleteFile)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		getContent := ctx.NewLogInfo("读取机器文件内容")
 | 
							getContent := ctx.NewLogInfo("读取机器文件内容").WithSave(true)
 | 
				
			||||||
		machineFile.GET(":machineId/files/:fileId/read", func(c *gin.Context) {
 | 
							machineFile.GET(":machineId/files/:fileId/read", func(c *gin.Context) {
 | 
				
			||||||
			rc := ctx.NewReqCtxWithGin(c).WithLog(getContent)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(getContent).
 | 
				
			||||||
			rc.Handle(mf.ReadFileContent)
 | 
									Handle(mf.ReadFileContent)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		getDir := ctx.NewLogInfo("读取机器目录")
 | 
							getDir := ctx.NewLogInfo("读取机器目录")
 | 
				
			||||||
		machineFile.GET(":machineId/files/:fileId/read-dir", func(c *gin.Context) {
 | 
							machineFile.GET(":machineId/files/:fileId/read-dir", func(c *gin.Context) {
 | 
				
			||||||
			rc := ctx.NewReqCtxWithGin(c).WithLog(getDir)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(getDir).
 | 
				
			||||||
			rc.Handle(mf.GetDirEntry)
 | 
									Handle(mf.GetDirEntry)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		writeFile := ctx.NewLogInfo("写入or下载文件内容")
 | 
							writeFile := ctx.NewLogInfo("写入or下载文件内容").WithSave(true)
 | 
				
			||||||
		wfP := ctx.NewPermission("machine:file:write")
 | 
							wfP := ctx.NewPermission("machine:file:write")
 | 
				
			||||||
		machineFile.POST(":machineId/files/:fileId/write", func(c *gin.Context) {
 | 
							machineFile.POST(":machineId/files/:fileId/write", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(writeFile).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(writeFile).
 | 
				
			||||||
@@ -61,7 +61,14 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(mf.WriteFileContent)
 | 
									Handle(mf.WriteFileContent)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		uploadFile := ctx.NewLogInfo("文件上传")
 | 
							createFile := ctx.NewLogInfo("创建机器文件or目录").WithSave(true)
 | 
				
			||||||
 | 
							machineFile.POST(":machineId/files/:fileId/create-file", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).WithLog(createFile).
 | 
				
			||||||
 | 
									WithRequiredPermission(wfP).
 | 
				
			||||||
 | 
									Handle(mf.CreateFile)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							uploadFile := ctx.NewLogInfo("文件上传").WithSave(true)
 | 
				
			||||||
		ufP := ctx.NewPermission("machine:file:upload")
 | 
							ufP := ctx.NewPermission("machine:file:upload")
 | 
				
			||||||
		machineFile.POST(":machineId/files/:fileId/upload", func(c *gin.Context) {
 | 
							machineFile.POST(":machineId/files/:fileId/upload", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(uploadFile).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(uploadFile).
 | 
				
			||||||
@@ -69,7 +76,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(mf.UploadFile)
 | 
									Handle(mf.UploadFile)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		removeFile := ctx.NewLogInfo("删除文件or文件夹")
 | 
							removeFile := ctx.NewLogInfo("删除文件or文件夹").WithSave(true)
 | 
				
			||||||
		rfP := ctx.NewPermission("machine:file:rm")
 | 
							rfP := ctx.NewPermission("machine:file:rm")
 | 
				
			||||||
		machineFile.DELETE(":machineId/files/:fileId/remove", func(c *gin.Context) {
 | 
							machineFile.DELETE(":machineId/files/:fileId/remove", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(removeFile).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(removeFile).
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,7 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(ms.MachineScripts)
 | 
								ctx.NewReqCtxWithGin(c).Handle(ms.MachineScripts)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		saveMachienScriptLog := ctx.NewLogInfo("保存脚本")
 | 
							saveMachienScriptLog := ctx.NewLogInfo("保存脚本").WithSave(true)
 | 
				
			||||||
		smsP := ctx.NewPermission("machine:script:save")
 | 
							smsP := ctx.NewPermission("machine:script:save")
 | 
				
			||||||
		// 保存脚本
 | 
							// 保存脚本
 | 
				
			||||||
		machines.POST(":machineId/scripts", func(c *gin.Context) {
 | 
							machines.POST(":machineId/scripts", func(c *gin.Context) {
 | 
				
			||||||
@@ -31,7 +31,7 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(ms.SaveMachineScript)
 | 
									Handle(ms.SaveMachineScript)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		deleteLog := ctx.NewLogInfo("删除脚本")
 | 
							deleteLog := ctx.NewLogInfo("删除脚本").WithSave(true)
 | 
				
			||||||
		dP := ctx.NewPermission("machine:script:del")
 | 
							dP := ctx.NewPermission("machine:script:del")
 | 
				
			||||||
		// 保存脚本
 | 
							// 保存脚本
 | 
				
			||||||
		machines.DELETE(":machineId/scripts/:scriptId", func(c *gin.Context) {
 | 
							machines.DELETE(":machineId/scripts/:scriptId", func(c *gin.Context) {
 | 
				
			||||||
@@ -40,7 +40,7 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(ms.DeleteMachineScript)
 | 
									Handle(ms.DeleteMachineScript)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		runLog := ctx.NewLogInfo("执行机器脚本")
 | 
							runLog := ctx.NewLogInfo("执行机器脚本").WithSave(true)
 | 
				
			||||||
		rP := ctx.NewPermission("machine:script:run")
 | 
							rP := ctx.NewPermission("machine:script:run")
 | 
				
			||||||
		// 运行脚本
 | 
							// 运行脚本
 | 
				
			||||||
		machines.GET(":machineId/scripts/:scriptId/run", func(c *gin.Context) {
 | 
							machines.GET(":machineId/scripts/:scriptId/run", func(c *gin.Context) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,7 +25,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(m.GetProjects)
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.GetProjects)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		saveProjectLog := ctx.NewLogInfo("保存项目信息")
 | 
							saveProjectLog := ctx.NewLogInfo("保存项目信息").WithSave(true)
 | 
				
			||||||
		savePP := ctx.NewPermission("project:save")
 | 
							savePP := ctx.NewPermission("project:save")
 | 
				
			||||||
		// 保存项目下的环境信息
 | 
							// 保存项目下的环境信息
 | 
				
			||||||
		project.POST("", func(c *gin.Context) {
 | 
							project.POST("", func(c *gin.Context) {
 | 
				
			||||||
@@ -34,7 +34,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(m.SaveProject)
 | 
									Handle(m.SaveProject)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		delProjectLog := ctx.NewLogInfo("删除项目信息")
 | 
							delProjectLog := ctx.NewLogInfo("删除项目信息").WithSave(true)
 | 
				
			||||||
		delPP := ctx.NewPermission("project:del")
 | 
							delPP := ctx.NewPermission("project:del")
 | 
				
			||||||
		// 删除项目
 | 
							// 删除项目
 | 
				
			||||||
		project.DELETE("", func(c *gin.Context) {
 | 
							project.DELETE("", func(c *gin.Context) {
 | 
				
			||||||
@@ -48,7 +48,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(m.GetProjectEnvs)
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.GetProjectEnvs)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		saveProjectEnvLog := ctx.NewLogInfo("新增项目环境信息")
 | 
							saveProjectEnvLog := ctx.NewLogInfo("新增项目环境信息").WithSave(true)
 | 
				
			||||||
		savePeP := ctx.NewPermission("project:env:add")
 | 
							savePeP := ctx.NewPermission("project:env:add")
 | 
				
			||||||
		// 保存项目下的环境信息
 | 
							// 保存项目下的环境信息
 | 
				
			||||||
		project.POST("/:projectId/envs", func(c *gin.Context) {
 | 
							project.POST("/:projectId/envs", func(c *gin.Context) {
 | 
				
			||||||
@@ -63,7 +63,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 保存项目成员
 | 
							// 保存项目成员
 | 
				
			||||||
		saveProjectMemLog := ctx.NewLogInfo("新增项目成员")
 | 
							saveProjectMemLog := ctx.NewLogInfo("新增项目成员").WithSave(true)
 | 
				
			||||||
		savePmP := ctx.NewPermission("project:member:add")
 | 
							savePmP := ctx.NewPermission("project:member:add")
 | 
				
			||||||
		project.POST("/:projectId/members", func(c *gin.Context) {
 | 
							project.POST("/:projectId/members", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(saveProjectMemLog).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(saveProjectMemLog).
 | 
				
			||||||
@@ -72,7 +72,7 @@ func InitProjectRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 删除项目成员
 | 
							// 删除项目成员
 | 
				
			||||||
		delProjectMemLog := ctx.NewLogInfo("删除项目成员")
 | 
							delProjectMemLog := ctx.NewLogInfo("删除项目成员").WithSave(true)
 | 
				
			||||||
		savePmdP := ctx.NewPermission("project:member:del")
 | 
							savePmdP := ctx.NewPermission("project:member:del")
 | 
				
			||||||
		project.DELETE("/:projectId/members/:accountId", func(c *gin.Context) {
 | 
							project.DELETE("/:projectId/members/:accountId", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(delProjectMemLog).
 | 
								ctx.NewReqCtxWithGin(c).WithLog(delProjectMemLog).
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,12 +21,16 @@ func InitRedisRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(rs.RedisList)
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.RedisList)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		save := ctx.NewLogInfo("保存redis信息")
 | 
							save := ctx.NewLogInfo("保存redis信息").WithSave(true)
 | 
				
			||||||
		redis.POST("", func(c *gin.Context) {
 | 
							redis.POST("", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(save).Handle(rs.Save)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(save).Handle(rs.Save)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		delRedis := ctx.NewLogInfo("删除redis信息")
 | 
							redis.GET(":id/pwd", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.GetRedisPwd)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							delRedis := ctx.NewLogInfo("删除redis信息").WithSave(true)
 | 
				
			||||||
		redis.DELETE(":id", func(c *gin.Context) {
 | 
							redis.DELETE(":id", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
@@ -35,14 +39,17 @@ func InitRedisRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(rs.RedisInfo)
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.RedisInfo)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							redis.GET(":id/cluster-info", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.ClusterInfo)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取指定redis keys
 | 
							// 获取指定redis keys
 | 
				
			||||||
		redis.GET(":id/scan/:cursor/:count", func(c *gin.Context) {
 | 
							redis.POST(":id/scan", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).Handle(rs.Scan)
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.Scan)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 删除key
 | 
							// 删除key
 | 
				
			||||||
		deleteKeyL := ctx.NewLogInfo("redis删除key")
 | 
							deleteKeyL := ctx.NewLogInfo("redis删除key").WithSave(true)
 | 
				
			||||||
		// deleteKey := ctx.NewPermission("project:save")
 | 
					 | 
				
			||||||
		redis.DELETE(":id/key", func(c *gin.Context) {
 | 
							redis.DELETE(":id/key", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(deleteKeyL).Handle(rs.DeleteKey)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(deleteKeyL).Handle(rs.DeleteKey)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
@@ -57,9 +64,17 @@ func InitRedisRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(rs.SetStringValue)
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.SetStringValue)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取hash类型值
 | 
							// hscan
 | 
				
			||||||
		redis.GET(":id/hash-value", func(c *gin.Context) {
 | 
							redis.GET(":id/hscan", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).Handle(rs.GetHashValue)
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.Hscan)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							redis.GET(":id/hget", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.Hget)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							redis.DELETE(":id/hdel", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.Hdel)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 设置hash类型值
 | 
							// 设置hash类型值
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,9 @@ import (
 | 
				
			|||||||
	"mayfly-go/pkg/captcha"
 | 
						"mayfly-go/pkg/captcha"
 | 
				
			||||||
	"mayfly-go/pkg/ctx"
 | 
						"mayfly-go/pkg/ctx"
 | 
				
			||||||
	"mayfly-go/pkg/ginx"
 | 
						"mayfly-go/pkg/ginx"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
	"mayfly-go/pkg/utils"
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -29,15 +31,22 @@ type Account struct {
 | 
				
			|||||||
func (a *Account) Login(rc *ctx.ReqCtx) {
 | 
					func (a *Account) Login(rc *ctx.ReqCtx) {
 | 
				
			||||||
	loginForm := &form.LoginForm{}
 | 
						loginForm := &form.LoginForm{}
 | 
				
			||||||
	ginx.BindJsonAndValid(rc.GinCtx, loginForm)
 | 
						ginx.BindJsonAndValid(rc.GinCtx, loginForm)
 | 
				
			||||||
	rc.ReqParam = loginForm.Username
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 校验验证码
 | 
						// 校验验证码
 | 
				
			||||||
	biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
 | 
						biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	account := &entity.Account{Username: loginForm.Username, Password: utils.Md5(loginForm.Password)}
 | 
						originPwd, err := utils.DefaultRsaDecrypt(loginForm.Password, true)
 | 
				
			||||||
	biz.ErrIsNil(a.AccountApp.GetAccount(account, "Id", "Username", "Status", "LastLoginTime", "LastLoginIp"), "用户名或密码错误")
 | 
						biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						account := &entity.Account{Username: loginForm.Username}
 | 
				
			||||||
 | 
						err = a.AccountApp.GetAccount(account, "Id", "Username", "Password", "Status", "LastLoginTime", "LastLoginIp")
 | 
				
			||||||
 | 
						biz.ErrIsNil(err, "用户名或密码错误")
 | 
				
			||||||
 | 
						biz.IsTrue(utils.CheckPwdHash(originPwd, account.Password), "用户名或密码错误")
 | 
				
			||||||
	biz.IsTrue(account.IsEnable(), "该账号不可用")
 | 
						biz.IsTrue(account.IsEnable(), "该账号不可用")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 校验密码强度是否符合
 | 
				
			||||||
 | 
						biz.IsTrueBy(CheckPasswordLever(originPwd), biz.NewBizErrCode(401, "您的密码安全等级较低,请修改后重新登录"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var resources vo.AccountResourceVOList
 | 
						var resources vo.AccountResourceVOList
 | 
				
			||||||
	// 获取账号菜单资源
 | 
						// 获取账号菜单资源
 | 
				
			||||||
	a.ResourceApp.GetAccountResources(account.Id, &resources)
 | 
						a.ResourceApp.GetAccountResources(account.Id, &resources)
 | 
				
			||||||
@@ -54,8 +63,13 @@ func (a *Account) Login(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	// 保存该账号的权限codes
 | 
						// 保存该账号的权限codes
 | 
				
			||||||
	ctx.SavePermissionCodes(account.Id, permissions)
 | 
						ctx.SavePermissionCodes(account.Id, permissions)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						clientIp := rc.GinCtx.ClientIP()
 | 
				
			||||||
	// 保存登录消息
 | 
						// 保存登录消息
 | 
				
			||||||
	go a.saveLogin(account, rc.GinCtx.ClientIP())
 | 
						go a.saveLogin(account, clientIp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rc.ReqParam = fmt.Sprintln("登录ip: ", clientIp)
 | 
				
			||||||
 | 
						// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
 | 
				
			||||||
 | 
						rc.LoginAccount = &model.LoginAccount{Id: account.Id, Username: account.Username}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rc.ResData = map[string]interface{}{
 | 
						rc.ResData = map[string]interface{}{
 | 
				
			||||||
		"token":         ctx.CreateToken(account.Id, account.Username),
 | 
							"token":         ctx.CreateToken(account.Id, account.Username),
 | 
				
			||||||
@@ -67,6 +81,51 @@ func (a *Account) Login(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *Account) ChangePassword(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						form := new(form.AccountChangePasswordForm)
 | 
				
			||||||
 | 
						ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						originOldPwd, err := utils.DefaultRsaDecrypt(form.OldPassword, true)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						account := &entity.Account{Username: form.Username}
 | 
				
			||||||
 | 
						err = a.AccountApp.GetAccount(account, "Id", "Username", "Password", "Status")
 | 
				
			||||||
 | 
						biz.ErrIsNil(err, "旧密码错误")
 | 
				
			||||||
 | 
						biz.IsTrue(utils.CheckPwdHash(originOldPwd, account.Password), "旧密码错误")
 | 
				
			||||||
 | 
						biz.IsTrue(account.IsEnable(), "该账号不可用")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						originNewPwd, err := utils.DefaultRsaDecrypt(form.NewPassword, true)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "解密新密码错误: %s")
 | 
				
			||||||
 | 
						biz.IsTrue(CheckPasswordLever(originNewPwd), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						updateAccount := new(entity.Account)
 | 
				
			||||||
 | 
						updateAccount.Id = account.Id
 | 
				
			||||||
 | 
						updateAccount.Password = utils.PwdHash(originNewPwd)
 | 
				
			||||||
 | 
						a.AccountApp.Update(updateAccount)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
 | 
				
			||||||
 | 
						rc.LoginAccount = &model.LoginAccount{Id: account.Id, Username: account.Username}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CheckPasswordLever(ps string) bool {
 | 
				
			||||||
 | 
						if len(ps) < 8 {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						num := `[0-9]{1}`
 | 
				
			||||||
 | 
						a_z := `[a-zA-Z]{1}`
 | 
				
			||||||
 | 
						symbol := `[!@#~$%^&*()+|_.,]{1}`
 | 
				
			||||||
 | 
						if b, err := regexp.MatchString(num, ps); !b || err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if b, err := regexp.MatchString(a_z, ps); !b || err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if b, err := regexp.MatchString(symbol, ps); !b || err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 保存更新账号登录信息
 | 
					// 保存更新账号登录信息
 | 
				
			||||||
func (a *Account) saveLogin(account *entity.Account, ip string) {
 | 
					func (a *Account) saveLogin(account *entity.Account, ip string) {
 | 
				
			||||||
	// 更新账号最后登录时间
 | 
						// 更新账号最后登录时间
 | 
				
			||||||
@@ -100,7 +159,7 @@ func (a *Account) saveLogin(account *entity.Account, ip string) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取个人账号信息
 | 
					// 获取个人账号信息
 | 
				
			||||||
func (a Account) AccountInfo(rc *ctx.ReqCtx) {
 | 
					func (a *Account) AccountInfo(rc *ctx.ReqCtx) {
 | 
				
			||||||
	ap := new(vo.AccountPersonVO)
 | 
						ap := new(vo.AccountPersonVO)
 | 
				
			||||||
	// 角色信息
 | 
						// 角色信息
 | 
				
			||||||
	roles := new([]vo.AccountRoleVO)
 | 
						roles := new([]vo.AccountRoleVO)
 | 
				
			||||||
@@ -111,7 +170,7 @@ func (a Account) AccountInfo(rc *ctx.ReqCtx) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 更新个人账号信息
 | 
					// 更新个人账号信息
 | 
				
			||||||
func (a Account) UpdateAccount(rc *ctx.ReqCtx) {
 | 
					func (a *Account) UpdateAccount(rc *ctx.ReqCtx) {
 | 
				
			||||||
	updateForm := &form.AccountUpdateForm{}
 | 
						updateForm := &form.AccountUpdateForm{}
 | 
				
			||||||
	ginx.BindJsonAndValid(rc.GinCtx, updateForm)
 | 
						ginx.BindJsonAndValid(rc.GinCtx, updateForm)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -121,13 +180,14 @@ func (a Account) UpdateAccount(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	updateAccount.Id = rc.LoginAccount.Id
 | 
						updateAccount.Id = rc.LoginAccount.Id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if updateAccount.Password != "" {
 | 
						if updateAccount.Password != "" {
 | 
				
			||||||
		updateAccount.Password = utils.Md5(updateAccount.Password)
 | 
							biz.IsTrue(CheckPasswordLever(updateAccount.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
 | 
				
			||||||
 | 
							updateAccount.Password = utils.PwdHash(updateAccount.Password)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	a.AccountApp.Update(updateAccount)
 | 
						a.AccountApp.Update(updateAccount)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取账号接收的消息列表
 | 
					// 获取账号接收的消息列表
 | 
				
			||||||
func (a Account) GetMsgs(rc *ctx.ReqCtx) {
 | 
					func (a *Account) GetMsgs(rc *ctx.ReqCtx) {
 | 
				
			||||||
	condition := &entity.Msg{
 | 
						condition := &entity.Msg{
 | 
				
			||||||
		RecipientId: int64(rc.LoginAccount.Id),
 | 
							RecipientId: int64(rc.LoginAccount.Id),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,3 +7,9 @@ type AccountCreateForm struct {
 | 
				
			|||||||
type AccountUpdateForm struct {
 | 
					type AccountUpdateForm struct {
 | 
				
			||||||
	Password *string `json:"password" binding:"min=6,max=16"`
 | 
						Password *string `json:"password" binding:"min=6,max=16"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type AccountChangePasswordForm struct {
 | 
				
			||||||
 | 
						Username    string `json:"username"`
 | 
				
			||||||
 | 
						OldPassword string `json:"oldPassword"`
 | 
				
			||||||
 | 
						NewPassword string `json:"newPassword"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,6 +29,7 @@ func (r *Role) SaveRole(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	g := rc.GinCtx
 | 
						g := rc.GinCtx
 | 
				
			||||||
	form := &form.RoleForm{}
 | 
						form := &form.RoleForm{}
 | 
				
			||||||
	ginx.BindJsonAndValid(g, form)
 | 
						ginx.BindJsonAndValid(g, form)
 | 
				
			||||||
 | 
						rc.ReqParam = form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	role := new(entity.Role)
 | 
						role := new(entity.Role)
 | 
				
			||||||
	utils.Copy(role, form)
 | 
						utils.Copy(role, form)
 | 
				
			||||||
 
 | 
				
			|||||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user