package api import ( "fmt" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "github.com/kptyun/KPTCOMM/pkg/app" "github.com/kptyun/KPTCOMM/pkg/e" "github.com/kptyun/KPTCOMM/pkg/util" "github.com/kptyun/KPTCOMM/routers/restful" "github.com/kptyun/KPTCOMM/service/user_service" "io/ioutil" "net/http" ) 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) } // @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 RegPasture(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) imei := fsion.Get("imei").ValueStr() fmt.Println(imei) data := imei //restful.GetUserInfo(imei) appG.Response(http.StatusOK, e.SUCCESS, data) }