Skip to content

Commit

Permalink
v1.4 增加userInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
何全 committed Dec 24, 2019
1 parent df4a228 commit e305d9e
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 5 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

## 部署
Expand Down
30 changes: 29 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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": [
Expand Down
28 changes: 28 additions & 0 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
18 changes: 18 additions & 0 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion pkg/app/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"github.com/astaxie/beego/validation"

"github.com/hequan2017/go-admin/pkg/logging"
)

Expand Down
14 changes: 14 additions & 0 deletions pkg/util/md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
69 changes: 69 additions & 0 deletions routers/api/user.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 获取所有角色
Expand Down
4 changes: 3 additions & 1 deletion routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) // 权限 验证

{
Expand Down

0 comments on commit e305d9e

Please sign in to comment.