123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | auth.php 用于做权限验证
- |--------------------------------------------------------------------------
- */
- namespace Component\Service;
- use Dever;
- use Main\Lib\Wechat;
- class Auth
- {
- const TYPE = 5;
- /**
- * wechat
- *
- * @var Wechat
- */
- private $wechat;
-
- /**
- * result
- *
- * @var array
- */
- private $result;
-
- /**
- * output
- *
- * @var string
- */
- private $output;
-
- /**
- * 构造函数 初始化
- *
- * @return mixed
- */
- public function __construct()
- {
- $this->wechat = new Wechat(self::TYPE);
- }
- /**
- * 获取component token 一般为系统token
- *
- * @return mixed
- */
- public function token_api()
- {
- return $this->wechat->token();
- }
- /**
- * 获取预授权码 开始用户授权
- *
- * @return mixed
- */
- public function test_api()
- {
- $link = $this->wechat->login('component/auth.oauth', false, false);
- echo '<a href="'.$link.'">第三方平台授权</a>';die;
- }
- /**
- * 获取预授权码 开始用户授权
- *
- * @return mixed
- */
- public function get_api()
- {
- $this->wechat->login('component/auth.oauth');
- }
- /**
- * 获取oauth的token 用户token
- *
- * @return mixed
- */
- public function oauth_api()
- {
- $this->wechat->oauth();
- }
-
- /**
- * 业务推送接口 微信服务器会将所有请求都推送到这里
- *
- * @return mixed
- */
- public function main()
- {
- $this->output = '';
-
- # 获取微信消息
- $this->request();
-
- # 对消息进行验证,并根据类型得到本平台的配置数据
- //$this->response();
-
- # 输出
- $this->output();
- }
-
- /**
- * 输出
- *
- * @return mixed
- */
- private function output()
- {
- if (!$this->output) {
- $this->output = 'success';
- }
- echo $this->output;die;
- }
- /**
- * 获取微信发送的内容
- *
- * @return mixed
- */
- private function request()
- {
- //$post = $_GET ? $_GET : $_POST;
- $data = array();
- $data['signature'] = '432ec06aa3a0f680924c9587ea215b4e490382b3';
- $data['timestamp'] = '1525677152';
- $data['nonce'] = '377902872';
- $data['encrypt_type'] = 'aes';
- $data['msg_signature'] = '7f62174a1402d955c2d8e61f69a309540714c579';
- $xml = file_get_contents('php://input');
- Dever::log(json_encode($_GET) . $xml);
- $xml = "<xml>
- <AppId><![CDATA[wxc50846069a0ae2d2]]></AppId>
- <Encrypt><![CDATA[SYTZJTU4ZWOKAWhjWYgVEZQMRplYNo1ZuN5dL3hQoQmbLbYM0O5Iaw/FqpigtOAyIL3dIgoA1ti91C0Z45/qgl9w4Fxa1yi94tugaWQfq5pQGbRwBte5uR3okaFxVYW0ZQd3k0HWuMgAvt/pDoMn5hLpvxhfEKX4ZVvkY4fcgb0D4bz1xba0DCdpVqHmzk1ewbD01LeUNw9JQSSBiFX24x7JruXSvsXMn0gKfYUR1s5vavnoVz6CeyUYzW63Id+b4gxKDnBGAfsBQWFdtgoJg9Ze6AN2EBGmbltx8qr7L+UKCrExgpdBRyG2vDu4QQvk+lp/jjbao721zQGpgFqr1hIJjSiXX6M8my7l3wI+nHJpdF3PpCsEiml2zcFGDrd7PSS1A8PAYrDG8W84NNpK4Onz2ZxrRI/BusI999TiS/I88fT5XM4qMo2j2F488vvSRHdJxo7C+3V+rQFkBXSbVA==]]></Encrypt>
- </xml>";
- if (!empty($xml)) {
- libxml_disable_entity_loader(true);
- $result = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- $result = $this->wechat->decode($data['msg_signature'], $data['timestamp'], $data['nonce'], $result['Encrypt']);
- if (isset($result['ComponentVerifyTicket'])) {
- $this->wechat->ticket($result['ComponentVerifyTicket'], 600, 100);
- }
- return true;
- } else {
- return false;
- }
- }
- }
|