123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | auth.php 用于做权限验证
- |--------------------------------------------------------------------------
- */
- namespace Component\Src;
- use Dever;
- use Main\Lib\Wechat;
- class Auth
- {
- /**
- * wechat
- *
- * @var Wechat
- */
- private $wechat;
-
- /**
- * result
- *
- * @var array
- */
- private $result;
-
- /**
- * output
- *
- * @var string
- */
- private $output;
-
- /**
- * 构造函数 初始化
- *
- * @return mixed
- */
- public function __construct()
- {
- $this->wechat = new Wechat();
- }
- /**
- * 获取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()
- {
- $param['auth_code'] = Dever::input('auth_code');
- $param['expires_in'] = Dever::input('expires_in');
- if ($param['auth_code']) {
- $data = $this->wechat->oauth($param);
- } else {
- $param = Dever::input('id', 1);
- $data = $this->wechat->oauth($param, 2000, true);
- }
- if ($data && isset($data['openid'])) {
- $result = $this->wechat->curl('user', $data);
- if ($result) {
- # 初始化操作
- $param = array();
- $qrcode = Dever::load('upload/save.copy', $result['authorizer_info']['qrcode_url']);
- $param['project_id'] = $data['project_id'];
- $param['oauth_id'] = $data['id'];
- $param['name'] = $result['authorizer_info']['nick_name'];
- $param['principal_name'] = $result['authorizer_info']['principal_name'];
- $param['pic'] = $result['authorizer_info']['head_img'];
- $param['qrcode'] = $qrcode['url'];
- $param['username'] = $result['authorizer_info']['user_name'];
- //$param['desc'] = '1';
- $param['mdate'] = time();
-
- if (isset($result['authorizer_info']['MiniProgramInfo'])) {
- $param['type'] = 1;
- } else {
- if ($result['authorizer_info']['service_type_info'] == 2) {
- $param['type'] = 3;
- } else {
- $param['type'] = 2;
- }
- }
- $id = Dever::upinto('component/user', array('option_oauth_id' => $data['id']), $param);
-
- $domain = Dever::db('component/domain')->one(array('option_project_id' => $param['project_id']));
- //自动设置域名
- if ($domain) {
- $param['id'] = $id;
- Dever::load('component/domain.set', $param, $domain['id']);
- }
-
- $tester = Dever::db('component/domain')->state(array('option_project_id' => $param['project_id']));
- if ($tester) {
- $param['id'] = $id;
- foreach ($tester as $k => $v) {
- //自动设置体验者
- Dever::load('component/tester.set', $param, $v['id']);
- }
- }
- }
- }
- return '绑定成功';
- }
- /**
- * wechat回调,直接将数据回写到该方法中
- *
- * @return mixed
- */
- public function saveOauthInfo($oauth_id, $data, $project_id)
- {
- echo 1;
- # 这里用于记录权限集func_info,先不记录了
- return;
- }
-
- /**
- * 业务推送接口 微信服务器会将所有请求都推送到这里
- *
- * @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()
- {
- $data = $_GET ? $_GET : $_POST;
- /*
- $data = array();
- $data['signature'] = '9474754223130bc1c820537b8113b590ef3cbf8e';
- $data['timestamp'] = '1525915492';
- $data['nonce'] = '1211005377';
- $data['encrypt_type'] = 'aes';
- $data['msg_signature'] = 'f17b5c2d2cf1592e07803f190048289ff468ef2b';
- */
- $xml = file_get_contents('php://input');
- Dever::log(json_encode($data) . $xml);
- /*
- $xml = "<xml>
- <AppId><![CDATA[wxc50846069a0ae2d2]]></AppId>
- <Encrypt><![CDATA[w19P5s2RRaWRUt+0Af0qSyBbTay3JdxDuYQxexTgm0PGE0U2UYknJKuH05GdaZPlruuTcbETR1MOfUGLmIniNlJ7UJ9GKgQWAnzVU0gezeolmnwA683fcGZpuH9yhSZHiSHoSbJaLpLZAl3ywu3FCMcpq6vhVVN/d/cQOUEgmS+rj06DOU105tBuKweOcD9LEGyoFPDUWRfvG4uGT4vS7C3hiR7UQgKYCGhSxBF56umkYHiGwpo3TNQ0QTFzIN/o6V43KvyFyIOfbvmQn9a3T9heDbijiJgcG+jpj/b9Vsps7jV5yePaJTnBjUWVbM7aW/HHlXWuYu+vagwG+shaMSGQHxCzdgjxiVYu2uR+M4kHYl90EUSdEJrcyQrFtia9BOwtkLkBwziZgogaVFXgLvSlN+krS3L0jHPVedevEgNubAM292jd/BPsU8Pd3o+EBZBG7qOU4IgHE6eVc09AxA==]]></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;
- }
- }
- }
|