Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Birjemin committed Dec 27, 2020
1 parent 3ae9f97 commit 6b42312
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 55 deletions.
117 changes: 89 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,108 @@
## wxgameod-微信小游戏开放数据后台接口
## go-socialite

[![Build Status](https://travis-ci.com/Birjemin/wxgameod.svg?branch=master)](https://travis-ci.com/Birjemin/wxgameod)
[![Go Report Card](https://goreportcard.com/badge/github.com/birjemin/wxgameod)](https://goreportcard.com/report/github.com/birjemin/wxgameod)
[![codecov](https://codecov.io/gh/Birjemin/wxgameod/branch/master/graph/badge.svg)](https://codecov.io/gh/Birjemin/wxgameod)
oauth2授权登录(QQ、Wchat、Weibo)

[![Build Status](https://travis-ci.com/Birjemin/go-socialite.svg?branch=master)](https://travis-ci.com/Birjemin/go-socialite)
[![Go Report Card](https://goreportcard.com/badge/github.com/birjemin/go-socialite)](https://goreportcard.com/report/github.com/birjemin/go-socialite)
[![codecov](https://codecov.io/gh/Birjemin/go-socialite/branch/master/graph/badge.svg)](https://codecov.io/gh/Birjemin/go-socialite)

[开发者中心](https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/data/storage.removeUserStorage.html)

### 引入方式
```
go get github.com/birjemin/wxgameod
go get github.com/birjemin/go-socialite
```

### 接口列表
### 使用方式

- [removeUserStorage](https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/data/storage.removeUserStorage.html)
- [setUserInteractiveData](https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/data/storage.setUserInteractiveData.html)
- [setUserStorage](https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/data/storage.setUserStorage.html)
- 初始化

### 使用方式
```golang
var (
httpClient = &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
}

defaultObj = &socialite.Default{}

qqObj = &socialite.Qq{
AppID: "",
AppSecret: "x",
RedirectURL: "https://domain/qq/callback",
HTTPRequest: httpClient,
}

wxObj = &socialite.Wechat{
AppID: "",
AppSecret: "",
RedirectURL: "https://domain/qq/callback",
HTTPRequest: httpClient,
}

wbObj = &socialite.Weibo{
ClientID: "",
ClientSecret: "",
RedirectURL: "http://domain.com/wb/callback",
HTTPRequest: httpClient,
}
)

func dispatch(platform string) socialite.ISocialite {
var obj socialite.ISocialite

switch platform {
case "qq":
obj = qqObj
case "wx":
obj = wxObj
case "wb":
obj = wbObj
default:
obj = defaultObj
}

return obj
}
obj := dispatch("wx")
// obj := dispatch("wb")
// obj := dispatch("qq")

```

- 示例
- 获取授权地址(登录完成之后会带上`CODE`跳转到回调地址中)
```golang
log.Print("authorize_url: ", obj.GetAuthorizeURL())
```

- 获取授权AccessToken()
```golang
httpClient := &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
// 上一步得到的CODE
resp, err := obj.Token("CODE")
// 断言
ret, ok := resp.(*socialite.WxRespToken)
if ok {
log.Printf("ret: %#v", ret)
}
var m = SetUserStorage{
AccessToken: "ACCESS_TOKEN",
SessionKey: "SESSION_KEY",
OpenID: "OPEN_ID",
KvList: "{\"kv_list\":[{\"key\":\"1\",\"value\":\"0\"}]}",
HTTPRequest: httpClient,
```

- 获取用户的OPEN_ID(QQ接口专有,wechat、weibo在上一步中已经返回用户标识)
```golang
resp, err := obj.GetMe("ACCESS_TOKEN")
// 断言
ret, ok := resp.(*socialite.QqRespMe)
if ok {
log.Printf("ret: %#v", ret)
}
```

if ret, err := m.doSetUserStorage(ts.URL); err != nil {
t.Error(err)
} else {
if ret.ErrCode != 0 {
t.Error(errors.New("msg: " + ret.ErrMsg))
}
- 获取用户信息
```golang
resp, err := obj.GetUserInfo("ACCESS_TOKEN", "OPEN_ID")
// 断言
ret, ok := resp.(*socialite.WxUserInfo)
if ok {
log.Printf("ret: %#v", ret)
}
```

Expand Down
1 change: 1 addition & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type RespToken struct {
OpenID string `json:"openid"`
}

// Default struct
type Default struct{}

// GetAuthorizeURL get authorize url
Expand Down
38 changes: 18 additions & 20 deletions qq.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
qqUserInfoURL = "https://graph.qq.com/user/get_user_info"
)

// Qq
// Qq struct
// @doc: https://wiki.open.qq.com/wiki/website/%E4%BD%BF%E7%94%A8Authorization_Code%E8%8E%B7%E5%8F%96Access_Token
type Qq struct {
AppID string
Expand Down Expand Up @@ -188,23 +188,22 @@ func (q *Qq) getRespToken(b []byte) (*QqRespToken, error) {
}

return ret, errors.New("get token error")
} else {
}

pattern, err := regexp.Compile(`access_token=(\S*)&expires_in=(\S*)&refresh_token=(\S*)`)
if err != nil {
return ret, err
}
temp := pattern.FindSubmatch(b)
pattern, err := regexp.Compile(`access_token=(\S*)&expires_in=(\S*)&refresh_token=(\S*)`)
if err != nil {
return ret, err
}
temp := pattern.FindSubmatch(b)

if len(temp) != 4 {
return ret, errors.New("length of result is invalid")
}
ret.AccessToken = string(temp[1])
expired, _ := strconv.ParseInt(string(temp[2]), 10, 64)
ret.ExpiresIn = int(expired)
ret.RefreshToken = string(temp[3])
return ret, nil
if len(temp) != 4 {
return ret, errors.New("length of result is invalid")
}
ret.AccessToken = string(temp[1])
expired, _ := strconv.ParseInt(string(temp[2]), 10, 64)
ret.ExpiresIn = int(expired)
ret.RefreshToken = string(temp[3])
return ret, nil
}

// GetMe get me
Expand Down Expand Up @@ -254,13 +253,12 @@ func (q *Qq) getRespMe(b []byte) (*QqRespMe, error) {
}

return ret, errors.New("get token error")
} else {
}

if err := jsoniter.Unmarshal(pattern.Find(b), &ret); err != nil {
return ret, err
}
return ret, nil
if err := jsoniter.Unmarshal(pattern.Find(b), &ret); err != nil {
return ret, err
}
return ret, nil
}

// GetUserInfo get user info
Expand Down
8 changes: 8 additions & 0 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func (c *HTTPClient) GetResponseJSON(response interface{}) error {
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
2 changes: 1 addition & 1 deletion wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
wxUserInfoURL = "https://api.weixin.qq.com/sns/userinfo"
)

// Wechat
// Wechat struct
// @doc: https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
type Wechat struct {
AppID string
Expand Down
4 changes: 2 additions & 2 deletions wechat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
wxHttpClient = &utils.HTTPClient{
wxHTTPClient = &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
Expand All @@ -20,7 +20,7 @@ var (
AppID: "APPID",
AppSecret: "SECRET",
RedirectURL: "REDIRECT_URI",
HTTPRequest: wxHttpClient,
HTTPRequest: wxHTTPClient,
}
)

Expand Down
5 changes: 3 additions & 2 deletions weibo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
wbUserInfoURL = "https://api.weibo.com/2/users/show.json"
)

// Weibo struct
type Weibo struct {
ClientID string
ClientSecret string
Expand All @@ -29,7 +30,7 @@ type wbRespErrorToken struct {
ErrorCode int `json:"error_code"`
Error string `json:"error"`
Request string `json:"request"`
ErrorUri string `json:"error_uri"`
ErrorURI string `json:"error_uri"`
}

// WbRespToken response of me
Expand All @@ -42,7 +43,7 @@ type WbRespToken struct {
IsRealName string `json:"isRealName"`
}

// WbUserInfoWbUserInfo user info
// WbUserInfo user info
type WbUserInfo struct {
wbRespErrorToken
ID int `json:"id"`
Expand Down
4 changes: 2 additions & 2 deletions weibo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
wbHttpClient = &utils.HTTPClient{
wbHTTPClient = &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
Expand All @@ -20,7 +20,7 @@ var (
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URI",
HTTPRequest: wbHttpClient,
HTTPRequest: wbHTTPClient,
}
)

Expand Down

0 comments on commit 6b42312

Please sign in to comment.