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); } }