ComController.class.php.bak.2021-08-31 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class ComController extends Controller
  5. {
  6. public $owner_name;
  7. public function _initialize()
  8. {
  9. header("Access-Control-Allow-Origin:*");
  10. header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
  11. header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
  12. $_SESSION['phone'] = '17621295894';
  13. }
  14. //获取设备编号
  15. public function getDeviceCode($company)
  16. {
  17. $device = M()->query("SELECT o.owner_code,c.owner_name FROM sp_owner o join sp_owner_company c ON o.company =c.owner_id WHERE o.company in(".$company.")");
  18. $this->owner_name = $device[0]['owner_name'];
  19. $str = "";
  20. foreach ($device as $val) {
  21. $str .= "'" . $val['owner_code'] . "',";
  22. }
  23. $newstr = substr($str, 0, strlen($str) - 1);
  24. return $newstr;
  25. }
  26. public function getCompileUrl()
  27. {
  28. $Data = I('');
  29. if (empty($Data['curl'])) {
  30. $R['flag'] = false;
  31. $R['msg'] = '缺少关键参数curl';
  32. echo json_encode($R);
  33. exit();
  34. }
  35. $curl = $Data['curl'];
  36. $tourl = urlencode($curl);
  37. $R['flag'] = true;
  38. $R['turl'] = $tourl;
  39. $R['msg'] = '当前路径编译成功';
  40. echo json_encode($R);
  41. exit();
  42. }
  43. public function getPageAuthorization()
  44. {
  45. if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  46. //跳转登录页
  47. $R['msg'] = '页面授权未通过,跳转登录页面';
  48. $R['flag'] = false;
  49. echo json_encode($R);
  50. exit();
  51. }
  52. if (!isset($_SESSION['idopenid']) && empty($_SESSION['idopenid'])) {
  53. $Data = I('');
  54. $code = $Data['code'];
  55. if (empty($code)) {
  56. $R['msg'] = '路径编译成功';
  57. $R['flag'] = false;
  58. echo json_encode($R);
  59. exit();
  60. } else {
  61. $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
  62. $res = M()->query("SELECT phone FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "' and type='wx'");
  63. if (count($res) > 0) {
  64. $_SESSION['idopenid'] = $user_openid['openid'];
  65. $_SESSION['openid'] = $user_openid['openid'];
  66. $this->iphone = $res[0]['phone'];
  67. $_GET['phone'] = $res[0]['phone'];
  68. $_SESSION['phone'] = $res[0]['phone'];
  69. $R['msg'] = '页面授权通过,用户权限通过';
  70. $R['flag'] = true;
  71. echo json_encode($R);
  72. exit();
  73. } else {
  74. unset($_SESSION['idopenid']);
  75. unset($_SESSION['phone']);
  76. $R['msg'] = '页面授权未通过,跳转登录页面';
  77. $R['flag'] = false;
  78. echo json_encode($R);
  79. exit();
  80. }
  81. }
  82. } else {
  83. $res = M()->query("SELECT phone FROM sp_wx_customuser WHERE openid='" . $_SESSION['idopenid'] . "' and type='wx'");
  84. if (count($res) > 0) {
  85. $this->iphone = $res[0]['phone'];
  86. $_GET['phone'] = $res[0]['phone'];
  87. $_SESSION['phone'] = $res[0]['phone'];
  88. $R['msg'] = '页面授权通过,用户权限通过';
  89. $R['flag'] = true;
  90. echo json_encode($R);
  91. exit();
  92. } else {
  93. unset($_SESSION['idopenid']);
  94. unset($_SESSION['phone']);
  95. $R['msg'] = '页面授权未通过,跳转登录页面';
  96. $R['flag'] = false;
  97. echo json_encode($R);
  98. exit();
  99. }
  100. }
  101. }
  102. public function getPageAuthorization1()
  103. {
  104. $http = 'https://';
  105. $code = I('get.code');
  106. if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  107. //跳转登录页
  108. $R['msg'] = '页面授权未通过,跳转登录页面';
  109. $R['flag'] = false;
  110. // header('Location:https://iot.usky.cn/ceshi.html');//登录
  111. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
  112. }
  113. if (!isset($_SESSION['idopenid']) && empty($_SESSION['idopenid'])) {
  114. if (empty($code)) {
  115. $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
  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=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
  119. $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "' and type='wx'");
  120. if (count($res) > 0) {
  121. $_SESSION['idopenid'] = $user_openid['openid'];
  122. $_SESSION['openid'] = $user_openid['openid'];
  123. $this->iphone = $res[0]['phone'];
  124. $_GET['phone'] = $res[0]['phone'];
  125. $_SESSION['phone'] = $res[0]['phone'];
  126. // header('Location:https://iot.usky.cn/ceshi.html?op='.$res['openid']);//项目首页
  127. header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);//项目首页
  128. } else {
  129. unset($_SESSION['idopenid']);
  130. unset($_SESSION['phone']);
  131. // header('Location:https://iot.usky.cn/ceshi.html?');//登录页
  132. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
  133. }
  134. }
  135. } else {
  136. $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $_SESSION['idopenid'] . "' and type='wx'");
  137. if (count($res) > 0) {
  138. $this->iphone = $res[0]['phone'];
  139. $_GET['phone'] = $res[0]['phone'];
  140. $_SESSION['phone'] = $res[0]['phone'];
  141. // print_r($res);
  142. // exit();
  143. // header('Location:https://iot.usky.cn/ceshi.html?op='.$res['openid']);//项目首页
  144. header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);
  145. } else {
  146. unset($_SESSION['idopenid']);
  147. unset($_SESSION['phone']);
  148. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');//登录页
  149. // header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
  150. }
  151. }
  152. }
  153. private function s_curl($url)
  154. {
  155. $ch = curl_init();
  156. curl_setopt($ch, CURLOPT_URL, $url);
  157. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  159. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  160. $file_contents = curl_exec($ch);
  161. curl_close($ch);
  162. $arr = json_decode($file_contents, true);
  163. return $arr;
  164. }
  165. }