LoginController.class.php.bak.2021-08-31 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class LoginController extends Controller
  5. {
  6. public $token = '';
  7. public $http = 'https';
  8. public function _initialize()
  9. {
  10. header("Access-Control-Allow-Origin:*");
  11. header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
  12. 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");
  13. if (strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  14. $_SESSION['type'] = 'wx';
  15. } else {
  16. $_SESSION['type'] = 'app';
  17. }
  18. }
  19. public function getCompileUrl()
  20. {
  21. $Data = I('');
  22. if (!isset($Data['curl'])) {
  23. $R['flag'] = false;
  24. $R['msg'] = '缺少关键参数curl';
  25. echo json_encode($R);
  26. exit();
  27. }
  28. $tourl = urlencode($Data['curl']);
  29. $R['flag'] = true;
  30. $R['tourl'] = $tourl;
  31. $R['msg'] = '当前路径编译成功';
  32. echo json_encode($R);
  33. }
  34. public function getFirst()
  35. {
  36. if (strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  37. $_SESSION['type'] = 'wx';
  38. $Data = I('');
  39. $code = $Data['code'];
  40. if (empty($code)) {
  41. $R['flag'] = false;
  42. $R['msg'] = '操作失败,Code获取失败';
  43. echo json_encode($R);
  44. exit();
  45. } else {
  46. $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
  47. $res = M()->query("select phone from sp_wx_customuser where openid='" . $user_openid['openid'] . "'");
  48. $_SESSION['openid'] = $user_openid['openid'];
  49. $this->token = $_SESSION['openid'];
  50. if (count($res) > 0) {
  51. $_SESSION['phone'] = $res[0]['phone'];
  52. $_SESSION['openid'] = $user_openid['openid'];
  53. // $_SESSION['idopenid'] = $_SESSION['openid'];
  54. $R['msg'] = '操作成功';
  55. $R['flag'] = true;
  56. echo json_encode($R);
  57. exit();
  58. }
  59. }
  60. } else {
  61. $_SESSION['type'] = 'app';
  62. $_SESSION['app_token'] = "38fff9e882bb58973942a16bb5bb05d005ad08d1e";
  63. }
  64. $R['msg'] = '请登录/注册';
  65. $R['flag'] = false;
  66. echo json_encode($R);
  67. }
  68. public function getFirst1()
  69. {
  70. $http = 'https://';
  71. $code = I('get.code');
  72. if (strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
  73. $_SESSION['type'] = 'wx';
  74. if (empty($code)) {
  75. $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
  76. 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 ');
  77. } else {
  78. $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
  79. $res = M()->query("select phone from sp_wx_customuser where openid='" . $user_openid['openid'] . "'");
  80. $_SESSION['openid'] = $user_openid['openid'];
  81. $this->token = $_SESSION['openid'];
  82. if (count($res) > 0) {
  83. $_SESSION['phone'] = $res[0]['phone'];
  84. $_SESSION['openid'] = $user_openid['openid'];
  85. $_SESSION['idopenid'] = $_SESSION['openid'];
  86. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login?flag=true');
  87. }
  88. }
  89. } else {
  90. $_SESSION['type'] = 'app';
  91. $_SESSION['app_token'] = "38fff9e882bb58973942a16bb5bb05d005ad08d1e";
  92. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login?flag=true');
  93. }
  94. header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login?flag=false');
  95. }
  96. //短信验证
  97. public function getVerificationCode()
  98. {
  99. include('/php/api_sdk/JdMessageSend2.php');
  100. $Data = I('phone');
  101. $Verx = rand('10000', '99999');
  102. $_SESSION['VerificationCode'] = '';
  103. $_SESSION['VerificationCode'] = $Verx . ',' . date('Y-m-d H:i:s');
  104. // print_r($_SESSION['VerificationCode']);
  105. sendSms("$Data", "$Verx");
  106. $arr[0]['VerificationCode'] = $_SESSION['VerificationCode'];
  107. $res['msg'] = '发送成功';
  108. $res['flag'] = true;
  109. $res['data'] = $arr;
  110. echo json_encode($res);
  111. }
  112. //登录并注册
  113. public function setSignUp()
  114. {
  115. $Data = I('');
  116. if (!isset($Data['phone']) || !isset($Data['VerificationCode'])) {
  117. $R['msg'] = '操作失败,缺少关键参数';
  118. $R['flag'] = true;
  119. echo json_encode($R);
  120. exit();
  121. }
  122. $Arr = explode(",", $_SESSION['VerificationCode']);
  123. $verifyTime = floor(strtotime(date('Y-m-d H:i:s'))) - floor(strtotime($Arr[1]));
  124. if ($verifyTime < 300) {
  125. if ($Arr[0] != $Data['VerificationCode']) {
  126. $R['msg'] = '验证码无效,请重新获取!';
  127. $R['flag'] = false;
  128. echo json_encode($R);
  129. exit();
  130. }
  131. $res = M()->query("select * from sp_wx_customuser where phone='" . $Data['phone'] . "' and type='" . $_SESSION['type'] . "'");
  132. if (empty($res)) {
  133. $data['phone'] = $Data['phone'];
  134. $data['idphone']= $Data['phone'];
  135. if ($_SESSION['type'] == 'app') {
  136. $data['openid'] = $_SESSION['app_token'];
  137. } else {
  138. $data['openid'] = $_SESSION['openid'];
  139. }
  140. $data['type'] = $_SESSION['type'];
  141. $data['createtime'] = date('Y-m-d H:i:s');
  142. $data['status'] = 1;
  143. $Res = M('wx_customuser')->add($data);
  144. if (!$Res){
  145. $R['msg'] = '注册失败,请重新注册';
  146. $R['flag'] = true;
  147. echo json_encode($R);
  148. exit();
  149. }
  150. }
  151. $_SESSION['phone'] = $data['phone'];
  152. $_SESSION['openid'] = $data['openid'];
  153. $_SESSION['idopenid'] = $_SESSION['openid'];
  154. $R['msg'] = '验证成功,正在登录';
  155. $R['flag'] = true;
  156. echo json_encode($R);
  157. } else {
  158. $R['msg'] = '验证码超时无效,请重新获取!';
  159. $R['flag'] = false;
  160. echo json_encode($R);
  161. }
  162. }
  163. private function s_curl($url)
  164. {
  165. $ch = curl_init();
  166. curl_setopt($ch, CURLOPT_URL, $url);
  167. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  168. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  169. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  170. $file_contents = curl_exec($ch);
  171. curl_close($ch);
  172. $arr = json_decode($file_contents, true);
  173. // var_dump($arr);
  174. return $arr;
  175. }
  176. private function s_curl2($url)
  177. {
  178. $ch = curl_init();
  179. curl_setopt($ch, CURLOPT_URL, $url);
  180. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  181. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  182. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  183. $file_contents = curl_exec($ch);
  184. curl_close($ch);
  185. return $file_contents;
  186. }
  187. }