From 14fd3f2ea49692e6ff2c7d0ff26821341f585550 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 16 Apr 2021 19:56:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=91=E9=80=81=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E8=AE=A2=E9=98=85=E6=B6=88=E6=81=AF=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 12 +++++ send_subscribe_message.client.go | 70 +++++++++++++++++++++++++++ send_subscribe_message.client_test.go | 30 ++++++++++++ send_uniform_message.client_test.go | 24 ++++----- weixin.pb.go | 6 +-- weixin.proto | 2 +- 6 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 send_subscribe_message.client.go create mode 100644 send_subscribe_message.client_test.go diff --git a/common.go b/common.go index 9f32a8d..8505a33 100644 --- a/common.go +++ b/common.go @@ -34,6 +34,18 @@ type UniformMessageReq struct { Data interface{} } +type SubscribeMessageReq struct { + Req + Appid string + Secret string + Touser string + TemplateId string + Page string + MiniprogramState string + Lang string + Data interface{} +} + /** * 获取小程序openid请求参数 */ diff --git a/send_subscribe_message.client.go b/send_subscribe_message.client.go new file mode 100644 index 0000000..f00b2f6 --- /dev/null +++ b/send_subscribe_message.client.go @@ -0,0 +1,70 @@ +package weixinrpc + +import ( + "encoding/json" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +/** + * 发送小程序订阅服务消息 + * appid,secret 小程序ID 密钥 + * touser 小程序openid + * mp_appid 关联公众号appid,要求与小程序有绑定且同主体 + * temp_id 模板id + * page 跳转的url + * lang 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN + * miniprogram_state 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版 + * data 公众号模板消息的数据 + */ + +func SendMiniSubscribeMessage(dbname, site_id string, appid, secret, touser, temp_id, page string, miniprogram_state, lang string, data interface{}, url ...string) (*WxApiRes, error) { + + conn, err := rpc_server_conn(url...) + + message_data := SubscribeMessageReq{} + message_data.SiteId = site_id + message_data.Dbname = dbname + message_data.Appid = appid + message_data.Secret = secret + message_data.Touser = touser + message_data.TemplateId = temp_id + message_data.Page = page + message_data.MiniprogramState = miniprogram_state + message_data.Lang = lang + message_data.Data = data + + data_json, err := json.Marshal(message_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.SendMiniappSubscribeMessage(req, res) + + if err != nil { + return nil, err + } + + return HandleRes(res) + +} diff --git a/send_subscribe_message.client_test.go b/send_subscribe_message.client_test.go new file mode 100644 index 0000000..75e2295 --- /dev/null +++ b/send_subscribe_message.client_test.go @@ -0,0 +1,30 @@ +package weixinrpc + +import ( + "testing" +) + +func Test_SendMiniSubscribeMessage(t *testing.T) { + + db := "gkxy_tetele_net" + site_id := "1101832" + appid := "wx7b72dc719ad1f6d3" + secret := "b8ae6d64605a963056ff067cceffefd2" + touser := "oDTib5GULMfLSGZcMz7vB2xToxG4" + temp_id := "0Qbw6nuzj3X4kvOqGbGgD-dlu06NF6u9_ZYIiw0TY4Y" + page := "pages/order/order" + miniprogram_state := "" + lang := "" + data := map[string]map[string]string{ + "character_string1": map[string]string{"value": "961617783456"}, + "phrase2": map[string]string{"value": "配送中"}, + "thing3": map[string]string{"value": "小火锅"}, + "thing4": map[string]string{"value": "香洲区测试用"}, + "thing5": map[string]string{"value": "下单备注"}, + } + + // data_json, _ := json.Marshal(data) + + ret, err := SendMiniSubscribeMessage(db, site_id, appid, secret, touser, temp_id, page, miniprogram_state, lang, data, "49.232.85.91:17969") + t.Log(ret, err) +} diff --git a/send_uniform_message.client_test.go b/send_uniform_message.client_test.go index cadd5b8..6c09b23 100644 --- a/send_uniform_message.client_test.go +++ b/send_uniform_message.client_test.go @@ -8,28 +8,28 @@ func Test_SendUniformMessage(t *testing.T) { db := "gkxy_tetele_net" site_id := "1101832" - appid := "wx7be627d0325135f4" - secret := "984c6d213c60419c4f30c159fbbe1ce9" - touser := "osxVD5S0PAh82gyIXSiXCJzM_k6s" + appid := "wx7b72dc719ad1f6d3" + secret := "b8ae6d64605a963056ff067cceffefd2" + touser := "oDTib5GULMfLSGZcMz7vB2xToxG4" mp_appid := "wx056995d61ca222f5" temp_id := "6xgj_65KivigMvwdJgiveR4wLXJLDpzsT2-hShczLmU" url := "" mini_program := map[string]string{ - "appid": "wx7be627d0325135f4", - "pagepath": "pages/index/index", + "appid": "wx7b72dc719ad1f6d3", + "pagepath": "pages/order/index", } data := map[string]map[string]string{ - "first": map[string]string{"value": "小程序商城来新订单了"}, - "keyword1": map[string]string{"value": "2002030211223"}, - "keyword2": map[string]string{"value": "五饭堂-沙县小吃"}, - "keyword3": map[string]string{"value": "王小二"}, - "keyword4": map[string]string{"value": "13022336652"}, - "keyword5": map[string]string{"value": "5号宿舍"}, + "first": map[string]string{"value": "AI广科商城小程序"}, + "keyword1": map[string]string{"value": "961617783456"}, + "keyword2": map[string]string{"value": "测试商家"}, + "keyword3": map[string]string{"value": "顾曾"}, + "keyword4": map[string]string{"value": "18607565510"}, + "keyword5": map[string]string{"value": "香洲区测试用"}, "remark": map[string]string{"value": "下单备注"}, } // data_json, _ := json.Marshal(data) - ret, err := SendUniformMessage(db, site_id, appid, secret, touser, mp_appid, temp_id, url, mini_program, data) + ret, err := SendUniformMessage(db, site_id, appid, secret, touser, mp_appid, temp_id, url, mini_program, data, "49.232.85.91:17969") t.Log(ret, err) } diff --git a/weixin.pb.go b/weixin.pb.go index fb63604..b690d13 100644 --- a/weixin.pb.go +++ b/weixin.pb.go @@ -99,7 +99,7 @@ func init() { type WeixinRpcService interface { GetAccessToken(in *Request, out *Response) error - SendMiniprogramSubscribeMessage(in *Request, out *Response) error + SendMiniappSubscribeMessage(in *Request, out *Response) error SendUniformMessage(in *Request, out *Response) error GetMiniAppOpenid(in *Request, out *Response) error } @@ -176,8 +176,8 @@ func NewWeixinRpcServiceClient(conn io.ReadWriteCloser) (*WeixinRpcServiceClient 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) +func (c *WeixinRpcServiceClient) SendMiniappSubscribeMessage(in *Request, out *Response) error { + return c.Call("WeixinRpcService.SendMiniappSubscribeMessage", in, out) } func (c *WeixinRpcServiceClient) SendUniformMessage(in *Request, out *Response) error { return c.Call("WeixinRpcService.SendUniformMessage", in, out) diff --git a/weixin.proto b/weixin.proto index af87c99..de376bf 100644 --- a/weixin.proto +++ b/weixin.proto @@ -19,7 +19,7 @@ message Response { // rpc方法 service WeixinRpcService { rpc getAccessToken (Request) returns (Response); // 查询access token - rpc sendMiniprogramSubscribeMessage (Request) returns (Response); // 发送小程序订阅消息 + rpc sendMiniappSubscribeMessage (Request) returns (Response); // 发送小程序订阅消息 rpc sendUniformMessage(Request) returns (Response); // 发送统一服务消息 rpc getMiniAppOpenid(Request) returns (Response); // 获取用户在小程序的openid } \ No newline at end of file