Score
0
Watch 30 Star 80 Fork 18

北京科技有限公司 / 微信Go SDKGoMIT

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
微信支付、公众号、小程序相关后端接口的Go语言实现。 spread retract

Clone or download
client_request.go 1.70 KB
Copy Edit Web IDE Raw Blame History
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 = httpPostXml(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
}
// 设置证书和连接池
if err = c.setCertData(c.certFilepath); err != nil {
return
}
// 发起请求
bytes, err = httpPostXmlWithCert(c.url(relativeUrl), GenerateXml(body), c.certClient)
return
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/cuckoopark/wechat.git
git@gitee.com:cuckoopark/wechat.git
cuckoopark
wechat
微信Go SDK
master

Help Search

191139_cd20d5fd_5186603 191143_ebef6f8d_5186603