您的位置:首页 > 移动开发 > 微信开发

小程序模板消息后台发送(集体通知)

2018-10-09 14:03 489 查看
原文链接:http://www.cnblogs.com/lvfish/p/9759965.html

1.在小程序后台配置好模板

2.其中curl需要加上一个参数设置(https发送需要)

3.项目代码示例:其中有token过期更新的处理方法

/*
* 通知最近7天小程序用户
* param()
*/
public function xiaoUsersNotice(Request $r) {
$data = $r->all();
$name = $data['name'];//商品名称
$tmp = $data['jianjie'];
$char = implode("、", $tmp);
$jianjie = $char;//商品简介

$where_time = (int)time() - 604800;
$result = \DB::table('step_form')
->select('form_uid')
->where('form_addtime','>',$where_time)
->distinct('form_uid')
->get();
//组织消息内容
$date = date("Y-m-d H:i:s",time());
$value = array(
"keyword1"=>array(
"value"=>$date,
),
"keyword2"=>array(
"value"=>$name,
),
"keyword3"=>array(
"value"=>$jianjie
),
);
$page='pages/index/index';
foreach($result as $k => $v){
//根据用户查询formid 进行发送并删除对应的第一条formid
$res_first = \DB::table('step_form')
->where('form_uid',$v->form_uid)
->where('form_addtime','>',$where_time)
->orderby('form_addtime')
->first();
//对用户发送第一条formid
$send_date = array();
$send_date['touser'] = $res_first->form_openid;
$send_date['template_id']='cTXT7hORoKy3lG4QYaBLnMIhaL3ECDX82w_6Zn4g85w';
$send_date['page']= $page; //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$send_date['form_id'] = $res_first->form_fid;
$send_date['data']=$value; //模板内容,不填则下发空模板
$send_date['color']=''; //模板内容字体的颜色,不填默认黑色
$send_date['emphasis_keyword']='';
//发送
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' .$this->get_access_token();
$tmp_send = json_encode($send_date);
$res_str = $this->curl($url,$tmp_send);
$res = json_decode($res_str, true);
if($res['errcode'] == 0){
//成功 删除用过的 formid
$sql = \DB::table('step_form')
->where('form_fid',$res_first->form_fid)
->delete();
}else{
return ['code' => 1001, 'data' => $res_first ,'mes' => $res['errcode']];
}
}
//删除过期的formid
$res_del = \DB::table('step_form')
->where('form_addtime','<=',$where_time)
->delete();
return ['code' => 1000, 'data' => ['message' => '通知并处理完毕!']];
}

public function get_access_token() {
$resu = \DB::table('step_token')
->where('token_id',1)
->first();
if(!$resu){
$tmp['token_value'] = 0;
$tmp['token_time'] = 0;
$rest = \DB::table('step_token')
->insert($tmp);
}
$result = \DB::table('step_token')
->where('token_id',1)
->first();
$token_time = $result->token_time;
$token_value = $result->token_value;
if ($token_time < time()) {
$info = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5bd&secret=cdea29f37");
$info = json_decode($info);
$token_value = $info->access_token;
$token_time = time() + 7000;
$token['token_value'] = $token_value;
$token['token_time'] = $token_time;
$res = \DB::table('step_token')
->where('token_id',1)
->update($token);
}
return $token_value;
}

//curl post方式
public function curl($url, $params) {
//初始化
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, false);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//请求https需要设置这个参数
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
//执行命令
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//显示获得的数据
return $data;
}

转载于:https://www.cnblogs.com/lvfish/p/9759965.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: