From 9f96115ccdece419b174084119c7d7c83527d0c7 Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Tue, 5 Oct 2021 14:47:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 10 ++++- miniapp_qrcode.client.go | 72 +++++++++++++++++++++++++++++++++++ miniapp_qrcode.client_test.go | 16 ++++++++ weixin.proto | 2 +- 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 miniapp_qrcode.client.go create mode 100644 miniapp_qrcode.client_test.go diff --git a/common.go b/common.go index a69b676..510d252 100644 --- a/common.go +++ b/common.go @@ -1,5 +1,9 @@ package weixinrpc +import ( + "git.tetele.net/tgo/conf" +) + const DES_KEY = "wxserrpc" type WxApiRes struct { @@ -104,10 +108,14 @@ type MpUserInfoRes struct { func rpc_server_conn(url ...string) (*WeixinRpcServiceClient, error) { - var wx_rpc_url string = "127.0.0.1:7969" + var wx_rpc_url string if len(url) > 0 && url[0] != "" { wx_rpc_url = url[0] + wx_rpc_url = conf.WEIXIN_RPC_PORT + } else { + wx_rpc_url = "127.0.0.1:" + conf.WEIXIN_RPC_PORT } + conn, _, err := DialWeixinRpcService("tcp", wx_rpc_url) if err != nil { return nil, err diff --git a/miniapp_qrcode.client.go b/miniapp_qrcode.client.go new file mode 100644 index 0000000..8035bbc --- /dev/null +++ b/miniapp_qrcode.client.go @@ -0,0 +1,72 @@ +package weixinrpc + +import ( + "encoding/json" + "log" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +func GetMiniAppQrcode(appId, appSecret, page,scene string, url ...string) (string, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return "", err + } + defer conn.Close() + + data := map[string]string{} + data["app_id"] = appId + data["app_secret"] = appSecret + data["page"] = page + data["scene"] = scene + + data_json, err := json.Marshal(data) + if err != nil { + return "", err + } + + encryData := crypter.DesEn(string(data_json), DES_KEY) + + now_int64 := time.Now().Unix() + + now := strconv.FormatInt(now_int64, 10) + + sign := Sign(encryData, now) + + req := &Request{ + proto.String(encryData), + proto.String(now), + proto.String(sign), + nil} + + res := &Response{} + + err = conn.GetMiniappCode(req, res) + + if err != nil { + return "", err + } + + //解密 + res_data_de, err := GetOrgData(res) + + if err != nil { + return "", err + } + + var result string + + err = json.Unmarshal([]byte(res_data_de), &result) + + if err != nil { + log.Println(err, res_data_de) + return "", err + } + + return result, nil +} diff --git a/miniapp_qrcode.client_test.go b/miniapp_qrcode.client_test.go new file mode 100644 index 0000000..b14f816 --- /dev/null +++ b/miniapp_qrcode.client_test.go @@ -0,0 +1,16 @@ +package weixinrpc + +import ( + "testing" +) + +func Test_GetMiniappCode(t *testing.T){ + appid := "wx3d53ccbaf69f7995" + appSecret := "165983626235636be54a16404e3e70a7" + page := "/pages/home/index/index" + scene := "user_id=2" + qrcode,err := GetMiniAppQrcode(appid,appSecret,page,scene) + + t.Log(qrcode) + t.Log(err) +} \ No newline at end of file diff --git a/weixin.proto b/weixin.proto index 355ff43..5359e71 100644 --- a/weixin.proto +++ b/weixin.proto @@ -24,5 +24,5 @@ service WeixinRpcService { rpc getMiniAppOpenid(Request) returns (Response); // 获取用户在小程序的openid rpc getMpOpenid(Request) returns (Response); // 获取用户在公众号的openid rpc getMpUserInfo(Request) returns (Response); //公众号获取用户信息 - rpc getMiniappCode(Request) returns (Response); //获取小程序码 + rpc getMiniappQrcode(Request) returns (Response); //获取小程序码 } \ No newline at end of file