Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Birjemin committed Dec 25, 2020
1 parent 384d826 commit ab6890b
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 352 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
test.go
main
47 changes: 22 additions & 25 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import (
"errors"
)

// ISocialite interface
type ISocialite interface {
// GetAuthorizeURL get authorize url
GetAuthorizeURL(args ...string) string

// Token get token
Token(code string) (interface{}, error)

// RefreshToken refresh token
RefreshToken(refreshToken string) (interface{}, error)

// GetMe get open_id if it needs necessarily
GetMe(accessToken string) (interface{}, error)

// GetUserInfo get user info
GetUserInfo(accessToken, openID string) (interface{}, error)
}

// RespToken struct
type RespToken struct {
Code int `json:"code"`
Expand All @@ -14,27 +32,6 @@ type RespToken struct {
OpenID string `json:"openid"`
}

// RespUserInfo user info
type RespUserInfo struct {
Code int `json:"code"`
Msg string `json:"msg"`
OpenID string `json:"open_id"`
Nickname string `json:"nickname"`
Gender int `json:"gender"`
Country string `json:"country"`
Province string `json:"province"`
City string `json:"city"`
Avatar string `json:"avatar"`
}

// RespMe response of me
type RespMe struct {
Code int `json:"code"`
Msg string `json:"msg"`
ClientID string `json:"client_id"`
OpenID string `json:"openid"`
}

type Default struct{}

// GetAuthorizeURL get authorize url
Expand All @@ -43,22 +40,22 @@ func (d *Default) GetAuthorizeURL(args ...string) string {
}

// Token token
func (d *Default) Token(code string) (*RespToken, error) {
func (d *Default) Token(code string) (interface{}, error) {
return nil, errors.New("invalid")
}

// RefreshToken refresh token
func (d *Default) RefreshToken(refreshToken string) (*RespToken, error) {
func (d *Default) RefreshToken(refreshToken string) (interface{}, error) {
return nil, errors.New("invalid")
}

// GetMe get me
func (d *Default) GetMe(accessToken string) (*RespMe, error) {
func (d *Default) GetMe(accessToken string) (interface{}, error) {
return nil, errors.New("can not support")
}

// GetUserInfo get user info
func (d *Default) GetUserInfo(accessToken, openID string) (*RespUserInfo, error) {
func (d *Default) GetUserInfo(accessToken, openID string) (interface{}, error) {
return nil, errors.New("invalid")
}

139 changes: 75 additions & 64 deletions qq.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ type qqRespErrorToken struct {
ErrDescription string `json:"error_description"`
}

// QqRespToken struct
type QqRespToken struct {
Code int `json:"code"`
Msg string `json:"msg"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}

// QqRespMe response of me
type QqRespMe struct {
Code int `json:"code"`
Expand All @@ -47,11 +56,30 @@ type QqRespMe struct {
OpenID string `json:"openid"`
}

// qqUserInfo user info
type qqUserInfo struct {
Ret int `json:"ret"`
Msg string `json:"msg"`
Nickname string `json:"nickname"`
// QqRespUserInfo user info
type QqRespUserInfo struct {
Ret int `json:"ret"`
Msg string `json:"msg"`
IsLost int `json:"is_lost"`
Nickname string `json:"nickname"`
Gender string `json:"gender"`
GenderType int `json:"gender_type"`
Province string `json:"province"`
City string `json:"city"`
Year string `json:"year"`
Constellation string `json:"constellation"`
FigureURL string `json:"figureurl"`
FigureURL1 string `json:"figureurl_1"`
FigureURL2 string `json:"figureurl_2"`
FigureQqURL string `json:"figureurl_qq"`
FigureQqURL1 string `json:"figureurl_qq_1"`
FigureQqURL2 string `json:"figureurl_qq_2"`
FigureURLType string `json:"figureurl_type"`
IsYellowVIP string `json:"is_yellow_vip"`
VIP string `json:"vip"`
YellowVIPLevel string `json:"yellow_vip_level"`
Level string `json:"level"`
IsYellowVIPLevel string `json:"is_yellow_year_vip"`
}

// GetAuthorizeURL get authorize url
Expand Down Expand Up @@ -80,7 +108,7 @@ func (q *Qq) GetAuthorizeURL(args ...string) string {
}

// Token get token
func (q *Qq) Token(code string) (*RespToken, error) {
func (q *Qq) Token(code string) (interface{}, error) {

b, err := q.doToken(qqTokenURL, code)
if err != nil {
Expand Down Expand Up @@ -111,12 +139,43 @@ func (q *Qq) doToken(url, code string) (ret []byte, err error) {
return ret, nil
}

// RefreshToken refresh token
func (q *Qq) RefreshToken(refreshToken string) (interface{}, error) {

b, err := q.doRefreshToken(qqTokenURL, refreshToken)
if err != nil {
return nil, err
}
return q.getRespToken(b)
}

// doRefreshToken handle
func (q *Qq) doRefreshToken(url, refreshToken string) (ret []byte, err error) {

params := map[string]string{
"grant_type": qqGrantTypeRefresh,
"client_id": q.AppID,
"client_secret": q.AppSecret,
"refresh_token": refreshToken,
}

if err := q.HTTPRequest.HTTPGet(url, params); err != nil {
return nil, err
}

ret, err = q.HTTPRequest.GetResponseByte()
if err != nil {
return nil, err
}
return ret, nil
}

// getRespToken response
func (q *Qq) getRespToken(b []byte) (*RespToken, error) {
func (q *Qq) getRespToken(b []byte) (*QqRespToken, error) {

match, _ := regexp.Match("error", b)

ret := new(RespToken)
ret := new(QqRespToken)

// error
if match {
Expand Down Expand Up @@ -153,39 +212,8 @@ func (q *Qq) getRespToken(b []byte) (*RespToken, error) {
}
}

// RefreshToken refresh token
func (q *Qq) RefreshToken(refreshToken string) (*RespToken, error) {

b, err := q.doRefreshToken(qqTokenURL, refreshToken)
if err != nil {
return nil, err
}
return q.getRespToken(b)
}

// doRefreshToken handle
func (q *Qq) doRefreshToken(url, refreshToken string) (ret []byte, err error) {

params := map[string]string{
"grant_type": qqGrantTypeRefresh,
"client_id": q.AppID,
"client_secret": q.AppSecret,
"refresh_token": refreshToken,
}

if err := q.HTTPRequest.HTTPGet(url, params); err != nil {
return nil, err
}

ret, err = q.HTTPRequest.GetResponseByte()
if err != nil {
return nil, err
}
return ret, nil
}

// GetMe get me
func (q *Qq) GetMe(accessToken string) (*RespMe, error) {
func (q *Qq) GetMe(accessToken string) (interface{}, error) {

b, err := q.doGetMe(qqMeURL, accessToken)
if err != nil {
Expand Down Expand Up @@ -213,10 +241,10 @@ func (q *Qq) doGetMe(url, accessToken string) (ret []byte, err error) {
}

// getRespMe response
func (q *Qq) getRespMe(b []byte) (*RespMe, error) {
func (q *Qq) getRespMe(b []byte) (*QqRespMe, error) {

match, _ := regexp.Match("error", b)
ret := new(RespMe)
ret := new(QqRespMe)

// regexp
pattern, err := regexp.Compile(`{.*}`)
Expand Down Expand Up @@ -244,17 +272,12 @@ func (q *Qq) getRespMe(b []byte) (*RespMe, error) {
}

// GetUserInfo get user info
func (q *Qq) GetUserInfo(accessToken, openID string) (*RespUserInfo, error) {

b, err := q.doGetUserInfo(qqUserInfoURL, accessToken, openID)
if err != nil {
return nil, err
}
return q.getRespUserInfo(b)
func (q *Qq) GetUserInfo(accessToken, openID string) (interface{}, error) {
return q.doGetUserInfo(qqUserInfoURL, accessToken, openID)
}

// doGetUserInfo handle
func (q *Qq) doGetUserInfo(url, accessToken, openID string) (ret *qqUserInfo, err error) {
func (q *Qq) doGetUserInfo(url, accessToken, openID string) (*QqRespUserInfo, error) {

params := map[string]string{
"access_token": accessToken,
Expand All @@ -266,22 +289,10 @@ func (q *Qq) doGetUserInfo(url, accessToken, openID string) (ret *qqUserInfo, er
return nil, err
}

ret = new(qqUserInfo)
if q.HTTPRequest.GetResponseJSON(ret) != nil {
var ret = new(QqRespUserInfo)
if err := q.HTTPRequest.GetResponseJSON(ret); err != nil {
return nil, err
}
return ret, nil
}

// getRespUserInfo response
func (q *Qq) getRespUserInfo(temp *qqUserInfo) (*RespUserInfo, error) {

ret := new(RespUserInfo)
if temp.Ret != 0 {
ret.Code = temp.Ret
ret.Msg = temp.Msg
return ret, errors.New("get user info error")
}
ret.Nickname = temp.Nickname
return ret, nil
}
60 changes: 4 additions & 56 deletions qq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,46 +278,6 @@ func TestQqMe(t *testing.T) {
ast.Equal(100016, ret.Code)
}


// TestQqGetErrRespUserInfo
func TestQqGetErrRespUserInfo(t *testing.T) {

ast := assert.New(t)

stack := new(qqUserInfo)
stack.Ret = 1001
stack.Msg = "invalid code"
ret, err := qqObj.getRespUserInfo(stack)

if err != nil {
ast.Equal("get user info error", err.Error())
}
if ret == nil {
ast.Fail("err result")
return
}
ast.Equal(1001, ret.Code)
}

// TestQqGetSuccessRespUserInfo
func TestQqGetSuccessRespUserInfo(t *testing.T) {

ast := assert.New(t)

stack := new(qqUserInfo)
stack.Nickname = "NICKNAME"
ret, err := qqObj.getRespUserInfo(stack)

if err != nil {
ast.Equal("get token error", err.Error())
}
if ret == nil {
ast.Fail("err result")
return
}
ast.Equal("NICKNAME", ret.Nickname)
}

// TestQqUserInfo
func TestQqUserInfo(t *testing.T) {

Expand All @@ -340,32 +300,20 @@ func TestQqUserInfo(t *testing.T) {
defer ts.Close()

// success
b, err := qqObj.doGetUserInfo(ts.URL, "YOUR_ACCESS_TOKEN", "YOUR_OPENID")
ret, err := qqObj.doGetUserInfo(ts.URL, "YOUR_ACCESS_TOKEN", "YOUR_OPENID")
if err != nil {
ast.Error(err)
}

ret, err := qqObj.getRespUserInfo(b)
if err != nil {
ast.Error(err)
}
ast.Equal(0, ret.Code)
ast.Equal(0, ret.Ret)
ast.Equal("YOUR_NICK_NAME", ret.Nickname)

// fail
b, err = qqObj.doGetUserInfo(ts.URL, "", "")
ret, err = qqObj.doGetUserInfo(ts.URL, "", "")
if err != nil {
ast.Fail(err.Error())
return
}

ret, err = qqObj.getRespUserInfo(b)
if err != nil {
ast.Equal("get user info error", err.Error())
}
if ret == nil {
ast.Fail("err result")
return
}
ast.Equal(1001, ret.Code)
ast.Equal(1001, ret.Ret)
}
8 changes: 8 additions & 0 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (c *HTTPClient) GetResponseJSON(response interface{}) error {
if c.Response.Body == nil {
return errors.New("http request response body is empty")
}
//
// b ,err := ioutil.ReadAll(c.Response.Body)
// if err != nil {
// log.Println("body11: ", err.Error())
// }
// log.Println("body22: ", string(b))
//

var json = jsoniter.ConfigCompatibleWithStandardLibrary
defer c.Response.Body.Close()
return json.NewDecoder(c.Response.Body).Decode(response)
Expand Down
Loading

0 comments on commit ab6890b

Please sign in to comment.