package api import ( "net/http" "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "kpt.xdmy/apiserver/routers/restful" "kpt.xdmy/apiserver/service/user_service" "kpt.xdmy/pkg/app" "kpt.xdmy/pkg/e" "kpt.xdmy/pkg/util" ) type auth struct { Id int `json:"id"` Username string `json:"username"` Password string `json:"password"` CaptchaKey string `json:"CaptchaKey"` Role int `json:"role_id"` } // @Summary 获取登录token 信息 // @Tags auth // @Accept json // @Produce json // @Param username formData string true "admin" // @Param password formData string true "123456" // @Success 200 {string} json "{ "code": 200e, "data": { "token": "xxx" }, "msg": "ok" }" // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}" // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}" // @Router /auth [POST] func Auth(c *gin.Context) { appG := app.Gin{C: c} var reqInfo auth err := c.BindJSON(&reqInfo) //c.Request.ParseForm() //reqInfo.Username = c.PostForm("username") //reqInfo.Password = c.PostForm("pwd") valid := validation.Validation{} valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符") valid.MaxSize(reqInfo.Password, 100, "password").Message("最长为100字符") if valid.HasErrors() { app.MarkErrors(valid.Errors) appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, valid.Errors) return } authService := user_service.User{Username: reqInfo.Username, Password: reqInfo.Password} isExist, err := authService.Check() if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, err) return } if !isExist { //appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil) appG.Response(http.StatusOK, e.ERROR_AUTH, "用户名或密码错误") return } token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password) if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, err) return } appG.Response(http.StatusOK, e.SUCCESS, map[string]string{ "token": token, }) } // @Summary 获取单个用户信息 // @Tags users // @Accept json // @Produce json // @Param id body int true "id" // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }" // @Router /authdata/userinfo [GET] func UserLogout(c *gin.Context) { appG := app.Gin{C: c} data := make(map[string]interface{}) data["name"] = "" data["avatar"] = "" data["introduction"] = "" appG.Response(http.StatusOK, e.SUCCESS, data) } // @Summary 获取单个用户信息 // @Tags users // @Accept json // @Produce json // @Param id path int true "iddd" // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }" // @Router /authdata/userinfo [GET] func GetUserinfo(c *gin.Context) { appG := app.Gin{C: c} data := restful.GetUserInfo(c.MustGet("jwt_username").(string)) appG.Response(http.StatusOK, e.SUCCESS, data) } func GetOpenID(c *gin.Context) { code := c.Param("code") appG := app.Gin{C: c} openid, err := util.SendWxAuthAPI(code) if err != nil { appG.Response(http.StatusOK, e.SUCCESS, map[string]string{ "err": "openid 获取失败 :" + err.Error(), }) return } appG.Response(http.StatusOK, e.SUCCESS, map[string]string{ "openid": openid, }) }