| | |
| | | |
| | | import ( |
| | | "basic.com/dbapi.git" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "net/http" |
| | | "time" |
| | | "webserver/extend/code" |
| | | "webserver/extend/util" |
| | |
| | | // @Accept json |
| | | // @Produce json |
| | | // @Tags 用户 |
| | | // @Param user body UserVo true "用户信息" |
| | | // @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 (controller UserController) Login(c *gin.Context) { |
| | | var userVo UserVo |
| | | if err := c.BindJSON(&userVo);err !=nil { |
| | | userName := c.PostForm("username") |
| | | password := c.PostForm("password") |
| | | if userName == "" || password == "" { |
| | | util.ResponseFormat(c,code.RequestParamError,"参数有误") |
| | | return |
| | | } |
| | | var api dbapi.UserApi |
| | | flag, userInfo := api.Login(userVo.UserName, userVo.Password) |
| | | flag, data := api.Login(userName, password) |
| | | if flag { |
| | | authDriver := auth.GenerateAuthDriver() |
| | | tokenStr := (*authDriver).Login(c.Request, c.Writer, util.Struct2Map(userInfo)) |
| | | util.ResponseFormat(c,code.LoginSuccess,map[string]interface{}{ |
| | | "userInfo":userInfo, |
| | | loginedM := util.Struct2Map(data) |
| | | tokenStr := (*authDriver).Login(c.Request, c.Writer, loginedM) |
| | | c.JSON(200,map[string]interface{}{ |
| | | "userInfo":loginedM, |
| | | "access_token":tokenStr, |
| | | "refresh_token":tokenStr, |
| | | "scope":"app", |
| | |
| | | "expires_in":time.Now().Add(time.Hour * 8).Unix(), |
| | | }) |
| | | } else { |
| | | util.ResponseFormat(c,code.SigninInfoError,"用户名或密码错误") |
| | | c.JSON(500,"用户名或密码错误") |
| | | } |
| | | } |
| | | |
| | |
| | | func (controller UserController) Current(c *gin.Context) { |
| | | authDriver := auth.GenerateAuthDriver() |
| | | user := (*authDriver).User(c) |
| | | fmt.Println("current:",user) |
| | | if user !=nil { |
| | | util.ResponseFormat(c,code.Success,user) |
| | | c.JSON(http.StatusOK,user) |
| | | } else { |
| | | util.ResponseFormat(c,code.NotLogin,"") |
| | | c.JSON(http.StatusUnauthorized,"") |
| | | } |
| | | } |
| | | // @Router /data/api-u/sys/refresh_token [post] |
| | | func (controller UserController) RefreshToken(c *gin.Context){ |
| | | |
| | | } |
| | | |
| | | // @Router /data/api-u/sys/logout [get] |
| | | func (controller UserController) Logout(c *gin.Context){ |
| | | |
| | | c.JSON(http.StatusOK,"退出成功") |
| | | } |