You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.3 KiB
35 lines
1.3 KiB
3 years ago
|
package wechatoffice
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
3 years ago
|
"net/http"
|
||
3 years ago
|
)
|
||
|
|
||
2 years ago
|
type SnsJsCode2sessionResponse struct {
|
||
3 years ago
|
OpenId string `json:"openid"` // 用户唯一标识
|
||
|
SessionKey string `json:"session_key"` // 会话密钥
|
||
|
Unionid string `json:"unionid"` // 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回
|
||
|
Errcode string `json:"errcode"` // 错误码
|
||
|
Errmsg string `json:"errmsg"` // 错误信息
|
||
|
}
|
||
|
|
||
2 years ago
|
type SnsJsCode2sessionResult struct {
|
||
|
Result SnsJsCode2sessionResponse // 结果
|
||
|
Body []byte // 内容
|
||
|
Err error // 错误
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func NewSnsJsCode2sessionResult(result SnsJsCode2sessionResponse, body []byte, err error) *SnsJsCode2sessionResult {
|
||
|
return &SnsJsCode2sessionResult{Result: result, Body: body, Err: err}
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func (app *App) SnsJsCode2session(jsCode string) *SnsJsCode2sessionResult {
|
||
3 years ago
|
// 请求
|
||
|
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, jsCode), map[string]interface{}{}, http.MethodGet)
|
||
|
// 定义
|
||
2 years ago
|
var response SnsJsCode2sessionResponse
|
||
3 years ago
|
err = json.Unmarshal(body, &response)
|
||
2 years ago
|
return NewSnsJsCode2sessionResult(response, body, err)
|
||
3 years ago
|
}
|