liuxiaolong
2019-09-21 000710072f9a725b4eb20bdd789e746fa8b677c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package controllers
 
import (
    "basic.com/dbapi.git"
    "github.com/gin-gonic/gin"
    "net/http"
    "time"
    "webserver/extend/code"
    "webserver/extend/util"
    "webserver/middlewares/auth"
)
 
type UserController struct {
 
}
 
type UserVo struct {
    UserName string `json:"username"`
    Password string `json:"password"`
}
 
// @Summary 用户登录
// @Description 用户登录
// @Accept json
// @Produce json
// @Tags 用户
// @Param username query string true "用户名"
// @Param password query string true "密码"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-v/sys/login [post]
func (uc UserController) Login(c *gin.Context) {
    userName := c.PostForm("username")
    password := c.PostForm("password")
    if userName == "" || password == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var api dbapi.UserApi
    flag, data := api.Login(userName, password)
    if flag {
        authDriver := auth.GenerateAuthDriver()
        loginedM := util.Struct2Map(data)
        tokenM := make(map[string]interface{},2)
        tokenM["id"] = loginedM["id"]
        tokenM["username"] = loginedM["username"]
        tokenM["permissions"] = loginedM["permissions"]
        tokenStr := (*authDriver).Login(c.Request, c.Writer, tokenM)
        c.JSON(200,map[string]interface{}{
            "userInfo":loginedM,
            "access_token":tokenStr,
            "refresh_token":tokenStr,
            "scope":"app",
            "token_type":"Bearer",
            "expires_in":time.Now().Add(time.Hour * 8).Unix(),
        })
    } else {
        c.JSON(500,"用户名或密码错误")
    }
}
 
// @Summary 获取当前用户信息
// @Description 获取当前用户信息
// @Accept json
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/users/current [get]
func (uc UserController) Current(c *gin.Context) {
    authDriver := auth.GenerateAuthDriver()
    user := (*authDriver).User(c)
    if user !=nil {
        c.JSON(http.StatusOK,user)
    } else {
        c.JSON(http.StatusUnauthorized,"")
    }
}
// @Router /data/api-u/sys/refresh_token [post]
func (uc UserController) RefreshToken(c *gin.Context){
 
}
 
// @Router /data/api-u/sys/logout [get]
func (uc UserController) Logout(c *gin.Context){
    c.JSON(http.StatusOK,"退出成功")
}
 
// @Router /data/api-u/users/findAllUser [get]
func (uc UserController) FindAllUser(c *gin.Context) {
    var api dbapi.UserApi
    b,d := api.FindAllUser()
    if b {
        util.ResponseFormat(c,code.Success,d)
    } else {
        util.ResponseFormat(c,code.ComError,"")
    }
}
 
type UserEditVo struct {
    Id string `json:"id"`
    UserName string `json:"username"`
    OldPwd string `json:"oldPwd"`
    NewPwd string `json:"newPwd"`
    RoleIds []string `json:"roleIds"`
}
 
// @Summary 更新用户名,密码和角色权限
// @Description 更新用户名,密码和角色权限
// @Accept json
// @Produce json
// @Tags 用户
// @Param userVo body UserEditVo true "用户编辑信息"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/users/edit [post]
func (uc UserController) Edit(c *gin.Context) {
    var userEditVo UserEditVo
    err := c.BindJSON(&userEditVo)
    if err !=nil || userEditVo.Id =="" || userEditVo.OldPwd == "" || userEditVo.NewPwd =="" || userEditVo.UserName == "" {
        util.ResponseFormat(c,code.RequestParamError,"")
        return
    }
    paramBody := util.Struct2Map(userEditVo)
    var api dbapi.UserApi
    b,d := api.Edit(paramBody)
    if b {
        util.ResponseFormat(c,code.UpdateSuccess,d)
    } else {
        util.ResponseFormat(c,code.UpdateFail,"更新失败")
    }
}