feat: 新增系统全局配置&修改账号密码

This commit is contained in:
meilin.huang
2022-08-26 20:15:36 +08:00
parent 09e6bdcf7e
commit 7761fe0288
89 changed files with 2035 additions and 209 deletions

View File

@@ -23,6 +23,7 @@ type Account struct {
ResourceApp application.Resource
RoleApp application.Role
MsgApp application.Msg
ConfigApp application.Config
}
/** 登录者个人相关操作 **/
@@ -32,8 +33,11 @@ func (a *Account) Login(rc *ctx.ReqCtx) {
loginForm := &form.LoginForm{}
ginx.BindJsonAndValid(rc.GinCtx, loginForm)
// 校验验证码
biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
// 判断是否有开启登录验证码校验
if a.ConfigApp.GetConfig(entity.ConfigKeyUseLoginCaptcha).BoolValue(true) {
// 校验验证码
biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
}
originPwd, err := utils.DefaultRsaDecrypt(loginForm.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
@@ -145,17 +149,6 @@ func (a *Account) saveLogin(account *entity.Account, ip string) {
loginMsg.Creator = account.Username
loginMsg.CreatorId = account.Id
a.MsgApp.Create(loginMsg)
// bodyMap, err := httpclient.NewRequest(fmt.Sprintf("http://ip-api.com/json/%s?lang=zh-CN", ip)).Get().BodyToMap()
// if err != nil {
// global.Log.Errorf("获取客户端ip地址信息失败%s", err.Error())
// return
// }
// if bodyMap["status"].(string) == "fail" {
// return
// }
// msg := fmt.Sprintf("%s于%s-%s登录", account.Username, bodyMap["regionName"], bodyMap["city"])
// global.Log.Info(msg)
}
// 获取个人账号信息
@@ -203,8 +196,8 @@ func (a *Account) Accounts(rc *ctx.ReqCtx) {
rc.ResData = a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
}
// @router /accounts [get]
func (a *Account) CreateAccount(rc *ctx.ReqCtx) {
// @router /accounts
func (a *Account) SaveAccount(rc *ctx.ReqCtx) {
form := &form.AccountCreateForm{}
ginx.BindJsonAndValid(rc.GinCtx, form)
rc.ReqParam = form
@@ -212,7 +205,16 @@ func (a *Account) CreateAccount(rc *ctx.ReqCtx) {
account := &entity.Account{}
utils.Copy(account, form)
account.SetBaseInfo(rc.LoginAccount)
a.AccountApp.Create(account)
if account.Id == 0 {
a.AccountApp.Create(account)
} else {
if account.Password != "" {
biz.IsTrue(CheckPasswordLever(account.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
account.Password = utils.PwdHash(account.Password)
}
a.AccountApp.Update(account)
}
}
func (a *Account) ChangeStatus(rc *ctx.ReqCtx) {

View File

@@ -0,0 +1,39 @@
package api
import (
"mayfly-go/internal/sys/api/form"
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/utils"
)
type Config struct {
ConfigApp application.Config
}
func (c *Config) Configs(rc *ctx.ReqCtx) {
g := rc.GinCtx
condition := &entity.Config{Key: g.Query("key")}
rc.ResData = c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config))
}
func (c *Config) GetConfigValueByKey(rc *ctx.ReqCtx) {
key := rc.GinCtx.Query("key")
biz.NotEmpty(key, "key不能为空")
rc.ResData = c.ConfigApp.GetConfig(key).Value
}
func (c *Config) SaveConfig(rc *ctx.ReqCtx) {
g := rc.GinCtx
form := &form.ConfigForm{}
ginx.BindJsonAndValid(g, form)
rc.ReqParam = form
config := new(entity.Config)
utils.Copy(config, form)
config.SetBaseInfo(rc.LoginAccount)
c.ConfigApp.Save(config)
}

View File

@@ -1,7 +1,9 @@
package form
type AccountCreateForm struct {
Id uint64
Username *string `json:"username" binding:"required,min=4,max=16"`
Password *string `json:"password"`
}
type AccountUpdateForm struct {

View File

@@ -0,0 +1,9 @@
package form
type ConfigForm struct {
Id int
Name string `binding:"required"`
Key string `binding:"required"`
Value string
Remark string `json:"remark"`
}

View File

@@ -4,6 +4,6 @@ package form
type LoginForm struct {
Username string `json:"username" binding:"required"`
Password string `binding:"required"`
Captcha string `binding:"required"`
Cid string `binding:"required"`
Captcha string
Cid string
}

View File

@@ -0,0 +1,46 @@
package application
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/internal/sys/infrastructure/persistence"
"mayfly-go/pkg/global"
"mayfly-go/pkg/model"
)
type Config interface {
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
Save(config *entity.Config)
// 获取指定key的配置信息, 不会返回nil, 若不存在则值都默认值即空字符串
GetConfig(key string) *entity.Config
}
type configAppImpl struct {
configRepo repository.Config
}
var ConfigApp Config = &configAppImpl{
configRepo: persistence.ConfigDao,
}
func (a *configAppImpl) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return a.configRepo.GetPageList(condition, pageParam, toEntity)
}
func (a *configAppImpl) Save(config *entity.Config) {
if config.Id == 0 {
a.configRepo.Insert(config)
} else {
a.configRepo.Update(config)
}
}
func (a *configAppImpl) GetConfig(key string) *entity.Config {
config := &entity.Config{Key: key}
if err := a.configRepo.GetConfig(config, "Id", "Key", "Value"); err != nil {
global.Log.Warnf("不存在key = [%s] 的系统配置", key)
}
return config
}

View File

@@ -0,0 +1,29 @@
package entity
import "mayfly-go/pkg/model"
const (
ConfigKeyUseLoginCaptcha string = "UseLoginCaptcha" // 是否使用登录验证码
)
type Config struct {
model.Model
Name string `json:"name"` // 配置名
Key string `json:"key"` // 配置key
Value string `json:"value"`
Remark string `json:"remark"`
}
func (a *Config) TableName() string {
return "t_sys_config"
}
// 若配置信息不存在, 则返回传递的默认值.
// 否则只有value == "1"为true其他为false
func (c *Config) BoolValue(defaultValue bool) bool {
// 如果值不存在,则返回默认值
if c.Id == 0 {
return defaultValue
}
return c.Value == "1"
}

View File

@@ -0,0 +1,18 @@
package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/model"
)
type Config interface {
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
Insert(config *entity.Config)
Update(config *entity.Config)
GetConfig(config *entity.Config, cols ...string) error
GetByCondition(condition *entity.Config, cols ...string) error
}

View File

@@ -0,0 +1,32 @@
package persistence
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type configRepo struct{}
var ConfigDao repository.Config = &configRepo{}
func (m *configRepo) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity)
}
func (m *configRepo) Insert(config *entity.Config) {
biz.ErrIsNil(model.Insert(config), "新增系统配置失败")
}
func (m *configRepo) Update(config *entity.Config) {
biz.ErrIsNil(model.UpdateById(config), "更新系统配置失败")
}
func (m *configRepo) GetConfig(condition *entity.Config, cols ...string) error {
return model.GetBy(condition, cols...)
}
func (r *configRepo) GetByCondition(condition *entity.Config, cols ...string) error {
return model.GetBy(condition, cols...)
}

View File

@@ -15,6 +15,7 @@ func InitAccountRouter(router *gin.RouterGroup) {
ResourceApp: application.ResourceApp,
RoleApp: application.RoleApp,
MsgApp: application.MsgApp,
ConfigApp: application.ConfigApp,
}
{
// 用户登录
@@ -61,7 +62,7 @@ func InitAccountRouter(router *gin.RouterGroup) {
ctx.NewReqCtxWithGin(c).
WithRequiredPermission(addAccountPermission).
WithLog(createAccount).
Handle(a.CreateAccount)
Handle(a.SaveAccount)
})
changeStatus := ctx.NewLogInfo("修改账号状态").WithSave(true)

View File

@@ -0,0 +1,30 @@
package router
import (
"mayfly-go/internal/sys/api"
"mayfly-go/internal/sys/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
)
func InitSysConfigRouter(router *gin.RouterGroup) {
r := &api.Config{ConfigApp: application.ConfigApp}
db := router.Group("sys/configs")
{
db.GET("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).Handle(r.Configs)
})
db.GET("/value", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithNeedToken(false).Handle(r.GetConfigValueByKey)
})
saveConfig := ctx.NewLogInfo("保存系统配置信息").WithSave(true)
db.POST("", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).
WithLog(saveConfig).
Handle(r.SaveConfig)
})
}
}