微信小程序模板消息发送
微信小程序模板消息,在微信小程序中不可缺少的,但是有些时候,一个小程序发送很多不一样的模板消息,这需要我们封装,还有发送模板消息,需要formid,prepay_id,他们都是有时间限制,7天过期,不能使用已用的formid,prepay_id,不然发送会报错。发送失败;下面是我已经在项目运用的。目前运行稳定,但是记得去除7天过期,因为项目急,我获取的formid,prepay_id没有考虑,希望你在封装中加上;我是在后台,处理了。希望注意一下;
PHP代码:
//一口价发个拍卖者模板消息
public function Template_message4($shop_list_id,$user_ids,$total_money,$other_openid){
$Template="***************************";
// $shop_list_id;
// $openid//拍卖者
// $other_openid//用户
// $total_money//金额
if($shop_list_id&&$user_ids&&$other_openid&&$total_money){
$access_token=M("config")->field("access_token")->where('id=1')->find();
$ACCESS_TOKEN=$access_token['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$ACCESS_TOKEN";
//拍卖者
$userinfo = M('user')->field("username,phone,openid,remak_username")->where(array('id'=>$user_ids))->find();//拍卖者
if($userinfo['remak_username']){
$userinfo['username']=$userinfo['remak_username'];
}
$other_userinfo = M('user')->field("username,remak_username")->where(array('openid'=>$other_openid))->find();//用户
if($other_userinfo['remak_username']){
$other_userinfo['username']=$other_userinfo['remak_username'];
}
$openid=$userinfo['openid'];
$form_idd=$this->formId($openid);
$skill_name=M('shop_list')->field("title")->where('id='.$shop_list_id)->find();
if($form_idd) {
$page = "pages/auctionPriceSummary/auctionPriceSummary?shop_list_id=" . $shop_list_id;
$data = array('touser' => $openid, //拍卖人
'template_id' => $Template, //订单发货提醒
'page' => $page,
'form_id' => $form_idd,
'data' => array(
'keyword1' => array(
'value' => $skill_name['title'], // 竞拍名称
'color' => '#173177'
),
'keyword2' => array(
'value' => urldecode($other_userinfo['username']), //竞拍人
'color' => '#173177'
),
'keyword3' => array(
'value' => $total_money, //竞拍金额
'color' => '#173177'
),
'keyword4' => array(
'value' => '【'.urldecode($other_userinfo['username']) . '】刚抢拍成功,快去确认发货吧!',
'color' => '#FFA500'
)
)
);
$data = json_encode($data);
$this->curt($url,$data);
}
$form_idds=$this->formId($other_openid);
$Template_one = "*************************";
$page_one = "pages/auctionPriceSummary/auctionPriceSummary?shop_list_id=" . $shop_list_id;
$data_info = array('touser' => $other_openid, //发给谁(竞拍第一名)
'template_id' => $Template_one, //订单发货提醒
'page' => $page_one,
'form_id' => $form_idds,
'data' => array(
'keyword1' => array(
'value' => $total_money.'元', // 成交价
'color' => '#173177'
),
'keyword2' => array(
'value' => $userinfo['phone'], //卖家电话
'color' => '#173177'
),
'keyword3' => array(
'value' => date("Y-m-d H:i:s"), //成交时间
'color' => '#173177'
),
'keyword4' => array(
'value' => urldecode($userinfo['username']),//卖家名称
'color' => '#173177'
),
'keyword5' => array(
'value' => $skill_name['title'],//竞拍名称
'color' => '#173177'
),
'keyword6' => array(
'value' => '你已经抢拍【'.urldecode($userinfo['username']) .'】的【'.$skill_name['title'].'】成功赶紧联系卖家',
'color' => '#FFA500'
)
)
);
$data_info = json_encode($data_info);
$this->curt($url, $data_info);
}else{
return('参数缺失');
}
}
//formid,prepay_id的获取
private function formId($openid){
$form_ids = M('form')->field("id,prepay_id,use_num,use_time,is_use")->where(array('prepay_id' => array('neq', ""), 'openid' => $openid, "is_use" => 1, 'use_num' => array('lt', 3)))->find();
$form_idd = $form_ids['prepay_id'];
if (!$form_idd) {
$form_idr = M('form')->field("id,form_id,use_time,is_use")->where(array('form_id' => array('neq', ""), 'openid' => $openid, "is_use" => 1))->order('c_time desc')->find();
$form_idd = $form_idr['form_id'];
if ($form_idd) {
//更改formid
$timed = time();
$rowd = M("form")->where("id=" . $form_idr['id'])->save(array("is_use" => 2, "use_time" => $timed));
if (!$rowd) {
return('更新失败1');
}
return $form_idd;
}
}else{
$use_num = $form_ids['use_num'] + 1;
if ($use_num == 3) {
//三次用完
$timed = time();
$row = M("form")->where("id=" . $form_ids['id'])->save(array('use_num' => $use_num, "is_use" => 2, "use_time" => $timed));
} else {
$row = M("form")->where("id=" . $form_ids['id'])->save(array('use_num' => $use_num, "use_time" => time()));
}
if (!$row) {
return('更新失败');
}
return $form_idd;
}
}
//发送模板消息
private function curt($url,$data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
// if (curl_errno($ch)) {
// return curl_error($ch);
// }
curl_close($ch);
}
报错指南:
40037 template_id不正确
41028 form_id不正确,或者过期
41029 form_id已被使用
41030 page不正确
45009 接口调用超过限额(目前默认每个帐号日调用限额为100万)
有些还可以封装,写的不好,可以给我留言,谢谢!