40 lines
1019 B
Go
40 lines
1019 B
Go
package weixin
|
|
|
|
import (
|
|
"catface/app/global/variable"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func Code2Session(js_code string) (string, error) {
|
|
appid := variable.ConfigYml.GetString("Weixin.AppId")
|
|
appSecret := variable.ConfigYml.GetString("Weixin.AppSecret")
|
|
grantType := variable.ConfigYml.GetString("Weixin.Code2Session.GrantType")
|
|
|
|
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=%s", appid, appSecret, js_code, grantType)
|
|
|
|
// 创建一个新的HTTP请求
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
// 发送HTTP请求
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error sending request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 读取响应体
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error reading response body: %v", err)
|
|
}
|
|
|
|
// 返回响应体
|
|
return string(body), nil
|
|
}
|