Jerry 6 лет назад
Родитель
Сommit
814b68ee65
1 измененных файлов с 45 добавлено и 10 удалено
  1. 45 10
      wechat_servier_api.go

+ 45 - 10
wechat_export.go → wechat_servier_api.go

@@ -12,11 +12,16 @@ import (
 	"crypto/sha256"
 	"crypto/sha256"
 	"crypto/tls"
 	"crypto/tls"
 	"encoding/hex"
 	"encoding/hex"
-	"fmt"
 	"github.com/parnurzeal/gorequest"
 	"github.com/parnurzeal/gorequest"
 	"strings"
 	"strings"
 )
 )
 
 
+func HttpAgent() (agent *gorequest.SuperAgent) {
+	agent = gorequest.New()
+	agent.TLSClientConfig(&tls.Config{InsecureSkipVerify: true})
+	return
+}
+
 //JSAPI支付,支付参数后,再次计算出小程序用的paySign
 //JSAPI支付,支付参数后,再次计算出小程序用的paySign
 func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
 func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
 	buffer := new(bytes.Buffer)
 	buffer := new(bytes.Buffer)
@@ -92,20 +97,50 @@ func GetH5PaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey stri
 }
 }
 
 
 //获取微信用户的OpenId、SessionKey、UnionId
 //获取微信用户的OpenId、SessionKey、UnionId
-func GetWeChatUserId(appId, secretKey, wxCode string) (userRsp *WeChatUserIdRsp, err error) {
-	userRsp = new(WeChatUserIdRsp)
+//    appId:APPID
+//    appSecret:AppSecret
+//    wxCode:小程序调用wx.login 获取的code
+func Code2Session(appId, appSecret, wxCode string) (userRsp *Code2SessionRsp, err error) {
+	userRsp = new(Code2SessionRsp)
+	url := "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + wxCode + "&grant_type=authorization_code"
+	agent := HttpAgent()
+	_, _, errs := agent.Get(url).EndStruct(userRsp)
+	if len(errs) > 0 {
+		return nil, errs[0]
+	} else {
+		return userRsp, nil
+	}
+}
 
 
-	url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%v&secret=%v&js_code=%v&grant_type=authorization_code", appId, secretKey, wxCode)
+//获取小程序全局唯一后台接口调用凭据(AccessToken:157字符)
+//    appId:APPID
+//    appSecret:AppSecret
+func GetAccessToken(appId, appSecret string) (rsp *GetAccessTokenRsp, err error) {
+	rsp = new(GetAccessTokenRsp)
+	url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret
 
 
-	agent := gorequest.New()
-	tlsCfg := &tls.Config{
-		InsecureSkipVerify: true,
+	agent := HttpAgent()
+	_, _, errs := agent.Get(url).EndStruct(rsp)
+	if len(errs) > 0 {
+		return nil, errs[0]
+	} else {
+		return rsp, nil
 	}
 	}
-	agent.TLSClientConfig(tlsCfg)
-	_, _, errs := agent.Get(url).EndStruct(userRsp)
+}
+
+//用户支付完成后,获取该用户的 UnionId,无需用户授权。
+//    accessToken:接口调用凭据
+//    openId:用户的OpenID
+//    transactionId:微信支付订单号
+func GetPaidUnionId(accessToken, openId, transactionId string) (rsp *GetPaidUnionIdRsp, err error) {
+	rsp = new(GetPaidUnionIdRsp)
+	url := "https://api.weixin.qq.com/wxa/getpaidunionid?access_token=" + accessToken + "&openid=" + openId + "&transaction_id=" + transactionId
+
+	agent := HttpAgent()
+	_, _, errs := agent.Get(url).EndStruct(rsp)
 	if len(errs) > 0 {
 	if len(errs) > 0 {
 		return nil, errs[0]
 		return nil, errs[0]
 	} else {
 	} else {
-		return userRsp, nil
+		return rsp, nil
 	}
 	}
 }
 }