feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -3,24 +3,22 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysConfigController 结构体
|
||||
var NewSysConfig = &SysConfigController{
|
||||
sysConfigService: service.NewSysConfigImpl,
|
||||
sysConfigService: service.NewSysConfig,
|
||||
}
|
||||
|
||||
// 参数配置信息
|
||||
@@ -28,7 +26,7 @@ var NewSysConfig = &SysConfigController{
|
||||
// PATH /system/config
|
||||
type SysConfigController struct {
|
||||
// 参数配置服务
|
||||
sysConfigService service.ISysConfig
|
||||
sysConfigService *service.SysConfig // 参数配置服务
|
||||
}
|
||||
|
||||
// 参数配置列表
|
||||
@@ -47,15 +45,14 @@ type SysConfigController struct {
|
||||
// @Description Config Information List
|
||||
// @Router /system/config/list [get]
|
||||
func (s *SysConfigController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["configName"]; ok && v != "" {
|
||||
querys["configName"] = i18n.TFindKeyPrefix(language, "config", v.(string))
|
||||
if v, ok := query["configName"]; ok && v != "" {
|
||||
query["configName"] = i18n.TFindKeyPrefix(language, "config", v)
|
||||
}
|
||||
|
||||
data := s.sysConfigService.SelectConfigPage(querys)
|
||||
rows := data["rows"].([]model.SysConfig)
|
||||
rows, total := s.sysConfigService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysConfig) {
|
||||
@@ -67,187 +64,200 @@ func (s *SysConfigController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 参数配置信息
|
||||
//
|
||||
// GET /:configId
|
||||
func (s *SysConfigController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
configId := c.Param("configId")
|
||||
if configId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
configId := parse.Number(c.Param("configId"))
|
||||
if configId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.sysConfigService.SelectConfigById(configId)
|
||||
if data.ConfigID == configId {
|
||||
data := s.sysConfigService.FindById(configId)
|
||||
if data.ConfigId == configId {
|
||||
// 处理多语言
|
||||
data.ConfigName = i18n.TKey(language, data.ConfigName)
|
||||
data.ConfigValue = i18n.TKey(language, data.ConfigValue)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 参数配置新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysConfigController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysConfig
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ConfigID != "" {
|
||||
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
|
||||
}
|
||||
if body.ConfigId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, "")
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueByKey(body.ConfigKey, 0)
|
||||
if !uniqueConfigKey {
|
||||
// 参数配置新增【%s】失败,参数键名已存在
|
||||
// msg := fmt.Sprintf("参数配置新增【%s】失败,参数键名已存在", body.ConfigKey)
|
||||
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysConfigService.InsertConfig(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysConfigService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 参数配置修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysConfigController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysConfig
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ConfigID == "" {
|
||||
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
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, body.ConfigID)
|
||||
if !uniqueConfigKey {
|
||||
// 参数配置修改【%s】失败,参数键名已存在
|
||||
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
if body.ConfigId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
configInfo := s.sysConfigService.SelectConfigById(body.ConfigID)
|
||||
if configInfo.ConfigID != body.ConfigID {
|
||||
// 没有可访问参数配置数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.noData")))
|
||||
configInfo := s.sysConfigService.FindById(body.ConfigId)
|
||||
if configInfo.ConfigId != body.ConfigId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问参数配置数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "config.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueByKey(body.ConfigKey, body.ConfigId)
|
||||
if !uniqueConfigKey {
|
||||
// msg := fmt.Sprintf("参数配置修改【%s】失败,参数键名已存在", body.ConfigKey)
|
||||
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, configInfo.ConfigName)
|
||||
if i18nValue != configInfo.ConfigName {
|
||||
i18n.UpdateKeyValue(language, configInfo.ConfigName, body.ConfigName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, configInfo.ConfigName, body.ConfigName)
|
||||
body.ConfigName = configInfo.ConfigName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, configInfo.ConfigValue)
|
||||
if i18nValue2 != configInfo.ConfigValue {
|
||||
i18n.UpdateKeyValue(language, configInfo.ConfigValue, body.ConfigValue)
|
||||
service.NewSysI18n.UpdateKeyValue(language, configInfo.ConfigValue, body.ConfigValue)
|
||||
body.ConfigValue = configInfo.ConfigValue
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue3 := i18n.TKey(language, configInfo.Remark)
|
||||
if i18nValue3 != configInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, configInfo.Remark, body.Remark)
|
||||
service.NewSysI18n.UpdateKeyValue(language, configInfo.Remark, body.Remark)
|
||||
body.Remark = configInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.UpdateConfig(body)
|
||||
configInfo.ConfigType = body.ConfigType
|
||||
configInfo.ConfigName = body.ConfigName
|
||||
configInfo.ConfigKey = body.ConfigKey
|
||||
configInfo.ConfigValue = body.ConfigValue
|
||||
configInfo.Remark = body.Remark
|
||||
configInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.Update(configInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 参数配置删除
|
||||
// Remove 参数配置删除
|
||||
//
|
||||
// DELETE /:configIds
|
||||
func (s *SysConfigController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
configIds := c.Param("configIds")
|
||||
if configIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:configId
|
||||
func (s SysConfigController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
configId := c.Param("configId")
|
||||
if configId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(configIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(configId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.sysConfigService.DeleteConfigByIds(uniqueIDs)
|
||||
|
||||
rows, err := s.sysConfigService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 参数配置刷新缓存
|
||||
// Refresh 参数配置刷新缓存
|
||||
//
|
||||
// PUT /refreshCache
|
||||
func (s *SysConfigController) RefreshCache(c *gin.Context) {
|
||||
s.sysConfigService.ResetConfigCache()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
// PUT /refresh
|
||||
func (s SysConfigController) Refresh(c *gin.Context) {
|
||||
s.sysConfigService.CacheClean("*")
|
||||
s.sysConfigService.CacheLoad("*")
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
}
|
||||
|
||||
// 参数配置根据参数键名
|
||||
// ConfigKey 参数配置根据参数键名
|
||||
//
|
||||
// GET /configKey/:configKey
|
||||
func (s *SysConfigController) ConfigKey(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /config-key/:configKey
|
||||
func (s SysConfigController) ConfigKey(c *gin.Context) {
|
||||
configKey := c.Param("configKey")
|
||||
if configKey == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configKey is empty"))
|
||||
return
|
||||
}
|
||||
key := s.sysConfigService.SelectConfigValueByKey(configKey)
|
||||
key := s.sysConfigService.FindValueByKey(configKey)
|
||||
if key != "" {
|
||||
c.JSON(200, result.OkData(i18n.TKey(language, key)))
|
||||
c.JSON(200, resp.OkData(key))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 导出参数配置信息
|
||||
// Export 导出参数配置信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysConfigController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /export
|
||||
func (s SysConfigController) Export(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
data := s.sysConfigService.SelectConfigPage(querys)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysConfigService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysConfig)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysConfig) {
|
||||
@@ -279,7 +289,7 @@ func (s *SysConfigController) Export(c *gin.Context) {
|
||||
typeValue = i18n.TKey(language, "dictData.yes")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.ConfigID,
|
||||
"A" + idx: row.ConfigId,
|
||||
"B" + idx: row.ConfigName,
|
||||
"C" + idx: row.ConfigKey,
|
||||
"D" + idx: row.ConfigValue,
|
||||
@@ -291,7 +301,7 @@ func (s *SysConfigController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -302,21 +312,22 @@ func (s *SysConfigController) Export(c *gin.Context) {
|
||||
//
|
||||
// PUT /changeValue
|
||||
func (s *SysConfigController) ConfigValue(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
Key string `json:"key" binding:"required"`
|
||||
Value string `json:"value" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); 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
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
info := s.sysConfigService.SelectConfigByKey(body.Key)
|
||||
info := s.sysConfigService.FindByKey(body.Key)
|
||||
if info.ConfigKey != body.Key {
|
||||
// 无效 key
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errKey")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "config.errKey")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -324,22 +335,22 @@ func (s *SysConfigController) ConfigValue(c *gin.Context) {
|
||||
i18nValue := i18n.TKey(language, info.ConfigValue)
|
||||
if i18nValue == body.Value {
|
||||
// 变更状态与旧值相等!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errValueEq")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "config.errValueEq")))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
if i18nValue != info.ConfigValue {
|
||||
i18n.UpdateKeyValue(language, info.ConfigValue, body.Value)
|
||||
service.NewSysI18n.UpdateKeyValue(language, info.ConfigValue, body.Value)
|
||||
} else {
|
||||
info.ConfigValue = body.Value
|
||||
}
|
||||
|
||||
info.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.UpdateConfig(info)
|
||||
info.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.Update(info)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/constants/common"
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/vo"
|
||||
"be.ems/src/framework/vo/result"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/model/vo"
|
||||
"be.ems/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysDeptController 结构体
|
||||
var NewSysDept = &SysDeptController{
|
||||
sysDeptService: service.NewSysDeptImpl,
|
||||
sysDeptService: service.NewSysDept,
|
||||
}
|
||||
|
||||
// 部门信息
|
||||
//
|
||||
// PATH /system/dept
|
||||
type SysDeptController struct {
|
||||
// 部门服务
|
||||
sysDeptService service.ISysDept
|
||||
sysDeptService *service.SysDept // 部门服务
|
||||
}
|
||||
|
||||
// 部门列表
|
||||
@@ -45,36 +45,32 @@ type SysDeptController struct {
|
||||
// @Description Dept Information List
|
||||
// @Router /system/dept/list [get]
|
||||
func (s *SysDeptController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
// 部门ID
|
||||
DeptID string `form:"deptId"`
|
||||
// 父部门ID
|
||||
ParentID string `form:"parentId" `
|
||||
// 部门名称
|
||||
DeptName string `form:"deptName" `
|
||||
// 部门状态(0正常 1停用)
|
||||
Status string `form:"status"`
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var query struct {
|
||||
DeptId int64 `form:"deptId"` // 部门ID
|
||||
ParentId int64 `form:"parentId"` // 父部门ID
|
||||
DeptName string `form:"deptName"` // 部门名称
|
||||
Status string `form:"status"` // 部门状态(0正常 1停用)
|
||||
}
|
||||
err := c.ShouldBindQuery(&querys)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindQuery(&query); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言值转key查询
|
||||
if querys.DeptName != "" {
|
||||
querys.DeptName = i18n.TFindKeyPrefix(language, "dept", querys.DeptName)
|
||||
if query.DeptName != "" {
|
||||
query.DeptName = i18n.TFindKeyPrefix(language, "dept", query.DeptName)
|
||||
}
|
||||
|
||||
SysDeptController := model.SysDept{
|
||||
DeptID: querys.DeptID,
|
||||
ParentID: querys.ParentID,
|
||||
DeptName: querys.DeptName,
|
||||
Status: querys.Status,
|
||||
sysDept := model.SysDept{
|
||||
DeptId: query.DeptId,
|
||||
ParentId: query.ParentId,
|
||||
DeptName: query.DeptName,
|
||||
StatusFlag: query.Status,
|
||||
}
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||||
data := s.sysDeptService.SelectDeptList(SysDeptController, dataScopeSQL)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_dept", "")
|
||||
data := s.sysDeptService.Find(sysDept, dataScopeSQL)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysDept) {
|
||||
@@ -84,137 +80,145 @@ func (s *SysDeptController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &data)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 部门信息
|
||||
// Info 部门信息
|
||||
//
|
||||
// GET /:deptId
|
||||
func (s *SysDeptController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
deptId := c.Param("deptId")
|
||||
if deptId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
func (s SysDeptController) Info(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
deptId := parse.Number(c.Param("deptId"))
|
||||
if deptId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: deptId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysDeptService.SelectDeptById(deptId)
|
||||
if data.DeptID == deptId {
|
||||
data := s.sysDeptService.FindById(deptId)
|
||||
if data.DeptId == deptId {
|
||||
// 处理多语言
|
||||
data.DeptName = i18n.TKey(language, data.DeptName)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 部门新增
|
||||
// Add 部门新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysDeptController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s SysDeptController) Add(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysDept
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DeptID != "" {
|
||||
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
|
||||
}
|
||||
if body.DeptId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: deptId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 父级ID不为0是要检查
|
||||
if body.ParentID != "0" {
|
||||
deptParent := s.sysDeptService.SelectDeptById(body.ParentID)
|
||||
if deptParent.DeptID != body.ParentID {
|
||||
// 没有可访问部门数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
if body.ParentId > 0 {
|
||||
deptParent := s.sysDeptService.FindById(body.ParentId)
|
||||
if deptParent.DeptId != body.ParentId {
|
||||
c.JSON(200, resp.ErrMsg("没有权限访问部门数据!"))
|
||||
return
|
||||
}
|
||||
if deptParent.Status == common.STATUS_NO {
|
||||
// 上级部门【%s】停用,不允许新增
|
||||
if deptParent.StatusFlag == constants.STATUS_NO {
|
||||
// msg := fmt.Sprintf("上级部门【%s】停用,不允许新增", deptParent.DeptName)
|
||||
msg := i18n.TTemplate(language, "dept.errParentStatus", map[string]any{"name": deptParent.DeptName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
if deptParent.DelFlag == common.STATUS_YES {
|
||||
// 上级部门【%s】已删除,不允许新增
|
||||
if deptParent.DelFlag == constants.STATUS_YES {
|
||||
// msg := fmt.Sprintf("上级部门【%s】已删除,不允许新增", deptParent.DeptName)
|
||||
msg := i18n.TTemplate(language, "dept.errParentDelFlag", map[string]any{"name": deptParent.DeptName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
body.Ancestors = deptParent.Ancestors + "," + body.ParentID
|
||||
body.Ancestors = fmt.Sprintf("%s,%d", deptParent.Ancestors, body.ParentId)
|
||||
} else {
|
||||
body.Ancestors = "0"
|
||||
}
|
||||
|
||||
// 检查同级下名称唯一
|
||||
uniqueDeptName := s.sysDeptService.CheckUniqueDeptName(body.DeptName, body.ParentID, "")
|
||||
if !uniqueDeptName {
|
||||
// 部门新增【%s】失败,部门名称已存在
|
||||
uniqueName := s.sysDeptService.CheckUniqueParentIdByDeptName(body.ParentId, body.DeptName, 0)
|
||||
if !uniqueName {
|
||||
// msg := fmt.Sprintf("部门新增【%s】失败,部门名称已存在", body.DeptName)
|
||||
msg := i18n.TTemplate(language, "dept.errNameExists", map[string]any{"name": body.DeptName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysDeptService.InsertDept(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysDeptService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 部门修改
|
||||
// Edit 部门修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysDeptController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s SysDeptController) Edit(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysDept
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DeptID == "" {
|
||||
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
|
||||
}
|
||||
if body.DeptId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: deptId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 上级部门不能选自己
|
||||
if body.DeptID == body.ParentID {
|
||||
// 部门修改【%s】失败,上级部门不能是自己
|
||||
if body.DeptId == body.ParentId {
|
||||
// msg := fmt.Sprintf("部门修改【%s】失败,上级部门不能是自己", body.DeptName)
|
||||
msg := i18n.TTemplate(language, "dept.errParentID", map[string]any{"name": body.DeptName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查数据是否存在
|
||||
deptInfo := s.sysDeptService.SelectDeptById(body.DeptID)
|
||||
if deptInfo.DeptID != body.DeptID {
|
||||
// 没有可访问部门数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
deptInfo := s.sysDeptService.FindById(body.DeptId)
|
||||
if deptInfo.DeptId != body.DeptId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问部门数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 父级ID不为0是要检查
|
||||
if body.ParentID != "0" {
|
||||
deptParent := s.sysDeptService.SelectDeptById(body.ParentID)
|
||||
if deptParent.DeptID != body.ParentID {
|
||||
// 没有可访问部门数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
if body.ParentId > 0 {
|
||||
deptParent := s.sysDeptService.FindById(body.ParentId)
|
||||
if deptParent.DeptId != body.ParentId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问部门数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检查同级下名称唯一
|
||||
uniqueDeptName := s.sysDeptService.CheckUniqueDeptName(body.DeptName, body.ParentID, body.DeptID)
|
||||
uniqueDeptName := s.sysDeptService.CheckUniqueParentIdByDeptName(body.ParentId, body.DeptName, body.DeptId)
|
||||
if !uniqueDeptName {
|
||||
// 部门修改【%s】失败,部门名称已存在
|
||||
// msg := fmt.Sprintf("部门修改【%s】失败,部门名称已存在", body.DeptName)
|
||||
msg := i18n.TTemplate(language, "dept.errNameExists", map[string]any{"name": body.DeptName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 上级停用需要检查下级是否有在使用
|
||||
if body.Status == common.STATUS_NO {
|
||||
hasChild := s.sysDeptService.HasChildByDeptId(body.DeptID)
|
||||
if body.StatusFlag == constants.STATUS_NO {
|
||||
hasChild := s.sysDeptService.ExistChildrenByDeptId(body.DeptId)
|
||||
if hasChild > 0 {
|
||||
// 该部门包含未停用的子部门数量:%d
|
||||
// msg := fmt.Sprintf("该部门包含未停用的子部门数量:%d", hasChild)
|
||||
msg := i18n.TTemplate(language, "dept.errHasChildUse", map[string]any{"num": hasChild})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -222,128 +226,124 @@ func (s *SysDeptController) Edit(c *gin.Context) {
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, deptInfo.DeptName)
|
||||
if i18nValue != deptInfo.DeptName {
|
||||
i18n.UpdateKeyValue(language, deptInfo.DeptName, body.DeptName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, deptInfo.DeptName, body.DeptName)
|
||||
body.DeptName = deptInfo.DeptName
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysDeptService.UpdateDept(body)
|
||||
deptInfo.DeptName = body.DeptName
|
||||
deptInfo.ParentId = body.ParentId
|
||||
deptInfo.DeptSort = body.DeptSort
|
||||
deptInfo.Leader = body.Leader
|
||||
deptInfo.Phone = body.Phone
|
||||
deptInfo.Email = body.Email
|
||||
deptInfo.StatusFlag = body.StatusFlag
|
||||
deptInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysDeptService.Update(deptInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 部门删除
|
||||
// Remove 部门删除
|
||||
//
|
||||
// DELETE /:deptId
|
||||
func (s *SysDeptController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
deptId := c.Param("deptId")
|
||||
if deptId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
func (s SysDeptController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
deptId := parse.Number(c.Param("deptId"))
|
||||
if deptId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: deptId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查数据是否存在
|
||||
dept := s.sysDeptService.SelectDeptById(deptId)
|
||||
if dept.DeptID != deptId {
|
||||
// 没有可访问部门数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
dept := s.sysDeptService.FindById(deptId)
|
||||
if dept.DeptId != deptId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问部门数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dept.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在子部门
|
||||
hasChild := s.sysDeptService.HasChildByDeptId(deptId)
|
||||
hasChild := s.sysDeptService.ExistChildrenByDeptId(deptId)
|
||||
if hasChild > 0 {
|
||||
// 不允许删除,存在子部门数:%d
|
||||
// msg := fmt.Sprintf("不允许删除,存在子部门数:%d", hasChild)
|
||||
msg := i18n.TTemplate(language, "dept.errHasChildUse", map[string]any{"num": hasChild})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否分配给用户
|
||||
existUser := s.sysDeptService.CheckDeptExistUser(deptId)
|
||||
existUser := s.sysDeptService.ExistUserByDeptId(deptId)
|
||||
if existUser > 0 {
|
||||
// 不允许删除,部门已分配给用户数:%d
|
||||
// msg := fmt.Sprintf("不允许删除,部门已分配给用户数:%d", existUser)
|
||||
msg := i18n.TTemplate(language, "dept.errHasUserUse", map[string]any{"num": existUser})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
rows := s.sysDeptService.DeleteDeptById(deptId)
|
||||
rows := s.sysDeptService.DeleteById(deptId)
|
||||
if rows > 0 {
|
||||
// 删除成功:%d
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 部门列表(排除节点)
|
||||
//
|
||||
// GET /list/exclude/:deptId
|
||||
func (s *SysDeptController) ExcludeChild(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
deptId := c.Param("deptId")
|
||||
if deptId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
deptIdStr := c.Param("deptId")
|
||||
if deptIdStr == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: deptId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||||
data := s.sysDeptService.SelectDeptList(model.SysDept{}, dataScopeSQL)
|
||||
deptId := parse.Number(deptIdStr)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_dept", "")
|
||||
data := s.sysDeptService.Find(model.SysDept{}, dataScopeSQL)
|
||||
|
||||
// 过滤排除节点
|
||||
filtered := make([]model.SysDept, 0)
|
||||
for _, dept := range data {
|
||||
hasAncestor := false
|
||||
ancestorList := strings.Split(dept.Ancestors, ",")
|
||||
for _, ancestor := range ancestorList {
|
||||
if ancestor == deptId {
|
||||
hasAncestor = true
|
||||
break
|
||||
}
|
||||
if dept.DeptId == deptId {
|
||||
continue
|
||||
}
|
||||
if !(dept.DeptID == deptId || hasAncestor) {
|
||||
dept.DeptName = i18n.TKey(language, dept.DeptName)
|
||||
// 如果当前部门的ancestors不包含要排除的deptId,则添加到filtered中
|
||||
if !strings.Contains(dept.Ancestors, deptIdStr) {
|
||||
filtered = append(filtered, dept)
|
||||
}
|
||||
}
|
||||
c.JSON(200, result.OkData(filtered))
|
||||
c.JSON(200, resp.OkData(filtered))
|
||||
}
|
||||
|
||||
// 部门树结构列表
|
||||
//
|
||||
// GET /treeSelect
|
||||
func (s *SysDeptController) TreeSelect(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
// 部门ID
|
||||
DeptID string `json:"deptId"`
|
||||
// 父部门ID
|
||||
ParentID string `json:"parentId" `
|
||||
// 部门名称
|
||||
DeptName string `json:"deptName" `
|
||||
// 部门状态(0正常 1停用)
|
||||
Status string `json:"status"`
|
||||
// GET /tree
|
||||
func (s *SysDeptController) Tree(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var query struct {
|
||||
DeptId int64 `form:"deptId"` // 部门ID
|
||||
ParentId int64 `form:"parentId"` // 父部门ID
|
||||
DeptName string `form:"deptName"` // 部门名称
|
||||
StatusFlag string `form:"statusFlag"` // 部门状态(0正常 1停用)
|
||||
}
|
||||
err := c.ShouldBindQuery(&querys)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindQuery(&query); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
SysDeptController := model.SysDept{
|
||||
DeptID: querys.DeptID,
|
||||
ParentID: querys.ParentID,
|
||||
DeptName: querys.DeptName,
|
||||
Status: querys.Status,
|
||||
sysDept := model.SysDept{
|
||||
DeptId: query.DeptId,
|
||||
ParentId: query.ParentId,
|
||||
DeptName: query.DeptName,
|
||||
StatusFlag: query.StatusFlag,
|
||||
}
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||||
deptTreeSelect := s.sysDeptService.SelectDeptTreeSelect(SysDeptController, dataScopeSQL)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_dept", "")
|
||||
data := s.sysDeptService.BuildTreeSelect(sysDept, dataScopeSQL)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
var converI18n func(language string, arr *[]vo.TreeSelect)
|
||||
@@ -355,25 +355,25 @@ func (s *SysDeptController) TreeSelect(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
converI18n(language, &deptTreeSelect)
|
||||
converI18n(language, &data)
|
||||
|
||||
c.JSON(200, result.OkData(deptTreeSelect))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 部门树结构列表(指定角色)
|
||||
//
|
||||
// GET /roleDeptTreeSelect/:roleId
|
||||
func (s *SysDeptController) RoleDeptTreeSelect(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
roleId := c.Param("roleId")
|
||||
if roleId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// GET /tree/role/:roleId
|
||||
func (s *SysDeptController) TreeRole(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
roleId := parse.Number(c.Param("roleId"))
|
||||
if roleId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||||
deptTreeSelect := s.sysDeptService.SelectDeptTreeSelect(model.SysDept{}, dataScopeSQL)
|
||||
checkedKeys := s.sysDeptService.SelectDeptListByRoleId(roleId)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_dept", "")
|
||||
deptTreeSelect := s.sysDeptService.BuildTreeSelect(model.SysDept{}, dataScopeSQL)
|
||||
checkedKeys := s.sysDeptService.FindDeptIdsByRoleId(roleId)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
var converI18n func(language string, arr *[]vo.TreeSelect)
|
||||
@@ -387,7 +387,7 @@ func (s *SysDeptController) RoleDeptTreeSelect(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &deptTreeSelect)
|
||||
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
c.JSON(200, resp.OkData(map[string]any{
|
||||
"depts": deptTreeSelect,
|
||||
"checkedKeys": checkedKeys,
|
||||
}))
|
||||
|
||||
@@ -7,15 +7,14 @@ import (
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysDictDataController 结构体
|
||||
@@ -48,15 +47,14 @@ type SysDictDataController struct {
|
||||
// @Description Dictionary Data List
|
||||
// @Router /system/dict/data/list [get]
|
||||
func (s *SysDictDataController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["dictLabel"]; ok && v != "" {
|
||||
querys["dictLabel"] = i18n.TFindKeyPrefix(language, "dictData", v.(string))
|
||||
if v, ok := query["dataLabel"]; ok && v != "" {
|
||||
query["dataLabel"] = i18n.TFindKeyPrefix(language, "dictData", v)
|
||||
}
|
||||
|
||||
data := s.sysDictDataService.SelectDictDataPage(querys)
|
||||
rows := data["rows"].([]model.SysDictData)
|
||||
rows, total := s.sysDictDataService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||||
@@ -64,208 +62,234 @@ func (s *SysDictDataController) List(c *gin.Context) {
|
||||
if strings.Contains((*arr)[i].DictType, "i18n") {
|
||||
continue
|
||||
}
|
||||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||||
(*arr)[i].DataLabel = i18n.TKey(language, (*arr)[i].DataLabel)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 字典数据详情
|
||||
// Info 字典数据详情
|
||||
//
|
||||
// GET /:dictCode
|
||||
func (s *SysDictDataController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictCode := c.Param("dictCode")
|
||||
if dictCode == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// GET /:dataId
|
||||
func (s SysDictDataController) Info(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
dataId := parse.Number(c.Param("dataId"))
|
||||
if dataId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: deptId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysDictDataService.SelectDictDataByCode(dictCode)
|
||||
if data.DictCode == dictCode {
|
||||
data := s.sysDictDataService.FindById(dataId)
|
||||
if data.DataId == dataId {
|
||||
// 处理多语言
|
||||
if !strings.Contains(data.DictType, "i18n") {
|
||||
data.DictLabel = i18n.TKey(language, data.DictLabel)
|
||||
data.DataLabel = i18n.TKey(language, data.DataLabel)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 字典数据新增
|
||||
// Add 字典数据新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysDictDataController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s SysDictDataController) Add(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysDictData
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DictCode != "" {
|
||||
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
|
||||
}
|
||||
if body.DataId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dataId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典类型是否存在
|
||||
sysDictType := s.sysDictTypeService.SelectDictTypeByType(body.DictType)
|
||||
if sysDictType.DictType != body.DictType {
|
||||
// 没有可访问字典类型数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
dictType := s.sysDictTypeService.FindByType(body.DictType)
|
||||
if dictType.DictType != body.DictType {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问字典类型数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典标签唯一
|
||||
uniqueDictLabel := s.sysDictDataService.CheckUniqueDictLabel(body.DictType, body.DictLabel, "")
|
||||
if !uniqueDictLabel {
|
||||
// 数据新增【%s】失败,该字典类型下标签名已存在
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DictLabel})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
uniqueLabel := s.sysDictDataService.CheckUniqueTypeByLabel(body.DictType, body.DataLabel, 0)
|
||||
if !uniqueLabel {
|
||||
// msg := fmt.Sprintf("数据新增【%s】失败,该字典类型下标签名已存在", body.DataLabel)
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DataLabel})
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysDictDataService.InsertDictData(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
// 检查字典键值唯一
|
||||
uniqueValue := s.sysDictDataService.CheckUniqueTypeByValue(body.DictType, body.DataValue, 0)
|
||||
if !uniqueValue {
|
||||
// msg := fmt.Sprintf("数据新增【%s】失败,该字典类型下标签值已存在", body.DataValue)
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DataLabel})
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysDictDataService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 字典类型修改
|
||||
// Edit 字典类型修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysDictDataController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s SysDictDataController) Edit(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysDictData
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DictCode == "" {
|
||||
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
|
||||
}
|
||||
if body.DataId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dataId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典类型是否存在
|
||||
sysDictType := s.sysDictTypeService.SelectDictTypeByType(body.DictType)
|
||||
if sysDictType.DictType != body.DictType {
|
||||
// 没有可访问字典类型数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
dictType := s.sysDictTypeService.FindByType(body.DictType)
|
||||
if dictType.DictType != body.DictType {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问字典类型数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典编码是否存在
|
||||
sysDictData := s.sysDictDataService.SelectDictDataByCode(body.DictCode)
|
||||
if sysDictData.DictCode != body.DictCode {
|
||||
// 没有可访问字典编码数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictData.noData")))
|
||||
dictData := s.sysDictDataService.FindById(body.DataId)
|
||||
if dictData.DataId != body.DataId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问字典编码数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dictData.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典标签唯一
|
||||
uniqueDictLabel := s.sysDictDataService.CheckUniqueDictLabel(body.DictType, body.DictLabel, body.DictCode)
|
||||
if !uniqueDictLabel {
|
||||
// 数据修改【%s】失败,该字典类型下标签名已存在
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DictLabel})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
uniqueLabel := s.sysDictDataService.CheckUniqueTypeByLabel(body.DictType, body.DataLabel, body.DataId)
|
||||
if !uniqueLabel {
|
||||
// msg := fmt.Sprintf("数据修改【%s】失败,该字典类型下标签名已存在", body.DataLabel)
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DataLabel})
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典键值唯一
|
||||
uniqueValue := s.sysDictDataService.CheckUniqueTypeByValue(body.DictType, body.DataValue, body.DataId)
|
||||
if !uniqueValue {
|
||||
// msg := fmt.Sprintf("数据修改【%s】失败,该字典类型下标签值已存在", body.DataLabel)
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DataLabel})
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, sysDictData.DictLabel)
|
||||
if i18nValue != sysDictData.DictLabel {
|
||||
i18n.UpdateKeyValue(language, sysDictData.DictLabel, body.DictLabel)
|
||||
body.DictLabel = sysDictData.DictLabel
|
||||
i18nValue := i18n.TKey(language, dictData.DataLabel)
|
||||
if i18nValue != dictData.DataLabel {
|
||||
service.NewSysI18n.UpdateKeyValue(language, dictData.DataLabel, body.DataLabel)
|
||||
body.DataLabel = dictData.DataLabel
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, sysDictData.Remark)
|
||||
if i18nValue2 != sysDictData.Remark {
|
||||
i18n.UpdateKeyValue(language, sysDictData.Remark, body.Remark)
|
||||
body.Remark = sysDictData.Remark
|
||||
i18nValue2 := i18n.TKey(language, dictData.Remark)
|
||||
if i18nValue2 != dictData.Remark {
|
||||
service.NewSysI18n.UpdateKeyValue(language, dictData.Remark, body.Remark)
|
||||
body.Remark = dictData.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysDictDataService.UpdateDictData(body)
|
||||
dictData.DictType = body.DictType
|
||||
dictData.DataLabel = body.DataLabel
|
||||
dictData.DataValue = body.DataValue
|
||||
dictData.DataSort = body.DataSort
|
||||
dictData.TagClass = body.TagClass
|
||||
dictData.TagType = body.TagType
|
||||
dictData.StatusFlag = body.StatusFlag
|
||||
dictData.Remark = body.Remark
|
||||
dictData.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysDictDataService.Update(dictData)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 字典数据删除
|
||||
// Remove 字典数据删除
|
||||
//
|
||||
// DELETE /:dictCodes
|
||||
func (s *SysDictDataController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictCodes := c.Param("dictCodes")
|
||||
if dictCodes == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:dataId
|
||||
func (s SysDictDataController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
dataId := c.Param("dataId")
|
||||
if dataId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dataId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(dictCodes, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(dataId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.sysDictDataService.DeleteDictDataByCodes(uniqueIDs)
|
||||
|
||||
rows, err := s.sysDictDataService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// 删除成功:%d
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 字典数据列表(指定字典类型)
|
||||
// DictType 字典数据列表(指定字典类型)
|
||||
//
|
||||
// GET /type/:dictType
|
||||
func (s *SysDictDataController) DictType(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s SysDictDataController) DictType(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
dictType := c.Param("dictType")
|
||||
if dictType == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dictType is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.sysDictDataService.SelectDictDataByType(dictType)
|
||||
data := s.sysDictDataService.FindByType(dictType)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||||
(*arr)[i].DataLabel = i18n.TKey(language, (*arr)[i].DataLabel)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &data)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 字典数据列表导出
|
||||
// Export 字典数据列表导出
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysDictDataController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /export
|
||||
func (s SysDictDataController) Export(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
data := s.sysDictDataService.SelectDictDataPage(querys)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysDictData)
|
||||
|
||||
// rows := s.sysDictDataService.SelectDictDataList(model.SysDictData{})
|
||||
if len(rows) <= 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysDictDataService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -275,7 +299,7 @@ func (s *SysDictDataController) Export(c *gin.Context) {
|
||||
if strings.Contains((*arr)[i].DictType, "i18n") {
|
||||
continue
|
||||
}
|
||||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||||
(*arr)[i].DataLabel = i18n.TKey(language, (*arr)[i].DataLabel)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
@@ -296,14 +320,14 @@ func (s *SysDictDataController) Export(c *gin.Context) {
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
statusValue := i18n.TKey(language, "dictData.disable")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.normal")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.DictCode,
|
||||
"B" + idx: row.DictLabel,
|
||||
"C" + idx: row.DictValue,
|
||||
"D" + idx: row.DictSort,
|
||||
"A" + idx: row.DataId,
|
||||
"B" + idx: row.DataLabel,
|
||||
"C" + idx: row.DataValue,
|
||||
"D" + idx: row.DataSort,
|
||||
"E" + idx: statusValue,
|
||||
})
|
||||
}
|
||||
@@ -311,7 +335,7 @@ func (s *SysDictDataController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,20 +3,17 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/constants/common"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysDictTypeController 结构体
|
||||
@@ -47,15 +44,15 @@ type SysDictTypeController struct {
|
||||
// @Description Dictionary Type List
|
||||
// @Router /system/dict/type/list [get]
|
||||
func (s *SysDictTypeController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["dictName"]; ok && v != "" {
|
||||
querys["dictName"] = i18n.TFindKeyPrefix(language, "dictType", v.(string))
|
||||
if v, ok := query["dictName"]; ok && v != "" {
|
||||
query["dictName"] = i18n.TFindKeyPrefix(language, "dictType", v)
|
||||
}
|
||||
|
||||
data := s.sysDictTypeService.SelectDictTypePage(querys)
|
||||
rows := data["rows"].([]model.SysDictType)
|
||||
rows, total := s.sysDictTypeService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysDictType) {
|
||||
@@ -66,210 +63,213 @@ func (s *SysDictTypeController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 字典类型信息
|
||||
//
|
||||
// GET /:dictId
|
||||
func (s *SysDictTypeController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictId := c.Param("dictId")
|
||||
if dictId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
dictId := parse.Number(c.Param("dictId"))
|
||||
if dictId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dictId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysDictTypeService.SelectDictTypeByID(dictId)
|
||||
if data.DictID == dictId {
|
||||
data := s.sysDictTypeService.FindById(dictId)
|
||||
if data.DictId == dictId {
|
||||
// 处理多语言
|
||||
data.DictName = i18n.TKey(language, data.DictName)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 字典类型新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysDictTypeController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysDictType
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DictID != "" {
|
||||
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
|
||||
}
|
||||
if body.DictId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dictId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典名称唯一
|
||||
uniqueDictName := s.sysDictTypeService.CheckUniqueDictName(body.DictName, "")
|
||||
if !uniqueDictName {
|
||||
// 字典新增【%s】失败,字典名称已存在
|
||||
uniqueName := s.sysDictTypeService.CheckUniqueByName(body.DictName, 0)
|
||||
if !uniqueName {
|
||||
// msg := fmt.Sprintf("字典新增【%s】失败,字典名称已存在", body.DictName)
|
||||
msg := i18n.TTemplate(language, "dictType.errNameExists", map[string]any{"name": body.DictName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典类型唯一
|
||||
uniqueDictType := s.sysDictTypeService.CheckUniqueDictType(body.DictType, "")
|
||||
if !uniqueDictType {
|
||||
// 字典新增【%s】失败,字典类型已存在
|
||||
uniqueType := s.sysDictTypeService.CheckUniqueByType(body.DictType, 0)
|
||||
if !uniqueType {
|
||||
// msg := fmt.Sprintf("字典新增【%s】失败,字典类型已存在", body.DictName)
|
||||
msg := i18n.TTemplate(language, "dictType.errTypeExists", map[string]any{"name": body.DictName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysDictTypeService.InsertDictType(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysDictTypeService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 字典类型修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysDictTypeController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysDictType
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DictID == "" {
|
||||
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
|
||||
}
|
||||
if body.DictId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dictId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查数据是否存在
|
||||
dictInfo := s.sysDictTypeService.SelectDictTypeByID(body.DictID)
|
||||
if dictInfo.DictID != body.DictID {
|
||||
// 没有可访问字典类型数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
dictTypeInfo := s.sysDictTypeService.FindById(body.DictId)
|
||||
if dictTypeInfo.DictId != body.DictId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问字典类型数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典名称唯一
|
||||
uniqueDictName := s.sysDictTypeService.CheckUniqueDictName(body.DictName, body.DictID)
|
||||
if !uniqueDictName {
|
||||
// 字典修改【%s】失败,字典名称已存在
|
||||
uniqueName := s.sysDictTypeService.CheckUniqueByName(body.DictName, body.DictId)
|
||||
if !uniqueName {
|
||||
// msg := fmt.Sprintf("字典修改【%s】失败,字典名称已存在", body.DictName)
|
||||
msg := i18n.TTemplate(language, "dictType.errNameExists", map[string]any{"name": body.DictName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典类型唯一
|
||||
uniqueDictType := s.sysDictTypeService.CheckUniqueDictType(body.DictType, body.DictID)
|
||||
if !uniqueDictType {
|
||||
// 字典修改【%s】失败,字典类型已存在
|
||||
uniqueType := s.sysDictTypeService.CheckUniqueByType(body.DictType, body.DictId)
|
||||
if !uniqueType {
|
||||
// msg := fmt.Sprintf("字典修改【%s】失败,字典类型已存在", body.DictName)
|
||||
msg := i18n.TTemplate(language, "dictType.errTypeExists", map[string]any{"name": body.DictName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, dictInfo.DictName)
|
||||
if i18nValue != dictInfo.DictName {
|
||||
i18n.UpdateKeyValue(language, dictInfo.DictName, body.DictName)
|
||||
body.DictName = dictInfo.DictName
|
||||
i18nValue := i18n.TKey(language, dictTypeInfo.DictName)
|
||||
if i18nValue != dictTypeInfo.DictName {
|
||||
service.NewSysI18n.UpdateKeyValue(language, dictTypeInfo.DictName, body.DictName)
|
||||
body.DictName = dictTypeInfo.DictName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, dictInfo.Remark)
|
||||
if i18nValue2 != dictInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, dictInfo.Remark, body.Remark)
|
||||
body.Remark = dictInfo.Remark
|
||||
i18nValue2 := i18n.TKey(language, dictTypeInfo.Remark)
|
||||
if i18nValue2 != dictTypeInfo.Remark {
|
||||
service.NewSysI18n.UpdateKeyValue(language, dictTypeInfo.Remark, body.Remark)
|
||||
body.Remark = dictTypeInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysDictTypeService.UpdateDictType(body)
|
||||
dictTypeInfo.DictName = body.DictName
|
||||
dictTypeInfo.DictType = body.DictType
|
||||
dictTypeInfo.StatusFlag = body.StatusFlag
|
||||
dictTypeInfo.Remark = body.Remark
|
||||
dictTypeInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysDictTypeService.Update(dictTypeInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 字典类型删除
|
||||
// Remove 字典类型删除
|
||||
//
|
||||
// DELETE /:dictIds
|
||||
func (s *SysDictTypeController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictIds := c.Param("dictIds")
|
||||
if dictIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:dictId
|
||||
func (s SysDictTypeController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
dictId := c.Param("dictId")
|
||||
if dictId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: dictId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(dictIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(dictId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.sysDictTypeService.DeleteDictTypeByIDs(uniqueIDs)
|
||||
|
||||
rows, err := s.sysDictTypeService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 字典类型刷新缓存
|
||||
// Refresh 字典类型刷新缓存
|
||||
//
|
||||
// PUT /refreshCache
|
||||
func (s *SysDictTypeController) RefreshCache(c *gin.Context) {
|
||||
s.sysDictTypeService.ResetDictCache()
|
||||
i18n.ClearLocaleData() // 清空国际化数据
|
||||
c.JSON(200, result.Ok(nil))
|
||||
// PUT /refresh
|
||||
func (s SysDictTypeController) Refresh(c *gin.Context) {
|
||||
s.sysDictTypeService.CacheClean("*")
|
||||
s.sysDictTypeService.CacheLoad("*")
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
}
|
||||
|
||||
// 字典类型选择框列表
|
||||
//
|
||||
// GET /getDictOptionselect
|
||||
func (s *SysDictTypeController) DictOptionselect(c *gin.Context) {
|
||||
data := s.sysDictTypeService.SelectDictTypeList(model.SysDictType{
|
||||
Status: common.STATUS_YES,
|
||||
})
|
||||
// GET /options
|
||||
func (s *SysDictTypeController) Options(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
data := s.sysDictTypeService.Find(model.SysDictType{})
|
||||
|
||||
type labelValue struct {
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// 数据组
|
||||
arr := []labelValue{}
|
||||
arr := make([]labelValue, 0)
|
||||
for _, v := range data {
|
||||
arr = append(arr, labelValue{
|
||||
Label: i18n.TKey(language, v.DictName),
|
||||
Value: v.DictType,
|
||||
})
|
||||
}
|
||||
c.JSON(200, result.OkData(arr))
|
||||
c.JSON(200, resp.OkData(arr))
|
||||
}
|
||||
|
||||
// 字典类型列表导出
|
||||
// Export 字典类型列表导出
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysDictTypeController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /export
|
||||
func (s SysDictTypeController) Export(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
data := s.sysDictTypeService.SelectDictTypePage(querys)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysDictType)
|
||||
|
||||
// rows := s.sysDictTypeService.SelectDictTypeList(model.SysDictType{})
|
||||
if len(rows) <= 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysDictTypeService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -296,11 +296,11 @@ func (s *SysDictTypeController) Export(c *gin.Context) {
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
statusValue := i18n.TKey(language, "dictData.disable")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.normal")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.DictID,
|
||||
"A" + idx: row.DictId,
|
||||
"B" + idx: row.DictName,
|
||||
"C" + idx: row.DictType,
|
||||
"D" + idx: statusValue,
|
||||
@@ -310,7 +310,7 @@ func (s *SysDictTypeController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,15 +3,13 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/date"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/vo/result"
|
||||
commonService "be.ems/src/modules/common/service"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/service"
|
||||
@@ -21,7 +19,7 @@ import (
|
||||
|
||||
// 实例化控制层 SysLogLoginController 结构体
|
||||
var NewSysLogLogin = &SysLogLoginController{
|
||||
sysLogLoginService: service.NewSysLogLoginImpl,
|
||||
sysLogLoginService: service.NewSysLogLogin,
|
||||
accountService: commonService.NewAccount,
|
||||
}
|
||||
|
||||
@@ -29,8 +27,7 @@ var NewSysLogLogin = &SysLogLoginController{
|
||||
//
|
||||
// PATH /system/log/login
|
||||
type SysLogLoginController struct {
|
||||
// 系统登录日志服务
|
||||
sysLogLoginService service.ISysLogLogin
|
||||
sysLogLoginService *service.SysLogLogin // 系统登录日志服务
|
||||
accountService *commonService.Account // 账号身份操作服务
|
||||
}
|
||||
|
||||
@@ -50,13 +47,12 @@ type SysLogLoginController struct {
|
||||
// @Description System Login Log List
|
||||
// @Router /system/log/login/list [get]
|
||||
func (s *SysLogLoginController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
|
||||
data := s.sysLogLoginService.SelectSysLogLoginPage(querys, dataScopeSQL)
|
||||
query := reqctx.QueryMap(c)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
|
||||
rows, total := s.sysLogLoginService.FindByPage(query, dataScopeSQL)
|
||||
|
||||
rows := data["rows"].([]model.SysLogLogin)
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
converI18n := func(language string, arr *[]model.SysLogLogin) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].LoginLocation = i18n.TKey(language, (*arr)[i].LoginLocation)
|
||||
@@ -67,88 +63,47 @@ func (s *SysLogLoginController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 系统登录日志删除
|
||||
//
|
||||
// DELETE /:loginIds
|
||||
func (s *SysLogLoginController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
loginIds := c.Param("loginIds")
|
||||
if loginIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(loginIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows := s.sysLogLoginService.DeleteSysLogLoginByIds(uniqueIDs)
|
||||
if rows > 0 {
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 系统登录日志清空
|
||||
// Clean 系统登录日志清空
|
||||
//
|
||||
// DELETE /clean
|
||||
func (s *SysLogLoginController) Clean(c *gin.Context) {
|
||||
err := s.sysLogLoginService.CleanSysLogLogin()
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Ok(nil))
|
||||
func (s SysLogLoginController) Clean(c *gin.Context) {
|
||||
rows := s.sysLogLoginService.Clean()
|
||||
c.JSON(200, resp.OkData(rows))
|
||||
}
|
||||
|
||||
// 系统登录日志账户解锁
|
||||
// Unlock 系统登录日志账户解锁
|
||||
//
|
||||
// PUT /unlock/:userName
|
||||
func (s *SysLogLoginController) Unlock(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s SysLogLoginController) Unlock(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
userName := c.Param("userName")
|
||||
if userName == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: userName is empty"))
|
||||
return
|
||||
}
|
||||
ok := s.accountService.ClearLoginRecordCache(userName)
|
||||
ok := s.accountService.CleanLoginRecordCache(userName)
|
||||
if ok {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errUnlock")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errUnlock")))
|
||||
}
|
||||
|
||||
// 导出系统登录日志信息
|
||||
// Export 导出系统登录日志信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysLogLoginController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /export
|
||||
func (s SysLogLoginController) Export(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
|
||||
data := s.sysLogLoginService.SelectSysLogLoginPage(querys, dataScopeSQL)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysLogLogin)
|
||||
|
||||
// rows := s.sysLogLoginService.SelectSysLogLoginList(model.SysLogLogin{})
|
||||
if len(rows) <= 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
|
||||
rows, total := s.sysLogLoginService.FindByPage(query, dataScopeSQL)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -183,13 +138,13 @@ func (s *SysLogLoginController) Export(c *gin.Context) {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
// 状态
|
||||
statusValue := i18n.TKey(language, "dictData.fail")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.success")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.LoginID,
|
||||
"A" + idx: row.ID,
|
||||
"B" + idx: row.UserName,
|
||||
"C" + idx: row.IPAddr,
|
||||
"C" + idx: row.LoginIp,
|
||||
"D" + idx: row.LoginLocation,
|
||||
"E" + idx: row.OS,
|
||||
"F" + idx: row.Browser,
|
||||
@@ -202,7 +157,7 @@ func (s *SysLogLoginController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,15 +3,13 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/date"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/vo/result"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/service"
|
||||
|
||||
@@ -20,15 +18,14 @@ import (
|
||||
|
||||
// 实例化控制层 SysLogOperateController 结构体
|
||||
var NewSysLogOperate = &SysLogOperateController{
|
||||
SysLogOperateService: service.NewSysLogOperateImpl,
|
||||
sysLogOperateService: service.NewSysLogOperate,
|
||||
}
|
||||
|
||||
// 操作日志记录信息
|
||||
//
|
||||
// PATH /system/log/operate
|
||||
type SysLogOperateController struct {
|
||||
// 操作日志服务
|
||||
SysLogOperateService service.ISysLogOperate
|
||||
sysLogOperateService *service.SysLogOperate // 操作日志服务
|
||||
}
|
||||
|
||||
// 操作日志列表
|
||||
@@ -47,90 +44,48 @@ type SysLogOperateController struct {
|
||||
// @Description System Operation Log List
|
||||
// @Router /system/log/operate/list [get]
|
||||
func (s *SysLogOperateController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["title"]; ok && v != "" {
|
||||
querys["title"] = i18n.TFindKeyPrefix(language, "log.operate.title", v.(string))
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
query["title"] = i18n.TFindKeyPrefix(language, "log.operate.title", v)
|
||||
}
|
||||
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
|
||||
data := s.SysLogOperateService.SelectSysLogOperatePage(querys, dataScopeSQL)
|
||||
rows := data["rows"].([]model.SysLogOperate)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
|
||||
rows, total := s.sysLogOperateService.FindByPage(query, dataScopeSQL)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysLogOperate) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].Title = i18n.TKey(language, (*arr)[i].Title)
|
||||
(*arr)[i].OperLocation = i18n.TKey(language, (*arr)[i].OperLocation)
|
||||
(*arr)[i].OperaLocation = i18n.TKey(language, (*arr)[i].OperaLocation)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 操作日志删除
|
||||
//
|
||||
// DELETE /:operIds
|
||||
func (s *SysLogOperateController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
operIds := c.Param("operIds")
|
||||
if operIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(operIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows := s.SysLogOperateService.DeleteSysLogOperateByIds(uniqueIDs)
|
||||
if rows > 0 {
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 操作日志清空
|
||||
// Clean 操作日志清空
|
||||
//
|
||||
// DELETE /clean
|
||||
func (s *SysLogOperateController) Clean(c *gin.Context) {
|
||||
err := s.SysLogOperateService.CleanSysLogOperate()
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Ok(nil))
|
||||
func (s SysLogOperateController) Clean(c *gin.Context) {
|
||||
rows := s.sysLogOperateService.Clean()
|
||||
c.JSON(200, resp.OkData(rows))
|
||||
}
|
||||
|
||||
// 导出操作日志
|
||||
// Export 导出操作日志
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysLogOperateController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /export
|
||||
func (s SysLogOperateController) Export(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
|
||||
data := s.SysLogOperateService.SelectSysLogOperatePage(querys, dataScopeSQL)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysLogOperate)
|
||||
|
||||
// rows := s.SysLogOperateService.SelectSysLogOperateList(model.SysLogOperate{})
|
||||
if len(rows) <= 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
|
||||
rows, total := s.sysLogOperateService.FindByPage(query, dataScopeSQL)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -138,7 +93,7 @@ func (s *SysLogOperateController) Export(c *gin.Context) {
|
||||
converI18n := func(language string, arr *[]model.SysLogOperate) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].Title = i18n.TKey(language, (*arr)[i].Title)
|
||||
(*arr)[i].OperLocation = i18n.TKey(language, (*arr)[i].OperLocation)
|
||||
(*arr)[i].OperaLocation = i18n.TKey(language, (*arr)[i].OperaLocation)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
@@ -195,18 +150,18 @@ func (s *SysLogOperateController) Export(c *gin.Context) {
|
||||
|
||||
// 状态
|
||||
statusValue := i18n.TKey(language, "dictData.fail")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.success")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.OperID,
|
||||
"A" + idx: row.ID,
|
||||
"B" + idx: row.Title,
|
||||
"C" + idx: businessType,
|
||||
"D" + idx: row.OperName,
|
||||
"E" + idx: row.RequestMethod,
|
||||
"F" + idx: row.OperIP,
|
||||
"D" + idx: row.OperaBy,
|
||||
"E" + idx: row.OperaUrlMethod,
|
||||
"F" + idx: row.OperaIp,
|
||||
"G" + idx: statusValue,
|
||||
"H" + idx: date.ParseDateToStr(row.OperTime, date.YYYY_MM_DDTHH_MM_SSZ),
|
||||
"H" + idx: date.ParseDateToStr(row.OperaTime, date.YYYY_MM_DDTHH_MM_SSZ),
|
||||
"I" + idx: row.CostTime,
|
||||
})
|
||||
}
|
||||
@@ -214,7 +169,7 @@ func (s *SysLogOperateController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"be.ems/src/framework/config"
|
||||
"be.ems/src/framework/constants/common"
|
||||
"be.ems/src/framework/constants/menu"
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/regular"
|
||||
"be.ems/src/framework/vo"
|
||||
"be.ems/src/framework/vo/result"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/model/vo"
|
||||
"be.ems/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysMenuController 结构体
|
||||
var NewSysMenu = &SysMenuController{
|
||||
sysMenuService: service.NewSysMenuImpl,
|
||||
sysMenuService: service.NewSysMenu,
|
||||
}
|
||||
|
||||
// 菜单信息
|
||||
//
|
||||
// PATH /system/menu
|
||||
type SysMenuController struct {
|
||||
// 菜单服务
|
||||
sysMenuService service.ISysMenu
|
||||
sysMenuService *service.SysMenu // 菜单服务
|
||||
}
|
||||
|
||||
// 菜单列表
|
||||
@@ -44,20 +44,20 @@ type SysMenuController struct {
|
||||
// @Description Menu Information List
|
||||
// @Router /system/menu/list [get]
|
||||
func (s *SysMenuController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := model.SysMenu{}
|
||||
if v, ok := c.GetQuery("menuName"); ok && v != "" {
|
||||
if v, ok := c.GetQuery("menuName"); ok {
|
||||
query.MenuName = i18n.TFindKeyPrefix(language, "menu", v)
|
||||
}
|
||||
if v, ok := c.GetQuery("status"); ok && v != "" {
|
||||
query.Status = v
|
||||
if v, ok := c.GetQuery("statusFlag"); ok {
|
||||
query.StatusFlag = v
|
||||
}
|
||||
|
||||
userId := ctx.LoginUserToUserID(c)
|
||||
if config.IsAdmin(userId) {
|
||||
userId = "*"
|
||||
userId := reqctx.LoginUserToUserID(c)
|
||||
if config.IsSystemUser(userId) {
|
||||
userId = 0
|
||||
}
|
||||
data := s.sysMenuService.SelectMenuList(query, userId)
|
||||
data := s.sysMenuService.Find(query, userId)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
var converI18n func(language string, arr *[]model.SysMenu)
|
||||
@@ -72,157 +72,166 @@ func (s *SysMenuController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &data)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 菜单信息
|
||||
//
|
||||
// GET /:menuId
|
||||
func (s *SysMenuController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
menuId := c.Param("menuId")
|
||||
if menuId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
menuId := parse.Number(c.Param("menuId"))
|
||||
if menuId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysMenuService.SelectMenuById(menuId)
|
||||
if data.MenuID == menuId {
|
||||
|
||||
data := s.sysMenuService.FindById(menuId)
|
||||
if data.MenuId == menuId {
|
||||
// 处理多语言
|
||||
data.MenuName = i18n.TKey(language, data.MenuName)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 菜单新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysMenuController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysMenu
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.MenuID != "" {
|
||||
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
|
||||
}
|
||||
if body.MenuId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 目录和菜单检查地址唯一
|
||||
if menu.TYPE_DIR == body.MenuType || menu.TYPE_MENU == body.MenuType {
|
||||
uniqueNenuPath := s.sysMenuService.CheckUniqueMenuPath(body.Path, body.ParentID, "")
|
||||
if !uniqueNenuPath {
|
||||
// 菜单新增【%s】失败,菜单路由地址已存在
|
||||
if constants.MENU_TYPE_DIR == body.MenuType || constants.MENU_TYPE_MENU == body.MenuType {
|
||||
uniqueMenuPath := s.sysMenuService.CheckUniqueParentIdByMenuPath(body.ParentId, body.MenuPath, 0)
|
||||
if !uniqueMenuPath {
|
||||
// msg := fmt.Sprintf("菜单新增【%s】失败,菜单路由地址已存在", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errPathExists", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检查名称唯一
|
||||
uniqueNenuName := s.sysMenuService.CheckUniqueMenuName(body.MenuName, body.ParentID, "")
|
||||
if !uniqueNenuName {
|
||||
// 菜单新增【%s】失败,菜单名称已存在
|
||||
uniqueMenuName := s.sysMenuService.CheckUniqueParentIdByMenuName(body.ParentId, body.MenuName, 0)
|
||||
if !uniqueMenuName {
|
||||
// msg := fmt.Sprintf("菜单新增【%s】失败,菜单名称已存在", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errNameExists", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 外链菜单需要符合网站http(s)开头
|
||||
if body.IsFrame == common.STATUS_NO && !regular.ValidHttp(body.Path) {
|
||||
// 操作菜单【{name}】失败,非内部地址请以http(s)://开头
|
||||
if body.FrameFlag == constants.STATUS_NO && !regular.ValidHttp(body.MenuPath) {
|
||||
// msg := fmt.Sprintf("菜单新增【%s】失败,非内部地址必须以http(s)://开头", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errFramePath", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysMenuService.InsertMenu(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysMenuService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 菜单修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysMenuController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysMenu
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.MenuID == "" {
|
||||
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
|
||||
}
|
||||
if body.MenuId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 上级菜单不能选自己
|
||||
if body.MenuID == body.ParentID {
|
||||
// 菜单修改【%s】失败,上级菜单不能选择自己
|
||||
if body.MenuId == body.ParentId {
|
||||
// msg := fmt.Sprintf("菜单修改【%s】失败,上级菜单不能选择自己", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errPathExists", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查数据是否存在
|
||||
menuInfo := s.sysMenuService.SelectMenuById(body.MenuID)
|
||||
if menuInfo.MenuID != body.MenuID {
|
||||
// 没有可访问菜单数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.noData")))
|
||||
menuInfo := s.sysMenuService.FindById(body.MenuId)
|
||||
if menuInfo.MenuId != body.MenuId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问菜单数据"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "menu.noData")))
|
||||
return
|
||||
}
|
||||
// 父级ID不为0是要检查
|
||||
if body.ParentID != "0" {
|
||||
menuParent := s.sysMenuService.SelectMenuById(body.ParentID)
|
||||
if menuParent.MenuID != body.ParentID {
|
||||
// 没有可访问菜单数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.noData")))
|
||||
if body.ParentId > 0 {
|
||||
menuParent := s.sysMenuService.FindById(body.ParentId)
|
||||
if menuParent.MenuId != body.ParentId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问菜单数据"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "menu.noData")))
|
||||
return
|
||||
}
|
||||
// 禁用菜单时检查父菜单是否使用
|
||||
if body.Status == common.STATUS_YES && menuParent.Status == common.STATUS_NO {
|
||||
// 上级菜单未启用!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.errParentStatus")))
|
||||
if body.StatusFlag == constants.STATUS_YES && menuParent.StatusFlag == constants.STATUS_NO {
|
||||
// c.JSON(200, resp.ErrMsg("上级菜单未启用!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "menu.errParentStatus")))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 目录和菜单检查地址唯一
|
||||
if menu.TYPE_DIR == body.MenuType || menu.TYPE_MENU == body.MenuType {
|
||||
uniqueNenuPath := s.sysMenuService.CheckUniqueMenuPath(body.Path, body.ParentID, body.MenuID)
|
||||
if !uniqueNenuPath {
|
||||
// 菜单修改【%s】失败,菜单路由地址已存在
|
||||
if constants.MENU_TYPE_DIR == body.MenuType || constants.MENU_TYPE_MENU == body.MenuType {
|
||||
uniqueMenuPath := s.sysMenuService.CheckUniqueParentIdByMenuPath(body.ParentId, body.MenuPath, body.MenuId)
|
||||
if !uniqueMenuPath {
|
||||
// msg := fmt.Sprintf("菜单修改【%s】失败,菜单路由地址已存在", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errPathExists", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检查名称唯一
|
||||
uniqueNenuName := s.sysMenuService.CheckUniqueMenuName(body.MenuName, body.ParentID, body.MenuID)
|
||||
if !uniqueNenuName {
|
||||
// 菜单修改【%s】失败,菜单名称已存在
|
||||
uniqueMenuName := s.sysMenuService.CheckUniqueParentIdByMenuName(body.ParentId, body.MenuName, body.MenuId)
|
||||
if !uniqueMenuName {
|
||||
// msg := fmt.Sprintf("菜单修改【%s】失败,菜单名称已存在", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errNameExists", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 外链菜单需要符合网站http(s)开头
|
||||
if body.IsFrame == common.STATUS_NO && !regular.ValidHttp(body.Path) {
|
||||
// 菜单修改【%s】失败,非内部地址必须以http(s)://开头
|
||||
if body.FrameFlag == constants.STATUS_NO && !regular.ValidHttp(body.MenuPath) {
|
||||
// msg := fmt.Sprintf("菜单修改【%s】失败,非内部地址必须以http(s)://开头", body.MenuName)
|
||||
msg := i18n.TTemplate(language, "menu.errFramePath", map[string]any{"name": body.MenuName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 禁用菜单时检查子菜单是否使用
|
||||
if body.Status == common.STATUS_NO {
|
||||
hasStatus := s.sysMenuService.HasChildByMenuIdAndStatus(body.MenuID, common.STATUS_YES)
|
||||
if body.StatusFlag == constants.STATUS_NO {
|
||||
hasStatus := s.sysMenuService.ExistChildrenByMenuIdAndStatus(body.MenuId, constants.STATUS_YES)
|
||||
if hasStatus > 0 {
|
||||
// 操作菜单【%s】失败,存在使用子菜单数:%d
|
||||
// msg := fmt.Sprintf("不允许禁用,存在使用子菜单数:%d", hasStatus)
|
||||
msg := i18n.TTemplate(language, "menu.errHasChildUse", map[string]any{"name": body.MenuName, "num": hasStatus})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -230,91 +239,104 @@ func (s *SysMenuController) Edit(c *gin.Context) {
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, menuInfo.MenuName)
|
||||
if i18nValue != menuInfo.MenuName {
|
||||
i18n.UpdateKeyValue(language, menuInfo.MenuName, body.MenuName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, menuInfo.MenuName, body.MenuName)
|
||||
body.MenuName = menuInfo.MenuName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, menuInfo.Remark)
|
||||
if i18nValue2 != menuInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, menuInfo.Remark, body.Remark)
|
||||
service.NewSysI18n.UpdateKeyValue(language, menuInfo.Remark, body.Remark)
|
||||
body.Remark = menuInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysMenuService.UpdateMenu(body)
|
||||
menuInfo.ParentId = body.ParentId
|
||||
menuInfo.MenuName = body.MenuName
|
||||
menuInfo.MenuType = body.MenuType
|
||||
menuInfo.MenuSort = body.MenuSort
|
||||
menuInfo.MenuPath = body.MenuPath
|
||||
menuInfo.Component = body.Component
|
||||
menuInfo.FrameFlag = body.FrameFlag
|
||||
menuInfo.CacheFlag = body.CacheFlag
|
||||
menuInfo.VisibleFlag = body.VisibleFlag
|
||||
menuInfo.StatusFlag = body.StatusFlag
|
||||
menuInfo.Perms = body.Perms
|
||||
menuInfo.Icon = body.Icon
|
||||
menuInfo.Remark = body.Remark
|
||||
menuInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysMenuService.Update(menuInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 菜单删除
|
||||
// Remove 菜单删除
|
||||
//
|
||||
// DELETE /:menuId
|
||||
func (s *SysMenuController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
menuId := c.Param("menuId")
|
||||
if menuId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
func (s SysMenuController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
menuId := parse.Number(c.Param("menuId"))
|
||||
if menuId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查数据是否存在
|
||||
menu := s.sysMenuService.SelectMenuById(menuId)
|
||||
if menu.MenuID != menuId {
|
||||
// 没有可访问菜单数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.noData")))
|
||||
menu := s.sysMenuService.FindById(menuId)
|
||||
if menu.MenuId != menuId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问菜单数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "menu.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在子菜单
|
||||
hasChild := s.sysMenuService.HasChildByMenuIdAndStatus(menuId, "")
|
||||
hasChild := s.sysMenuService.ExistChildrenByMenuIdAndStatus(menuId, "")
|
||||
if hasChild > 0 {
|
||||
// 不允许删除,存在子菜单数:%d
|
||||
// msg := fmt.Sprintf("不允许删除,存在子菜单数:%d", hasChild)
|
||||
msg := i18n.TTemplate(language, "menu.errHasChildUse", map[string]any{"name": menu.MenuName, "num": hasChild})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否分配给角色
|
||||
existRole := s.sysMenuService.CheckMenuExistRole(menuId)
|
||||
existRole := s.sysMenuService.ExistRoleByMenuId(menuId)
|
||||
if existRole > 0 {
|
||||
// 不允许删除,菜单已分配给角色数:%d
|
||||
// msg := fmt.Sprintf("不允许删除,菜单已分配给角色数:%d", existRole)
|
||||
msg := i18n.TTemplate(language, "menu.errHasRoleUse", map[string]any{"name": menu.MenuName, "num": existRole})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
rows := s.sysMenuService.DeleteMenuById(menuId)
|
||||
rows := s.sysMenuService.DeleteById(menuId)
|
||||
if rows > 0 {
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 菜单树结构列表
|
||||
//
|
||||
// GET /treeSelect
|
||||
func (s *SysMenuController) TreeSelect(c *gin.Context) {
|
||||
// GET /tree
|
||||
func (s *SysMenuController) Tree(c *gin.Context) {
|
||||
query := model.SysMenu{}
|
||||
if v, ok := c.GetQuery("menuName"); ok && v != "" {
|
||||
if v, ok := c.GetQuery("menuName"); ok {
|
||||
query.MenuName = v
|
||||
}
|
||||
if v, ok := c.GetQuery("status"); ok && v != "" {
|
||||
query.Status = v
|
||||
if v, ok := c.GetQuery("statusFlag"); ok {
|
||||
query.StatusFlag = v
|
||||
}
|
||||
|
||||
userId := ctx.LoginUserToUserID(c)
|
||||
if config.IsAdmin(userId) {
|
||||
userId = "*"
|
||||
userId := reqctx.LoginUserToUserID(c)
|
||||
if config.IsSystemUser(userId) {
|
||||
userId = 0
|
||||
}
|
||||
data := s.sysMenuService.SelectMenuTreeSelectByUserId(query, userId)
|
||||
trees := s.sysMenuService.BuildTreeSelectByUserId(query, userId)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var converI18n func(language string, arr *[]vo.TreeSelect)
|
||||
converI18n = func(language string, arr *[]vo.TreeSelect) {
|
||||
for i := range *arr {
|
||||
@@ -324,37 +346,37 @@ func (s *SysMenuController) TreeSelect(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
converI18n(language, &data)
|
||||
converI18n(language, &trees)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(trees))
|
||||
|
||||
}
|
||||
|
||||
// 菜单树结构列表(指定角色)
|
||||
//
|
||||
// GET /roleMenuTreeSelect/:roleId
|
||||
func (s *SysMenuController) RoleMenuTreeSelect(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
roleId := c.Param("roleId")
|
||||
if roleId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// GET /tree/role/:roleId
|
||||
func (s *SysMenuController) TreeRole(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
roleId := parse.Number(c.Param("roleId"))
|
||||
if roleId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
query := model.SysMenu{}
|
||||
if v, ok := c.GetQuery("menuName"); ok && v != "" {
|
||||
query.MenuName = v
|
||||
sysMenu := model.SysMenu{}
|
||||
if v, ok := c.GetQuery("menuName"); ok {
|
||||
sysMenu.MenuName = v
|
||||
}
|
||||
if v, ok := c.GetQuery("status"); ok && v != "" {
|
||||
query.Status = v
|
||||
if v, ok := c.GetQuery("statusFlag"); ok {
|
||||
sysMenu.StatusFlag = v
|
||||
}
|
||||
|
||||
userId := ctx.LoginUserToUserID(c)
|
||||
if config.IsAdmin(userId) {
|
||||
userId = "*"
|
||||
userId := reqctx.LoginUserToUserID(c)
|
||||
if config.IsSystemUser(userId) {
|
||||
userId = 0
|
||||
}
|
||||
menuTreeSelect := s.sysMenuService.SelectMenuTreeSelectByUserId(query, userId)
|
||||
checkedKeys := s.sysMenuService.SelectMenuListByRoleId(roleId)
|
||||
menuTreeSelect := s.sysMenuService.BuildTreeSelectByUserId(sysMenu, userId)
|
||||
checkedKeys := s.sysMenuService.FindByRoleId(roleId)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
var converI18n func(language string, arr *[]vo.TreeSelect)
|
||||
@@ -368,7 +390,7 @@ func (s *SysMenuController) RoleMenuTreeSelect(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &menuTreeSelect)
|
||||
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
c.JSON(200, resp.OkData(map[string]any{
|
||||
"menus": menuTreeSelect,
|
||||
"checkedKeys": checkedKeys,
|
||||
}))
|
||||
|
||||
@@ -3,33 +3,30 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/date"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysPostController 结构体
|
||||
var NewSysPost = &SysPostController{
|
||||
sysPostService: service.NewSysPostImpl,
|
||||
sysPostService: service.NewSysPost,
|
||||
}
|
||||
|
||||
// 岗位信息
|
||||
//
|
||||
// PATH /system/post
|
||||
type SysPostController struct {
|
||||
// 岗位服务
|
||||
sysPostService service.ISysPost
|
||||
sysPostService *service.SysPost // 岗位服务
|
||||
}
|
||||
|
||||
// 岗位列表
|
||||
@@ -48,15 +45,14 @@ type SysPostController struct {
|
||||
// @Description Post Information List
|
||||
// @Router /system/post/list [get]
|
||||
func (s *SysPostController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["postName"]; ok && v != "" {
|
||||
querys["postName"] = i18n.TFindKeyPrefix(language, "post", v.(string))
|
||||
if v, ok := query["postName"]; ok && v != "" {
|
||||
query["postName"] = i18n.TFindKeyPrefix(language, "post", v)
|
||||
}
|
||||
|
||||
data := s.sysPostService.SelectPostPage(querys)
|
||||
rows := data["rows"].([]model.SysPost)
|
||||
rows, total := s.sysPostService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysPost) {
|
||||
@@ -67,171 +63,184 @@ func (s *SysPostController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 岗位信息
|
||||
// Info 岗位信息
|
||||
//
|
||||
// GET /:postId
|
||||
func (s *SysPostController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
postId := c.Param("postId")
|
||||
if postId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
postId := parse.Number(c.Param("postId"))
|
||||
if postId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysPostService.SelectPostById(postId)
|
||||
if data.PostID == postId {
|
||||
data := s.sysPostService.FindById(postId)
|
||||
if data.PostId == postId {
|
||||
// 处理多语言
|
||||
data.PostName = i18n.TKey(language, data.PostName)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 岗位新增
|
||||
// Add 岗位新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysPostController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysPost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.PostID != "" {
|
||||
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
|
||||
}
|
||||
if body.PostId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查名称唯一
|
||||
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, "")
|
||||
if !uniqueuPostName {
|
||||
// 岗位新增【%s】失败,岗位名称已存在
|
||||
uniqueuName := s.sysPostService.CheckUniqueByName(body.PostName, 0)
|
||||
if !uniqueuName {
|
||||
// msg := fmt.Sprintf("岗位新增【%s】失败,岗位名称已存在", body.PostName)
|
||||
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查编码属性值唯一
|
||||
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, "")
|
||||
if !uniquePostCode {
|
||||
// 岗位新增【%s】失败,岗位编码已存在
|
||||
uniqueCode := s.sysPostService.CheckUniqueByCode(body.PostCode, 0)
|
||||
if !uniqueCode {
|
||||
// msg := fmt.Sprintf("岗位新增【%s】失败,岗位编码已存在", body.PostCode)
|
||||
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysPostService.InsertPost(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysPostService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 岗位修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysPostController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysPost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.PostID == "" {
|
||||
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
|
||||
}
|
||||
if body.PostId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
postInfo := s.sysPostService.SelectPostById(body.PostID)
|
||||
if postInfo.PostID != body.PostID {
|
||||
// 没有可访问岗位数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "post.noData")))
|
||||
postInfo := s.sysPostService.FindById(body.PostId)
|
||||
if postInfo.PostId != body.PostId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问岗位数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "post.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查名称唯一
|
||||
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, body.PostID)
|
||||
if !uniqueuPostName {
|
||||
// 岗位修改【%s】失败,岗位名称已存在
|
||||
uniquePostName := s.sysPostService.CheckUniqueByName(body.PostName, body.PostId)
|
||||
if !uniquePostName {
|
||||
// msg := fmt.Sprintf("岗位修改【%s】失败,岗位名称已存在", body.PostName)
|
||||
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查编码属性值唯一
|
||||
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, body.PostID)
|
||||
uniquePostCode := s.sysPostService.CheckUniqueByCode(body.PostCode, body.PostId)
|
||||
if !uniquePostCode {
|
||||
// 岗位修改【%s】失败,岗位编码已存在
|
||||
// msg := fmt.Sprintf("岗位修改【%s】失败,岗位编码已存在", body.PostCode)
|
||||
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, postInfo.PostName)
|
||||
if i18nValue != postInfo.PostName {
|
||||
i18n.UpdateKeyValue(language, postInfo.PostName, body.PostName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, postInfo.PostName, body.PostName)
|
||||
body.PostName = postInfo.PostName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, postInfo.Remark)
|
||||
if i18nValue2 != postInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, postInfo.Remark, body.Remark)
|
||||
service.NewSysI18n.UpdateKeyValue(language, postInfo.Remark, body.Remark)
|
||||
body.Remark = postInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysPostService.UpdatePost(body)
|
||||
postInfo.PostCode = body.PostCode
|
||||
postInfo.PostName = body.PostName
|
||||
postInfo.PostSort = body.PostSort
|
||||
postInfo.StatusFlag = body.StatusFlag
|
||||
postInfo.Remark = body.Remark
|
||||
postInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysPostService.Update(postInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 岗位删除
|
||||
// Remove 岗位删除
|
||||
//
|
||||
// DELETE /:postIds
|
||||
func (s *SysPostController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
postIds := c.Param("postIds")
|
||||
if postIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:postId
|
||||
func (s SysPostController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
postId := c.Param("postId")
|
||||
if postId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(postIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(postId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.sysPostService.DeletePostByIds(uniqueIDs)
|
||||
|
||||
rows, err := s.sysPostService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 导出岗位信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysPostController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
data := s.sysPostService.SelectPostPage(querys)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysPostService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysPost)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysPost) {
|
||||
@@ -258,11 +267,11 @@ func (s *SysPostController) Export(c *gin.Context) {
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
statusValue := i18n.TKey(language, "dictData.disable")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.normal")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.PostID,
|
||||
"A" + idx: row.PostId,
|
||||
"B" + idx: row.PostName,
|
||||
"C" + idx: row.PostCode,
|
||||
"D" + idx: row.PostSort,
|
||||
@@ -274,7 +283,7 @@ func (s *SysPostController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -3,39 +3,33 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/constants/admin"
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/date"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysRoleController 结构体
|
||||
var NewSysRole = &SysRoleController{
|
||||
sysRoleService: service.NewSysRoleImpl,
|
||||
sysUserService: service.NewSysUserImpl,
|
||||
sysDictDataService: service.NewSysDictData,
|
||||
sysRoleService: service.NewSysRole,
|
||||
sysUserService: service.NewSysUser,
|
||||
}
|
||||
|
||||
// 角色信息
|
||||
//
|
||||
// PATH /system/role
|
||||
type SysRoleController struct {
|
||||
// 角色服务
|
||||
sysRoleService service.ISysRole
|
||||
// 用户服务
|
||||
sysUserService service.ISysUser
|
||||
sysDictDataService *service.SysDictData // 字典数据服务
|
||||
sysRoleService *service.SysRole // 角色服务
|
||||
sysUserService *service.SysUser // 用户服务
|
||||
}
|
||||
|
||||
// 角色列表
|
||||
@@ -54,16 +48,13 @@ type SysRoleController struct {
|
||||
// @Description Role Information List
|
||||
// @Router /system/role/list [get]
|
||||
func (s *SysRoleController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["roleName"]; ok && v != "" {
|
||||
querys["roleName"] = i18n.TFindKeyPrefix(language, "role", v.(string))
|
||||
if v, ok := query["roleName"]; ok && v != "" {
|
||||
query["roleName"] = i18n.TFindKeyPrefix(language, "role", v)
|
||||
}
|
||||
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||||
data := s.sysRoleService.SelectRolePage(querys, dataScopeSQL)
|
||||
rows := data["rows"].([]model.SysRole)
|
||||
rows, total := s.sysRoleService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysRole) {
|
||||
@@ -74,367 +65,363 @@ func (s *SysRoleController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 角色信息详情
|
||||
//
|
||||
// GET /:roleId
|
||||
func (s *SysRoleController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
roleId := c.Param("roleId")
|
||||
if roleId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
roleId := parse.Number(c.Param("roleId"))
|
||||
if roleId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
|
||||
return
|
||||
}
|
||||
data := s.sysRoleService.SelectRoleById(roleId)
|
||||
if data.RoleID == roleId {
|
||||
data := s.sysRoleService.FindById(roleId)
|
||||
if data.RoleId == roleId {
|
||||
data.RoleName = i18n.TKey(language, data.RoleName)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 角色信息新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysRoleController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysRole
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.RoleID != "" {
|
||||
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
|
||||
}
|
||||
if body.RoleId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 判断角色名称是否唯一
|
||||
uniqueRoleName := s.sysRoleService.CheckUniqueRoleName(body.RoleName, "")
|
||||
if !uniqueRoleName {
|
||||
// 角色新增【%s】失败,角色名称已存在
|
||||
uniqueName := s.sysRoleService.CheckUniqueByName(body.RoleName, 0)
|
||||
if !uniqueName {
|
||||
// msg := fmt.Sprintf("角色新增【%s】失败,角色名称已存在", body.RoleName)
|
||||
msg := i18n.TTemplate(language, "role.errNameExists", map[string]any{"name": body.RoleName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 判断角色键值是否唯一
|
||||
uniqueRoleKey := s.sysRoleService.CheckUniqueRoleKey(body.RoleKey, "")
|
||||
if !uniqueRoleKey {
|
||||
// 角色新增【%s】失败,角色键值已存在
|
||||
uniqueKey := s.sysRoleService.CheckUniqueByKey(body.RoleKey, 0)
|
||||
if !uniqueKey {
|
||||
// msg := fmt.Sprintf("角色新增【%s】失败,角色键值已存在", body.RoleName)
|
||||
msg := i18n.TTemplate(language, "role.errKeyExists", map[string]any{"name": body.RoleName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysRoleService.InsertRole(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysRoleService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 角色信息修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysRoleController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysRole
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.RoleID == "" {
|
||||
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
|
||||
}
|
||||
if body.RoleId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否管理员角色
|
||||
if body.RoleID == admin.ROLE_ID {
|
||||
// 不允许操作管理员角色
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
// 检查是否系统管理员角色
|
||||
if body.RoleId == constants.SYS_ROLE_SYSTEM_ID {
|
||||
// c.JSON(200, resp.ErrMsg("不允许操作系统管理员角色"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
roleInfo := s.sysRoleService.SelectRoleById(body.RoleID)
|
||||
if roleInfo.RoleID != body.RoleID {
|
||||
// 没有可访问角色数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
roleInfo := s.sysRoleService.FindById(body.RoleId)
|
||||
if roleInfo.RoleId != body.RoleId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问角色数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 判断角色名称是否唯一
|
||||
uniqueRoleName := s.sysRoleService.CheckUniqueRoleName(body.RoleName, body.RoleID)
|
||||
if !uniqueRoleName {
|
||||
// 操作角色【%s】失败,角色名称已存在
|
||||
// msg := fmt.Sprintf("Character modification [%s] failed, character name already exists", body.RoleName)
|
||||
uniqueName := s.sysRoleService.CheckUniqueByName(body.RoleName, body.RoleId)
|
||||
if !uniqueName {
|
||||
// msg := fmt.Sprintf("角色修改【%s】失败,角色名称已存在", body.RoleName)
|
||||
msg := i18n.TTemplate(language, "role.errNameExists", map[string]any{"name": body.RoleName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 判断角色键值是否唯一
|
||||
uniqueRoleKey := s.sysRoleService.CheckUniqueRoleKey(body.RoleKey, body.RoleID)
|
||||
if !uniqueRoleKey {
|
||||
// 角色修改【%s】失败,角色键值已存在
|
||||
// msg := fmt.Sprintf("Character modification [%s] failed, character key already exists", body.RoleName)
|
||||
uniqueKey := s.sysRoleService.CheckUniqueByKey(body.RoleKey, body.RoleId)
|
||||
if !uniqueKey {
|
||||
// msg := fmt.Sprintf("角色修改【%s】失败,角色键值已存在", body.RoleName)
|
||||
msg := i18n.TTemplate(language, "role.errKeyExists", map[string]any{"name": body.RoleName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, roleInfo.RoleName)
|
||||
if i18nValue != roleInfo.RoleName {
|
||||
i18n.UpdateKeyValue(language, roleInfo.RoleName, body.RoleName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, roleInfo.RoleName, body.RoleName)
|
||||
body.RoleName = roleInfo.RoleName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, roleInfo.Remark)
|
||||
if i18nValue2 != roleInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, roleInfo.Remark, body.Remark)
|
||||
service.NewSysI18n.UpdateKeyValue(language, roleInfo.Remark, body.Remark)
|
||||
body.Remark = roleInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysRoleService.UpdateRole(body)
|
||||
roleInfo.Remark = body.Remark
|
||||
roleInfo.RoleName = body.RoleName
|
||||
roleInfo.RoleKey = body.RoleKey
|
||||
roleInfo.RoleSort = body.RoleSort
|
||||
roleInfo.DataScope = body.DataScope
|
||||
roleInfo.MenuCheckStrictly = body.MenuCheckStrictly
|
||||
roleInfo.DeptCheckStrictly = body.DeptCheckStrictly
|
||||
roleInfo.StatusFlag = body.StatusFlag
|
||||
roleInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
roleInfo.MenuIds = body.MenuIds
|
||||
roleInfo.DeptIds = body.DeptIds
|
||||
rows := s.sysRoleService.Update(roleInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 角色信息删除
|
||||
// Remove 角色信息删除
|
||||
//
|
||||
// DELETE /:roleIds
|
||||
// DELETE /:roleId
|
||||
func (s *SysRoleController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
roleIds := c.Param("roleIds")
|
||||
if roleIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
roleId := c.Param("roleId")
|
||||
if roleId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(roleIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(roleId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
// 检查是否管理员角色
|
||||
for _, id := range uniqueIDs {
|
||||
if id == admin.ROLE_ID {
|
||||
// 不允许操作管理员角色
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
|
||||
// 检查是否系统管理员角色
|
||||
for _, id := range ids {
|
||||
if id == constants.SYS_ROLE_SYSTEM_ID {
|
||||
// c.JSON(200, resp.ErrMsg("不允许操作系统管理员角色"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
return
|
||||
}
|
||||
}
|
||||
rows, err := s.sysRoleService.DeleteRoleByIds(uniqueIDs)
|
||||
|
||||
rows, err := s.sysRoleService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 角色状态变更
|
||||
// Status 角色状态变更
|
||||
//
|
||||
// PUT /changeStatus
|
||||
func (s *SysRoleController) Status(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// PUT /status
|
||||
func (s SysRoleController) Status(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
// 角色ID
|
||||
RoleID string `json:"roleId" binding:"required"`
|
||||
// 状态
|
||||
Status string `json:"status" binding:"required"`
|
||||
RoleID int64 `json:"roleId" binding:"required"` // 角色ID
|
||||
StatusFlag string `json:"statusFlag" binding:"required,oneof=0 1"` // 状态
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 检查是否管理员角色
|
||||
if body.RoleID == admin.ROLE_ID {
|
||||
// 不允许操作管理员角色
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
// 检查是否系统管理员角色
|
||||
if body.RoleID == constants.SYS_ROLE_SYSTEM_ID {
|
||||
// c.JSON(200, resp.ErrMsg("不允许操作系统管理员角色"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
role := s.sysRoleService.SelectRoleById(body.RoleID)
|
||||
if role.RoleID != body.RoleID {
|
||||
// 没有可访问角色数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
role := s.sysRoleService.FindById(body.RoleID)
|
||||
if role.RoleId != body.RoleID {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问角色数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 与旧值相等不变更
|
||||
if role.Status == body.Status {
|
||||
// 变更状态与旧值相等!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.statusEq")))
|
||||
if role.StatusFlag == body.StatusFlag {
|
||||
// c.JSON(200, resp.ErrMsg("变更状态与旧值相等!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "role.statusEq")))
|
||||
return
|
||||
}
|
||||
|
||||
// 更新状态不刷新缓存
|
||||
userName := ctx.LoginUserToUserName(c)
|
||||
SysRoleController := model.SysRole{
|
||||
RoleID: body.RoleID,
|
||||
Status: body.Status,
|
||||
UpdateBy: userName,
|
||||
}
|
||||
rows := s.sysRoleService.UpdateRole(SysRoleController)
|
||||
role.StatusFlag = body.StatusFlag
|
||||
role.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysRoleService.Update(role)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 角色数据权限修改
|
||||
// DataScope 角色数据权限修改
|
||||
//
|
||||
// PUT /dataScope
|
||||
// PUT /data-scope
|
||||
func (s *SysRoleController) DataScope(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
// 角色ID
|
||||
RoleID string `json:"roleId"`
|
||||
// 部门组(数据权限)
|
||||
DeptIds []string `json:"deptIds"`
|
||||
// 数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限 5:仅本人数据权限)
|
||||
DataScope string `json:"dataScope"`
|
||||
// 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示)
|
||||
DeptCheckStrictly string `json:"deptCheckStrictly"`
|
||||
RoleId int64 `json:"roleId" binding:"required"` // 角色ID
|
||||
DeptIds []int64 `json:"deptIds"` // 部门组(数据权限)
|
||||
DataScope string `json:"dataScope" binding:"required,oneof=1 2 3 4 5"` // 数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限 5:仅本人数据权限)
|
||||
DeptCheckStrictly string `json:"deptCheckStrictly" binding:"required,oneof=0 1"` // 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 检查是否管理员角色
|
||||
if body.RoleID == admin.ROLE_ID {
|
||||
// 不允许操作管理员角色
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
// 检查是否系统管理员角色
|
||||
if body.RoleId == constants.SYS_ROLE_SYSTEM_ID {
|
||||
// c.JSON(200, resp.ErrMsg("不允许操作系统管理员角色"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
role := s.sysRoleService.SelectRoleById(body.RoleID)
|
||||
if role.RoleID != body.RoleID {
|
||||
// 没有可访问角色数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
roleInfo := s.sysRoleService.FindById(body.RoleId)
|
||||
if roleInfo.RoleId != body.RoleId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问角色数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 更新数据权限
|
||||
userName := ctx.LoginUserToUserName(c)
|
||||
SysRoleController := model.SysRole{
|
||||
RoleID: body.RoleID,
|
||||
DeptIds: body.DeptIds,
|
||||
DataScope: body.DataScope,
|
||||
DeptCheckStrictly: body.DeptCheckStrictly,
|
||||
UpdateBy: userName,
|
||||
}
|
||||
rows := s.sysRoleService.AuthDataScope(SysRoleController)
|
||||
roleInfo.DeptIds = body.DeptIds
|
||||
roleInfo.DataScope = body.DataScope
|
||||
roleInfo.DeptCheckStrictly = body.DeptCheckStrictly
|
||||
roleInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysRoleService.UpdateAndDataScope(roleInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 角色分配用户列表
|
||||
// UserAuthList 角色分配用户列表
|
||||
//
|
||||
// GET /authUser/allocatedList
|
||||
func (s *SysRoleController) AuthUserAllocatedList(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
roleId, ok := querys["roleId"]
|
||||
if !ok || roleId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// GET /user/list
|
||||
func (s SysRoleController) UserAuthList(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
roleId := parse.Number(c.Query("roleId"))
|
||||
if roleId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
role := s.sysRoleService.SelectRoleById(roleId.(string))
|
||||
if role.RoleID != roleId {
|
||||
// 没有可访问角色数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
role := s.sysRoleService.FindById(roleId)
|
||||
if role.RoleId != roleId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问角色数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
|
||||
data := s.sysUserService.SelectAllocatedPage(querys, dataScopeSQL)
|
||||
c.JSON(200, result.Ok(data))
|
||||
query := reqctx.QueryMap(c)
|
||||
dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
|
||||
rows, total := s.sysUserService.FindAuthUsersPage(query, dataScopeSQL)
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 角色分配选择授权
|
||||
// UserAuthChecked 角色分配选择授权
|
||||
//
|
||||
// PUT /authUser/checked
|
||||
func (s *SysRoleController) AuthUserChecked(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// PUT /user/auth
|
||||
func (s SysRoleController) UserAuthChecked(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
// 角色ID
|
||||
RoleID string `json:"roleId" binding:"required"`
|
||||
// 用户ID组
|
||||
UserIDs string `json:"userIds" binding:"required"`
|
||||
// 选择操作 添加true 取消false
|
||||
Checked bool `json:"checked"`
|
||||
RoleId int64 `json:"roleId" binding:"required"` // 角色ID
|
||||
UserIds []int64 `json:"userIds" binding:"required"` // 用户ID组
|
||||
Auth bool `json:"auth" binding:"required"` // 选择操作 添加true 取消false
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(body.UserIDs, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
if len(body.UserIds) <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: userIds is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
role := s.sysRoleService.SelectRoleById(body.RoleID)
|
||||
if role.RoleID != body.RoleID {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
role := s.sysRoleService.FindById(body.RoleId)
|
||||
if role.RoleId != body.RoleId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问角色数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "role.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
var rows int64
|
||||
if body.Checked {
|
||||
rows = s.sysRoleService.InsertAuthUsers(body.RoleID, uniqueIDs)
|
||||
if body.Auth {
|
||||
rows = s.sysRoleService.InsertAuthUsers(body.RoleId, body.UserIds)
|
||||
} else {
|
||||
rows = s.sysRoleService.DeleteAuthUsers(body.RoleID, uniqueIDs)
|
||||
rows = s.sysRoleService.DeleteAuthUsers(body.RoleId, body.UserIds)
|
||||
}
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 导出角色信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysRoleController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||||
data := s.sysRoleService.SelectRolePage(querys, dataScopeSQL)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysRoleService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysRole)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysRole) {
|
||||
@@ -473,11 +460,11 @@ func (s *SysRoleController) Export(c *gin.Context) {
|
||||
// }
|
||||
// 角色状态
|
||||
statusValue := i18n.TKey(language, "dictData.disable")
|
||||
if row.Status == "1" {
|
||||
if row.StatusFlag == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.normal")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.RoleID,
|
||||
"A" + idx: row.RoleId,
|
||||
"B" + idx: row.RoleName,
|
||||
"C" + idx: row.RoleKey,
|
||||
"D" + idx: row.RoleSort,
|
||||
@@ -490,7 +477,7 @@ func (s *SysRoleController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user