123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- package v1
- import (
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/tasker/httptasker"
- "git.getensh.com/common/gopkgs/util"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- "net/http"
- "property-system-gateway/errors"
- param_v1 "property-system-gateway/param/v1"
- "property-system-gateway/pb"
- "property-system-gateway/pb/v1"
- "property-system-gateway/utils"
- )
- //
- // @Summary 添加权限组
- // @Description 添加权限组
- // @Tags 权限管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.GroupAddBody true "组信息"
- // @Success 200 {object} v1.GroupAddResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/permission/group [post]
- func (c *Controller) GroupAdd(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.GroupAddRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.GroupAddBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.GroupAddResponse{}
- rpcReq := &v1.GroupAddRequest{
- GroupDesc: req.GroupDesc,
- GroupName: req.GroupName,
- PermissionCodes: req.PermissionCodes,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- }
- rpcRsp, err := pb.System.GroupAdd(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.GroupAdd"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- logReq := OperationLogRequest{
- Module: ModulePermission,
- Action: ActionGroupAdd,
- Origin: nil,
- Target: req.GroupAddBody,
- UserName: tokenInfo.UserName,
- Uid: tokenInfo.Uid,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- }
- go OperationLogAdd(&logReq)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 更改权限组
- // @Description 更改权限组
- // @Tags 权限管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.GroupUpdateBody true "组信息"
- // @Success 200 {object} v1.GroupUpdateResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/permission/group [put]
- func (c *Controller) GroupUpdate(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.GroupUpdateRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.GroupUpdateBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.GroupUpdateResponse{}
- rpcReq := &v1.GroupUpdateRequest{
- GroupDesc: req.GroupDesc,
- GroupName: req.GroupName,
- PermissionCodes: req.PermissionCodes,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- Id: req.Id,
- }
- rpcRsp, err := pb.System.GroupUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.GroupUpdate"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- logReq := OperationLogRequest{
- Module: ModulePermission,
- Action: ActionGroupUpdate,
- Origin: rpcRsp.Origin,
- Target: req.GroupUpdateBody,
- UserName: tokenInfo.UserName,
- Uid: tokenInfo.Uid,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- }
- go OperationLogAdd(&logReq)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 删除权限组
- // @Description 删除权限组
- // @Tags 权限管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param id query int true "组id"
- // @Success 200 {object} v1.GroupDelResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/permission/group [delete]
- func (c *Controller) GroupDel(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.GroupDelRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.GroupDelQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.GroupDelResponse{}
- rpcReq := &v1.GroupDelRequest{
- Id: req.Id,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- }
- rpcRsp, err := pb.System.GroupDel(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.GroupDel"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- logReq := OperationLogRequest{
- Module: ModulePermission,
- Action: ActionGroupDel,
- Origin: rpcRsp.Origin,
- Target: nil,
- UserName: tokenInfo.UserName,
- Uid: tokenInfo.Uid,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- }
- go OperationLogAdd(&logReq)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 权限组列表
- // @Description 权限组列表
- // @Tags 权限管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Success 200 {object} v1.GroupListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/permission/group_list [get]
- func (c *Controller) GroupList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.GroupListRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.GroupListResponse{}
- rpcReq := &v1.GroupListRequest{
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- }
- rpcRsp, err := pb.System.GroupList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.GroupList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcRsp.List == nil {
- rpcRsp.List = make([]*v1.GroupItem, 0)
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- func handleGroupPermission(data []*v1.SystemGroupPermissionItem) []*v1.SystemGroupPermissionItem {
- if len(data) == 0 {
- return make([]*v1.SystemGroupPermissionItem, 0)
- }
- for i, _ := range data {
- data[i].Childs = handleGroupPermission(data[i].Childs)
- }
- return data
- }
- //
- // @Summary 权限组信息
- // @Description 权限组信息
- // @Tags 权限管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param id query int true "组id"
- // @Success 200 {object} v1.GroupInfoResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/permission/group_info [get]
- func (c *Controller) GroupInfo(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.GroupInfoRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.GroupInfoQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.GroupInfoResponse{}
- rpcReq := &v1.GroupInfoRequest{
- Id: req.Id,
- }
- rpcRsp, err := pb.System.GroupInfo(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.GroupInfo"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- rpcRsp.List = handleGroupPermission(rpcRsp.List)
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- func handleAllPermission(data []*v1.GardenPermissionItem) []*v1.GardenPermissionItem {
- if len(data) == 0 {
- return make([]*v1.GardenPermissionItem, 0)
- }
- for i, _ := range data {
- data[i].Childs = handleAllPermission(data[i].Childs)
- }
- return data
- }
- //
- // @Summary 所有权限节点信息
- // @Description 所有权限节点信息
- // @Tags 权限管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Success 200 {object} v1.AllPermissionResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/permission/all [get]
- func (c *Controller) AllPermission(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.AllPermissionRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.AllPermissionResponse{}
- rpcReq := &v1.GardenPermissionListRequest{
- GardenId: tokenInfo.GardenId,
- }
- rpcRsp, err := pb.System.GardenPermissionList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.GardenPermissionList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- rpcRsp.List = handleAllPermission(rpcRsp.List)
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
|