欢迎您光临本站
faddei头像
faddei

2021-07-10

评论者
微信小程序
0 1

微信小程序的模板消息通知需为用户自主触发才可使用

/*
 * 模板消息
 * $touser 接收者(用户)的 openid
 * $template_id 所需下发的模板消息的id
 * $page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例/index/index?foo=bar)。该字段不填则模板无跳转
 * $form_id 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
 * $data 模板内容,不填则下发空模板。具体格式请参考示例 
 * 示例:$data['keyword1'] = array('value'=>$orderinfo['price']);
 *     $data['keyword2'] = array('value'=>$kaname);
 *     $data['keyword3'] = array('value'=>date('Y-m-d H:i',$vipendtime));
 *     $data['keyword4'] = array('value'=>$daynumber.'天');
*/
public function templateMessage($touser,$template_id,$page,$form_id,$data)
{
    $datas['touser']      = $touser;
    $datas['template_id'] = $template_id;
    $datas['page']        = $page;
    $datas['form_id']     = $form_id;
    $datas['data']        = $data;
    $access_token = $this->getwxtoken();
    $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
    $res = https_request($url,$datas,'json');
    return $res;
}
//获取token
public function getwxtoken()
{
    //$access_token = Cache::get('access_token');
    //if($access_token){
    //  return $access_token;
    //}else{
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('wechat.appid')."&secret=".config('wechat.secret');
        $token_json = http_request($url);
        $tokenarr = json_decode($token_json,true);
    //  Cache::set('access_token', $tokenarr['access_token'] ,3600);
        return $tokenarr['access_token'];
    //}
}

注:上述的token可以存储缓存或者存储文件中使用,有效期2小时

版权声明:本站所提供的文章、图片等内容均为用户发布或互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系我们客服人员删除。

320

精彩推荐

暂无评论

文明用语