feat: 更新多个模块以支持新的数据结构和日志格式

This commit is contained in:
TsMask
2025-02-20 10:08:27 +08:00
parent 045a2b6b01
commit f3c33b31ac
272 changed files with 13246 additions and 15885 deletions

View File

@@ -1,44 +1,36 @@
package controller
import (
"be.ems/src/framework/config"
"be.ems/src/framework/constants/admin"
"be.ems/src/framework/constants/uploadsubpath"
"fmt"
"be.ems/src/framework/i18n"
"be.ems/src/framework/reqctx"
"be.ems/src/framework/resp"
"be.ems/src/framework/token"
"be.ems/src/framework/utils/crypto"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/regular"
"be.ems/src/framework/utils/token"
"be.ems/src/framework/vo/result"
"be.ems/src/modules/system/model"
"be.ems/src/modules/system/service"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// 实例化控制层 SysProfileController 结构体
var NewSysProfile = &SysProfileController{
sysUserService: service.NewSysUserImpl,
sysRoleService: service.NewSysRoleImpl,
sysPostService: service.NewSysPostImpl,
sysMenuService: service.NewSysMenuImpl,
sysUserService: service.NewSysUser,
sysRoleService: service.NewSysRole,
sysPostService: service.NewSysPost,
sysMenuService: service.NewSysMenu,
}
// 个人信息
//
// PATH /system/user/profile
type SysProfileController struct {
// 用户服务
sysUserService service.ISysUser
// 角色服务
sysRoleService service.ISysRole
// 岗位服务
sysPostService service.ISysPost
// 菜单服务
sysMenuService service.ISysMenu
sysUserService *service.SysUser // 用户服务
sysRoleService *service.SysRole // 角色服务
sysPostService *service.SysPost // 岗位服务
sysMenuService *service.SysMenu // 菜单服务
}
// 个人信息
@@ -54,27 +46,23 @@ type SysProfileController struct {
// @Description Personal Information
// @Router /system/user/profile [get]
func (s *SysProfileController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
loginUser, err := ctx.LoginUser(c)
language := reqctx.AcceptLanguage(c)
loginUser, err := reqctx.LoginUser(c)
if err != nil {
c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
c.JSON(401, resp.CodeMsg(401, i18n.TKey(language, err.Error())))
return
}
// 查询用户所属角色组
roleGroup := []string{}
roles := s.sysRoleService.SelectRoleListByUserId(loginUser.UserID)
roles := s.sysRoleService.FindByUserId(loginUser.User.UserId)
for _, role := range roles {
roleGroup = append(roleGroup, i18n.TKey(language, role.RoleName))
}
isAdmin := config.IsAdmin(loginUser.UserID)
if isAdmin {
roleGroup = append(roleGroup, i18n.TKey(language, "role.system"))
}
// 查询用户所属岗位组
postGroup := []string{}
posts := s.sysPostService.SelectPostListByUserId(loginUser.UserID)
posts := s.sysPostService.FindByUserId(loginUser.User.UserId)
for _, post := range posts {
postGroup = append(postGroup, i18n.TKey(language, post.PostName))
}
@@ -85,7 +73,7 @@ func (s *SysProfileController) Info(c *gin.Context) {
for ri := range loginUser.User.Roles {
loginUser.User.Roles[ri].RoleName = i18n.TKey(language, loginUser.User.Roles[ri].RoleName)
}
c.JSON(200, result.OkData(map[string]any{
c.JSON(200, resp.OkData(map[string]any{
"user": loginUser.User,
"roleGroup": parse.RemoveDuplicates(roleGroup),
"postGroup": parse.RemoveDuplicates(postGroup),
@@ -106,46 +94,43 @@ func (s *SysProfileController) Info(c *gin.Context) {
// @Description Personal Information Modification
// @Router /system/user/profile [put]
func (s *SysProfileController) UpdateProfile(c *gin.Context) {
language := ctx.AcceptLanguage(c)
language := reqctx.AcceptLanguage(c)
var body struct {
// 昵称
NickName string `json:"nickName" binding:"required"`
// 性别
Sex string `json:"sex" binding:"required"`
// 手机号
PhoneNumber string `json:"phonenumber"`
// 邮箱
Email string `json:"email"`
NickName string `json:"nickName" binding:"required"` // 昵称
Sex string `json:"sex" binding:"required,oneof=0 1 2"` // 性别
Phone string `json:"phone"` // 手机号
Email string `json:"email"` // 邮箱
Avatar string `json:"avatar"` // 头像地址
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.Sex == "" {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
// 登录用户信息
loginUser, err := ctx.LoginUser(c)
loginUser, err := reqctx.LoginUser(c)
if err != nil {
c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
c.JSON(401, resp.CodeMsg(401, i18n.TKey(language, err.Error())))
return
}
userId := loginUser.UserID
userId := loginUser.UserId
userName := loginUser.User.UserName
// 检查手机号码格式并判断是否唯一
if body.PhoneNumber != "" {
if regular.ValidMobile(body.PhoneNumber) {
uniquePhone := s.sysUserService.CheckUniquePhone(body.PhoneNumber, userId)
if body.Phone != "" {
if regular.ValidMobile(body.Phone) {
uniquePhone := s.sysUserService.CheckUniqueByPhone(body.Phone, userId)
if !uniquePhone {
// 修改用户【%s】失败,手机号码已存在
// c.JSON(200, resp.CodeMsg(40018, "抱歉,手机号码已存在"))
msg := i18n.TTemplate(language, "user.errPhoneExists", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
c.JSON(200, resp.ErrMsg(msg))
return
}
} else {
// 修改用户【%s】失败,手机号码格式错误
// c.JSON(200, resp.CodeMsg(40018, "抱歉,手机号码格式错误"))
msg := i18n.TTemplate(language, "user.errPhoneFormat", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
c.JSON(200, resp.ErrMsg(msg))
return
}
}
@@ -153,55 +138,53 @@ func (s *SysProfileController) UpdateProfile(c *gin.Context) {
// 检查邮箱格式并判断是否唯一
if body.Email != "" {
if regular.ValidEmail(body.Email) {
uniqueEmail := s.sysUserService.CheckUniqueEmail(body.Email, userId)
uniqueEmail := s.sysUserService.CheckUniqueByEmail(body.Email, userId)
if !uniqueEmail {
// 修改用户【%s】失败,邮箱已存在
// c.JSON(200, resp.CodeMsg(40019, "抱歉,邮箱已存在"))
msg := i18n.TTemplate(language, "user.errEmailExists", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
c.JSON(200, resp.ErrMsg(msg))
return
}
} else {
// 修改用户【%s】失败,邮箱格式错误
// c.JSON(200, resp.CodeMsg(40019, "抱歉,邮箱格式错误"))
msg := i18n.TTemplate(language, "user.errEmailFormat", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
c.JSON(200, resp.ErrMsg(msg))
return
}
}
// 用户基本资料
sysUser := model.SysUser{
UserID: userId,
UpdateBy: userName,
NickName: body.NickName,
PhoneNumber: body.PhoneNumber,
Email: body.Email,
Sex: body.Sex,
Remark: loginUser.User.Remark,
// 查询当前登录用户信息
userInfo := s.sysUserService.FindById(userId)
if userInfo.UserId != userId {
// c.JSON(200, resp.ErrMsg("没有权限访问用户数据!"))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.noData")))
return
}
rows := s.sysUserService.UpdateUser(sysUser)
// 用户基本资料
userInfo.NickName = body.NickName
userInfo.Phone = body.Phone
userInfo.Email = body.Email
userInfo.Sex = body.Sex
userInfo.Avatar = body.Avatar
userInfo.Password = "" // 密码不更新
userInfo.UpdateBy = userInfo.UserName
rows := s.sysUserService.Update(userInfo)
if rows > 0 {
// 更新缓存用户信息
loginUser.User = s.sysUserService.SelectUserByUserName(userName)
// 用户权限组标识
isAdmin := config.IsAdmin(sysUser.UserID)
if isAdmin {
loginUser.Permissions = []string{admin.PERMISSION}
} else {
perms := s.sysMenuService.SelectMenuPermsByUserId(sysUser.UserID)
loginUser.Permissions = parse.RemoveDuplicates(perms)
}
loginUser.User = userInfo
// 刷新令牌信息
token.Cache(&loginUser)
c.JSON(200, result.Ok(nil))
c.JSON(200, resp.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
c.JSON(200, resp.ErrMsg("Modification of personal information exception"))
}
// 个人重置密码
//
// PUT /updatePwd
// PUT /password
//
// @Tags system/user/profile
// @Accept json
@@ -212,131 +195,55 @@ func (s *SysProfileController) UpdateProfile(c *gin.Context) {
// @Summary Personal Reset Password
// @Description Personal Reset Password
// @Router /system/user/profile/updatePwd [put]
func (s *SysProfileController) UpdatePwd(c *gin.Context) {
language := ctx.AcceptLanguage(c)
func (s *SysProfileController) UpdatePassword(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body struct {
// 旧密码
OldPassword string `json:"oldPassword" binding:"required"`
// 新密码
NewPassword string `json:"newPassword" binding:"required"`
OldPassword string `json:"oldPassword" binding:"required"` // 旧密码
NewPassword string `json:"newPassword" binding:"required"` // 新密码
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
// 登录用户信息
loginUser, err := ctx.LoginUser(c)
loginUser, err := reqctx.LoginUser(c)
if err != nil {
c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
c.JSON(401, resp.CodeMsg(401, i18n.TKey(language, err.Error())))
return
}
userId := loginUser.UserID
userName := loginUser.User.UserName
userId := loginUser.UserId
// 查询当前登录用户信息得到密码值
user := s.sysUserService.SelectUserById(userId)
if user.UserID != userId {
// 没有可访问用户数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.noData")))
userInfo := s.sysUserService.FindById(userId)
if userInfo.UserId != userId {
// c.JSON(200, resp.ErrMsg("没有权限访问用户数据!"))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.noData")))
return
}
// 检查匹配用户密码
oldCompare := crypto.BcryptCompare(body.OldPassword, user.Password)
oldCompare := crypto.BcryptCompare(body.OldPassword, userInfo.Password)
if !oldCompare {
// 修改密码失败,旧密码错误
c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.errPasswdOld")))
// c.JSON(200, resp.ErrMsg("修改密码失败,旧密码错误"))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.errPasswdOld")))
return
}
newCompare := crypto.BcryptCompare(body.NewPassword, user.Password)
newCompare := crypto.BcryptCompare(body.NewPassword, userInfo.Password)
if newCompare {
// 新密码不能与旧密码相同
c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.errPasswdEqOld")))
// c.JSON(200, resp.ErrMsg("新密码不能与旧密码相同"))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.errPasswdEqOld")))
return
}
// 修改新密码
sysUser := model.SysUser{
UserID: userId,
UpdateBy: userName,
Password: body.NewPassword,
Sex: user.Sex,
PhoneNumber: user.PhoneNumber,
Email: user.Email,
Remark: user.Remark,
}
rows := s.sysUserService.UpdateUser(sysUser)
userInfo.Password = body.NewPassword
userInfo.UpdateBy = userInfo.UserName
rows := s.sysUserService.Update(userInfo)
if rows > 0 {
c.JSON(200, result.Ok(nil))
c.JSON(200, resp.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 个人头像上传
//
// POST /avatar
//
// @Tags system/user/profile
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "The file to upload."
// @Success 200 {object} object "Response Results"
// @Security TokenAuth
// @Summary Personal avatar upload
// @Description Personal avatar upload
// @Router /system/user/profile/avatar [post]
func (s *SysProfileController) Avatar(c *gin.Context) {
language := ctx.AcceptLanguage(c)
formFile, err := c.FormFile("file")
if err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 上传文件转存
filePath, err := file.TransferUploadFile(formFile, uploadsubpath.AVATART, []string{".jpg", ".jpeg", ".png"})
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 登录用户信息
loginUser, err := ctx.LoginUser(c)
if err != nil {
c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
return
}
// 更新头像地址
sysUser := model.SysUser{
UserID: loginUser.UserID,
UpdateBy: loginUser.User.UserName,
Avatar: filePath,
Sex: loginUser.User.Sex,
PhoneNumber: loginUser.User.PhoneNumber,
Email: loginUser.User.Email,
Remark: loginUser.User.Remark,
}
rows := s.sysUserService.UpdateUser(sysUser)
if rows > 0 {
// 更新缓存用户信息
loginUser.User = s.sysUserService.SelectUserByUserName(loginUser.User.UserName)
// 用户权限组标识
isAdmin := config.IsAdmin(sysUser.UserID)
if isAdmin {
loginUser.Permissions = []string{admin.PERMISSION}
} else {
perms := s.sysMenuService.SelectMenuPermsByUserId(sysUser.UserID)
loginUser.Permissions = parse.RemoveDuplicates(perms)
}
// 刷新令牌信息
token.Cache(&loginUser)
c.JSON(200, result.OkData(filePath))
return
}
c.JSON(200, result.Err(nil))
c.JSON(200, resp.Err(nil))
}