ComController.class.php.bak.2021-09-03 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // print_r("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $_SESSION['app_token'] . "' and type='app' and phone='".$_SESSION['phone']."'");
  107. // exit();
  108. if (isset($_SESSION['app_token'])&&isset($_SESSION['phone'])){
  109. $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE type='app' and phone='".$_SESSION['phone']."'");
  110. header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);//项目首页
  111. }else{
  112. if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  113. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
  114. exit();
  115. }
  116. if (!isset($_SESSION['idopenid']) && empty($_SESSION['idopenid'])) {
  117. if (empty($code)) {
  118. $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
  119. 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 ');
  120. } else {
  121. $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
  122. $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "' and type='wx'");
  123. if (count($res) > 0) {
  124. $_SESSION['idopenid'] = $user_openid['openid'];
  125. $_SESSION['openid'] = $user_openid['openid'];
  126. $this->iphone = $res[0]['phone'];
  127. $_GET['phone'] = $res[0]['phone'];
  128. $_SESSION['phone'] = $res[0]['phone'];
  129. // header('Location:https://iot.usky.cn/ceshi.html?op='.$res['openid']);//项目首页
  130. header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);//项目首页
  131. } else {
  132. unset($_SESSION['idopenid']);
  133. unset($_SESSION['phone']);
  134. // header('Location:https://iot.usky.cn/ceshi.html?');//登录页
  135. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
  136. }
  137. }
  138. } else {
  139. $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $_SESSION['idopenid'] . "' and type='wx'");
  140. if (count($res) > 0) {
  141. $this->iphone = $res[0]['phone'];
  142. $_GET['phone'] = $res[0]['phone'];
  143. $_SESSION['phone'] = $res[0]['phone'];
  144. // print_r($res);
  145. // exit();
  146. // header('Location:https://iot.usky.cn/ceshi.html?op='.$res['openid']);//项目首页
  147. header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);
  148. } else {
  149. unset($_SESSION['idopenid']);
  150. unset($_SESSION['phone']);
  151. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');//登录页
  152. // header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
  153. }
  154. }
  155. }
  156. }
  157. private function s_curl($url)
  158. {
  159. $ch = curl_init();
  160. curl_setopt($ch, CURLOPT_URL, $url);
  161. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  163. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  164. $file_contents = curl_exec($ch);
  165. curl_close($ch);
  166. $arr = json_decode($file_contents, true);
  167. return $arr;
  168. }
  169. }