Skip to content

Commit

Permalink
Merge pull request #48 from NJUPT-SAST/dev-windpo
Browse files Browse the repository at this point in the history
new feature:add api:uploadAvatar, complete api:/oauth2/userinfo
  • Loading branch information
windpo authored Oct 12, 2023
2 parents 79816d4 + 7c6e93f commit 8ff1acc
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 236 deletions.
30 changes: 26 additions & 4 deletions api/v1/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,32 @@ func OauthUserInfo(c *gin.Context) {
return
}

c.JSON(http.StatusOK, result.Success(gin.H{
"email": user.Email,
"userId": user.Uid,
}))
profileInfo, serErr := service.GetProfileInfo(*user.Uid)
if serErr != nil {
controllerLogger.Errorln("GetProfile service wrong", serErr)
c.JSON(http.StatusOK, result.Failed(result.HandleError(serErr)))
return
}

if dep, org, getOrgErr := service.GetProfileOrg(profileInfo.OrgId); getOrgErr != nil {
controllerLogger.Errorln("GetProfileOrg Err", getOrgErr)
c.JSON(http.StatusOK, result.Failed(result.HandleError(getOrgErr)))
return
} else {
c.JSON(http.StatusOK, result.Success(gin.H{
"nickname": profileInfo.Nickname,
"userId": user.Uid,
"dep": dep,
"org": org,
"email": profileInfo.Email,
"avatar": profileInfo.Avatar,
"bio": profileInfo.Bio,
"link": profileInfo.Link,
"badge": profileInfo.Badge,
"hide": profileInfo.Hide,
}))
return
}
}

func Authorize(c *gin.Context) {
Expand Down
47 changes: 42 additions & 5 deletions api/v1/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func GetProfile(ctx *gin.Context) {
}
username, err := util.GetUsername(token, model.LOGIN_TOKEN_SUB)
if username == "" || err != nil {
controllerLogger.Errorln("Can`t get username by token")
controllerLogger.Errorln("Can`t get username by token", err)
ctx.JSON(http.StatusOK, result.Failed(result.TokenError))
return
}
Expand All @@ -36,8 +36,10 @@ func GetProfile(ctx *gin.Context) {
return
}

if dep, org, err := service.GetProfileOrg(profileInfo.OrgId); err != nil {

if dep, org, getOrgErr := service.GetProfileOrg(profileInfo.OrgId); getOrgErr != nil {
controllerLogger.Errorln("GetProfileOrg Err", getOrgErr)
ctx.JSON(http.StatusOK, result.Failed(result.HandleError(getOrgErr)))
return
} else {
ctx.JSON(http.StatusOK, result.Success(gin.H{
"nickname": profileInfo.Nickname,
Expand All @@ -62,7 +64,7 @@ func ChangeProfile(ctx *gin.Context) {

username, err := util.GetUsername(token, model.LOGIN_TOKEN_SUB)
if username == "" || err != nil {
controllerLogger.Errorln("Can`t get username by token")
controllerLogger.Errorln("Can`t get username by token", err)
ctx.JSON(http.StatusOK, result.Failed(result.TokenError))
return
}
Expand All @@ -88,8 +90,43 @@ func ChangeProfile(ctx *gin.Context) {
}

func UploadAvatar(ctx *gin.Context) {
ctx.JSON(200, "success")
token := ctx.GetHeader("TOKEN")
if token == "" {
ctx.JSON(http.StatusBadRequest, result.Failed(result.RequestParamError))
return
}

username, err := util.GetUsername(token, model.LOGIN_TOKEN_SUB)
if username == "" || err != nil {
controllerLogger.Errorln("Can`t get username by token", err)
ctx.JSON(http.StatusOK, result.Failed(result.TokenError))
return
}
// split email with @
split := regexp.MustCompile(`@`)
uid := split.Split(username, 2)[0]
uid = strings.ToLower(uid)

//obtain avatar file from body
avatar, err := ctx.FormFile("avatarFile")
if err != nil || avatar == nil {
controllerLogger.Errorln("get avatarFile Error", err)
ctx.JSON(http.StatusBadRequest, result.Failed(result.RequestParamError))
return
}

if err := service.UploadAvatar(avatar, uid, ctx); err != nil {
controllerLogger.Errorln("uploadAvatar Error", err)
ctx.JSON(http.StatusOK, result.Failed(result.HandleError(err)))
return
}

ctx.JSON(http.StatusOK, result.Success(nil))
}
func ChangeEmail(ctx *gin.Context) {
ctx.JSON(200, "success")
}

func DealCensorRes(ctx *gin.Context) {

}
38 changes: 10 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ require (
)

require (
github.com/gin-contrib/sessions v0.0.5
github.com/go-oauth2/oauth2/v4 v4.5.2
github.com/go-session/redis/v3 v3.1.0
github.com/go-session/session v3.1.2+incompatible
github.com/go-session/session/v3 v3.1.5
github.com/gofrs/uuid v4.0.0+incompatible
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.3.0
github.com/google/uuid v1.1.2
github.com/jackc/pgx/v4 v4.15.0
github.com/lib/pq v1.10.9
github.com/redis/go-redis/v9 v9.0.3
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
github.com/sirupsen/logrus v1.9.0
github.com/smartystreets/goconvey v1.8.1
github.com/tencentyun/cos-go-sdk-v5 v0.7.44
github.com/vgarvardt/go-oauth2-pg/v4 v4.4.3
github.com/vgarvardt/go-pg-adapter v1.0.0
golang.org/x/oauth2 v0.10.0
)

require (
Expand All @@ -36,36 +40,24 @@ require (
)

require (
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/bytedance/sonic v1.8.8 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cilium/ebpf v0.10.0 // indirect
github.com/cosiner/argv v0.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/frankban/quicktest v1.14.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-delve/delve v1.20.2 // indirect
github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d // indirect
github.com/go-oauth2/oauth2 v3.9.2+incompatible // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.13.0 // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/google/go-dap v0.8.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand All @@ -82,35 +74,25 @@ require (
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mozillazg/go-httpheader v0.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/smarty/assertions v1.15.0 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vgarvardt/pgx-helpers/v4 v4.0.0-20200225100150-876aee3d1a22 // indirect
go.starlark.net v0.0.0-20230302034142-4b1e35fe2254 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 8ff1acc

Please sign in to comment.