102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package services
|
|
|
|
import (
|
|
"cmii-uav-watchdog-common/models"
|
|
"cmii-uav-watchdog-common/utils"
|
|
"cmii-uav-watchdog-common/wdd_log"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
ProjectFilePath = "C:\\Users\\wddsh\\Documents\\IdeaProjects\\cmii-uav-watchdog-project\\cmii-uav-watchdog-center\\project_file\\"
|
|
)
|
|
|
|
func CreateProject(project *models.Project) (string, error) {
|
|
|
|
// 生成TierOneSecret
|
|
secret, err := GenerateTierOneTOTPSecret()
|
|
if err != nil {
|
|
wdd_log.Error("生成TierOneSecret失败: %v", err)
|
|
return "", err
|
|
}
|
|
|
|
project.TierOneSecret = secret
|
|
project.CreateTime = utils.CurentTimeString()
|
|
|
|
// 存储 project到file中
|
|
project.AuthFilePath = fmt.Sprintf("%s%s-%s.json", ProjectFilePath, project.Namespace, project.Name)
|
|
|
|
// 存储到file中
|
|
jsonData, err := json.Marshal(project)
|
|
if err != nil {
|
|
wdd_log.Error("序列化失败: %v", err)
|
|
return "", err
|
|
}
|
|
|
|
// 写入文件
|
|
err = os.WriteFile(project.AuthFilePath, jsonData, 0644)
|
|
if err != nil {
|
|
wdd_log.Error("写入文件失败: %v", err)
|
|
return "", err
|
|
}
|
|
|
|
return project.AuthFilePath, nil
|
|
}
|
|
|
|
// GetProjectList 获取项目列表
|
|
func GetProjectList() ([]models.Project, error) {
|
|
|
|
// 获取项目列表
|
|
projectFileList, err := os.ReadDir(ProjectFilePath)
|
|
if err != nil {
|
|
wdd_log.Error("读取项目列表失败: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
projectList := make([]models.Project, 0)
|
|
|
|
// 遍历项目列表
|
|
for _, projectFilePath := range projectFileList {
|
|
// 读取项目文件
|
|
projectFile, err := os.ReadFile(fmt.Sprintf("%s%s", ProjectFilePath, projectFilePath.Name()))
|
|
if err != nil {
|
|
wdd_log.Error("读取项目文件失败: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// 反序列化
|
|
var project models.Project
|
|
err = json.Unmarshal(projectFile, &project)
|
|
if err != nil {
|
|
wdd_log.Error("反序列化失败: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// 添加到项目列表
|
|
projectList = append(projectList, project)
|
|
}
|
|
|
|
return projectList, nil
|
|
}
|
|
|
|
// GetProjectInfo 获取项目信息
|
|
func GetProjectInfo(projectNamespace string) (models.Project, error) {
|
|
// 获取项目列表
|
|
projectList, err := GetProjectList()
|
|
if err != nil {
|
|
wdd_log.Error("获取项目列表失败: %v", err)
|
|
return models.Project{}, err
|
|
}
|
|
|
|
// 遍历项目列表
|
|
for _, project := range projectList {
|
|
if project.Namespace == projectNamespace {
|
|
return project, nil
|
|
}
|
|
}
|
|
|
|
return models.Project{}, fmt.Errorf("项目不存在")
|
|
}
|