feat: 系统初始引导使用重置接口
This commit is contained in:
155
src/modules/common/controller/bootloader.go
Normal file
155
src/modules/common/controller/bootloader.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
adminConstants "be.ems/src/framework/constants/admin"
|
||||
"be.ems/src/framework/constants/common"
|
||||
tokenConstants "be.ems/src/framework/constants/token"
|
||||
"be.ems/src/framework/utils/cmd"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/utils/machine"
|
||||
tokenUtils "be.ems/src/framework/utils/token"
|
||||
"be.ems/src/framework/vo"
|
||||
"be.ems/src/framework/vo/result"
|
||||
commonService "be.ems/src/modules/common/service"
|
||||
systemService "be.ems/src/modules/system/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 BootloaderController 结构体
|
||||
var NewBootloader = &BootloaderController{
|
||||
accountService: commonService.NewAccountImpl,
|
||||
sysUserService: systemService.NewSysUserImpl,
|
||||
}
|
||||
|
||||
// 系统引导初始化
|
||||
//
|
||||
// PATH /bootloader
|
||||
type BootloaderController struct {
|
||||
// 账号身份操作服务
|
||||
accountService commonService.IAccount
|
||||
// 用户信息服务
|
||||
sysUserService systemService.ISysUser
|
||||
}
|
||||
|
||||
// 首次引导开始
|
||||
//
|
||||
// POST /
|
||||
func (s *BootloaderController) BootloaderStart(c *gin.Context) {
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[common.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||
c.JSON(200, result.ErrMsg("bootloader done"))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询用户登录账号
|
||||
sysUser := s.sysUserService.SelectUserById("1")
|
||||
if sysUser.UserID != "1" {
|
||||
c.JSON(200, result.ErrMsg("not found user data"))
|
||||
return
|
||||
}
|
||||
|
||||
// 登录用户信息
|
||||
loginUser := vo.LoginUser{
|
||||
UserID: sysUser.UserID,
|
||||
DeptID: sysUser.DeptID,
|
||||
User: sysUser,
|
||||
Permissions: []string{adminConstants.PERMISSION},
|
||||
}
|
||||
|
||||
// 当前请求信息
|
||||
ipaddr, location := ctx.IPAddrLocation(c)
|
||||
os, browser := ctx.UaOsBrowser(c)
|
||||
|
||||
// 生成令牌,创建系统访问记录
|
||||
tokenStr := tokenUtils.Create(&loginUser, ipaddr, location, os, browser)
|
||||
if tokenStr == "" {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
} else {
|
||||
s.accountService.UpdateLoginDateAndIP(&loginUser)
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
tokenConstants.RESPONSE_FIELD: tokenStr,
|
||||
}))
|
||||
}
|
||||
|
||||
// 首次引导完成
|
||||
//
|
||||
// PUT /
|
||||
func (s *BootloaderController) BootloaderDone(c *gin.Context) {
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[common.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||
c.JSON(200, result.ErrMsg("bootloader done"))
|
||||
return
|
||||
}
|
||||
|
||||
// 记录完成时间
|
||||
err := machine.SetLaunchInfo(map[string]any{
|
||||
common.LAUNCH_BOOTLOADER: false, // 启动引导
|
||||
common.LAUNCH_BOOTLOADER + "Time": time.Now().UnixMilli(), // 引导完成时间
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 清除授权信息
|
||||
tokenUtils.Remove(ctx.Authorization(c))
|
||||
c.JSON(200, result.Ok(nil))
|
||||
}
|
||||
|
||||
// 引导系统数据重置
|
||||
//
|
||||
// DELETE /
|
||||
func (s *BootloaderController) BootloaderReset(c *gin.Context) {
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[common.LAUNCH_BOOTLOADER]; ok && v.(bool) {
|
||||
c.JSON(200, result.ErrMsg("bootloader not done"))
|
||||
return
|
||||
}
|
||||
|
||||
// 重置数据库
|
||||
if runtime.GOOS == "windows" {
|
||||
c.JSON(200, result.ErrMsg("Does not support window operations"))
|
||||
return
|
||||
} else {
|
||||
_, err := cmd.ExecWithCheck("sudo", "/usr/local/omc/bin/setomc.sh", "-m", "install")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 重置引导标记
|
||||
err := machine.SetLaunchInfo(map[string]any{
|
||||
"bootloader": true, // 启动引导
|
||||
"bootloaderTime": 0, // 引导完成时间
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 清除授权信息
|
||||
tokenUtils.Remove(ctx.Authorization(c))
|
||||
c.JSON(200, result.Ok(nil))
|
||||
}
|
||||
Reference in New Issue
Block a user