<?php
namespace Ebox\Controllers;
use Glar\Http\Controllers\Controller;
use Common\Models\User;
use Cache;
class WechatController extends Controller
{
private $appid = '***************';
private $appsecret = '************';
private $token = '************';
/**
* 动态设置菜单
*
* @return string
*/
public function serve()
{
//验证token
if(isset($_GET['echostr'])){
$res = $this->checkSignature();
if($res){
echo $_GET['echostr'];exit;
}
}
//自定义菜单
$menu = $this->menu();
print_r($menu);
}
//菜单url
private function gotourl(){
$redirect_uri = [
0=>"http://m.admin.co.gegebox.com/#/",//运维工单
1=>"http://m.admin.co.gegebox.com/#/task/patrol/personal",//巡检任务
2=>"http://m.admin.co.gegebox.com/#/task/warn/personal",//告警终端
3=>"http://m.admin.co.gegebox.com/#/terminal",//我的终端
];
return $redirect_uri;
}
//处理微信菜单点击后的请求
public function auth(){
$code = \Input::get('code','');
try{
$accesstoken_ = $this->get_access_token();//获取全局access_token
$access_token_arr = $this->global_access_token($code);//获取openid
$openid_ = $access_token_arr['openid'];
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$accesstoken_}&openid={$openid_}&lang=zh_CN";
//拉取用户信息
$userinfo = json_decode(file_get_contents($url),true);
}catch(\Exception $e){
echo $e->getMessage();
exit;
}
//跳转到对应页面
$action = \Input::get('action',0);
$gotourl = array_get($this->gotourl(),$action);
//判断是否绑定过openid
if($this->findOpenidUser($userinfo)){
//此处应该自动登录 以后补上
header("location:".$gotourl);exit;
}else{
header("location:".'http://m.admin.co.gegebox.com/#/login?openid='.$userinfo['openid']);exit;
}
}
//微信消息推送 此处主要是工单推送 开放给后台的请求的接口
public function sendmsg(){
$postdata = \Input::get('datas',[]);
$openid = \Input::get('openid','');
$url = \Input::get('url','');
$token = $this->get_access_token();
$MBID = 'n9bcx_J8RuqIauMImwC198qGpC9_E2hBr2qaIKMVX3M';
$data = [
'touser'=>$openid,
'template_id'=>$MBID,
'url'=>$url,
'data'=>$postdata
];
try{
\Requests::post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token, array(), json_encode($data));
}catch(\Exception $e){
}
}
//去数据库查找openid是否存在
private function findOpenidUser($userinfo){
// dump($userinfo);exit;
if($userinfo['openid']){
$user = User::where("weixin_id",$userinfo['openid'])->first();
if($user){
//该用户已经存在 可以自动登录
return true;
}else{
return false;
}
}
return false;
}
//创建自定义菜单
private function menu(){
$access_token = $this->get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
$base_url = [];
for($i=0;$i<=3;$i++){
$redirect_uri = urlencode("http://m.admin.co.gegebox.com/weixin/auth?action={$i}");
$base_url[$i] ="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
}
$post = '{
"button":
[
{
"type":"view",
"name":"运维工单",
"url":"'.$base_url[0].'"
},
{
"type":"view",
"name":"巡检任务",
"url":"'.$base_url[1].'"
},
{
"name":"我的基地",
"sub_button":
[
{
"type":"view",
"name":"告警终端",
"url":"'.$base_url[2].'"
},
{
"type":"view",
"name":"我的终端",
"url":"'.$base_url[3].'"
}
]
}
]
}';
return $this->https_request($url,$post);
}
//获取取得openid
private function global_access_token($code){
// $session = app('session.store');
// $session->put('userid', '');
// $session->get('url.intended', '/');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
$tokens = json_decode($this->https_request($url),true);
return $tokens;
}
//获取全局access_token
private function get_access_token(){
// $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
// $access_token_arr = json_decode($this->https_request($url),true);
// $access_token = $access_token_arr['access_token'];
// return $access_token;
$_accesstoken = Cache::remember("qwebh55667g555555rt12345t",60,function(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
$access_token_arr = json_decode($this->https_request($url),true);
$access_token = $access_token_arr['access_token'];
return $access_token;
});
return $_accesstoken;
}
//拉取用户基本信息
private function get_user_info($access_token,$openid){
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$info = json_decode(file_get_contents($url),true);
return $info;
}
//验证token
private function checkSignature(){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$tmpArr = array($this->token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
//模拟post,get请求
private function https_request($url,$data = null){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
if(!empty($data)){
curl_setopt($ch,CURLOPT_POST,1);//模拟POST
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//POST内容
}
$outopt = curl_exec($ch);
curl_close($ch);
return $outopt;
}
}
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/9081165.html
浙公网安备 33010602011771号