当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 97

开发团队 / 微信Go SDK
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
client_request.go 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
leexs 提交于 2019-08-25 16:59 . 微信找零接口的对接
package wechat
import (
"encoding/json"
)
func (c *Client) buildBody(bodyObj interface{}) (body map[string]interface{}, err error) {
// 将bodyObj转换为map[string]interface{}类型
bodyJson, _ := json.Marshal(bodyObj)
body = make(map[string]interface{})
_ = json.Unmarshal(bodyJson, &body)
// 添加固定参数
if c.isMch {
body["mch_appid"] = c.config.AppId
body["mchid"] = c.config.MchId
} else {
body["appid"] = c.config.AppId
body["mch_id"] = c.config.MchId
}
if c.isFacilitator() {
body["sub_appid"] = c.config.SubAppId
body["sub_mch_id"] = c.config.SubMchId
}
nonceStr := GetRandomString(32)
body["nonce_str"] = nonceStr
// 生成签名
signType, _ := body["sign_type"].(string)
var sign string
if c.isProd {
sign = c.localSign(body, signType, c.apiKey)
} else {
body["sign_type"] = SignTypeMD5
key, iErr := c.sandboxSign(nonceStr, SignTypeMD5)
if err = iErr; iErr != nil {
return
}
sign = c.localSign(body, SignTypeMD5, key)
}
body["sign"] = sign
return
}
// 向微信发送请求
func (c *Client) doWeChat(relativeUrl string, bodyObj interface{}) (bytes []byte, err error) {
// 转换参数
body, err := c.buildBody(bodyObj)
if err != nil {
return
}
// 发起请求
bytes, err = httpPost(c.url(relativeUrl), GenerateXml(body))
return
}
// 向微信发送带证书请求
func (c *Client) doWeChatWithCert(relativeUrl string, bodyObj interface{}) (bytes []byte, err error) {
// 转换参数
body, err := c.buildBody(bodyObj)
if err != nil {
return
}
// 设置证书
transport := c.setCertData(c.certFilepath)
if transport == nil {
return
}
// 发起请求
bytes, err = httpPostWithCert(c.url(relativeUrl), GenerateXml(body), transport)
return
}
Go
1
https://gitee.com/OAGroup/wechat.git
git@gitee.com:OAGroup/wechat.git
OAGroup
wechat
微信Go SDK
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891