ComController.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class ComController extends Controller {
  5. public $iphone;
  6. //app token 微信授权验证用户
  7. public function _initialize(){
  8. // print_r($_SERVER);
  9. $http='https://';
  10. $token = I('token');
  11. if (empty($token)){
  12. $token=$_GET['token'];
  13. }
  14. if(!empty($_SESSION['idtoken'])){
  15. $token=$_SESSION['idtoken'];
  16. }
  17. if(strpos($_SERVER['HTTP_USER_AGENT'],"MicroMessenger") != false){
  18. $token='';
  19. $_SESSION['idtoken']='';
  20. }
  21. $key = I('key');
  22. $secret = I('secret');
  23. $messign='';
  24. $code='';
  25. $val='';
  26. // print_r($key);
  27. // print_r('测试2'.$secret);
  28. $data88=I();
  29. // print_r($data88);
  30. // print_r($data88);
  31. // print_r('测试1'.$token);
  32. // print_r($_SESSION);
  33. $urls=$http.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  34. if (!empty($token)){
  35. // echo 111111111111;
  36. if (!empty($_SESSION['idphone'])){
  37. $_GET['phone']=$_SESSION['idphone'];
  38. $this->iphone=$_GET['phone'];
  39. //dump($_SESSION);
  40. }else{
  41. header("Location: https://qhome.usky.cn/jdxf/wxapp2.php/Home/Login/index");
  42. }
  43. $t=time();
  44. $m=$t-7200;
  45. $time=date('Y-m-d H:i:s',$m);
  46. $tokens=M()->query("SELECT id FROM jdxf.user_token WHERE token='$token'");// AND time > '$time' 永久有效
  47. if(count($tokens)>0){
  48. $res=M()->execute("update jdxf.user_token set time=NOW() WHERE id='".$tokens[0]['id']."'");
  49. if($res !== false){
  50. $code='200';
  51. $messign='token更新成功';
  52. $this->assign('tokenurl','/token/'.$token);
  53. $this->tokenlog($token,$urls,$messign);
  54. }else{
  55. $code='201';
  56. $messign='token更新失败';
  57. echo json_encode(array('code'=>$code,'messign'=>$messign));
  58. $this->tokenlog($token,$urls,$messign);
  59. exit;
  60. }
  61. }else{
  62. $code='204';
  63. $messign='token失效';
  64. echo json_encode(array('code'=>$code,'messign'=>$messign));
  65. $this->tokenlog($token,$urls,$messign);
  66. exit;
  67. }
  68. }
  69. elseif (!empty($key) && !empty($secret))
  70. {
  71. // echo 222222222;
  72. $gettoken=M()->query("SELECT id,`key`,secret FROM jdxf.key_secret WHERE `key`='$key' AND secret='$secret'");
  73. if(count($gettoken)>0){
  74. $token=md5($key.uniqid().$secret);
  75. $res=M()->execute("insert into jdxf.user_token(token,`time`) VALUES('$token',NOW())");
  76. if($res>0){
  77. $code='205';
  78. $messign='token成功生成';
  79. $val=$token;
  80. }else{
  81. $code='203';
  82. $messign='token生成失败';
  83. }
  84. }else{
  85. $code='206';
  86. $messign='key-secret验证失败';
  87. }
  88. echo json_encode(array('code'=>$code,'messign'=>$messign,'val'=>$val));
  89. $this->tokenlog($val,$urls,$messign);
  90. exit;
  91. }else {
  92. // echo 33333333333;
  93. // print_r($_SERVER['HTTP_USER_AGENT']);
  94. if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  95. header("Location: https://qhome.usky.cn/jdxf/wxapp2.php/Home/Login/index");
  96. // echo 444444444444;
  97. exit;
  98. }
  99. //exit(‘微信登陆未开通。’);
  100. //header('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]);
  101. //$ss='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  102. // print_r('测试'.$_GET['user']);
  103. if($_GET['user']){
  104. $_SESSION['idopenid111']='123456789wx';
  105. }
  106. // echo 55555555555;
  107. // print_r($_SESSION['idopenid111']);
  108. if(!isset($_SESSION['idopenid111']) && empty($_SESSION['idopenid111'])){
  109. // echo 66666666666;
  110. $code = I('get.code');
  111. if (empty($code)) {
  112. //$tourl=urlencode('http://47.98.201.73/jdxf/wxapp.php/'.MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
  113. //不能带端口.':'.$_SERVER["SERVER_PORT"]
  114. $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
  115. // 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 ');
  116. 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 ');
  117. } else {
  118. // $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');
  119. $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
  120. $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "'");
  121. if (count($res) > 0) {
  122. $_SESSION['idopenid111']=$user_openid['openid'];
  123. $this->iphone = $res[0]['idphone'];
  124. $_GET['phone'] = $res[0]['idphone'];
  125. $_SESSION['phone']=$res[0]['idphone'];
  126. } else {
  127. unset($_SESSION['idopenid111']);
  128. unset($_SESSION['phone']);
  129. header("Location: https://qhome.usky.cn/jdxf/wxapp2.php/Home/Login/index");
  130. }
  131. }
  132. }else{
  133. // echo 77777777777777;
  134. // print_r($_SESSION['idopenid111']);
  135. $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" .$_SESSION['idopenid111'] . "'");
  136. if (count($res) > 0) {
  137. // echo 88888888888;
  138. //$_SESSION['idopenid111']=$user_openid['openid'];
  139. $this->iphone = $res[0]['idphone'];
  140. $_GET['phone'] = $res[0]['idphone'];
  141. $_SESSION['phone']=$res[0]['idphone'];
  142. } else {
  143. // echo 999999999999;
  144. unset($_SESSION['idopenid111']);
  145. unset($_SESSION['phone']);
  146. header("Location: https://qhome.usky.cn/jdxf/wxapp2.php/Home/Login/index");
  147. }
  148. }
  149. //dump($_SESSION);
  150. //不等于空,可能是第一次进入
  151. }
  152. }
  153. //token 日志
  154. private final function tokenlog($token,$urls,$data=''){
  155. try {
  156. M()->execute("INSERT INTO jdxf.token_log(token,url,`time`,data1) VALUES('$token','$urls',NOW(),'$data')");
  157. } catch (Exception $e) {
  158. }
  159. }
  160. //远程调用连接
  161. private function s_curl($url){
  162. $ch = curl_init();
  163. curl_setopt ($ch, CURLOPT_URL, $url);
  164. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
  165. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  166. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
  167. $file_contents = curl_exec($ch);
  168. curl_close($ch);
  169. $arr = json_decode($file_contents,true);
  170. return $arr;
  171. }
  172. // private final function curl($url,$data=''){
  173. // $ch = curl_init();
  174. // curl_setopt($ch, CURLOPT_URL, $url);
  175. // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  176. // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  177. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  178. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  179. // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  180. // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  181. // curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  182. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  183. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  184. // $info = curl_exec($ch);
  185. // if (curl_errno($ch)) {
  186. // return 'Errno'.curl_error($ch);
  187. // }else{
  188. // return $info;
  189. // }
  190. // curl_close($ch);
  191. // }
  192. }