style: 目录变更系统配置和引导初始化
This commit is contained in:
@@ -52,4 +52,16 @@ func Setup(router *gin.Engine) {
|
||||
fileGroup.DELETE("", middleware.AuthorizeUser(nil), controller.NewFile.Remove)
|
||||
fileGroup.POST("/transfer-static-file", middleware.AuthorizeUser(nil), controller.NewFile.TransferStaticFile)
|
||||
}
|
||||
|
||||
// 系统可暴露的配置信息
|
||||
router.GET("/sys-conf", controller.NewSysConf.Handler)
|
||||
|
||||
// 系统引导初始化
|
||||
guideGroup := router.Group("/bootloader")
|
||||
{
|
||||
guideGroup.POST("", controller.NewBootloader.Start)
|
||||
guideGroup.PUT("", middleware.AuthorizeUser(nil), controller.NewBootloader.Done)
|
||||
guideGroup.DELETE("", middleware.AuthorizeUser(nil), controller.NewBootloader.Reset)
|
||||
guideGroup.PUT("/account", middleware.AuthorizeUser(nil), controller.NewBootloader.Account)
|
||||
}
|
||||
}
|
||||
|
||||
192
src/modules/common/controller/bootloader.go
Normal file
192
src/modules/common/controller/bootloader.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/token"
|
||||
"be.ems/src/framework/utils/machine"
|
||||
"be.ems/src/modules/auth/service"
|
||||
systemService "be.ems/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 BootloaderController 结构体
|
||||
var NewBootloader = &BootloaderController{
|
||||
accountService: service.NewAccount,
|
||||
sysUserService: systemService.NewSysUser,
|
||||
}
|
||||
|
||||
// 系统引导初始化
|
||||
//
|
||||
// PATH /bootloader
|
||||
type BootloaderController struct {
|
||||
accountService *service.Account // 账号身份操作服务
|
||||
sysUserService *systemService.SysUser // 用户信息服务
|
||||
}
|
||||
|
||||
// 首次引导开始
|
||||
//
|
||||
// POST /
|
||||
func (s *BootloaderController) Start(c *gin.Context) {
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, resp.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[constants.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||
c.JSON(200, resp.ErrMsg("bootloader done"))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询用户登录账号
|
||||
sysUser := s.sysUserService.FindById(1)
|
||||
if sysUser.UserId != 1 {
|
||||
c.JSON(200, resp.ErrMsg("not found user data"))
|
||||
return
|
||||
}
|
||||
|
||||
// 登录用户信息
|
||||
info := token.UserInfo{
|
||||
UserId: sysUser.UserId,
|
||||
DeptId: sysUser.DeptId,
|
||||
User: sysUser,
|
||||
Permissions: []string{constants.SYS_PERMISSION_SYSTEM},
|
||||
}
|
||||
|
||||
// 当前请求信息
|
||||
ipaddr, location := reqctx.IPAddrLocation(c)
|
||||
os, browser := reqctx.UaOsBrowser(c)
|
||||
deviceFingerprint := reqctx.DeviceFingerprint(c, info.UserId)
|
||||
|
||||
// 生成访问令牌
|
||||
accessToken, expiresIn := token.UserTokenCreate(info.UserId, deviceFingerprint, "access")
|
||||
if accessToken == "" || expiresIn == 0 {
|
||||
c.JSON(200, resp.ErrMsg("token generation failed"))
|
||||
return
|
||||
}
|
||||
// 记录令牌,创建系统访问记录
|
||||
token.UserInfoCreate(&info, deviceFingerprint, [4]string{ipaddr, location, os, browser})
|
||||
// 创建系统访问记录
|
||||
s.accountService.UpdateLoginDateAndIP(info)
|
||||
|
||||
c.JSON(200, resp.OkData(map[string]any{
|
||||
"tokenType": constants.HEADER_PREFIX,
|
||||
"accessToken": accessToken,
|
||||
"expiresIn": expiresIn,
|
||||
"refreshToken": "",
|
||||
"refreshExpiresIn": 0,
|
||||
"userId": info.UserId,
|
||||
}))
|
||||
}
|
||||
|
||||
// 首次引导完成
|
||||
//
|
||||
// PUT /
|
||||
func (s *BootloaderController) Done(c *gin.Context) {
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, resp.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[constants.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||
c.JSON(200, resp.ErrMsg("bootloader done"))
|
||||
return
|
||||
}
|
||||
|
||||
// 标记引导完成
|
||||
if err := machine.Bootloader(false); err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 清除授权信息
|
||||
token.UserInfoRemove(reqctx.Authorization(c))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
}
|
||||
|
||||
// 引导系统数据重置
|
||||
//
|
||||
// DELETE /
|
||||
func (s *BootloaderController) Reset(c *gin.Context) {
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, resp.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[constants.LAUNCH_BOOTLOADER]; ok && v.(bool) {
|
||||
c.JSON(200, resp.ErrMsg("bootloader not done"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := machine.Reset(); err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 清除授权信息
|
||||
token.UserInfoRemove(reqctx.Authorization(c))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
}
|
||||
|
||||
// 账号变更
|
||||
//
|
||||
// PUT /account
|
||||
func (s *BootloaderController) Account(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
UserName string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(422001, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查用户密码策略强度
|
||||
ok, errMsg := s.sysUserService.ValidatePasswordPolicy(body.Password, language)
|
||||
if !ok {
|
||||
c.JSON(200, resp.ErrMsg(errMsg))
|
||||
return
|
||||
}
|
||||
// if !regular.ValidPassword(body.Password) {
|
||||
// // 登录密码至少包含大小写字母、数字、特殊符号,且不少于6位
|
||||
// c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.errPasswd")))
|
||||
// return
|
||||
// }
|
||||
|
||||
// 是否完成引导
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo == nil {
|
||||
c.JSON(200, resp.Err(nil))
|
||||
return
|
||||
}
|
||||
if v, ok := launchInfo[constants.LAUNCH_BOOTLOADER]; ok && !v.(bool) {
|
||||
c.JSON(200, resp.ErrMsg("bootloader done"))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询用户登录账号
|
||||
sysUser := s.sysUserService.FindById(2)
|
||||
if sysUser.UserId != 2 {
|
||||
c.JSON(200, resp.ErrMsg("not found user data"))
|
||||
return
|
||||
}
|
||||
sysUser.UserName = body.UserName
|
||||
sysUser.NickName = body.UserName
|
||||
sysUser.Password = body.Password
|
||||
sysUser.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysUserService.Update(sysUser)
|
||||
if rows > 0 {
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
100
src/modules/common/controller/sys_conf.go
Normal file
100
src/modules/common/controller/sys_conf.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"be.ems/src/framework/config"
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/machine"
|
||||
systemService "be.ems/src/modules/system/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysConfController 结构体
|
||||
var NewSysConf = &SysConfController{
|
||||
sysUserService: systemService.NewSysUser,
|
||||
sysConfigService: systemService.NewSysConfig,
|
||||
}
|
||||
|
||||
// 系统的配置信息
|
||||
//
|
||||
// PATH /sys-conf
|
||||
type SysConfController struct {
|
||||
sysUserService *systemService.SysUser // 用户信息服务
|
||||
sysConfigService *systemService.SysConfig // 参数配置服务
|
||||
}
|
||||
|
||||
// 系统的配置信息
|
||||
//
|
||||
// GET /
|
||||
//
|
||||
// @Tags common
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} object "Response Results"
|
||||
// @Summary Configuration information for the system
|
||||
// @Description Configuration information for the system
|
||||
// @Router /sys-conf [get]
|
||||
func (s SysConfController) Handler(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
|
||||
infoMap := map[string]string{}
|
||||
// 获取打包注入的全局变量信息
|
||||
infoMap["version"] = config.Version
|
||||
// 系统首次使用标记
|
||||
launchInfo := machine.LaunchInfo
|
||||
if launchInfo != nil {
|
||||
if v, ok := launchInfo[constants.LAUNCH_BOOTLOADER]; ok {
|
||||
infoMap[constants.LAUNCH_BOOTLOADER] = fmt.Sprint(v)
|
||||
} else {
|
||||
infoMap[constants.LAUNCH_BOOTLOADER] = "true"
|
||||
}
|
||||
} else {
|
||||
infoMap[constants.LAUNCH_BOOTLOADER] = "true"
|
||||
}
|
||||
// 服务版本
|
||||
infoMap["serverVersion"] = fmt.Sprint(config.Get("serverVersion"))
|
||||
// 用户登录认证
|
||||
infoMap["loginAuth"] = fmt.Sprint(config.Get("serverLoginAuth"))
|
||||
// 用户接口加密
|
||||
infoMap["cryptoApi"] = fmt.Sprint(config.Get("serverCryptoApi"))
|
||||
// 序列号
|
||||
infoMap["serialNum"] = fmt.Sprint(config.Get("omc.sn"))
|
||||
// 获取LOGO类型
|
||||
logoType := s.sysConfigService.FindValueByKey("sys.logo.type")
|
||||
infoMap["logoType"] = logoType
|
||||
// 获取LOGO文件
|
||||
filePathIcon := s.sysConfigService.FindValueByKey("sys.logo.filePathIcon")
|
||||
infoMap["filePathIcon"] = filePathIcon
|
||||
filePathBrand := s.sysConfigService.FindValueByKey("sys.logo.filePathBrand")
|
||||
infoMap["filePathBrand"] = filePathBrand
|
||||
// 获取系统名称
|
||||
title := s.sysConfigService.FindValueByKey("sys.title")
|
||||
infoMap["title"] = i18n.TKey(language, title)
|
||||
// 获取版权声明
|
||||
copyright := s.sysConfigService.FindValueByKey("sys.copyright")
|
||||
infoMap["copyright"] = i18n.TKey(language, copyright)
|
||||
// 获取是否开启用户注册功能
|
||||
registerUser := s.sysConfigService.FindValueByKey("sys.account.registerUser")
|
||||
infoMap["registerUser"] = registerUser
|
||||
// 获取登录界面背景
|
||||
loginBackground := s.sysConfigService.FindValueByKey("sys.loginBackground")
|
||||
infoMap["loginBackground"] = loginBackground
|
||||
// 系统设置-官网网址
|
||||
officialUrl := s.sysConfigService.FindValueByKey("sys.officialUrl")
|
||||
infoMap["officialUrl"] = officialUrl
|
||||
// 系统设置-系统使用文档
|
||||
helpDoc := s.sysConfigService.FindValueByKey("sys.helpDoc")
|
||||
infoMap["helpDoc"] = helpDoc
|
||||
// 国际化切换
|
||||
i18nOpen := s.sysConfigService.FindValueByKey("sys.i18n.open")
|
||||
infoMap["i18nOpen"] = i18nOpen
|
||||
// 国际化默认语言
|
||||
i18nDefault := s.sysConfigService.FindValueByKey("sys.i18n.default")
|
||||
infoMap["i18nDefault"] = i18nDefault
|
||||
|
||||
c.JSON(200, resp.OkData(infoMap))
|
||||
}
|
||||
Reference in New Issue
Block a user