|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* 第三方登陆实例抽象类 |
|
4
|
|
|
* @author: JiaMeng <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace tinymeng\OAuth2; |
|
7
|
|
|
|
|
8
|
|
|
use tinymeng\OAuth2\Connector\GatewayInterface; |
|
9
|
|
|
use tinymeng\OAuth2\Exception\OAuthException; |
|
10
|
|
|
use tinymeng\OAuth2\Helper\Str; |
|
11
|
|
|
/** |
|
12
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Alipay Alipay(array $config) 阿里云 |
|
13
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Wechat wechat(array $config) 微信 |
|
14
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Wechat weixin(array $config) 微信 |
|
15
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Qq Qq(array $config) QQ |
|
16
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Facebook Facebook(array $config) Facebook |
|
17
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Github Github(array $config) Github |
|
18
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Google Google(array $config) Google |
|
19
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Line Line(array $config) Line |
|
20
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Sina Sina(array $config) Sina |
|
21
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Twitter Twitter(array $config) Twitter |
|
22
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Douyin Douyin(array $config) 抖音 |
|
23
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Baidu Baidu(array $config) 百度 |
|
24
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Coding Coding(array $config) Coding |
|
25
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Csdn Csdn(array $config) CSDN |
|
26
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Gitee Gitee(array $config) Gitee |
|
27
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Gitlab GitLab(array $config) GitLab |
|
28
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Oschina OSChina(array $config) OSChina |
|
29
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Wecom Wecom(array $config) 企业微信 |
|
30
|
|
|
* @method static \tinymeng\OAuth2\Gateways\Kuaishou Kuaishou(array $config) 快手 |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class OAuth |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Description: init |
|
37
|
|
|
* @author: JiaMeng <[email protected]> |
|
38
|
|
|
* Updater: |
|
39
|
|
|
* @param $gateway |
|
40
|
|
|
* @param null $config |
|
|
|
|
|
|
41
|
|
|
* @return mixed |
|
42
|
|
|
* @throws OAuthException |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static function init($gateway, $config) |
|
45
|
|
|
{ |
|
46
|
|
|
if(empty($config)){ |
|
47
|
|
|
throw new OAuthException("第三方登录 [$gateway] config配置不能为空"); |
|
48
|
|
|
} |
|
49
|
|
|
$baseConfig = [ |
|
50
|
|
|
'app_id' => '', |
|
51
|
|
|
'app_secret'=> '', |
|
52
|
|
|
'callback' => '', |
|
53
|
|
|
'scope' => '', |
|
54
|
|
|
'type' => '', |
|
55
|
|
|
]; |
|
56
|
|
|
if($gateway == 'weixin'){ |
|
57
|
|
|
/** 兼容 tinymeng/oauth v1.0.0完美升级 */ |
|
58
|
|
|
$gateway = 'wechat'; |
|
59
|
|
|
} |
|
60
|
|
|
$gateway = Str::uFirst($gateway); |
|
61
|
|
|
$class = __NAMESPACE__ . '\\Gateways\\' . $gateway; |
|
62
|
|
|
if (class_exists($class)) { |
|
63
|
|
|
$app = new $class(array_replace_recursive($baseConfig,$config)); |
|
64
|
|
|
if ($app instanceof GatewayInterface) { |
|
65
|
|
|
return $app; |
|
66
|
|
|
} |
|
67
|
|
|
throw new OAuthException("第三方登录基类 [$gateway] 必须继承抽象类 [GatewayInterface]"); |
|
68
|
|
|
} |
|
69
|
|
|
throw new OAuthException("第三方登录基类 [$gateway] 不存在"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Description: __callStatic |
|
74
|
|
|
* @author: JiaMeng <[email protected]> |
|
75
|
|
|
* Updater: |
|
76
|
|
|
* @param $gateway |
|
77
|
|
|
* @param $config |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function __callStatic($gateway, $config) |
|
81
|
|
|
{ |
|
82
|
|
|
return self::init($gateway, ...$config); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|