123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace Home\Controller;
- use Think\Controller;
- class ComController extends Controller {
- public $iphone;
- //app token 微信授权验证用户
- public function _initialize(){
- // print_r($_SERVER);
- $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='';
- // print_r($key);
- // print_r('测试2'.$secret);
- $data88=I();
- // print_r($data88);
- // print_r($data88);
- // print_r('测试1'.$token);
- // print_r($_SESSION);
- $urls=$http.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- if (!empty($token)){
- // echo 111111111111;
- if (!empty($_SESSION['idphone'])){
- $_GET['phone']=$_SESSION['idphone'];
- $this->iphone=$_GET['phone'];
- //dump($_SESSION);
- }else{
- header("Location: https://qhome.usky.cn/jdxf/wxapp2.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))
- {
- // echo 222222222;
- $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 {
- // echo 33333333333;
- // print_r($_SERVER['HTTP_USER_AGENT']);
- if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
- header("Location: https://qhome.usky.cn/jdxf/wxapp2.php/Home/Login/index");
- // echo 444444444444;
- exit;
- }
- //exit(‘微信登陆未开通。’);
- //header('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]);
- //$ss='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- // print_r('测试'.$_GET['user']);
- if($_GET['user']){
- $_SESSION['idopenid111']='123456789wx';
- }
- // echo 55555555555;
- // print_r($_SESSION['idopenid111']);
- if(!isset($_SESSION['idopenid111']) && empty($_SESSION['idopenid111'])){
- // echo 66666666666;
- $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 ');
- header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx41e478e592f9e04a&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');
- $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&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/wxapp2.php/Home/Login/index");
- }
- }
- }else{
- // echo 77777777777777;
- // print_r($_SESSION['idopenid111']);
- $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" .$_SESSION['idopenid111'] . "'");
- if (count($res) > 0) {
- // echo 88888888888;
- //$_SESSION['idopenid111']=$user_openid['openid'];
- $this->iphone = $res[0]['idphone'];
- $_GET['phone'] = $res[0]['idphone'];
- $_SESSION['phone']=$res[0]['idphone'];
- } else {
- // echo 999999999999;
- unset($_SESSION['idopenid111']);
- unset($_SESSION['phone']);
- header("Location: https://qhome.usky.cn/jdxf/wxapp2.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);
- // }
- }
|