sunty
2019-07-02 c4ab4a96dfdd097d0faedd14f10b93b18786101d
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
package controllers
 
import (
    "basic.com/dbapi.git"
    "github.com/gin-gonic/gin"
    "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 user body UserVo 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/user/login [post]
func (controller UserController) Login(c *gin.Context) {
    var userVo UserVo
    if err := c.BindJSON(&userVo);err !=nil {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var api dbapi.UserApi
    flag, userInfo := api.Login(userVo.UserName, userVo.Password)
    if flag {
        authDriver := auth.GenerateAuthDriver()
        tokenStr := (*authDriver).Login(c.Request, c.Writer, util.Struct2Map(userInfo))
        util.ResponseFormat(c,code.Success,map[string]interface{}{
            "userInfo":userInfo,
            "token":tokenStr,
        })
    } else {
        util.ResponseFormat(c,code.SigninInfoError,"用户名或密码错误")
    }
}