diff --git a/README.md b/README.md index 715b038..adb068b 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,37 @@ hequan 运维部 访问 /api/v1/menus?page=2 页面是page 请求头设置 Authorization: Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + + +访问 /api/v1/userInfo 获取用户信息 +前端所需的权限 放在password字段里面,已经去重了。实际项目,可以换成 英文。 + +{ + "code": 200, + "data": { + "lists": [ + { + "id": 2, + "created_on": 1550642309, + "modified_on": 1550642309, + "deleted_on": 0, + "username": "hequan", + "password": ",查询所有菜单,查询单个菜单,创建单个菜单,更新单个菜单,删除单个菜单,查询所有用户,查询单个用户,创建单个用户,更新单个用户,删除单个用户,查询所有角色,查询单个角色,创建单个角色,更新单个角色,删除单个角色", + "role": [ + { + "id": 2, + "created_on": 0, + "modified_on": 0, + "deleted_on": 0, + "name": "运维部", + "menu": null + } + ] + } + ] + }, + "msg": "ok" +} ``` ## 部署 diff --git a/docs/docs.go b/docs/docs.go index 68fce08..702efba 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2019-07-16 17:05:43.3531011 +0800 CST m=+0.059847101 +// 2019-12-24 12:19:55.4766668 +0800 CST m=+0.090628501 package docs @@ -333,6 +333,34 @@ var doc = `{ } } }, + "/api/v1/userInfo": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "获取登录用户信息", + "responses": { + "200": { + "description": "{ \"code\": 200, \"data\": {}, \"msg\": \"ok\" }", + "schema": { + "type": "string" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "type": "string" + } + } + } + } + }, "/api/v1/users": { "get": { "consumes": [ diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index a4230fa..684c79e 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -323,6 +323,34 @@ } } }, + "/api/v1/userInfo": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "获取登录用户信息", + "responses": { + "200": { + "description": "{ \"code\": 200, \"data\": {}, \"msg\": \"ok\" }", + "schema": { + "type": "string" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "type": "string" + } + } + } + } + }, "/api/v1/users": { "get": { "consumes": [ diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index 6af63ea..e81c208 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -273,6 +273,24 @@ paths: summary: 更新角色 tags: - role + /api/v1/userInfo: + get: + consumes: + - application/json + produces: + - application/json + responses: + "200": + description: '{ "code": 200, "data": {}, "msg": "ok" }' + schema: + type: string + "400": + description: Bad Request + schema: + type: string + summary: 获取登录用户信息 + tags: + - users /api/v1/users: get: consumes: diff --git a/pkg/app/request.go b/pkg/app/request.go index 44ff3e2..eddcbed 100644 --- a/pkg/app/request.go +++ b/pkg/app/request.go @@ -2,7 +2,6 @@ package app import ( "github.com/astaxie/beego/validation" - "github.com/hequan2017/go-admin/pkg/logging" ) diff --git a/pkg/util/md5.go b/pkg/util/md5.go index 8a00f54..b348873 100644 --- a/pkg/util/md5.go +++ b/pkg/util/md5.go @@ -11,3 +11,17 @@ func EncodeMD5(value string) string { return hex.EncodeToString(m.Sum(nil)) } + +func RemoveRepByMap(slc []string) []string { + result := []string{} //存放返回的不重复切片 + tempMap := map[string]byte{} // 存放不重复主键 + for _, e := range slc { + l := len(tempMap) + tempMap[e] = 0 //当e存在于tempMap中时,再次添加是添加不进去的,,因为key不允许重复 + //如果上一行添加成功,那么长度发生变化且此时元素一定不重复 + if len(tempMap) != l { // 加入map后,map长度变化,则元素不重复 + result = append(result, e) //当元素不重复时,将元素添加到切片result中 + } + } + return result +} diff --git a/routers/api/user.go b/routers/api/user.go index 7ba2ef4..09f38a2 100644 --- a/routers/api/user.go +++ b/routers/api/user.go @@ -1,16 +1,21 @@ package api import ( + "fmt" "github.com/astaxie/beego/validation" + "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "github.com/hequan2017/go-admin/middleware/inject" "github.com/hequan2017/go-admin/pkg/app" "github.com/hequan2017/go-admin/pkg/e" "github.com/hequan2017/go-admin/pkg/setting" "github.com/hequan2017/go-admin/pkg/util" + jwtGet "github.com/hequan2017/go-admin/pkg/util" + Role_service "github.com/hequan2017/go-admin/service/role_service" "github.com/hequan2017/go-admin/service/user_service" "github.com/unknwon/com" "net/http" + "strings" ) type auth struct { @@ -118,6 +123,70 @@ func GetUser(c *gin.Context) { appG.Response(http.StatusOK, e.SUCCESS, user) } +// @Summary 获取登录用户信息 +// @Tags users +// @Accept json +// @Produce json +// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }" +// @Failure 400 {string} json +// @Router /api/v1/userInfo [GET] +func GetUserInfo(c *gin.Context) { + appG := app.Gin{C: c} + Authorization := c.GetHeader("Authorization") + token := strings.Split(Authorization, " ") + t, _ := jwt.Parse(token[1], func(*jwt.Token) (interface{}, error) { + return jwtGet.JwtSecret, nil + }) + fmt.Print(jwtGet.GetIdFromClaims("username", t.Claims)) + + userService := user_service.User{ + Username: jwtGet.GetIdFromClaims("username", t.Claims), + PageNum: util.GetPage(c), + PageSize: setting.AppSetting.PageSize, + } + + user, err := userService.GetAll() + + for _, v := range user { + menus := make([]string, 1) + for _, v1 := range v.Role { + RoleService := Role_service.Role{ID: v1.ID} + exists, err := RoleService.ExistByID() + + if err != nil { + appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil) + return + } + if !exists { + appG.Response(http.StatusOK, e.ERROR_NOT_EXIST, nil) + return + } + + r, err := RoleService.Get() + if err != nil { + appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil) + return + } + for _, v2 := range r.Menu { + menus = append(menus, v2.Name) + } + fmt.Print(menus) + } + menus = util.RemoveRepByMap(menus) + v.Password = strings.Join(menus, ",") + } + + if err != nil { + appG.Response(http.StatusInternalServerError, e.ERROR_GET_S_FAIL, nil) + return + } + + data := make(map[string]interface{}) + data["lists"] = user + + appG.Response(http.StatusOK, e.SUCCESS, data) +} + // @Summary 获取所有用户 // @Tags users // @Accept json diff --git a/routers/api/v1/role.go b/routers/api/v1/role.go index c8120a7..fbc0d0d 100644 --- a/routers/api/v1/role.go +++ b/routers/api/v1/role.go @@ -48,13 +48,13 @@ func GetRole(c *gin.Context) { return } - article, err := RoleService.Get() + role, err := RoleService.Get() if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil) return } - appG.Response(http.StatusOK, e.SUCCESS, article) + appG.Response(http.StatusOK, e.SUCCESS, role) } // @Summary 获取所有角色 diff --git a/routers/router.go b/routers/router.go index 06965ca..8eb93d5 100644 --- a/routers/router.go +++ b/routers/router.go @@ -30,7 +30,9 @@ func InitRouter() *gin.Engine { apiV1 := r.Group("/api/v1") - apiV1.Use(jwt.JWT()) // token 验证 + apiV1.Use(jwt.JWT()) // token 验证 + + apiV1.GET("/userInfo", api.GetUserInfo) apiV1.Use(permission.CasbinMiddleware()) // 权限 验证 {