zl程序教程

您现在的位置是:首页 >  APP

当前栏目

EasyWechat 4.x 微信小程序订阅消息

2023-02-19 12:29:04 时间

1. 前言


EasyWechat 4.x 订阅消息文档: https://easywechat.com/docs/4.x/mini-program/subscribe_message

微信官方文档订阅消息:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html

下发订阅消息官方文档(注意:订阅消息参数值内容限制):https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html

2. 发送订阅消息


page 参数

点击模板卡片后的跳转页面,仅限本小程序内的页面。

站长源码网

支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。

miniprogram_state 参数

跳转小程序类型 formal 正式版 trial 体验版 developer 开发版, 省略时默认为正式版

EasyWechat 订阅消息示例中并没有该参数,我是在微信订阅消息官方文档发现的,经测试,该参数可用

$data = [
'template_id' => 'bDmywsp2oEHjwAadTGKxxxxxx', // 订阅消息模板id
'touser' => 'oSyZp5OBNPBRhG-7BVgWxbiNZm', // 接收者用户openid
'page' => 'pages/index/index', // 小程序页面路径
'data' => [ // 模板内容
'date01' => [
'value' => '2019-12-01',
],
'number01' => [
'value' => 10,
],
],
'miniprogram_state' => 'formal', // formal 正式版 trial 体验版 developer 开发版
];
// 返回数组
$result = $app->subscribe_message->send($data);

特别注意:模板id为空时会抛出异常而不是以返回值的形式返回,所以最好使用 try catch 捕获下错误

try {
// 返回数组 模板id为空时抛出异常
$result = $app->subscribe_message->send($data);
} catch (\Throwable $e) {
fault($e->getMessage());
}

发送成功

[
"errcode" => 0,
"errmsg" => "ok",
"msgid" => 1888884277765816322,
]

判断是否发送成功

if (isset($result['errcode']) && $result['errcode'] == 0 ) {
    // 发送成功
}

3. 发送失败时常见返回值


用户没有授权或授权的次数已用尽

[
    "errcode" => 43101,
    "errmsg"  => "user refuse to accept the msg rid: 60b07a4d-07ed4b8e-286b09ae",
]