Issues (27)

example/oauth2.php (2 issues)

1
<?php
2
use tinymeng\tools\Tool;
3
use tinymeng\OAuth2\OAuth;
0 ignored issues
show
This use statement conflicts with another class in this namespace, OAuth. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
5
/**
6
 *
7
 */
8
class oauth2{
9
10
    /**
11
     * 全部配置文件
12
     * @var
13
     */
14
    public $configAll;
15
    /**
16
     * 单类型配置文件
17
     * @var
18
     */
19
    public $config;
20
    /**
21
     * 登录类型
22
     * @var
23
     */
24
    public $name;
25
26
    /**
27
     * @param $conf
28
     */
29
    public function __construct($conf){
30
        $this->configAll = $conf;
31
    }
32
33
34
    /**
35
     * Description:  获取配置文件
36
     * @author: JiaMeng <[email protected]>
37
     * Updater:
38
     * @param $name
39
     */
40
    public function getConfig($name){
41
        $this->config = $this->configAll[$name]??[];
42
        if($name == 'wechat'){
43
            if(!Tool::isMobile()){
44
                $this->config = $this->config['pc'];//微信pc扫码登录
45
            }elseif(Tool::isWeiXin()){
46
                $this->config = $this->config['mobile'];//微信浏览器中打开
47
            }else{
48
                echo '请使用微信打开!';exit();//手机浏览器打开
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
49
            }
50
        }
51
        //可以设置代理服务器,一般用于调试国外平台
52
        //$this->config['proxy'] = 'http://127.0.0.1:1080';
53
    }
54
55
    /**
56
     * Description:  登录链接分配,执行跳转操作
57
     * Author: JiaMeng <[email protected]>
58
     * Updater:
59
     */
60
    public function login($name,$state=""){
61
        /** 获取配置 */
62
        if($name == 'wx') $name = 'wechat';
63
        $this->getConfig($name);
64
        $this->config['state'] = $state;
65
66
        /** 初始化实例类 */
67
        $oauth = OAuth::$name($this->config);
68
        if(Tool::isMobile() || Tool::isWeiXin()){
69
            /**
70
             * 对于微博,如果登录界面要适用于手机,则需要设定->setDisplay('mobile')
71
             * 对于微信,如果是公众号登录,则需要设定->setDisplay('mobile'),否则是WEB网站扫码登录
72
             * 其他登录渠道的这个设置没有任何影响,为了统一,可以都写上
73
             */
74
            $oauth->setDisplay('mobile');
75
        }
76
77
        $login_url = $oauth->getRedirectUrl();
78
        $result = [
79
            'code'=>0,
80
            'msg'=>'succ',
81
            'type'=>$name,
82
            'url'=>$login_url
83
        ];
84
        return $result;
85
    }
86
87
    /**
88
     * Description:  登录回调
89
     * @author: JiaMeng <[email protected]>
90
     * Updater:
91
     * @param $name
92
     * @return array
93
     */
94
    public function callback($name)
95
    {
96
        /** 获取配置 */
97
        if($name == 'wx') $name = 'wechat';
98
        $this->getConfig($name);
99
100
        /** 初始化实例类 */
101
        $oauth = OAuth::$name($this->config);
102
103
        /** 获取第三方用户信息 */
104
        $userInfo = $oauth->userInfo();
105
        /**
106
         * 如果是App登录
107
         * $type = "applets";
108
         * $userInfo = OAuth::$name($this->config)->setType($type)->userInfo();
109
         */
110
        /**
111
         * 如果是App登录
112
         * $type = "applets";
113
         * $userInfo = OAuth::$name($this->config)->setType($type)->userInfo();
114
         */
115
116
        // 获取登录类型
117
        $userInfo['type'] = \tinymeng\OAuth2\Helper\ConstCode::getTypeConst($userInfo['channel']);
118
119
        // 处理用户信息
120
        $userInfo = $this->handleUserInfo($userInfo);
121
        $result = [
122
            'code'=>0,
123
            'msg'=>'succ',
124
            'type'=>$name,
125
            'userInfo'=>$userInfo
126
        ];
127
        return $result;
128
    }
129
130
    /**
131
     * 处理用户信息
132
     * @param $userInfo
133
     * @return mixed
134
     */
135
    public function handleUserInfo($userInfo)
136
    {
137
        /**
138
         * TODO... 存储用户信息到数据库,请自行完善代码
139
         */
140
        return $userInfo;
141
    }
142
143
}