Commit 46d217c8 by yemin

初始化

parents
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/WeChatSDK.iml" filepath="$PROJECT_DIR$/.idea/WeChatSDK.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package response
type ResponseMiniProgram struct {
ErrCode int `json:"errcode"` //错误码
ErrMSG string `json:"errmsg,omitempty"`//错误信息
}
module "WeChatSDK"
\ No newline at end of file
package httpclient
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// HttpClient 本项目中使用的http客户端
var HttpClient *http.Client
func init() {
HttpClient = &http.Client{}
//HttpClient.Timeout = config.Conf.Http.Client.Timeout * time.Millisecond //设置请求超时时间
}
// Get get方法的http请求,返回原始字符串
func Get(url string) ([]byte, error) {
rep, err := HttpClient.Get(url)
if err != nil {
return nil, err
}
defer rep.Body.Close()
data, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
// GetWithHeader get方法的http请求,返回原始字符串
func GetWithHeader(url string, header *map[string]string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
//设置header
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
if header != nil {
for k, v := range *header {
req.Header.Set(k, v)
}
}
//发送请求
rep, err := HttpClient.Do(req)
if err != nil {
return nil, err
}
defer rep.Body.Close()
//处理返回值
data, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
// Post post请求,json
// url 请求地址
// header header参数
// badyParam body参数
func Post(url string, header *map[string]string, badyParam *interface{}) ([]byte, error) {
//处理body参数
var bt []byte
if badyParam != nil {
b, err := json.Marshal(*badyParam)
if err != nil {
return nil, err
}
bt = b
}
//创建post请求
req, err := http.NewRequest("POST", url, bytes.NewReader(bt))
if err != nil {
return nil, err
}
//设置header
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
if header != nil {
for k, v := range *header {
req.Header.Set(k, v)
}
}
//发送请求
rep, err := HttpClient.Do(req)
if err != nil {
return nil, err
}
defer rep.Body.Close()
//处理返回值
data, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
package main
func main() {
}
package auth
import (
"WeChatSDK/httpclient"
"WeChatSDK/miniprogram/auth/response"
"encoding/json"
"fmt"
)
type Client struct {
}
func NewClient() *Client {
return &Client{}
}
// Code2Session 登录凭证校验。
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
func (comp *Client) Code2Session(code string, appId string, secret string) (*response.ResponseCode2Session, error) {
result := &response.ResponseCode2Session{}
var url = fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, secret, code)
r,err := httpclient.Get(url)
if err != nil{
return nil, err
}
e := json.Unmarshal(r, result)
if err != nil{
return nil, e
}
return result, nil
}
package response
import (
response2 "WeChatSDK/core/response"
)
type ResponseCode2Session struct {
OpenID string `json:"openid"` //用户唯一标识
SessionKey string `json:"session_key"` //会话密钥
UnionID string `json:"unionid"` //用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回,详见 UnionID 机制说明。
*response2.ResponseMiniProgram
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment