Auth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | auth.php 用于做权限验证
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Component\Src;
  8. use Dever;
  9. use Main\Lib\Wechat;
  10. class Auth
  11. {
  12. /**
  13. * wechat
  14. *
  15. * @var Wechat
  16. */
  17. private $wechat;
  18. /**
  19. * result
  20. *
  21. * @var array
  22. */
  23. private $result;
  24. /**
  25. * output
  26. *
  27. * @var string
  28. */
  29. private $output;
  30. /**
  31. * 构造函数 初始化
  32. *
  33. * @return mixed
  34. */
  35. public function __construct()
  36. {
  37. $this->wechat = new Wechat();
  38. }
  39. /**
  40. * 获取component token 一般为系统token
  41. *
  42. * @return mixed
  43. */
  44. public function token_api()
  45. {
  46. return $this->wechat->token();
  47. }
  48. /**
  49. * 获取预授权码 开始用户授权
  50. *
  51. * @return mixed
  52. */
  53. public function test_api()
  54. {
  55. $link = $this->wechat->login('component/auth.oauth', false, false);
  56. echo '<a href="'.$link.'">第三方平台授权</a>';die;
  57. }
  58. /**
  59. * 获取预授权码 开始用户授权
  60. *
  61. * @return mixed
  62. */
  63. public function get_api()
  64. {
  65. $this->wechat->login('component/auth.oauth');
  66. }
  67. /**
  68. * 获取oauth的token 用户token
  69. *
  70. * @return mixed
  71. */
  72. public function oauth_api()
  73. {
  74. $param['auth_code'] = Dever::input('auth_code');
  75. $param['expires_in'] = Dever::input('expires_in');
  76. if ($param['auth_code']) {
  77. $data = $this->wechat->oauth($param);
  78. } else {
  79. $param = Dever::input('id', 1);
  80. $data = $this->wechat->oauth($param, 2000, true);
  81. }
  82. if ($data && isset($data['openid'])) {
  83. $result = $this->wechat->curl('user', $data);
  84. if ($result) {
  85. # 初始化操作
  86. $param = array();
  87. $qrcode = Dever::load('upload/save.copy', $result['authorizer_info']['qrcode_url']);
  88. $param['project_id'] = $data['project_id'];
  89. $param['oauth_id'] = $data['id'];
  90. $param['name'] = $result['authorizer_info']['nick_name'];
  91. $param['principal_name'] = $result['authorizer_info']['principal_name'];
  92. $param['pic'] = $result['authorizer_info']['head_img'];
  93. $param['qrcode'] = $qrcode['url'];
  94. $param['username'] = $result['authorizer_info']['user_name'];
  95. //$param['desc'] = '1';
  96. $param['mdate'] = time();
  97. if (isset($result['authorizer_info']['MiniProgramInfo'])) {
  98. $param['type'] = 1;
  99. } else {
  100. if ($result['authorizer_info']['service_type_info'] == 2) {
  101. $param['type'] = 3;
  102. } else {
  103. $param['type'] = 2;
  104. }
  105. }
  106. $id = Dever::upinto('component/user', array('option_oauth_id' => $data['id']), $param);
  107. $domain = Dever::db('component/domain')->one(array('option_project_id' => $param['project_id']));
  108. //自动设置域名
  109. if ($domain) {
  110. $param['id'] = $id;
  111. Dever::load('component/domain.set', $param, $domain['id']);
  112. }
  113. $tester = Dever::db('component/domain')->state(array('option_project_id' => $param['project_id']));
  114. if ($tester) {
  115. $param['id'] = $id;
  116. foreach ($tester as $k => $v) {
  117. //自动设置体验者
  118. Dever::load('component/tester.set', $param, $v['id']);
  119. }
  120. }
  121. }
  122. }
  123. return '绑定成功';
  124. }
  125. /**
  126. * wechat回调,直接将数据回写到该方法中
  127. *
  128. * @return mixed
  129. */
  130. public function saveOauthInfo($oauth_id, $data, $project_id)
  131. {
  132. echo 1;
  133. # 这里用于记录权限集func_info,先不记录了
  134. return;
  135. }
  136. /**
  137. * 业务推送接口 微信服务器会将所有请求都推送到这里
  138. *
  139. * @return mixed
  140. */
  141. public function main()
  142. {
  143. $this->output = '';
  144. # 获取微信消息
  145. $this->request();
  146. # 对消息进行验证,并根据类型得到本平台的配置数据
  147. //$this->response();
  148. # 输出
  149. $this->output();
  150. }
  151. /**
  152. * 输出
  153. *
  154. * @return mixed
  155. */
  156. private function output()
  157. {
  158. if (!$this->output) {
  159. $this->output = 'success';
  160. }
  161. echo $this->output;die;
  162. }
  163. /**
  164. * 获取微信发送的内容
  165. *
  166. * @return mixed
  167. */
  168. private function request()
  169. {
  170. $data = $_GET ? $_GET : $_POST;
  171. /*
  172. $data = array();
  173. $data['signature'] = '9474754223130bc1c820537b8113b590ef3cbf8e';
  174. $data['timestamp'] = '1525915492';
  175. $data['nonce'] = '1211005377';
  176. $data['encrypt_type'] = 'aes';
  177. $data['msg_signature'] = 'f17b5c2d2cf1592e07803f190048289ff468ef2b';
  178. */
  179. $xml = file_get_contents('php://input');
  180. Dever::log(json_encode($data) . $xml);
  181. /*
  182. $xml = "<xml>
  183. <AppId><![CDATA[wxc50846069a0ae2d2]]></AppId>
  184. <Encrypt><![CDATA[w19P5s2RRaWRUt+0Af0qSyBbTay3JdxDuYQxexTgm0PGE0U2UYknJKuH05GdaZPlruuTcbETR1MOfUGLmIniNlJ7UJ9GKgQWAnzVU0gezeolmnwA683fcGZpuH9yhSZHiSHoSbJaLpLZAl3ywu3FCMcpq6vhVVN/d/cQOUEgmS+rj06DOU105tBuKweOcD9LEGyoFPDUWRfvG4uGT4vS7C3hiR7UQgKYCGhSxBF56umkYHiGwpo3TNQ0QTFzIN/o6V43KvyFyIOfbvmQn9a3T9heDbijiJgcG+jpj/b9Vsps7jV5yePaJTnBjUWVbM7aW/HHlXWuYu+vagwG+shaMSGQHxCzdgjxiVYu2uR+M4kHYl90EUSdEJrcyQrFtia9BOwtkLkBwziZgogaVFXgLvSlN+krS3L0jHPVedevEgNubAM292jd/BPsU8Pd3o+EBZBG7qOU4IgHE6eVc09AxA==]]></Encrypt>
  185. </xml>";
  186. */
  187. if (!empty($xml)) {
  188. libxml_disable_entity_loader(true);
  189. $result = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  190. $result = $this->wechat->decode($data['msg_signature'], $data['timestamp'], $data['nonce'], $result['Encrypt']);
  191. if (isset($result['ComponentVerifyTicket'])) {
  192. $this->wechat->ticket($result['ComponentVerifyTicket'], 600, 100);
  193. }
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. }