Browse Source

增加获取access_token接口及发送统一服务接口

master v0.1.0
guzeng 3 years ago
parent
commit
f9d82f45d8
6 changed files with 513 additions and 0 deletions
  1. +79
    -0
      access_token.client.go
  2. +29
    -0
      common.go
  3. +82
    -0
      send_uniform_message.client.go
  4. +101
    -0
      sign.go
  5. +198
    -0
      weixin.pb.go
  6. +24
    -0
      weixin.proto

+ 79
- 0
access_token.client.go View File

@ -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
}

+ 29
- 0
common.go View File

@ -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
}

+ 82
- 0
send_uniform_message.client.go View File

@ -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)
}

+ 101
- 0
sign.go View File

@ -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
}

+ 198
- 0
weixin.pb.go View File

@ -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
}

+ 24
- 0
weixin.proto View File

@ -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); //
}

Loading…
Cancel
Save