-
Notifications
You must be signed in to change notification settings - Fork 165
/
client.go
51 lines (41 loc) · 1.5 KB
/
client.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
48
49
50
51
package auth
import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/auth"
"github.com/ArtisanCloud/PowerWeChat/v3/src/work/miniProgram/response"
)
type Client struct {
*kernel.BaseClient
// PowerWechat\OpenPlatform\Application
component kernel.ApplicationInterface
}
func NewClient(app kernel.ApplicationInterface, component kernel.ApplicationInterface) (*Client, error) {
baseClient, err := kernel.NewBaseClient(&app, nil)
if err != nil {
return nil, err
}
return &Client{
baseClient,
component,
}, nil
}
// 小程序登录
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/others/WeChat_login.html
func (comp *Client) Session(ctx context.Context, code string) (*response.ResponseSession, error) {
result := &response.ResponseSession{}
config := (*comp.App).GetConfig()
componentConfig := comp.component.GetConfig()
token := comp.component.GetComponent("AccessToken").(*auth.AccessToken)
componentToken, err := token.GetToken(false)
query := &object.StringMap{
"appid": config.GetString("app_id", ""),
"js_code": code,
"grant_type": "authorization_code",
"component_appid": componentConfig.GetString("app_id", ""),
"component_access_token": componentToken.ComponentAccessToken,
}
_, err = comp.BaseClient.HttpGet(ctx, "sns/component/jscode2session", query, nil, result)
return result, err
}