ComController.class.php.bak.2021-07-30 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. private function s_curl($url)
  103. {
  104. $ch = curl_init();
  105. curl_setopt($ch, CURLOPT_URL, $url);
  106. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  107. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  108. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  109. $file_contents = curl_exec($ch);
  110. curl_close($ch);
  111. $arr = json_decode($file_contents, true);
  112. return $arr;
  113. }
  114. }