ComController0caidan.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. $jsoninfo1 = $this->s_curl('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146');
  9. $access_token1 = $jsoninfo1["access_token"];
  10. $menu_url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token1;
  11. $menu_data='{
  12. "button": [
  13. {
  14. "name": "软件平台",
  15. "sub_button": [
  16. {
  17. "type": "view",
  18. "name": "巡检",
  19. "url": "http://www.jd-ioe.com/jdxf/weixin.php/home/xunj/"
  20. },
  21. {
  22. "type": "view",
  23. "name": "建筑物安全评估",
  24. "url": "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Fireinsur"
  25. }
  26. ]
  27. },
  28. {
  29. "name": "消防系统",
  30. "sub_button": [
  31. {
  32. "type": "view",
  33. "name": "消防综合信息",
  34. "url": "http://www.jd-ioe.com/jdxf/wxapp2.php"
  35. },
  36. {
  37. "type": "view",
  38. "name": "设置",
  39. "url": "http://www.jd-ioe.com/jdxf/weixin2.php/Home/Fire/setperm"
  40. }
  41. ]
  42. },
  43. {
  44. "name": "关于永天",
  45. "sub_button": [
  46. {
  47. "type": "view",
  48. "name": "成功案例",
  49. "url": "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Public/Vexample"
  50. },
  51. {
  52. "type": "click",
  53. "name": "联系我们",
  54. "key": "V1001_EVET_ADDRESS",
  55. "sub_button": [ ]
  56. },
  57. {
  58. "type": "view",
  59. "name": "客户建议",
  60. "url": "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Public/Mainfback"
  61. },
  62. {
  63. "type": "view",
  64. "name": "客户投诉",
  65. "url": "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Public/Profback"
  66. }
  67. ]
  68. }
  69. ]
  70. }';
  71. $user_openid = $this->curl($menu_url,$menu_data);
  72. print_r($user_openid);
  73. $http='https://';
  74. $token = I('token');
  75. if (empty($token)){
  76. $token=$_GET['token'];
  77. }
  78. if(!empty($_SESSION['idtoken'])){
  79. $token=$_SESSION['idtoken'];
  80. }
  81. if(strpos($_SERVER['HTTP_USER_AGENT'],"MicroMessenger") != false){
  82. $token='';
  83. $_SESSION['idtoken']='';
  84. }
  85. $key = I('key');
  86. $secret = I('secret');
  87. $messign='';
  88. $code='';
  89. $val='';
  90. $urls=$http.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  91. if (!empty($token)){
  92. if (!empty($_SESSION['idphone'])){
  93. $_GET['phone']=$_SESSION['idphone'];
  94. $this->iphone=$_GET['phone'];
  95. //dump($_SESSION);
  96. }else{
  97. header("Location: ".$http."www.jd-ioe.com/jdxf/wxapp.php/Home/Login/index");
  98. }
  99. $t=time();
  100. $m=$t-7200;
  101. $time=date('Y-m-d H:i:s',$m);
  102. $tokens=M()->query("SELECT id FROM jdxf.user_token WHERE token='$token'");// AND time > '$time' 永久有效
  103. if(count($tokens)>0){
  104. $res=M()->execute("update jdxf.user_token set time=NOW() WHERE id='".$tokens[0]['id']."'");
  105. if($res !== false){
  106. $code='200';
  107. $messign='token更新成功';
  108. $this->assign('tokenurl','/token/'.$token);
  109. $this->tokenlog($token,$urls,$messign);
  110. }else{
  111. $code='201';
  112. $messign='token更新失败';
  113. echo json_encode(array('code'=>$code,'messign'=>$messign));
  114. $this->tokenlog($token,$urls,$messign);
  115. exit;
  116. }
  117. }else{
  118. $code='204';
  119. $messign='token失效';
  120. echo json_encode(array('code'=>$code,'messign'=>$messign));
  121. $this->tokenlog($token,$urls,$messign);
  122. exit;
  123. }
  124. }
  125. elseif (!empty($key) && !empty($secret))
  126. {
  127. $gettoken=M()->query("SELECT id,`key`,secret FROM jdxf.key_secret WHERE `key`='$key' AND secret='$secret'");
  128. if(count($gettoken)>0){
  129. $token=md5($key.uniqid().$secret);
  130. $res=M()->execute("insert into jdxf.user_token(token,`time`) VALUES('$token',NOW())");
  131. if($res>0){
  132. $code='205';
  133. $messign='token成功生成';
  134. $val=$token;
  135. }else{
  136. $code='203';
  137. $messign='token生成失败';
  138. }
  139. }else{
  140. $code='206';
  141. $messign='key-secret验证失败';
  142. }
  143. echo json_encode(array('code'=>$code,'messign'=>$messign,'val'=>$val));
  144. $this->tokenlog($val,$urls,$messign);
  145. exit;
  146. }else
  147. {
  148. if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  149. header("Location: " . $http . "www.jd-ioe.com/jdxf/wxapp.php/Home/Login/index");
  150. exit;
  151. }
  152. //exit(‘微信登陆未开通。’);
  153. //header('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]);
  154. //$ss='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  155. if($_GET['user']){
  156. $_SESSION['idopenid111']='123456789wx';
  157. }
  158. if(!isset($_SESSION['idopenid111']) && empty($_SESSION['idopenid111'])){
  159. $code = I('get.code');
  160. if (empty($code)) {
  161. //$tourl=urlencode('http://47.98.201.73/jdxf/wxapp.php/'.MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
  162. //不能带端口.':'.$_SERVER["SERVER_PORT"]
  163. $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
  164. 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 ');
  165. } else {
  166. $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');
  167. $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "'");
  168. if (count($res) > 0) {
  169. $_SESSION['idopenid111']=$user_openid['openid'];
  170. $this->iphone = $res[0]['idphone'];
  171. $_GET['phone'] = $res[0]['idphone'];
  172. $_SESSION['phone']=$res[0]['idphone'];
  173. } else {
  174. unset($_SESSION['idopenid111']);
  175. unset($_SESSION['phone']);
  176. header("Location: " . $http . "www.jd-ioe.com/jdxf/wxapp.php/Home/Login/index");
  177. }
  178. }
  179. }else{
  180. $res = M()->query("SELECT idphone FROM sp_wx_customuser WHERE openid='" .$_SESSION['idopenid111'] . "'");
  181. if (count($res) > 0) {
  182. //$_SESSION['idopenid111']=$user_openid['openid'];
  183. $this->iphone = $res[0]['idphone'];
  184. $_GET['phone'] = $res[0]['idphone'];
  185. $_SESSION['phone']=$res[0]['idphone'];
  186. } else {
  187. unset($_SESSION['idopenid111']);
  188. unset($_SESSION['phone']);
  189. header("Location: " . $http . "www.jd-ioe.com/jdxf/wxapp.php/Home/Login/index");
  190. }
  191. }
  192. //dump($_SESSION);
  193. //不等于空,可能是第一次进入
  194. }
  195. }
  196. //token 日志
  197. private final function tokenlog($token,$urls,$data=''){
  198. try {
  199. M()->execute("INSERT INTO jdxf.token_log(token,url,`time`,data1) VALUES('$token','$urls',NOW(),'$data')");
  200. } catch (Exception $e) {
  201. }
  202. }
  203. //远程调用连接
  204. private function s_curl($url){
  205. $ch = curl_init();
  206. curl_setopt ($ch, CURLOPT_URL, $url);
  207. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
  208. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  209. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
  210. $file_contents = curl_exec($ch);
  211. curl_close($ch);
  212. $arr = json_decode($file_contents,true);
  213. return $arr;
  214. }
  215. private final function curl($url,$data=''){
  216. $ch = curl_init();
  217. curl_setopt($ch, CURLOPT_URL, $url);
  218. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  219. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  220. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  221. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  222. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  223. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  224. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  225. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  226. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  227. $info = curl_exec($ch);
  228. if (curl_errno($ch)) {
  229. return 'Errno'.curl_error($ch);
  230. }else{
  231. return $info;
  232. }
  233. curl_close($ch);
  234. }
  235. }