本周我们做了一个有技术含量的一个新功能:客服功能的群发消息功能。此功能主要用于公众号客服群发提醒,比如客户的商家认证审核通过,用户购买商品成功等功能提醒,可以有效减少网站项目开发所必要的资金节约。
- 接口代码。
接口代码如下:
public function reply_customer(){
$mtime=strtotime("-1 hour");
$where[‘add_time‘]=array("gt",$mtime);
$res=M("fa_need")->where($where)->order("id desc")->find();
$id=$res[‘id‘];
$new_time=time();
$go_time=$res[‘add_time‘]+$res[‘show_time‘]*60;
if($new_time<$go_time){
$credit_level=$res[‘credit_level‘];
$ya_money=$res[‘ya_money‘];
$wh[‘credit_level‘]=array("egt",$credit_level);
$wh[‘ya_money‘]=array("egt",$ya_money);
$wh[‘ya_money‘]=array("neq",$ya_money);
$wh[‘jie_status‘]=3;
$list=M("user")->where($wh)->limit(199)->select();
foreach ($list as $key => $value) {
$info[]=$value[‘openid‘];
}
$touser=I(‘touser‘);
$content="亲,有新任务哦,可以点击链接接单啦!";
//更换成自己的APPID和APPSECRET
$APPID="wx4ae938a141e9193a";
$APPSECRET="d0ef15664f42de92875f86b8f9f98edf";
$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
$json=file_get_contents($TOKEN_URL);
$result=json_decode($json);
$ACC_TOKEN=$result->access_token;
foreach($info as $val){
$data = ‘{
"touser":"‘.$val.‘",
"msgtype":"text",
"text":
{
"content":"‘.$content.‘http://www.xxx.com/Task/task_details?id=‘.$id.‘"
}
}‘;
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$ACC_TOKEN;
$result = $this->https_post($url,$data);
$final = json_decode($result);
echo $final;
}
}
}
public function https_post($url,$data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_errno($curl)) {
return ‘Errno‘.curl_error($curl);
}
curl_close($curl);
return $result;
}
- php调用接口代码
Php代码如下:
public function fa_need(){
vendor(‘Weixinup.jssdk‘);
$jssdk = new JSSDK("wx4ae938a141e9193a", "d0ef15664f42de92875f86b8f9f98edf");
$signPackage = $jssdk->GetSignPackage();
// var_dump($signPackage);die;
$this->assign(‘signPackage‘, $signPackage);
$uid=session(‘uid‘);
$model=M(‘user‘);
$list=$model->where("uid=$uid")->find();
$mod=M("school");
$school=$mod->select();
$this->reply_customer();
// if (I(‘id‘)!=""||!empty(I(‘id‘))) {
// $faid["id"] = I(‘id‘);
// $res = M("fa_need")->where($faid)->find();
// $this->assign("fa_needinfo",$res);
// }
$this->assign("school",$school);
$this->assign("list",$list);
$this->display();
}
好了,到这里大家应该清楚是如何实现的吧,那么如果还是存在有不理解的地方,可以留言咨询获取帮助解答。
本文由专业的郑州小程序开发公司燚轩科技整理发布,原创不易,如需转载请注明出处!
原文地址:http://blog.51cto.com/13686158/2176716