/
sns.go
47 lines (35 loc) · 950 Bytes
/
sns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mp
import (
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"github.com/arden/wechat/utils"
)
// AuthSession 小程序授权Session
type AuthSession struct {
SessionKey string `json:"session_key"`
OpenID string `json:"openid"`
UnionID string `json:"unionid"`
}
// Sns sns
type Sns struct {
mp *WXMP
options []utils.HTTPRequestOption
}
// Code2Session 获取小程序授权SessionKey
func (s *Sns) Code2Session(code string) (*AuthSession, error) {
resp, err := s.mp.Client.Get(fmt.Sprintf("%s?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", Code2SessionURL, s.mp.AppID, s.mp.AppSecret, code), s.options...)
if err != nil {
return nil, err
}
r := gjson.ParseBytes(resp)
if r.Get("errcode").Int() != 0 {
return nil, errors.New(r.Get("errmsg").String())
}
reply := new(AuthSession)
if err := json.Unmarshal(resp, reply); err != nil {
return nil, err
}
return reply, nil
}