123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- 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"
- "strconv"
- "strings"
- )
- //
- // @Summary 添加用户
- // @Description 添加用户
- // @Tags 用户管理
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.UserAddBody true "用户信息"
- // @Success 200 {object} v1.UserAddResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/system_user [post]
- func (c *Controller) UserAdd(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.UserAddRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserAddBody)
- 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.UserAddResponse{}
- rpcReq := &v1.UserAddRequest{
- UserName: req.UserName,
- Password: req.Password,
- Phone: req.Phone,
- Email: req.Email,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- Uid: tokenInfo.Uid,
- GardenName: tokenInfo.GardenName,
- GroupId: req.GroupId,
- RealName: req.RealName,
- DepartmentId: req.DepartmentId,
- ByCompany: tokenInfo.ByCompany,
- BasePermission: req.BasePermission,
- Gender: req.Gender,
- }
- rpcRsp, err := pb.System.UserAdd(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.UserAdd"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- logReq := OperationLogRequest{
- Module: ModuleUser,
- Action: ActionUserAdd,
- Origin: nil,
- Target: req.UserAddBody,
- 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.UserUpdateBody true "用户信息"
- // @Success 200 {object} v1.UserUpdateResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/system_user [put]
- func (c *Controller) UserUpdate(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.UserUpdateRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserUpdateBody)
- 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.UserUpdateResponse{}
- rpcReq := &v1.UserUpdateRequest{
- UserName: req.UserName,
- Password: req.Password,
- Phone: req.Phone,
- Email: req.Email,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- Uid: tokenInfo.Uid,
- GardenName: tokenInfo.GardenName,
- GroupId: req.GroupId,
- Id: req.Id,
- RealName: req.RealName,
- DepartmentId: req.DepartmentId,
- ByCompany: tokenInfo.ByCompany,
- BasePermission: req.BasePermission,
- Gender: req.Gender,
- }
- rpcRsp, err := pb.System.UserUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.UserUpdate"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- logReq := OperationLogRequest{
- Module: ModuleUser,
- Action: ActionUserUpdate,
- Origin: rpcRsp.Origin,
- Target: req.UserUpdateBody,
- 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 " "
- // @Success 200 {object} v1.UserDelResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/system_user [delete]
- func (c *Controller) UserDel(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.UserDelRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.UserDelQuery, 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.UserDelResponse{}
- rpcReq := &v1.UserDelRequest{
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- Uid: tokenInfo.Uid,
- Id: req.Id,
- ByCompany: tokenInfo.ByCompany,
- }
- rpcRsp, err := pb.System.UserDel(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.UserDel"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- logReq := OperationLogRequest{
- Module: ModuleUser,
- Action: ActionUserDel,
- 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"
- // @Param page query int true " "
- // @Param page_size query int false " "
- // @Param group_id query int false "权限组id"
- // @Param user_name query string false "用户名"
- // @Param phone query string false "电话号"
- // @Param exclude query string false "需要排除的用户id,以逗号分隔,如11,2,13,46 (不需要则留空)"
- // @Success 200 {object} v1.UserListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/system_user [get]
- func (c *Controller) UserList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.UserListRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.UserListQuery, 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.UserListResponse{}
- rpcReq := &v1.UserListRequest{
- UserName: req.UserName,
- Phone: req.Phone,
- Cid: tokenInfo.Cid,
- GardenId: tokenInfo.GardenId,
- GroupId: req.GroupId,
- }
- array := strings.Split(req.Exclude, ",")
- rpcReq.Exclude = make([]int64, len(array))
- for i, v := range array {
- rpcReq.Exclude[i], _ = strconv.ParseInt(v, 10, 64)
- }
- rpcRsp, err := pb.System.UserList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.UserList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcRsp.List == nil {
- rpcRsp.List = make([]*v1.UserItem, 0)
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
|