Passport.php
2.5 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace app\web\controller;
use think\Validate;
use think\Config;
use think\Session;
use app\web\model\User as UserModel;
use app\common\service\Render as RenderTrait;
/**
* 登录认证
* Class Passport
* @package app\web\controller
*/
class Passport extends Controller
{
use RenderTrait;
/**
* 后台登录
* @return array|mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function login()
{
if ($this->request->isAjax()) {
return $this->renderError('登录失败');
}
$this->view->engine->layout(false);
return $this->fetch('login');
}
/**
* 退出登录
*/
public function logout()
{
Session::clear();
$this->redirect('passport/login');
}
/**
* 处理登录请求
*/
public function loginHandle()
{
$data = $this->request->post('data');
$data = json_decode($data, true);
$validate = new Validate([
'username'=> 'require',
'password'=> 'require',
'captcha'=> 'require',
], [
'username.require' => '账号不能为空',
'password.require' => '密码不能为空',
'captcha.require' => '验证码不能为空',
]);
if (!$validate->check($data)) {
$errmsg = $validate->getError();
return json($this->renderError($errmsg));
}
if (!captcha_check($data['captcha'])){
return json($this->renderError('验证码错误'));
}
$password = md5(Config::get('password_salt').$data['password']);
$user = UserModel::where('login_account' , $data['username'])->where('password' , $password)->find();
if (!$user) {
return json($this->renderError('账号或密码错误'));
}
if ($user->invalid_flag != 1) {
return json($this->renderError('账号暂不可用'));
}
session('user', $user->toArray());
// 获取组织结构信息
$organization = new \app\web\controller\Organization();
$organization_info = $organization->getDetail(['org_code' => $user->org_code]);
if ($organization_info['code'] == 0) {
session('organization', $organization_info['data']);
}
return json($this->renderSuccess(['url' => url('web/index/index')]));
}
}