123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace huolib\oauth;
- use think\Exception;
- use think\Session;
- abstract class OAuth {
-
- protected $version = '2.0';
-
- protected $app_key = '';
-
- protected $app_secret = '';
-
- protected $response_type = 'code';
-
- protected $grant_type = 'authorization_code';
-
- protected $callback = '';
-
- protected $authorize = '';
-
- protected $request_code_url = '';
-
- protected $access_token_url = '';
-
- protected $refresh_token_url = '';
-
- protected $api_base = '';
-
- protected $token = ['openid' => '', 'access_token' => '', 'expires_in' => 0];
-
- private $type = '';
- protected $state = '';
-
- public function __construct($config = [], $token = null) {
-
- $class = get_class($this);
- $this->type = strtoupper(substr($class, 0, strlen($class) - 3));
-
- if (empty($config['APP_KEY']) || empty($config['APP_SECRET'])) {
- throw new Exception('请配置您申请的APP_KEY和APP_SECRET');
- } else {
- $this->app_key = $config['APP_KEY'];
- if (!empty($token['oauth_app_key'])) {
- $this->app_key = $token['oauth_app_key'];
- }
- $this->app_secret = $config['APP_SECRET'];
- $this->callback = $config['CALLBACK'];
- $this->token = $token;
- }
- }
-
- public static function ins($type, $config = [], $token = null) {
- $_name = ucfirst(strtolower($type));
- $_class = '\\huolib\\oauth\\driver\\'.$_name;
- if (class_exists($_class)) {
- return new $_class($config, $token);
- } else {
- throw new Exception(lang('_CLASS_NOT_EXIST_').':'.$_name);
- }
- }
-
- public function getState() {
- if (empty($this->state)) {
- $this->state = time();
- }
- return $this->state;
- }
- public function setState() {
- $this->state = md5(uniqid(rand(), true));
- Session::set('oauth.state', $this->state);
- }
- public function compareState($state) {
-
- $_old_state = Session::get('oauth.state');
- if ($_old_state != $state) {
- return false;
- }
- return true;
- }
- public function getToken() {
- return $this->token['expires_in'];
- }
-
- public function getCallback() {
- return $this->callback;
- }
-
- public function setCallback($callback) {
- $this->callback = $callback;
- }
-
- protected function url($api, $fix = '') {
- return $this->api_base.$api.$fix;
- }
-
- abstract public function getRequestCodeUrl($display = 'pc');
-
- abstract public function getAccessToken($code, $extend = null);
-
- abstract protected function call($api, $param = '', $method = 'GET', $multi = false);
-
- abstract protected function parseToken($result, $extend);
-
- abstract public function getOpenid();
- }
|