package controller import ( v1 "demo/api/v1" "demo/internal/app" "demo/internal/util" "fmt" "github.com/astaxie/beego/validation" "github.com/gogf/gf/v2/net/ghttp" "github.com/siddontang/go/log" "net/http" ) func Auth(r *ghttp.Request) { appG := app.Ghttp{C: r} var reqInfo v1.Auth err := appG.C.Parse(&reqInfo) valid := validation.Validation{} valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符") valid.MaxSize(reqInfo.Password, 100, "password").Message("最长为100字符") if valid.HasErrors() { log.Error(valid.Errors) appG.Response(http.StatusInternalServerError, app.ERROR_ADD_FAIL, valid.Errors) return } isExist, err := srv.CheckUser(reqInfo.Username, reqInfo.Password) if err != nil { log.Error(err) appG.Response(http.StatusInternalServerError, app.ERROR_AUTH_CHECK_TOKEN_FAIL, err) return } if !isExist { appG.Response(http.StatusOK, app.ERROR_AUTH, "用户名或密码错误") return } token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password) if err != nil { appG.Response(http.StatusInternalServerError, app.ERROR_AUTH_TOKEN, err) return } appG.Response(http.StatusOK, app.SUCCESS, map[string]string{ "token": token, }) } func GetUserInfo(r *ghttp.Request) { appG := app.Ghttp{C: r} data := srv.GetUserInfo(r.Context().Value("jwt_username").(string)) fmt.Println(r.Get("jwt_username").String()) appG.Response(http.StatusOK, app.SUCCESS, data) } func UserLogout(r *ghttp.Request) { appG := app.Ghttp{C: r} data := make(map[string]interface{}) data["name"] = "" data["avatar"] = "" data["introduction"] = "" appG.Response(http.StatusOK, app.SUCCESS, data) }