From f9d82f45d88ff233ab5f59a7267b84ff3411fb5f Mon Sep 17 00:00:00 2001 From: guzeng Date: Mon, 29 Mar 2021 16:54:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96access=5Fto?= =?UTF-8?q?ken=E6=8E=A5=E5=8F=A3=E5=8F=8A=E5=8F=91=E9=80=81=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=9C=8D=E5=8A=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- access_token.client.go | 79 +++++++++++++ common.go | 29 +++++ send_uniform_message.client.go | 82 ++++++++++++++ sign.go | 101 +++++++++++++++++ weixin.pb.go | 198 +++++++++++++++++++++++++++++++++ weixin.proto | 24 ++++ 6 files changed, 513 insertions(+) create mode 100644 access_token.client.go create mode 100644 common.go create mode 100644 send_uniform_message.client.go create mode 100644 sign.go create mode 100644 weixin.pb.go create mode 100644 weixin.proto diff --git a/access_token.client.go b/access_token.client.go new file mode 100644 index 0000000..27fe7f3 --- /dev/null +++ b/access_token.client.go @@ -0,0 +1,79 @@ +package weixinrpc + +import ( + "encoding/json" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +type AccessTokenReq struct { + Appid string + Secret string +} + +func GetAccessToken(dbname, site_id string, appid, secret string, url ...string) (*AccessTokenRes, error) { + + conn, err := rpc_server_conn(url...) + + data["dbname"] = dbname + data["site_id"] = site_id + data["appid"] = appid + data["secret"] = secret + + 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.GetAccessToken(req, res) + + if err != nil { + return nil, err + } + + return HandleAccessToken(res) + +} + +/** + * 处理返回结果 + */ +func HandleAccessToken(res *Response) (*AccessTokenRes, error) { + + //解密 + res_data_de, err := GetOrgData(res) + + if err != nil { + return nil, err + } + + var res_arr AccessTokenRes + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return &res_arr, nil +} diff --git a/common.go b/common.go new file mode 100644 index 0000000..efcd300 --- /dev/null +++ b/common.go @@ -0,0 +1,29 @@ +package weixinrpc + +const DES_KEY = "wxserrpc" + +type WxApiRes struct { + Errcode string + Errmsg string +} + +type AccessTokenRes struct { + AccessToken string + ExpiresIn string + WxApiRes +} + +func rpc_server_conn(url ...string) (*WeixinRpcServiceClient, error) { + + var wx_rpc_url string = "127.0.0.1:7969" + if len(url) > 0 && url[0] != "" { + wx_rpc_url = url[0] + } + conn, _, err := DialWeixinRpcService("tcp", wx_rpc_url) + if err != nil { + return nil, err + } + defer conn.Close() + + return conn, nil +} diff --git a/send_uniform_message.client.go b/send_uniform_message.client.go new file mode 100644 index 0000000..44f77e6 --- /dev/null +++ b/send_uniform_message.client.go @@ -0,0 +1,82 @@ +package weixinrpc + +import ( + "encoding/json" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +//公众号模板消息 +type MpTempMsg struct { + Appid string + TemplateId string + Url string + Miniprogram string + Data string +} +type UniformMessageReq struct { + MiniAppid string + Touser string + MpTemplateMsg MpTempMsg +} + +/** + * 发送小程序统一服务消息,默认发送公众号消息 + * appid,secret 小程序ID 密钥 + * touser 小程序openid + * mp_appid 关联公众号appid,要求与小程序有绑定且同主体 + * temp_id 公众号模板id + * url 跳转的url + * mini_program 公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系 + * data 公众号模板消息的数据 + */ + +func SendUniformMessage(dbname, site_id string, appid, secret, touser, mp_appid, temp_id, url, mini_program, data string, url ...string) (*WxApiRes, error) { + + conn, err := rpc_server_conn(url...) + + data["dbname"] = dbname + data["site_id"] = site_id + data["appid"] = appid + data["secret"] = secret + data["touser"] = touser + data["mp_appid"] = mp_appid + data["temp_id"] = temp_id + data["url"] = url + data["mini_program"] = mini_program + data["data"] = data + + 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.SendUniformMessage(req, res) + + if err != nil { + return nil, err + } + + return HandleRes(res) + +} diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..8db002a --- /dev/null +++ b/sign.go @@ -0,0 +1,101 @@ +package weixinrpc + +import ( + "crypto/md5" + "encoding/hex" + "encoding/json" + "errors" + "strconv" + "strings" + "time" + + "git.tetele.net/tgo/crypter" +) + +/** + * 签名 + */ +func Sign(data string, salt string) string { + + var build strings.Builder + + build.WriteString(data) + build.WriteString(salt) + build.WriteString(DES_KEY) + + data_str := build.String() + + h := md5.New() + h.Write([]byte(data_str)) // 需要加密的字符串 + return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 + +} + +/** + * 验证签名 + */ +func CheckSign(sign_str, data, salt string) bool { + sign := Sign(data, salt) + if strings.Compare(sign_str, sign) > -1 { + return true + } + return false +} + +/** + * 解密 + */ +func GetOrgData(res *Response) (string, error) { + + res_data := res.GetData() + + if res_data == "" { + + return nil, errors.New("未收到收据") + } + + time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) + if err != nil { + return nil, err + } + + now_int64 := time.Now().Unix() + + if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { + //时间误差前后10秒,返回 + return nil, errors.New("返回时间错误") + } + + check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) + if !check_sign { + return nil, errors.New("返回数据签名错误") + } + + //解密 + res_data_de := crypter.DesDe(res_data, DES_KEY) + + return res_data_de, nil +} + +/** + * 处理返回结果 + */ +func HandleRes(res *Response) (*WxApiRes, error) { + + //解密 + res_data_de, err := GetOrgData(res) + + if err != nil { + return nil, err + } + + var res_arr WxApiRes + + 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 new file mode 100644 index 0000000..dc475c2 --- /dev/null +++ b/weixin.pb.go @@ -0,0 +1,198 @@ +// Code generated by protoc-gen-go. +// source: weixin.proto +// DO NOT EDIT! + +/* +Package weixinrpc is a generated protocol buffer package. + +It is generated from these files: + weixin.proto + +It has these top-level messages: + Request + Response +*/ +package weixinrpc + +import proto "github.com/chai2010/protorpc/proto" +import math "math" + +import "io" +import "log" +import "net" +import "net/rpc" +import "time" +import protorpc "github.com/chai2010/protorpc" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = math.Inf + +// 配置信息请求结构 +type Request struct { + Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` + Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` + Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} + +func (m *Request) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Request) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *Request) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +// 配置信息响应结构 +type Response struct { + Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` + Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` + Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} + +func (m *Response) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Response) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *Response) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +func init() { +} + +type WeixinRpcService interface { + GetAccessToken(in *Request, out *Response) error + SendMiniprogramSubscribeMessage(in *Request, out *Response) error +} + +// AcceptWeixinRpcServiceClient accepts connections on the listener and serves requests +// for each incoming connection. Accept blocks; the caller typically +// invokes it in a go statement. +func AcceptWeixinRpcServiceClient(lis net.Listener, x WeixinRpcService) { + srv := rpc.NewServer() + if err := srv.RegisterName("WeixinRpcService", x); err != nil { + log.Fatal(err) + } + + for { + conn, err := lis.Accept() + if err != nil { + log.Fatalf("lis.Accept(): %v\n", err) + } + go srv.ServeCodec(protorpc.NewServerCodec(conn)) + } +} + +// RegisterWeixinRpcService publish the given WeixinRpcService implementation on the server. +func RegisterWeixinRpcService(srv *rpc.Server, x WeixinRpcService) error { + if err := srv.RegisterName("WeixinRpcService", x); err != nil { + return err + } + return nil +} + +// NewWeixinRpcServiceServer returns a new WeixinRpcService Server. +func NewWeixinRpcServiceServer(x WeixinRpcService) *rpc.Server { + srv := rpc.NewServer() + if err := srv.RegisterName("WeixinRpcService", x); err != nil { + log.Fatal(err) + } + return srv +} + +// ListenAndServeWeixinRpcService listen announces on the local network address laddr +// and serves the given WeixinRpcService implementation. +func ListenAndServeWeixinRpcService(network, addr string, x WeixinRpcService) error { + lis, err := net.Listen(network, addr) + if err != nil { + return err + } + defer lis.Close() + + srv := rpc.NewServer() + if err := srv.RegisterName("WeixinRpcService", x); err != nil { + return err + } + + for { + conn, err := lis.Accept() + if err != nil { + log.Fatalf("lis.Accept(): %v\n", err) + } + go srv.ServeCodec(protorpc.NewServerCodec(conn)) + } +} + +type WeixinRpcServiceClient struct { + *rpc.Client +} + +// NewWeixinRpcServiceClient returns a WeixinRpcService rpc.Client and stub to handle +// requests to the set of WeixinRpcService at the other end of the connection. +func NewWeixinRpcServiceClient(conn io.ReadWriteCloser) (*WeixinRpcServiceClient, *rpc.Client) { + c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) + return &WeixinRpcServiceClient{c}, c +} + +func (c *WeixinRpcServiceClient) GetAccessToken(in *Request, out *Response) error { + return c.Call("WeixinRpcService.GetAccessToken", in, out) +} +func (c *WeixinRpcServiceClient) SendMiniprogramSubscribeMessage(in *Request, out *Response) error { + return c.Call("WeixinRpcService.SendMiniprogramSubscribeMessage", in, out) +} + +// DialWeixinRpcService connects to an WeixinRpcService at the specified network address. +func DialWeixinRpcService(network, addr string) (*WeixinRpcServiceClient, *rpc.Client, error) { + c, err := protorpc.Dial(network, addr) + if err != nil { + return nil, nil, err + } + return &WeixinRpcServiceClient{c}, c, nil +} + +// DialWeixinRpcServiceTimeout connects to an WeixinRpcService at the specified network address. +func DialWeixinRpcServiceTimeout(network, addr string, + timeout time.Duration) (*WeixinRpcServiceClient, *rpc.Client, error) { + c, err := protorpc.DialTimeout(network, addr, timeout) + if err != nil { + return nil, nil, err + } + return &WeixinRpcServiceClient{c}, c, nil +} diff --git a/weixin.proto b/weixin.proto new file mode 100644 index 0000000..48434a0 --- /dev/null +++ b/weixin.proto @@ -0,0 +1,24 @@ +syntax = "proto3"; +package weixinrpc; + +// 配置信息请求结构 +message Request { + string data = 1; + string time = 2; + string sign = 3; +} + +// 配置信息响应结构 +message Response { + string data = 1; + string time = 2; + string sign = 3; +} + + +// rpc方法 +service WeixinRpcService { + rpc getAccessToken (Request) returns (Response); // 查询access token + rpc sendMiniprogramSubscribeMessage (Request) returns (Response); // 发送小程序订阅消息 + rpc sendUniformMessage(Request) returns (Response); // 发送统一服务消息 +} \ No newline at end of file