Skip to content

Commit

Permalink
Update User
Browse files Browse the repository at this point in the history
  • Loading branch information
amorist committed Feb 27, 2021
1 parent 5e08d50 commit 77083ff
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 60 deletions.
42 changes: 0 additions & 42 deletions open/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const (
accessTokenURL string = "https://open.douyin.com/oauth/access_token?client_key=%s&client_secret=%s&code=%s&grant_type=authorization_code"
refreshAccessTokenURL string = "https://open.douyin.com/oauth/oauth/refresh_token?client_key=%s&grant_type=refresh_token&refresh_token=%s"
clientTokenURL string = "https://open.douyin.com/oauth/oauth/client_token/?client_key=%s&client_secret=%s&grant_type=client_credential"
userInfoURL string = "https://open.douyin.com/oauth/oauth/userinfo?access_token=%s&open_id=%s"
)

// Oauth 保存用户授权信息
Expand Down Expand Up @@ -80,44 +79,3 @@ func (oauth *Oauth) GetUserAccessToken(code string) (accessToken credential.Acce

return
}

// UserInfo 用户信息
type UserInfo struct {
util.CommonError

Avatar string `json:"avatar"`
City string `json:"city"`
Country string `json:"country"`
EAccountRole string `json:"e_account_role"`
Gender int32 `json:"gender"`
Nickname string `json:"nickname"`
OpenID string `json:"open_id"`
Province string `json:"province"`
Unionid string `json:"union_id"`
}

type userInforRes struct {
Message string `json:"message"`
Data UserInfo `json:"data"`
}

// GetUserInfo 获取用户信息.
func (oauth *Oauth) GetUserInfo(accessToken, openID string) (userInfo *UserInfo, err error) {
uri := fmt.Sprintf(userInfoURL, accessToken, openID)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
var result userInforRes
err = json.Unmarshal(response, &result)
if err != nil {
return
}
if result.Data.ErrCode != 0 {
err = fmt.Errorf("GetUserInfo error : errcode=%v , errmsg=%v", result.Data.ErrCode, result.Data.ErrMsg)
return
}
userInfo = &result.Data
return
}
72 changes: 54 additions & 18 deletions open/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

const (
userInfoURL string = "https://open.douyin.com/oauth/oauth/userinfo?access_token=%s&open_id=%s"
fansListURL string = "https://open.douyin.com/fans/list?access_token=%s&open_id=%s&cursor=%d&count=%d"
followingListURL string = "https://open.douyin.com/following/list?access_token=%s&open_id=%s&cursor=%d&count=%d"
)
Expand All @@ -25,33 +26,68 @@ func NewUser(context *context.Context) *User {
return user
}

// Info user info.
// Info .
type Info struct {
util.CommonError

Total int64 `json:"total"`
Cursor int64 `json:"cursor"`
HasMore bool `json:"has_more"`
List []struct {
Avatar string `json:"avatar"`
City string `json:"city"`
Country string `json:"country"`
EAccountRole string `json:"e_account_role"`
Gender int32 `json:"gender"`
Nickname string `json:"nickname"`
OpenID string `json:"open_id"`
Province string `json:"province"`
Unionid string `json:"union_id"`
} `json:"list"`
Avatar string `json:"avatar"`
City string `json:"city"`
Country string `json:"country"`
EAccountRole string `json:"e_account_role"`
Gender int32 `json:"gender"`
Nickname string `json:"nickname"`
OpenID string `json:"open_id"`
Province string `json:"province"`
Unionid string `json:"union_id"`
}

type infoRes struct {
type userInfoRes struct {
Message string `json:"message"`
Data Info `json:"data"`
}

// GetUserInfo 获取用户信息.
func (user *User) GetUserInfo(openid string) (userInfo *Info, err error) {
accessToken, err := user.GetAccessToken(openid)
if err != nil {
return
}
uri := fmt.Sprintf(userInfoURL, accessToken, openid)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
var result userInfoRes
err = json.Unmarshal(response, &result)
if err != nil {
return
}
if result.Data.ErrCode != 0 {
err = fmt.Errorf("GetUserInfo error : errcode=%v , errmsg=%v", result.Data.ErrCode, result.Data.ErrMsg)
return
}
userInfo = &result.Data
return
}

// ListInfo user list.
type ListInfo struct {
util.CommonError

Total int64 `json:"total"`
Cursor int64 `json:"cursor"`
HasMore bool `json:"has_more"`
List []Info `json:"list"`
}

type infoRes struct {
Message string `json:"message"`
Data ListInfo `json:"data"`
}

// ListFans 粉丝列表
func (user *User) ListFans(openid string, cursor, count int64) (fans *Info, err error) {
func (user *User) ListFans(openid string, cursor, count int64) (fans *ListInfo, err error) {
accessToken, err := user.GetAccessToken(openid)
if err != nil {
return
Expand All @@ -76,7 +112,7 @@ func (user *User) ListFans(openid string, cursor, count int64) (fans *Info, err
}

// ListFollowing 关注列表
func (user *User) ListFollowing(openid string, cursor, count int64) (following *Info, err error) {
func (user *User) ListFollowing(openid string, cursor, count int64) (following *ListInfo, err error) {
accessToken, err := user.GetAccessToken(openid)
if err != nil {
return
Expand Down

0 comments on commit 77083ff

Please sign in to comment.