小程序消息推送到公众号
根据公司业务需求,需要在小程序某些触发条件后,推送公众号模板消息到关联用户
首先用户需要绑定小程序和公众号到统一微信开放平台
绑定后使用小程序的统一消息服务
官方参考文档https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.addTemplate.html
这里我用的是easywechat4.x版本
use think\Request; use EasyWeChat\Factory; class Wechat { protected $app; public function __construct(Request $request = null) { $config = [ 'app_id' => 'appid', 'secret' => 'secret', // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 'response_type' => 'array', ]; $this -> app = Factory::miniProgram($config); } public function send_msg($openid=null,$lock_name=null){ $data = [ 'touser' => $openid, 'mp_template_msg' => [ 'template_id' => 'template', // 公众号管理模板id 'miniprogram' => [ 'appid' => 'wxddc7077e561c91c3', //这里写小程序的appid ], 'appid' => 'wx103e2696ca271597', //这里是公众号的appid 'data' => [ 'first' => 'data1', 'keyword1' => 'data2', 'keyword2' => 'data3', 'keyword3' => date('Y-m-d H:i:s'), ], ] ]; return $this -> app -> uniform_message->send($data); } }