|
@@ -0,0 +1,77 @@
|
|
|
|
|
+package server
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "encoding/json"
|
|
|
|
|
+ "github.com/shopspring/decimal"
|
|
|
|
|
+ "github.com/spf13/viper"
|
|
|
|
|
+ "go_zh/pkg/http"
|
|
|
|
|
+ "sync"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+type WeChatServer struct {
|
|
|
|
|
+ MchId string
|
|
|
|
|
+ PrivateKey string
|
|
|
|
|
+ CertFile string
|
|
|
|
|
+ KeyFile string
|
|
|
|
|
+ AppId string
|
|
|
|
|
+ AppSecret string
|
|
|
|
|
+ AppletId string
|
|
|
|
|
+ AppletKey string
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type AppletsOpenData struct {
|
|
|
|
|
+ Errormsg string `json:"errmsg"`
|
|
|
|
|
+ Errorcode int `json:"errcode"`
|
|
|
|
|
+ Unionid string `json:"unionid"`
|
|
|
|
|
+ SessionKey string `json:"session_key"`
|
|
|
|
|
+ Openid string `json:"openid"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+var (
|
|
|
|
|
+ weChatServerInstance *WeChatServer
|
|
|
|
|
+ weChatServerOnce sync.Once
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+// GetWeChatServerInstance 获取单例实例
|
|
|
|
|
+func GetWeChatServerInstance() *WeChatServer {
|
|
|
|
|
+ weChatServerOnce.Do(func() {
|
|
|
|
|
+ weChatServerInstance = &WeChatServer{
|
|
|
|
|
+ MchId: viper.GetString("weChat.mchId"),
|
|
|
|
|
+ PrivateKey: viper.GetString("weChat.privateKey"),
|
|
|
|
|
+ CertFile: viper.GetString("weChat.certFile"),
|
|
|
|
|
+ KeyFile: viper.GetString("weChat.keyFile"),
|
|
|
|
|
+ AppId: viper.GetString("weChat.appId"),
|
|
|
|
|
+ AppSecret: viper.GetString("weChat.appSecret"),
|
|
|
|
|
+ AppletId: viper.GetString("weChat.appletId"),
|
|
|
|
|
+ AppletKey: viper.GetString("weChat.appletKey"),
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ return weChatServerInstance
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetAppletsOpenId 微信小程序获取登录openId
|
|
|
|
|
+func (wx *WeChatServer) GetAppletsOpenId(code string) (AppletsOpenData, error) {
|
|
|
|
|
+
|
|
|
|
|
+ url := "https://api.weixin.qq.com/sns/jscode2session"
|
|
|
|
|
+ //发起http get请求
|
|
|
|
|
+
|
|
|
|
|
+ var appletsOpenRes AppletsOpenData
|
|
|
|
|
+ requestData := http.RequestData{}
|
|
|
|
|
+ err := requestData.ConstructRequest(url)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return appletsOpenRes, err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ requestData.SetData("grant_type", "authorization_code")
|
|
|
|
|
+ requestData.SetData("appid", wx.AppletId)
|
|
|
|
|
+ requestData.SetData("secret", wx.AppletKey)
|
|
|
|
|
+ requestData.SetData("js_code", code)
|
|
|
|
|
+
|
|
|
|
|
+ ret, err := requestData.GetRequest()
|
|
|
|
|
+
|
|
|
|
|
+ _ = json.Unmarshal(ret, &appletsOpenRes)
|
|
|
|
|
+ return appletsOpenRes, nil
|
|
|
|
|
+}
|
|
|
|
|
+func (wx *WeChatServer) WeChatJsPay(orderSn string, payMoney decimal.Decimal, notifyUrl string, boby string, wxOpenid string) {
|
|
|
|
|
+
|
|
|
|
|
+}
|