/**
* 获取token
*/
public function htoken(){
$config = json_decode(\addons\shopro\model\Config::where(['name' => 'wxOfficialAccount'])->value('value'), true);
$appid = $config['app_id'];
$secret = $config['secret'];
$get_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_token_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
return $json_obj['access_token'];
}
/**
* 生成二维码
*/
public function erweima()
{
$type =input('param.type');
$user_id =123;
$url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->htoken();
// $this->success('获取成功',$url);
if ($type == 1){//永久二维码
$data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": "'.$user_id.'"}}}';
// {"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
}else{
$data = '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene_id": "'.$user_id.'"}}}';
}
$res = $this->posts($url,'post','json',$data);
if (empty($res['ticket'])){
return false;
}
$codes='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($res['ticket']);
$urls=$this->down_load_qrcode($codes);
return $urls;
}
/**
* 下载到服务器
*/
public function down_load_qrcode($url){
if (empty($url)) return false;
$filename = time()."-".rand(1000,9999).".jpg";
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$file = 'code'.'/'. $filename;
$fp2 = fopen($file, "a");
if (fwrite($fp2, $img) === false) {
return jsonData(1001,'生成二维码失败');
}
fclose($fp2);
return 'code'.'/'.$filename;
}
/**
* 微信公众号服务端API对接、处理消息回复
*/
public function index()
{
$wechat = new WechatLibrary('wxOfficialAccount');
$this->app = $wechat->getApp();
$this->app->server->push(function ($message) {
//初始化信息
$this->userOpenId = $message['FromUserName'];
// return json_encode($message, JSON_UNESCAPED_UNICODE); //调试使用
switch ($message['MsgType']) {
case 'event': //收到事件消息
switch ($message['Event']) {
case 'subscribe': //订阅(关注)事件
//获取粉丝信息并保存
$subscribe = WechatModel::get(['type' => 'subscribe']);
if ($subscribe) {
$openid =$message['FromUserName'];
$userid = $message['EventKey'];
return $userid.'--'.$openid;
// qrscene_ 截取掉
// return $this->response($subscribe);
}
break;
case 'unsubscribe': //取消订阅(关注)事件
//获取粉丝信息并保存
break;
case 'CLICK': //自定义菜单事件
return $this->response($message, 'CLICK');
break;
case 'SCAN': //扫码事件
$subscribe = WechatModel::get(['type' => 'subscribe']);
if ($subscribe) {
$openid =$message['FromUserName'];
$userid = $message['EventKey'];
return $userid.'--'.$openid;
// return $this->response($subscribe);
}
//关联 人物关系
break;
}
break;
case 'text': //收到文本消息
//检测关键字回复
$content = $message['Content'];
$auto_reply = WechatModel::where('type', 'auto_reply')->where('find_in_set(:keywords,rules)', ['keywords' => $content])->find();
if ($auto_reply) {
return $this->response($auto_reply);
}
case 'image': //收到图片消息
case 'voice': //收到语音消息
case 'video': //收到视频消息
case 'location': //收到坐标消息
case 'link': //收到链接消息
case 'file': //收到文件消息
default: // ... 默认回复消息
$default_reply = WechatModel::where('type', 'default_reply')->find();
if ($default_reply) {
return $this->response($default_reply);
}
}
});
$response = $this->app->server->serve();
// 将响应输出
$response->send();
}
公众号生成带参数二维码,扫描识别参数
最新推荐文章于 2023-04-14 16:16:14 发布