liuxiaolong
2019-08-22 f4e8f206a6760bdc31734dfcb1c65916b5b76311
controllers/user.go
@@ -1,9 +1,83 @@
package controllers
import (
   "basic.com/dbapi.git"
   "github.com/gin-gonic/gin"
   "net/http"
   "time"
   "webserver/extend/code"
   "webserver/extend/util"
   "webserver/middlewares/auth"
)
func Login(c *gin.Context) {
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 (controller 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)
      tokenStr := (*authDriver).Login(c.Request, c.Writer, loginedM)
      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-v/users/current [get]
func (controller 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 (controller UserController) RefreshToken(c *gin.Context){
}
// @Router /data/api-u/sys/logout [get]
func (controller UserController) Logout(c *gin.Context){
   c.JSON(http.StatusOK,"退出成功")
}