123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace Home\Controller;
- use Think\Controller;
- class ComController extends Controller {
- public $iphone;
- //app token 微信授权验证用户
- public function _initialize(){
- $jsoninfo1 = $this->s_curl('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146');
- $access_token1 = $jsoninfo1["access_token"];
- $menu_url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token1;
- $menu_data='{
- "button": [
-
- {
- "name": "软件平台",
- "sub_button": [
- {
- "type": "view",
- "name": "巡检",
- "url": "https://iot.usky.cn/jdxf/weixin.php/home/xunj/"
- },
- {
- "type": "view",
- "name": "建筑物安全评估",
- "url": "https://iot.usky.cn/jdxf/wxapp2.php/Home/Fireinsur"
- }
- ]
- },
- {
- "name": "智慧应用",
- "sub_button": [
- {
- "type": "view",
- "name": "消防综合信息",
- "url": "https://qhome.usky.cn/jdxf/wxapp2.php"
- },
- {
- "type": "view",
- "name": "安防综合信息",
- "url": "https://iot.usky.cn/jdxf/wxapp3.php"
- },{
- "type": "view",
- "name": "楼宇自控系统",
- "url": "https://iot.usky.cn/jdxf/wxapp5.php"
- }
- ]
- },
- {
- "name": "关于永天",
- "sub_button": [
- {
- "type": "view",
- "name": "成功案例",
- "url": "https://iot.usky.cn/jdxf/wxapp2.php/Home/Public/Vexample"
- },
- {
- "type": "click",
- "name": "联系我们",
- "key": "V1001_EVET_ADDRESS",
- "sub_button": [ ]
- },
- {
- "type": "view",
- "name": "客户建议",
- "url": "https://iot.usky.cn/jdxf/wxapp2.php/Home/Public/Mainfback"
- },
- {
- "type": "view",
- "name": "客户投诉",
- "url": "https://iot.usky.cn/jdxf/wxapp2.php/Home/Public/Profback"
- }
- ]
- }
- ]
- }';
- $user_openid = $this->curl($menu_url,$menu_data);
- // print_r($user_openid);
- $http='https://';
- $token = I('token');
- if (empty($token)){
- $token=$_GET['token'];
- }
- if(!empty($_SESSION['idtoken'])){
- $token=$_SESSION['idtoken'];
- }
- if(strpos($_SERVER['HTTP_USER_AGENT'],"MicroMessenger") != false){
- $token='';
- $_SESSION['idtoken']='';
- }
- $key = I('key');
- $secret = I('secret');
- $messign='';
- $code='';
- $val='';
- $urls=$http.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- if (!empty($token)){
- if (!empty($_SESSION['idphone'])){
- $_GET['phone']=$_SESSION['idphone'];
- $this->iphone=$_GET['phone'];
- //dump($_SESSION);
- }else{
- header("Location: https://qhome.usky.cn/jdxf/wxapp.php/Home/Login/index");
- }
- $t=time();
- $m=$t-7200;
- $time=date('Y-m-d H:i:s',$m);
- $tokens=M()->query("SELECT id FROM jdxf.user_token WHERE token='$token'");// AND time > '$time' 永久有效
- if(count($tokens)>0){
- $res=M()->execute("update jdxf.user_token set time=NOW() WHERE id='".$tokens[0]['id']."'");
- if($res !== false){
- $code='200';
- $messign='token更新成功';
- $this->assign('tokenurl','/token/'.$token);
- $this->tokenlog($token,$urls,$messign);
- }else{
- $code='201';
- $messign='token更新失败';
- echo json_encode(array('code'=>$code,'messign'=>$messign));
- $this->tokenlog($token,$urls,$messign);
- exit;
- }
- }else{
- $code='204';
- $messign='token失效';
- echo json_encode(array('code'=>$code,'messign'=>$messign));
- $this->tokenlog($token,$urls,$messign);
- exit;
- }
- }
- elseif (!empty($key) && !empty($secret))
- {
- $gettoken=M()->query("SELECT id,`key`,secret FROM jdxf.key_secret WHERE `key`='$key' AND secret='$secret'");
- if(count($gettoken)>0){
- $token=md5($key.uniqid().$secret);
- $res=M()->execute("insert into jdxf.user_token(token,`time`) VALUES('$token',NOW())");
- if($res>0){
- $code='205';
- $messign='token成功生成';
- $val=$token;
- }else{
- $code='203';
- $messign='token生成失败';
- }
- }else{
- $code='206';
- $messign='key-secret验证失败';
- }
- echo json_encode(array('code'=>$code,'messign'=>$messign,'val'=>$val));
- $this->tokenlog($val,$urls,$messign);
- exit;
- }else
- {
- if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
- header("Location: https://qhome.usky.cn/jdxf/wxapp.php/Home/Login/index");
- exit;
- }
- //exit(‘微信登陆未开通。’);
- //header('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]);
- //$ss='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- if($_GET['user']){
- $_SESSION['idopenid111']='123456789wx';
- }
- if(!isset($_SESSION['idopenid111']) && empty($_SESSION['idopenid111'])){
- $code = I('get.code');
- if (empty($code)) {
- //$tourl=urlencode('http://47.98.201.73/jdxf/wxapp.php/'.MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
- //不能带端口.':'.$_SERVER["SERVER_PORT"]
- $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
- header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . C('APPID') . '&redirect_uri=' . $tourl . '&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect ');
- } else {
- $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . C('APPID') . '&secret=' . C('SECRET') . '&code=' . $code . '&grant_type=authorization_code');
- $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "'");
- if (count($res) > 0) {
- $_SESSION['idopenid111']=$user_openid['openid'];
- $this->iphone = $res[0]['idphone'];
- $_GET['phone'] = $res[0]['idphone'];
- $_SESSION['phone']=$res[0]['idphone'];
- } else {
- unset($_SESSION['idopenid111']);
- unset($_SESSION['phone']);
- header("Location: https://qhome.usky.cn/jdxf/wxapp.php/Home/Login/index");
- }
- }
- }else{
- $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" .$_SESSION['idopenid111'] . "'");
- if (count($res) > 0) {
- //$_SESSION['idopenid111']=$user_openid['openid'];
- $this->iphone = $res[0]['idphone'];
- $_GET['phone'] = $res[0]['idphone'];
- $_SESSION['phone']=$res[0]['idphone'];
- } else {
- unset($_SESSION['idopenid111']);
- unset($_SESSION['phone']);
- header("Location: https://qhome.usky.cn/jdxf/wxapp.php/Home/Login/index");
- }
- }
- //dump($_SESSION);
- //不等于空,可能是第一次进入
- }
- }
- //token 日志
- private final function tokenlog($token,$urls,$data=''){
- try {
- M()->execute("INSERT INTO jdxf.token_log(token,url,`time`,data1) VALUES('$token','$urls',NOW(),'$data')");
- } catch (Exception $e) {
- }
- }
- //远程调用连接
- private function s_curl($url){
- $ch = curl_init();
- curl_setopt ($ch, CURLOPT_URL, $url);
- curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- $arr = json_decode($file_contents,true);
- return $arr;
- }
- private final function curl($url,$data=''){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- 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);
- $info = curl_exec($ch);
- if (curl_errno($ch)) {
- return 'Errno'.curl_error($ch);
- }else{
- return $info;
- }
- curl_close($ch);
- }
- }
|