feat: 系统初始引导使用重置接口
This commit is contained in:
@@ -19,3 +19,6 @@ const STATUS_NO = "0"
|
|||||||
|
|
||||||
// 上下文信息-登录用户
|
// 上下文信息-登录用户
|
||||||
const CTX_LOGIN_USER = "loginuser"
|
const CTX_LOGIN_USER = "loginuser"
|
||||||
|
|
||||||
|
// 启动-引导系统初始
|
||||||
|
const LAUNCH_BOOTLOADER = "bootloader"
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"be.ems/src/framework/constants/common"
|
||||||
"be.ems/src/framework/logger"
|
"be.ems/src/framework/logger"
|
||||||
"be.ems/src/framework/utils/crypto"
|
"be.ems/src/framework/utils/crypto"
|
||||||
"be.ems/src/framework/utils/parse"
|
"be.ems/src/framework/utils/parse"
|
||||||
@@ -98,11 +99,11 @@ func Launch() {
|
|||||||
// 检查文件是否存在
|
// 检查文件是否存在
|
||||||
if _, err := os.Stat(filePath()); err != nil {
|
if _, err := os.Stat(filePath()); err != nil {
|
||||||
LaunchInfo = map[string]any{
|
LaunchInfo = map[string]any{
|
||||||
"code": Code, // 机器码
|
"code": Code, // 机器码
|
||||||
"firstTime": time.Now().UnixMilli(), // 首次启动使用时间
|
"useTime": time.Now().UnixMilli(), // 首次使用时间
|
||||||
|
|
||||||
"sysGuide": true, // 首次引导
|
common.LAUNCH_BOOTLOADER: true, // 启动引导
|
||||||
"sysGuideTime": 0, // 引导完成时间
|
common.LAUNCH_BOOTLOADER + "Time": 0, // 引导完成时间
|
||||||
}
|
}
|
||||||
codeFileWrite(LaunchInfo)
|
codeFileWrite(LaunchInfo)
|
||||||
} else {
|
} else {
|
||||||
@@ -125,7 +126,7 @@ func SetLaunchInfo(info map[string]any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 固定值禁止变更
|
// 固定值禁止变更
|
||||||
constKeys := []string{"code", "firstTime"}
|
constKeys := []string{"code", "useTime"}
|
||||||
for k, v := range info {
|
for k, v := range info {
|
||||||
constKey := false
|
constKey := false
|
||||||
for _, ck := range constKeys {
|
for _, ck := range constKeys {
|
||||||
|
|||||||
@@ -25,11 +25,12 @@ func Setup(router *gin.Engine) {
|
|||||||
|
|
||||||
// 系统可暴露的配置信息
|
// 系统可暴露的配置信息
|
||||||
indexGroup.GET("/sys-conf", controller.NewCommont.SysConfig)
|
indexGroup.GET("/sys-conf", controller.NewCommont.SysConfig)
|
||||||
// 首次引导安装
|
// 系统引导初始化
|
||||||
guideGroup := router.Group("/guide")
|
guideGroup := router.Group("/bootloader")
|
||||||
{
|
{
|
||||||
guideGroup.POST("", controller.NewLaunch.GuideStart)
|
guideGroup.POST("", controller.NewBootloader.BootloaderStart)
|
||||||
guideGroup.PUT("", middleware.PreAuthorize(nil), controller.NewLaunch.GuideDone)
|
guideGroup.PUT("", middleware.PreAuthorize(nil), controller.NewBootloader.BootloaderDone)
|
||||||
|
guideGroup.DELETE("", middleware.PreAuthorize(nil), controller.NewBootloader.BootloaderReset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证码操作处理
|
// 验证码操作处理
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
adminConstants "be.ems/src/framework/constants/admin"
|
adminConstants "be.ems/src/framework/constants/admin"
|
||||||
|
"be.ems/src/framework/constants/common"
|
||||||
tokenConstants "be.ems/src/framework/constants/token"
|
tokenConstants "be.ems/src/framework/constants/token"
|
||||||
|
"be.ems/src/framework/utils/cmd"
|
||||||
"be.ems/src/framework/utils/ctx"
|
"be.ems/src/framework/utils/ctx"
|
||||||
"be.ems/src/framework/utils/machine"
|
"be.ems/src/framework/utils/machine"
|
||||||
tokenUtils "be.ems/src/framework/utils/token"
|
tokenUtils "be.ems/src/framework/utils/token"
|
||||||
@@ -15,34 +18,34 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 实例化控制层 LaunchController 结构体
|
// 实例化控制层 BootloaderController 结构体
|
||||||
var NewLaunch = &LaunchController{
|
var NewBootloader = &BootloaderController{
|
||||||
accountService: commonService.NewAccountImpl,
|
accountService: commonService.NewAccountImpl,
|
||||||
sysUserService: systemService.NewSysUserImpl,
|
sysUserService: systemService.NewSysUserImpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首次启动安装
|
// 系统引导初始化
|
||||||
//
|
//
|
||||||
// PATH /launch
|
// PATH /bootloader
|
||||||
type LaunchController struct {
|
type BootloaderController struct {
|
||||||
// 账号身份操作服务
|
// 账号身份操作服务
|
||||||
accountService commonService.IAccount
|
accountService commonService.IAccount
|
||||||
// 用户信息服务
|
// 用户信息服务
|
||||||
sysUserService systemService.ISysUser
|
sysUserService systemService.ISysUser
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首次引导安装开始
|
// 首次引导开始
|
||||||
//
|
//
|
||||||
// POST /guide
|
// POST /
|
||||||
func (s *LaunchController) GuideStart(c *gin.Context) {
|
func (s *BootloaderController) BootloaderStart(c *gin.Context) {
|
||||||
// 是否完成引导
|
// 是否完成引导
|
||||||
launchInfo := machine.LaunchInfo
|
launchInfo := machine.LaunchInfo
|
||||||
if launchInfo == nil {
|
if launchInfo == nil {
|
||||||
c.JSON(200, result.Err(nil))
|
c.JSON(200, result.Err(nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if v, ok := launchInfo["sysGuide"]; ok && !v.(bool) {
|
if v, ok := launchInfo[common.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||||
c.JSON(200, result.ErrMsg("guide done"))
|
c.JSON(200, result.ErrMsg("bootloader done"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,25 +82,67 @@ func (s *LaunchController) GuideStart(c *gin.Context) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首次引导安装完成
|
// 首次引导完成
|
||||||
//
|
//
|
||||||
// PUT /guide
|
// PUT /
|
||||||
func (s *LaunchController) GuideDone(c *gin.Context) {
|
func (s *BootloaderController) BootloaderDone(c *gin.Context) {
|
||||||
// 是否完成引导
|
// 是否完成引导
|
||||||
launchInfo := machine.LaunchInfo
|
launchInfo := machine.LaunchInfo
|
||||||
if launchInfo == nil {
|
if launchInfo == nil {
|
||||||
c.JSON(200, result.Err(nil))
|
c.JSON(200, result.Err(nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if v, ok := launchInfo["sysGuide"]; ok && !v.(bool) {
|
if v, ok := launchInfo[common.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||||
c.JSON(200, result.ErrMsg("guide done"))
|
c.JSON(200, result.ErrMsg("bootloader done"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录完成时间
|
// 记录完成时间
|
||||||
err := machine.SetLaunchInfo(map[string]any{
|
err := machine.SetLaunchInfo(map[string]any{
|
||||||
"sysGuide": false, // 首次引导
|
common.LAUNCH_BOOTLOADER: false, // 启动引导
|
||||||
"sysGuideTime": time.Now().UnixMilli(), // 引导完成时间
|
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 {
|
if err != nil {
|
||||||
c.JSON(200, result.ErrMsg(err.Error()))
|
c.JSON(200, result.ErrMsg(err.Error()))
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"be.ems/lib/global"
|
"be.ems/lib/global"
|
||||||
"be.ems/src/framework/config"
|
"be.ems/src/framework/config"
|
||||||
|
"be.ems/src/framework/constants/common"
|
||||||
"be.ems/src/framework/utils/machine"
|
"be.ems/src/framework/utils/machine"
|
||||||
systemService "be.ems/src/modules/system/service"
|
systemService "be.ems/src/modules/system/service"
|
||||||
)
|
)
|
||||||
@@ -33,13 +34,13 @@ func (s *CommontImpl) SystemConfigInfo() map[string]string {
|
|||||||
// 系统首次使用标记
|
// 系统首次使用标记
|
||||||
launchInfo := machine.LaunchInfo
|
launchInfo := machine.LaunchInfo
|
||||||
if launchInfo != nil {
|
if launchInfo != nil {
|
||||||
if v, ok := launchInfo["sysGuide"]; ok {
|
if v, ok := launchInfo[common.LAUNCH_BOOTLOADER]; ok {
|
||||||
infoMap["sysGuide"] = fmt.Sprint(v)
|
infoMap[common.LAUNCH_BOOTLOADER] = fmt.Sprint(v)
|
||||||
} else {
|
} else {
|
||||||
infoMap["sysGuide"] = "true"
|
infoMap[common.LAUNCH_BOOTLOADER] = "true"
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
infoMap["sysGuide"] = "true"
|
infoMap[common.LAUNCH_BOOTLOADER] = "true"
|
||||||
}
|
}
|
||||||
// 序列号
|
// 序列号
|
||||||
infoMap["serialNum"] = fmt.Sprint(config.Get("omc.sn"))
|
infoMap["serialNum"] = fmt.Sprint(config.Get("omc.sn"))
|
||||||
|
|||||||
Reference in New Issue
Block a user