Browse Source

增加发送小程序订阅消息方法

master v0.3.0
guzeng 3 years ago
parent
commit
14fd3f2ea4
6 changed files with 128 additions and 16 deletions
  1. +12
    -0
      common.go
  2. +70
    -0
      send_subscribe_message.client.go
  3. +30
    -0
      send_subscribe_message.client_test.go
  4. +12
    -12
      send_uniform_message.client_test.go
  5. +3
    -3
      weixin.pb.go
  6. +1
    -1
      weixin.proto

+ 12
- 0
common.go View File

@ -34,6 +34,18 @@ type UniformMessageReq struct {
Data interface{} Data interface{}
} }
type SubscribeMessageReq struct {
Req
Appid string
Secret string
Touser string
TemplateId string
Page string
MiniprogramState string
Lang string
Data interface{}
}
/** /**
* 获取小程序openid请求参数 * 获取小程序openid请求参数
*/ */


+ 70
- 0
send_subscribe_message.client.go View File

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

+ 30
- 0
send_subscribe_message.client_test.go View File

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

+ 12
- 12
send_uniform_message.client_test.go View File

@ -8,28 +8,28 @@ func Test_SendUniformMessage(t *testing.T) {
db := "gkxy_tetele_net" db := "gkxy_tetele_net"
site_id := "1101832" site_id := "1101832"
appid := "wx7be627d0325135f4"
secret := "984c6d213c60419c4f30c159fbbe1ce9"
touser := "osxVD5S0PAh82gyIXSiXCJzM_k6s"
appid := "wx7b72dc719ad1f6d3"
secret := "b8ae6d64605a963056ff067cceffefd2"
touser := "oDTib5GULMfLSGZcMz7vB2xToxG4"
mp_appid := "wx056995d61ca222f5" mp_appid := "wx056995d61ca222f5"
temp_id := "6xgj_65KivigMvwdJgiveR4wLXJLDpzsT2-hShczLmU" temp_id := "6xgj_65KivigMvwdJgiveR4wLXJLDpzsT2-hShczLmU"
url := "" url := ""
mini_program := map[string]string{ mini_program := map[string]string{
"appid": "wx7be627d0325135f4",
"pagepath": "pages/index/index",
"appid": "wx7b72dc719ad1f6d3",
"pagepath": "pages/order/index",
} }
data := map[string]map[string]string{ 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": "下单备注"}, "remark": map[string]string{"value": "下单备注"},
} }
// data_json, _ := json.Marshal(data) // 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) t.Log(ret, err)
} }

+ 3
- 3
weixin.pb.go View File

@ -99,7 +99,7 @@ func init() {
type WeixinRpcService interface { type WeixinRpcService interface {
GetAccessToken(in *Request, out *Response) error GetAccessToken(in *Request, out *Response) error
SendMiniprogramSubscribeMessage(in *Request, out *Response) error
SendMiniappSubscribeMessage(in *Request, out *Response) error
SendUniformMessage(in *Request, out *Response) error SendUniformMessage(in *Request, out *Response) error
GetMiniAppOpenid(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 { func (c *WeixinRpcServiceClient) GetAccessToken(in *Request, out *Response) error {
return c.Call("WeixinRpcService.GetAccessToken", in, out) 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 { func (c *WeixinRpcServiceClient) SendUniformMessage(in *Request, out *Response) error {
return c.Call("WeixinRpcService.SendUniformMessage", in, out) return c.Call("WeixinRpcService.SendUniformMessage", in, out)


+ 1
- 1
weixin.proto View File

@ -19,7 +19,7 @@ message Response {
// rpc方法 // rpc方法
service WeixinRpcService { service WeixinRpcService {
rpc getAccessToken (Request) returns (Response); // access token rpc getAccessToken (Request) returns (Response); // access token
rpc sendMiniprogramSubscribeMessage (Request) returns (Response); //
rpc sendMiniappSubscribeMessage (Request) returns (Response); //
rpc sendUniformMessage(Request) returns (Response); // rpc sendUniformMessage(Request) returns (Response); //
rpc getMiniAppOpenid(Request) returns (Response); // openid rpc getMiniAppOpenid(Request) returns (Response); // openid
} }

Loading…
Cancel
Save