|
- package hdl
-
- import (
- "applet/app/e"
- "applet/app/enum"
- "applet/app/lib/validate"
- "applet/app/md"
- "applet/app/svc"
- "applet/app/utils"
- db "code.fnuoos.com/zhimeng/model.git/src"
- "code.fnuoos.com/zhimeng/model.git/src/implement"
- "code.fnuoos.com/zhimeng/model.git/src/model"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- // PermissionGroupList
- // @Summary 权限组列表
- // @Tags ADMIN-权限管理
- // @Description 权限组列表
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @param adm_id query string true "管理员id"
- // @Success 200 {string} "具体看返回内容"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/permissionGroupList [GET]
- func PermissionGroupList(c *gin.Context) {
- roleId := c.DefaultQuery("role_id", "")
- engine := db.DBs[svc.GetMasterId(c)]
- qrcodeWithBatchRecordsDb := implement.NewAgentPermissionGroupDb(engine)
- groupList, err := qrcodeWithBatchRecordsDb.FindAgentPermissionGroup()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- roleDb := implement.NewRoleDb(engine, utils.StrToInt(roleId))
- list, _, err := roleDb.FindPermissionGroupByRole(utils.StrToInt(roleId))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var isHasPermissionGroupId []string
- for _, v := range list {
- isHasPermissionGroupId = append(isHasPermissionGroupId, utils.IntToStr(v.PermissionGroup.Id))
- }
-
- var tempRespMap = map[string]*md.PermissionGroupListResp{}
- var tempRespMapKeys []string
- for _, v := range *groupList {
- isCheck := false
- if utils.InArr(utils.IntToStr(v.Id), isHasPermissionGroupId) {
- isCheck = true
- }
- tempRespMap[utils.IntToStr(v.Id)] = &md.PermissionGroupListResp{
- Id: v.Id,
- Name: v.Name,
- Key: v.Key,
- State: v.State,
- ParentId: v.ParentId,
- CreateAt: v.CreateAt,
- UpdateAt: v.UpdateAt,
- IsCheck: isCheck,
- }
- tempRespMapKeys = append(tempRespMapKeys, utils.IntToStr(v.Id))
- }
- for _, v := range tempRespMap {
- if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId != 0 {
- tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
- }
- }
- for _, v := range tempRespMap {
- if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId == 0 {
- tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
- }
- }
-
- var resp []*md.PermissionGroupListResp
- for _, v := range tempRespMapKeys {
- if tempRespMap[v].ParentId == 0 {
- resp = append(resp, tempRespMap[v])
- }
- }
-
- e.OutSuc(c, map[string]interface{}{
- "list": resp,
- "state": []map[string]interface{}{
- {
- "name": enum.PermissionGroupState(enum.PermissionGroupStateForNormal).String(),
- "value": enum.PermissionGroupStateForNormal,
- },
- {
- "name": enum.PermissionGroupState(enum.PermissionGroupStateForDiscard).String(),
- "value": enum.PermissionGroupStateForDiscard,
- },
- },
- }, nil)
- return
- }
-
- // RoleList
- // @Summary 角色列表
- // @Tags ADMIN-权限管理
- // @Description 角色列表
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Success 200 {string} "具体看返回内容"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/roleList [GET]
- func RoleList(c *gin.Context) {
- engine := db.DBs[svc.GetMasterId(c)]
- roleDb := implement.NewAgentRoleDb(engine)
- roleList, err := roleDb.FindAgentRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- adminRoleDb := implement.NewAgentWithRoleDb(engine)
- adminDb := implement.NewAgentDb(engine)
- var result []*md.RoleListResp
- for _, v := range *roleList {
- var temp md.RoleListResp
- temp.Data = v
- adminRoles, err1 := adminRoleDb.FindAgentWithRoleByRoleId(v.Id)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- for _, adminRole := range *adminRoles {
- admin, err2 := adminDb.GetAgent(adminRole.Id)
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- temp.AdminList = append(temp.AdminList, struct {
- Name string `json:"name"`
- }{
- Name: admin.Username,
- })
- }
- result = append(result, &temp)
- }
- e.OutSuc(c, map[string]interface{}{
- "list": result,
- "state": []map[string]interface{}{
- {
- "name": enum.RoleState(enum.RoleStateForNormal).String(),
- "value": enum.RoleStateForNormal,
- },
- {
- "name": enum.RoleState(enum.RoleStateForFreeze).String(),
- "value": enum.RoleStateForFreeze,
- },
- },
- }, nil)
- return
- }
-
- // AddRole
- // @Summary 添加角色
- // @Tags ADMIN-权限管理
- // @Description 添加角色
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.AddRoleReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/addRole [POST]
- func AddRole(c *gin.Context) {
- var req md.AddRoleReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- engine := db.DBs[svc.GetMasterId(c)]
- roleDb := implement.NewAgentRoleDb(engine)
- now := time.Now()
-
- _, err = roleDb.AgentRoleInsert(&model.AgentRole{
- Name: req.Name,
- State: enum.RoleStateForNormal,
- Memo: req.Memo,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- // UpdateRole
- // @Summary 修改角色
- // @Tags ADMIN-权限管理
- // @Description 修改角色
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.UpdateRoleReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/updateRole [POST]
- func UpdateRole(c *gin.Context) {
- var req md.UpdateRoleReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- engine := db.DBs[svc.GetMasterId(c)]
- roleDb := implement.NewAgentRoleDb(engine)
- role, err := roleDb.GetAgentRole(req.RoleId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if role == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- role.Name = req.Name
- role.Memo = req.Memo
- _, err = roleDb.UpdateAgentRole(role, "name", "memo")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // RoleBindPermissionGroup
- // @Summary 角色绑定权限组
- // @Tags ADMIN-权限管理
- // @Description 角色绑定权限组
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.RoleBindPermissionGroupReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/roleBindPermissionGroup [POST]
- func RoleBindPermissionGroup(c *gin.Context) {
- var req md.RoleBindPermissionGroupReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- err = svc.RoleBindPermissionGroup(c, req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- // UpdateRoleState
- // @Summary 修改角色状态
- // @Tags ADMIN-权限管理
- // @Description 修改角色状态
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.UpdateRoleStateReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/updateRoleState [POST]
- func UpdateRoleState(c *gin.Context) {
- var req md.UpdateRoleStateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- engine := db.DBs[svc.GetMasterId(c)]
- roleDb := implement.NewRoleDb(engine, req.RoleId)
- role, err := roleDb.GetRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if role == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- role.State = req.State
- _, err = roleDb.UpdateRole(role, "state")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // DeleteRole
- // @Summary 删除角色
- // @Tags ADMIN-权限管理
- // @Description 删除角色
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.UpdateRoleStateReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/deleteRole/{$id} [DELETE]
- func DeleteRole(c *gin.Context) {
- id := c.Param("id")
- engine := db.DBs[svc.GetMasterId(c)]
- roleDb := implement.NewRoleDb(engine, utils.StrToInt(id))
- role, err := roleDb.GetRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if role == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
-
- err = svc.DeleteRole(c, utils.StrToInt(id))
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- // AdminList
- // @Summary 管理员列表
- // @Tags ADMIN-权限管理
- // @Description 管理员列表
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.AdminListReq true "请求参数"
- // @Success 200 {string} "具体看返回内容"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/adminList [POST]
- func AdminList(c *gin.Context) {
- var req md.AdminListReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- if req.Limit == 0 {
- req.Limit = 10
- }
- if req.Page == 0 {
- req.Page = 10
- }
- engine := db.DBs[svc.GetMasterId(c)]
- adminDb := implement.NewAdminDb(engine)
- adminList, total, err := adminDb.FindAdmin(req.UserName, req.State, req.Page, req.Limit)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var result []md.AdminListResp
- for _, v := range adminList {
- permissionGroupList, _, err1 := adminDb.FindAdminRolePermissionGroup(v.AdmId)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- var roleList []string
- for _, v1 := range permissionGroupList {
- roleList = append(roleList, v1.Role.Name)
- }
-
- result = append(result, md.AdminListResp{
- Id: v.AdmId,
- Username: v.Username,
- State: v.State,
- IsSuperAdministrator: v.IsSuperAdministrator,
- Memo: v.Memo,
- CreateAt: v.CreateAt,
- UpdateAt: v.UpdateAt,
- RoleList: roleList,
- })
- }
-
- e.OutSuc(c, map[string]interface{}{
- "list": result,
- "total": total,
- "state": []map[string]interface{}{
- {
- "name": enum.RoleState(enum.RoleStateForNormal).String(),
- "value": enum.RoleStateForNormal,
- },
- {
- "name": enum.RoleState(enum.RoleStateForFreeze).String(),
- "value": enum.RoleStateForFreeze,
- },
- },
- }, nil)
- return
- }
-
- // UpdateAdminState
- // @Summary 修改管理员状态
- // @Tags ADMIN-权限管理
- // @Description 修改管理员状态
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.UpdateAdminStateReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/updateAdminState [POST]
- func UpdateAdminState(c *gin.Context) {
- var req md.UpdateAdminStateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
-
- engine := db.DBs[svc.GetMasterId(c)]
- admDb := implement.NewAdminDb(engine)
- admin, err := admDb.GetAdmin(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if admin == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- admin.State = req.State
- _, err = admDb.UpdateAdmin(admin, "state")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // UpdateAdmin
- // @Summary 修改管理员信息
- // @Tags ADMIN-权限管理
- // @Description 修改管理员信息
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.UpdateAdminReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/updateAdmin [POST]
- func UpdateAdmin(c *gin.Context) {
- var req md.UpdateAdminReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- engine := db.DBs[svc.GetMasterId(c)]
- admDb := implement.NewAdminDb(engine)
- admin, err := admDb.GetAdmin(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if admin == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- admin.Username = req.Username
- admin.Memo = req.Memo
- admin.Password = utils.Md5(req.Password)
- _, err = admDb.UpdateAdmin(admin, "username", "memo", "password")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // AddAdmin
- // @Summary 新增管理员
- // @Tags ADMIN-权限管理
- // @Description 新增管理员
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.AddAdminReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/addAdmin [POST]
- func AddAdmin(c *gin.Context) {
- var req md.AddAdminReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- engine := db.DBs[svc.GetMasterId(c)]
- admDb := implement.NewAdminDb(engine)
- isHasAdmin, err := admDb.GetAdminByUserName(req.Username)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if isHasAdmin != nil {
- e.OutErr(c, e.ERR, "当前用户名已存在,请勿重复添加")
- return
- }
-
- admId, err := admDb.CreateAdminId()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- admin := model.Admin{
- AdmId: admId,
- Username: req.Username,
- Password: utils.Md5(req.Password),
- State: enum.AdminStateForNormal,
- IsSuperAdministrator: 0,
- Memo: req.Memo,
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- _, err = admDb.AdminInsert(&admin)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // DeleteAdmin
- // @Summary 删除管理员
- // @Tags ADMIN-权限管理
- // @Description 删除管理员
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/deleteAdmin/{$adm_id} [DELETE]
- func DeleteAdmin(c *gin.Context) {
- admId := c.Param("adm_id")
- err := svc.AdminDelete(c, []int{utils.StrToInt(admId)})
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // BindAdminRole
- // @Summary 管理员绑定角色
- // @Tags ADMIN-权限管理
- // @Description 管理员绑定角色
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @Param args body md.BindAdminRoleReq true "请求参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/bindAdminRole/ [POST]
- func BindAdminRole(c *gin.Context) {
- var req md.BindAdminRoleReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- err = svc.BindAdminRole(c, req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // AdminInfo
- // @Summary 管理员信息
- // @Tags ADMIN-权限管理
- // @Description 管理员信息
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Accept json
- // @Produce json
- // @param adm_id query string true "管理员id"
- // @Success 200 {string} "具体看返回内容"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/role/adminInfo [GET]
- func AdminInfo(c *gin.Context) {
- admId := c.DefaultQuery("adm_id", "")
- engine := db.DBs[svc.GetMasterId(c)]
- admDb := implement.NewAdminDb(engine)
- admin, err := admDb.GetAdmin(utils.StrToInt(admId))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- admin.Password = ""
- e.OutSuc(c, map[string]interface{}{
- "info": admin,
- "state": []map[string]interface{}{
- {
- "name": enum.RoleState(enum.RoleStateForNormal).String(),
- "value": enum.RoleStateForNormal,
- },
- {
- "name": enum.RoleState(enum.RoleStateForFreeze).String(),
- "value": enum.RoleStateForFreeze,
- },
- },
- }, nil)
- return
- }
|