Auth.php 6.1 KB

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