From 27950743ffdbadcc1fe2987752d317c458f5cc45 Mon Sep 17 00:00:00 2001 From: guzeng Date: Thu, 1 Apr 2021 23:11:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8Fopenid=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 19 ++++++++++ miniapp_openid.client.go | 78 ++++++++++++++++++++++++++++++++++++++++ weixin.pb.go | 4 +++ weixin.proto | 1 + 4 files changed, 102 insertions(+) create mode 100644 miniapp_openid.client.go diff --git a/common.go b/common.go index 69a776b..d698a6b 100644 --- a/common.go +++ b/common.go @@ -34,6 +34,25 @@ type UniformMessageReq struct { Data interface{} } +/** + * 获取小程序openid请求参数 + */ +type MiniAppOpenidReq struct { + Appid string + Secret string + JsCode string +} + +/** + * 获取小程序openid返回参数 + */ +type MiniAppOpenidReqRes struct { + Openid string + SessionKey string + Unionid string + WxApiRes +} + func rpc_server_conn(url ...string) (*WeixinRpcServiceClient, error) { var wx_rpc_url string = "127.0.0.1:7969" diff --git a/miniapp_openid.client.go b/miniapp_openid.client.go new file mode 100644 index 0000000..b0eb445 --- /dev/null +++ b/miniapp_openid.client.go @@ -0,0 +1,78 @@ +package weixinrpc + +import ( + "encoding/json" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +func GetMiniAppOpenid(appid, secret, js_code string, url ...string) (*AccessTokenRes, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := MiniAppOpenidReq{} + data.Appid = appid + data.Secret = secret + data.JsCode = js_code + + data_json, err := json.Marshal(data) + if err != nil { + return nil, 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.GetMiniAppOpenid(req, res) + + if err != nil { + return nil, err + } + + return HandleMiniAppOpenid(res) + +} + +/** + * 处理返回结果 + */ +func HandleMiniAppOpenid(res *Response) (*MiniAppOpenidReqRes, error) { + + //解密 + res_data_de, err := GetOrgData(res) + + if err != nil { + return nil, err + } + + var res_arr MiniAppOpenidReqRes + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return &res_arr, nil +} diff --git a/weixin.pb.go b/weixin.pb.go index ec8edd3..fb63604 100644 --- a/weixin.pb.go +++ b/weixin.pb.go @@ -101,6 +101,7 @@ type WeixinRpcService interface { GetAccessToken(in *Request, out *Response) error SendMiniprogramSubscribeMessage(in *Request, out *Response) error SendUniformMessage(in *Request, out *Response) error + GetMiniAppOpenid(in *Request, out *Response) error } // AcceptWeixinRpcServiceClient accepts connections on the listener and serves requests @@ -181,6 +182,9 @@ func (c *WeixinRpcServiceClient) SendMiniprogramSubscribeMessage(in *Request, ou func (c *WeixinRpcServiceClient) SendUniformMessage(in *Request, out *Response) error { return c.Call("WeixinRpcService.SendUniformMessage", in, out) } +func (c *WeixinRpcServiceClient) GetMiniAppOpenid(in *Request, out *Response) error { + return c.Call("WeixinRpcService.GetMiniAppOpenid", in, out) +} // DialWeixinRpcService connects to an WeixinRpcService at the specified network address. func DialWeixinRpcService(network, addr string) (*WeixinRpcServiceClient, *rpc.Client, error) { diff --git a/weixin.proto b/weixin.proto index 48434a0..af87c99 100644 --- a/weixin.proto +++ b/weixin.proto @@ -21,4 +21,5 @@ service WeixinRpcService { rpc getAccessToken (Request) returns (Response); // 查询access token rpc sendMiniprogramSubscribeMessage (Request) returns (Response); // 发送小程序订阅消息 rpc sendUniformMessage(Request) returns (Response); // 发送统一服务消息 + rpc getMiniAppOpenid(Request) returns (Response); // 获取用户在小程序的openid } \ No newline at end of file