ComController.class.php.bak.2021-11-11-caidan 10.0 KB

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