123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package api
- import (
- "encoding/base64"
- "entrance-grpc/iam"
- "gadmin/config"
- "gadmin/internal/admin/forms"
- "gadmin/internal/admin/service"
- "gadmin/internal/gorm/query"
- "gadmin/utility/serializer"
- "gadmin/utility/token"
- "github.com/gin-gonic/gin"
- "github.com/sirupsen/logrus"
- )
- func UserRegister(c *gin.Context) {
- var req forms.UserRegisterReq
- if err := c.ShouldBind(&req); err == nil {
- res := service.User.Register(req)
- c.JSON(200, res)
- } else {
- c.JSON(200, ErrorResponse(err))
- }
- }
- //func UserLogin(c *gin.Context) {
- // ip := c.ClientIP()
- // var req forms.UserLoginReq
- // if err := c.ShouldBind(&req); err == nil {
- // res := service.User.Login(req, ip)
- // c.JSON(200, res)
- // } else {
- // c.JSON(200, ErrorResponse(err))
- // }
- //}
- func UserMe(c *gin.Context) {
- var (
- user = token.CurrentUser(c)
- )
- permissions := make([]forms.UserLoginPermissions, 0)
- permissions = append(permissions, forms.UserLoginPermissions{
- Label: "控制台",
- Value: "value",
- })
- //获取用户角色权限
- //roleInfo, err := service.AdminRole.GetRole(c, forms.AdminRoleReq{ID: user.RoleId})
- //if err != nil || roleInfo == nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- //}
- if user.RoleID == 1 {
- for k, _ := range config.AuthMenuMap {
- permissions = append(permissions, forms.UserLoginPermissions{
- Label: config.AuthNameMap[k],
- Value: config.AuthMenuMap[k],
- })
- }
- } else {
- rpdb := query.Use(config.DB).AdminRolePermission
- rolePermission := make([]int, 0)
- err := rpdb.Pluck(rpdb.PermissionID, &rolePermission)
- //err = json.Unmarshal([]byte(roleInfo.Permissions), &rolePermission)
- if err != nil {
- c.JSON(200, ErrorResponse(err))
- return
- }
- for _, v := range rolePermission {
- if _, ok := config.AuthMenuMap[v]; ok {
- permissions = append(permissions, forms.UserLoginPermissions{
- Label: config.AuthNameMap[v],
- Value: config.AuthMenuMap[v],
- })
- }
- }
- }
- //将权限赋值给permission
- info := forms.UserMeReq{
- ID: user.ID,
- UserName: user.UserName,
- RoleId: user.RoleID,
- Avatar: user.Avatar,
- Nickname: user.NickName,
- Permissions: permissions,
- IsSuper: config.IsSuperRole(user.RoleID),
- }
- c.JSON(200, serializer.Suc(info, "获取成功"))
- }
- func UserLogout(c *gin.Context) {
- encodeToken := token.GetAuthorization(c)
- if encodeToken == "" {
- c.JSON(200, serializer.CheckLogin())
- return
- }
- bytesT, err := base64.URLEncoding.DecodeString(encodeToken)
- if err != nil {
- logrus.Warningf("middleware base64.URLEncoding.DecodeString:%+v", err.Error())
- c.JSON(200, serializer.CheckLogin())
- return
- }
- t := string(bytesT)
- resp, err := config.GetIamClient().DeleteToken(c, &iam.DeleteTokenReq{
- Token: t,
- })
- if err != nil {
- logrus.Warningf("middleware config.GetIamClient().DeleteToken:%+v", err.Error())
- c.JSON(200, serializer.CheckLogin())
- return
- }
- if resp.Code != 0 {
- logrus.Warningf("middleware config.GetIamClient().DeleteToken code:%+v,msg:%+v", resp.Code, resp.Msg)
- c.JSON(200, serializer.CheckLogin())
- return
- }
- c.JSON(200, serializer.Suc(nil, "退出成功"))
- }
- //func AdminUserList(c *gin.Context) {
- // var req forms.AdminUserListReq
- // if err := c.ShouldBind(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- // if err := forms.ParseParams(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- //
- // c.JSON(200, service.User.List(c, req))
- //}
- //func AdminUserEdit(c *gin.Context) {
- // var req forms.AdminUserEditReq
- // if err := c.ShouldBind(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- // if err := forms.ParseParams(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- //
- // c.JSON(200, service.User.Edit(c, req))
- //}
- //func ResetPassword(c *gin.Context) {
- // var req forms.AdminUserResetPasswordReq
- // if err := c.ShouldBind(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- // if err := forms.ParseParams(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- //
- // c.JSON(200, service.User.ResetPassword(c, req))
- //}
- //
- //func UpdatePassword(c *gin.Context) {
- // var req forms.AdminUserUpdatePasswordReq
- // if err := c.ShouldBind(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- // if err := forms.ParseParams(&req); err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- //
- // c.JSON(200, service.User.UpdatePassword(c, req))
- //}
- //
- //func RolePermission(c *gin.Context) {
- // is, err := service.User.GetUserRolePermission(c)
- // if err != nil {
- // c.JSON(200, ErrorResponse(err))
- // return
- // }
- // c.JSON(200, serializer.Suc(is, "获取成功"))
- //}
|