Auth.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | auth.php 用于做权限验证
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Component\Service;
  8. use Dever;
  9. use Main\Lib\Wechat;
  10. class Auth
  11. {
  12. const TYPE = 5;
  13. /**
  14. * wechat
  15. *
  16. * @var Wechat
  17. */
  18. private $wechat;
  19. /**
  20. * result
  21. *
  22. * @var array
  23. */
  24. private $result;
  25. /**
  26. * output
  27. *
  28. * @var string
  29. */
  30. private $output;
  31. /**
  32. * 构造函数 初始化
  33. *
  34. * @return mixed
  35. */
  36. public function __construct()
  37. {
  38. $this->wechat = new Wechat(self::TYPE);
  39. }
  40. /**
  41. * 获取component token 一般为系统token
  42. *
  43. * @return mixed
  44. */
  45. public function token_api()
  46. {
  47. return $this->wechat->token();
  48. }
  49. /**
  50. * 获取预授权码 开始用户授权
  51. *
  52. * @return mixed
  53. */
  54. public function test_api()
  55. {
  56. $link = $this->wechat->login('component/auth.oauth', false, false);
  57. echo '<a href="'.$link.'">第三方平台授权</a>';die;
  58. }
  59. /**
  60. * 获取预授权码 开始用户授权
  61. *
  62. * @return mixed
  63. */
  64. public function get_api()
  65. {
  66. $this->wechat->login('component/auth.oauth');
  67. }
  68. /**
  69. * 获取oauth的token 用户token
  70. *
  71. * @return mixed
  72. */
  73. public function oauth_api()
  74. {
  75. $this->wechat->oauth();
  76. }
  77. /**
  78. * 业务推送接口 微信服务器会将所有请求都推送到这里
  79. *
  80. * @return mixed
  81. */
  82. public function main()
  83. {
  84. $this->output = '';
  85. # 获取微信消息
  86. $this->request();
  87. # 对消息进行验证,并根据类型得到本平台的配置数据
  88. //$this->response();
  89. # 输出
  90. $this->output();
  91. }
  92. /**
  93. * 输出
  94. *
  95. * @return mixed
  96. */
  97. private function output()
  98. {
  99. if (!$this->output) {
  100. $this->output = 'success';
  101. }
  102. echo $this->output;die;
  103. }
  104. /**
  105. * 获取微信发送的内容
  106. *
  107. * @return mixed
  108. */
  109. private function request()
  110. {
  111. //$post = $_GET ? $_GET : $_POST;
  112. $data = array();
  113. $data['signature'] = '432ec06aa3a0f680924c9587ea215b4e490382b3';
  114. $data['timestamp'] = '1525677152';
  115. $data['nonce'] = '377902872';
  116. $data['encrypt_type'] = 'aes';
  117. $data['msg_signature'] = '7f62174a1402d955c2d8e61f69a309540714c579';
  118. $xml = file_get_contents('php://input');
  119. $xml = "<xml>
  120. <AppId><![CDATA[wxc50846069a0ae2d2]]></AppId>
  121. <Encrypt><![CDATA[SYTZJTU4ZWOKAWhjWYgVEZQMRplYNo1ZuN5dL3hQoQmbLbYM0O5Iaw/FqpigtOAyIL3dIgoA1ti91C0Z45/qgl9w4Fxa1yi94tugaWQfq5pQGbRwBte5uR3okaFxVYW0ZQd3k0HWuMgAvt/pDoMn5hLpvxhfEKX4ZVvkY4fcgb0D4bz1xba0DCdpVqHmzk1ewbD01LeUNw9JQSSBiFX24x7JruXSvsXMn0gKfYUR1s5vavnoVz6CeyUYzW63Id+b4gxKDnBGAfsBQWFdtgoJg9Ze6AN2EBGmbltx8qr7L+UKCrExgpdBRyG2vDu4QQvk+lp/jjbao721zQGpgFqr1hIJjSiXX6M8my7l3wI+nHJpdF3PpCsEiml2zcFGDrd7PSS1A8PAYrDG8W84NNpK4Onz2ZxrRI/BusI999TiS/I88fT5XM4qMo2j2F488vvSRHdJxo7C+3V+rQFkBXSbVA==]]></Encrypt>
  122. </xml>";
  123. if (!empty($xml)) {
  124. libxml_disable_entity_loader(true);
  125. $result = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  126. $result = $this->wechat->decode($data['msg_signature'], $data['timestamp'], $data['nonce'], $result['Encrypt']);
  127. if (isset($result['ComponentVerifyTicket'])) {
  128. $this->wechat->ticket($result['ComponentVerifyTicket'], 600, 100);
  129. }
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. }