Login.php
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace app\api\controller;
use addons\third\controller\Api;
/**
* 微信登录
* @package app\api\controller
*/
class Login extends BaseApi
{
protected $noNeedLogin = ['getLoginUrl','third'];
protected $noNeedRight = '*';
/**
* 获取会员登录地址
* @ApiTitle (获取会员登录地址)
* @ApiMethod (POST)
* @ApiRoute (/api/login/getLoginUrl)
* @ApiParams (name="url", type="string", required=true, description="前端回调路径")
* @ApiParams (name="platform", type="string", required=true, description="请填写:wechat")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function getLoginUrl(){
(new Api())->getAuthUrl();
}
/**
* 第三方登录
* @ApiTitle (第三方登录)
* @ApiMethod (POST)
* @ApiRoute (/api/login/third)
* @ApiParams (name="platform", type="string", required=true, description="平台名称:wechat")
* @ApiParams (name="code", type="string", required=true, description="Code码")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function third()
{
$platform = $this->request->request("platform");
$code = $this->request->request("code");
$config = get_addon_config('third');
if (!$config || !isset($config[$platform])) {
$this->error(__('Invalid parameters'));
}
$app = new \addons\third\library\Application($config);
//通过code换access_token和绑定会员
session('state','STATE');
$result = $app->{$platform}->getUserInfo(['state'=>'STATE','code' => $code]);
if ($result) {
$loginret = \addons\third\library\Service::connect($platform, $result);
if ($loginret) {
$data = [
'token' => $this->auth->getToken(),
'userinfo' => $this->auth->getUserinfo(),
'thirdinfo' => $result,
];
$this->success(__('Logged in successful'), $data);
}
}
$this->error(__('Operation failed'));
}
}