数据库连接初始化优化

权限部分修改
This commit is contained in:
linwenxiang committed 2021-02-21 14:06:13 +08:00
1 parent 4979fd659f
commit deeb82048b
37 files changed
+726 -721

No files matched your search

-167
View File
@@ -1,167 +0,0 @@
package system
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/service/dto"
"go-admin/app/admin/models"
"go-admin/common/global"
"go-admin/tools"
"go-admin/tools/app"
)
// @Summary 角色列表数据
// @Description Get JSON
// @Tags 角色/Role
// @Param roleName query string false "roleName"
// @Param status query string false "status"
// @Param roleKey query string false "roleKey"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/rolelist [get]
// @Security Bearer
func GetRoleList(c *gin.Context) {
var data models.SysRole
var err error
var pageSize = 10
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex, err = tools.StringToInt(index)
}
data.RoleKey = c.Request.FormValue("roleKey")
data.RoleName = c.Request.FormValue("roleName")
data.Status = c.Request.FormValue("status")
data.DataScope = tools.GetUserIdStr(c)
result, count, err := data.GetPage(pageSize, pageIndex)
tools.HasError(err, "", -1)
app.PageOK(c, result, count, pageIndex, pageSize, "")
}
// @Summary 获取Role数据
// @Description 获取JSON
// @Tags 角色/Role
// @Param roleId path string false "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/role [get]
// @Security Bearer
func GetRole(c *gin.Context) {
var Role models.SysRole
Role.RoleId, _ = tools.StringToInt(c.Param("roleId"))
result, err := Role.Get()
menuIds := make([]int, 0)
menuIds, err = Role.GetRoleMeunId()
tools.HasError(err, "抱歉未找到相关信息", -1)
result.MenuIds = menuIds
app.OK(c, result, "")
}
// @Summary 创建角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body models.SysRole true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/role [post]
func InsertRole(c *gin.Context) {
var data models.SysRole
data.CreateBy = tools.GetUserIdStr(c)
err := c.Bind(&data)
tools.HasError(err, "数据解析失败", 500)
id, err := data.Insert()
tools.HasError(err, "", -1)
data.RoleId = id
var t models.RoleMenu
if len(data.MenuIds) > 0 {
_, err = t.Insert(id, data.MenuIds)
tools.HasError(err, "", -1)
}
_, err = global.LoadPolicy()
tools.HasError(err, "", -1)
app.OK(c, data, "添加成功")
}
// @Summary 修改用户角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body models.SysRole true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
// @Router /api/v1/role [put]
func UpdateRole(c *gin.Context) {
var data models.SysRole
data.UpdateBy = tools.GetUserIdStr(c)
err := c.Bind(&data)
tools.HasError(err, "数据解析失败", -1)
result, err := data.Update(data.RoleId)
tools.HasError(err, "", -1)
var t models.RoleMenu
_, err = t.DeleteRoleMenu(data.RoleId)
tools.HasError(err, "修改失败(delete rm)", -1)
if len(data.MenuIds) > 0 {
_, err2 := t.Insert(data.RoleId, data.MenuIds)
tools.HasError(err2, "修改失败(insert)", -1)
}
_, err = global.LoadPolicy()
tools.HasError(err, "", -1)
app.OK(c, result, "修改成功")
}
func UpdateRoleDataScope(c *gin.Context) {
var data models.SysRole
var req dto.RoleDataScopeReq
data.UpdateBy = tools.GetUserIdStr(c)
err := c.Bind(&req)
tools.HasError(err, "数据解析失败", -1)
data.RoleId = req.RoleId
data.DataScope = req.DataScope
result, err := data.Update(data.RoleId)
var t models.SysRoleDept
_, err = t.DeleteRoleDept(data.RoleId)
tools.HasError(err, "添加失败1", -1)
if data.DataScope == "2" {
_, err2 := t.Insert(data.RoleId, data.DeptIds)
tools.HasError(err2, "添加失败2", -1)
}
app.OK(c, result, "修改成功")
}
// @Summary 删除用户角色
// @Description 删除数据
// @Tags 角色/Role
// @Param roleId path int true "roleId"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/role/{roleId} [delete]
func DeleteRole(c *gin.Context) {
var Role models.SysRole
Role.UpdateBy = tools.GetUserIdStr(c)
IDS := tools.IdsStrToIdsIntGroup("roleId", c)
_, err := Role.BatchDelete(IDS)
tools.HasError(err, "删除失败", -1)
_, err = global.LoadPolicy()
tools.HasError(err, "", -1)
app.OK(c, "", "删除成功")
}
+108 -16
View File
@@ -2,14 +2,13 @@ package sys_role
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
"go-admin/app/admin/service/dto"
"go-admin/common/apis"
"go-admin/common/global"
"go-admin/common/log"
"go-admin/tools"
"net/http"
)
@@ -17,6 +16,17 @@ type SysRole struct {
apis.Api
}
// @Summary 角色列表数据
// @Description Get JSON
// @Tags 角色/Role
// @Param roleName query string false "roleName"
// @Param status query string false "status"
// @Param roleKey query string false "roleKey"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/role [get]
// @Security Bearer
func (e *SysRole) GetSysRoleList(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
d := new(dto.SysRoleSearch)
@@ -47,6 +57,14 @@ func (e *SysRole) GetSysRoleList(c *gin.Context) {
e.PageOK(c, list, int(count), d.GetPageIndex(), d.GetPageSize(), "查询成功")
}
// @Summary 获取Role数据
// @Description 获取JSON
// @Tags 角色/Role
// @Param roleId path string false "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/role/{id} [get]
// @Security Bearer
func (e *SysRole) GetSysRole(c *gin.Context) {
control := new(dto.SysRoleById)
db, err := tools.GetOrm(c)
@@ -76,6 +94,16 @@ func (e *SysRole) GetSysRole(c *gin.Context) {
e.OK(c, object, "查看成功")
}
// @Summary 创建角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body models.SysRole true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/role [post]
// @Security Bearer
func (e *SysRole) InsertSysRole(c *gin.Context) {
control := new(dto.SysRoleControl)
db, err := tools.GetOrm(c)
@@ -97,21 +125,35 @@ func (e *SysRole) InsertSysRole(c *gin.Context) {
return
}
// 设置创建人
object.CreateBy= tools.GetUserId(c)
object.CreateBy = tools.GetUserId(c)
serviceSysRole := service.SysRole{}
serviceSysRole.Orm = db
serviceSysRole.MsgID = msgID
err = serviceSysRole.InsertSysRole(object)
s := service.SysRole{}
s.Orm = db
s.MsgID = msgID
err = s.InsertSysRole(object)
if err != nil {
log.Error(err)
e.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "")
return
}
e.OK(c, object.GetId(), "创建成功")
}
// @Summary 修改用户角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept application/json
// @Product application/json
// @Param data body models.SysRole true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
// @Router /api/v1/role/{id} [put]
// @Security Bearer
func (e *SysRole) UpdateSysRole(c *gin.Context) {
control := new(dto.SysRoleControl)
db, err := tools.GetOrm(c)
@@ -134,17 +176,30 @@ func (e *SysRole) UpdateSysRole(c *gin.Context) {
}
object.UpdateBy = tools.GetUserId(c)
serviceSysRole := service.SysRole{}
serviceSysRole.Orm = db
serviceSysRole.MsgID = msgID
err = serviceSysRole.UpdateSysRole(object)
s := service.SysRole{}
s.Orm = db
s.MsgID = msgID
err = s.UpdateSysRole(object)
if err != nil {
log.Error(err)
return
}
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "")
return
}
e.OK(c, object.GetId(), "更新成功")
}
// @Summary 删除用户角色
// @Description 删除数据
// @Tags 角色/Role
// @Param roleId path int true "roleId"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/role/{roleId} [delete]
// @Security Bearer
func (e *SysRole) DeleteSysRole(c *gin.Context) {
control := new(dto.SysRoleById)
db, err := tools.GetOrm(c)
@@ -162,14 +217,51 @@ func (e *SysRole) DeleteSysRole(c *gin.Context) {
return
}
serviceSysRole := service.SysRole{}
serviceSysRole.Orm = db
serviceSysRole.MsgID = msgID
err = serviceSysRole.RemoveSysRole(control)
s := service.SysRole{}
s.Orm = db
s.MsgID = msgID
err = s.RemoveSysRole(control)
if err != nil {
log.Error(err)
return
}
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "")
return
}
e.OK(c, control.GetId(), "删除成功")
}
func (e *SysRole) UpdateRoleDataScope(c *gin.Context) {
control := new(dto.RoleDataScopeReq)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//更新操作
err = c.Bind(control)
if err != nil {
log.Errorf("msgID[%s] request bind error, %s", msgID, err.Error())
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
data := &system.SysRole{
RoleId: control.RoleId,
DataScope: control.DataScope,
DeptIds: control.DeptIds,
}
data.UpdateBy = tools.GetUserId(c)
s := &service.SysRole{}
s.Orm = db
s.MsgID = msgID
err = s.UpdateDataScope(data)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "")
return
}
e.OK(c, nil, "操作成功")
}