小程序发送模板消息

 /**
     * 获取 access_token
     */
    public function getAccessToken()
    {
        if ($wx = Config::get('wechat')) {
            $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$wx['xcx_appid'].'&secret='.$wx['xcx_appsecret'];
            $data=[];
            $result =  send_post($url,$data);
            if ($result) {
                $json = json_decode($result, true);
                if (!$json || isset($json['errcode'])) {
                    $this->errCode = $json['errcode'];
                    $this->errMsg = $json['errmsg'];
                    return false;
                }
                $this->access_token = $json['access_token'];
                return $this->access_token;
            }
        }
    }
    //发送模板消息
    public function sendTemplateMessage($data){
        if (!$this->access_token && !$this->getAccessToken()) {
            return false;
        }
        //echo "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$this->access_token}";
        //$result =  send_post("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$this->access_token}",$data);
        $url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$this->access_token}";
        $result =  $this->https_request($url,$data,'json');
        if ($result) {
            $json = json_decode($result, true);
            if (empty($json) || !empty($json['errcode'])) {
                $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
                $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
            }
            return $json;
        }
        return false;
    }
    $datas = array (
                            'keyword1' => array (
                                    'value' => ''


                            ),
                            'keyword2' => array (
                                    'value' => ''

                            ),
                            'keyword3' => array (
                                    'value' => ''

                            ),
                            'keyword4' => array (
                                    'value' => '已完成'

                            ),
                            'keyword5' => array (
                                    'value' => '恭喜您,订单:'.$order['orderno'].'打卡任务全部完成'

                            )
                    );
                    $templateid='模板id';
                    $openid=$member['openid'];
                    $data = array ();
                    $data ['touser'] = $openid;
                    $data ['template_id'] = $templateid;
                    $data ['page'] = 'pages/wodekecheng_success/wodekecheng_success?id='. $order['productid'];
                    $data ['form_id'] = $formid;
                    $data ['data'] = $datas;
                    $result = $this->sendTemplateMessage ( $data );
### 如何在支付宝小程序发送模板消息 支付宝小程序支持通过调用其提供的 **`alipay.open.app.templatemessage.send`** 接口来发送模板消息。以下是关于该接口的具体说明以及示例代码。 #### 1. 前置条件 为了能够成功发送模板消息,开发者需满足以下条件: - 小程序已开通服务通知权限[^2]。 - 已经申请并通过审核的模板消息 ID(可在支付宝开放平台的小程序管理后台找到)。 - 用户授权同意接收来自当前小程序消息推送。 #### 2. 参数说明 以下是 `alipay.open.app.templatemessage.send` 的主要参数及其含义: | 字段名 | 类型 | 是否必填 | 描述 | |--------|------|----------|------| | toUserId | String | 是 | 收到消息的目标用户的 userId 或 openid | | templateId | String | 是 | 模板消息 ID,在支付宝开放平台上申请获得 | | data | Object | 是 | 要填充的数据对象,键值对形式表示占位符和实际内容 | #### 3. 示例代码 下面是一个完整的 Python 后端示例代码,展示如何调用此 API 来发送模板消息: ```python import requests import json def send_template_message(to_user_id, template_id, data): url = "https://openapi.alipay.com/gateway.do" params = { "app_id": "your_app_id", # 替换为自己的 App ID "method": "alipay.open.app.templatemessage.send", "format": "JSON", "charset": "utf-8", "sign_type": "RSA2", "timestamp": "2023-09-27 14:56:00", # 当前时间戳 "version": "1.0", "biz_content": json.dumps({ "to_user_id": to_user_id, "template_id": template_id, "data": data }) } headers = {"Content-Type": "application/x-www-form-urlencoded"} response = requests.post(url, data=params, headers=headers) result = response.json() return result if __name__ == "__main__": user_id = "example_openid" # 替换为目标用户的 OpenID message_template_id = "TM_example_001" # 替换为已申请的模板 ID payload_data = { "first": {"value": "您好"}, "keyword1": {"value": "订单号:123456789"}, "keyword2": {"value": "已完成"}, "remark": {"value": "感谢您的使用!"} } res = send_template_message(user_id, message_template_id, payload_data) print(res) ``` 以上代码展示了如何构建请求体并向支付宝网关发起 POST 请求以触发模板消息发送过程[^1]。 #### 4. 注意事项 - 需要确保签名算法正确无误,推荐使用 RSA2 加密方式。 - 如果返回错误码,则应仔细检查输入参数是否符合预期格式及逻辑要求。 - 数据字段中的 key 应严格匹配所选模板定义的内容项名称。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值