/
LoginController.php
executable file
·127 lines (121 loc) · 4.88 KB
/
LoginController.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace App\Http\Controllers;
use App\Api\ApiUser\ApiCompany;
use App\Api\ApiUser\ApiLog;
use App\Api\ApiUser\ApiPerson;
use App\Api\ApiUser\ApiUsers;
use App\Tools;
use Session;
use Hash;
use Illuminate\Support\Facades\Input;
use Validator;
class LoginController extends Controller
{
/**
* 会员注册页面
* 支持手机、邮箱、用户名
*/
public function index()
{
return view('loginOrRegist.login');
}
public function dologin()
{
// //查看2次密码输入是否一致
// if (Input::get('password')!=Input::get('password2')) {
// echo "<script>alert('2次密码输入不一致!');history.go(-1);</script>";exit;
// }
//验证码验证
$rules = [
'captcha' => 'required|captcha',
];
$messages = [
'captcha.required' => '请输入验证码',
'captcha.captcha' => '验证码错误,请重试',
];
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails()) {
echo "<script>alert('验证码错误!');history.go(-1);</script>";exit;
}
//接口验证数据,写入用户表,或者返回错误信息
$ip = Tools::getIp();
$data = [
'username'=> Input::get('username'),
'password'=> Hash::make(Input::get('password')),
'pwd'=> Input::get('password'),
'ip'=> $ip,
//以下用户日志用
'ipaddress'=> Tools::getCityByIp($ip),
'genre'=> 1, //1代表用户,2代表管理员
'action'=> $_SERVER['REQUEST_URI'],
];
$rstUser = ApiUsers::getOneUserByUname(Input::get('username'));
if ($rstUser['code']!=0) {
echo "<script>alert('".$rstUser['msg']."');history.go(-1);</script>";exit;
}
//验证密码正确否
if (!(Hash::check(Input::get('password'),$rstUser['password']))) {
echo "<script>alert('密码错误!');history.go(-1);</script>";exit;
}
//个人资料
if (in_array($rstUser['data']['isuser'],[1,2,4,50])) {
$personInfo = ApiPerson::getPersonInfo($rstUser['data']['id']);
if ($personInfo['code'] != 0) {
$person = array();
} else {
$person['per_id'] = $personInfo['data']['id'];
$person['realname'] = $personInfo['data']['realname'];
$person['sex'] = $personInfo['data']['sex'];
$person['idcard'] = $personInfo['data']['idcard'];
$person['idfront'] = $personInfo['data']['idfront'];
}
}
//企业资料
if (in_array($rstUser['data']['isuser'],[3,5,6,7,50])) {
$companyInfo = ApiCompany::getOneCompany($rstUser['data']['id']);
if ($companyInfo['code'] != 0) {
$company = array();
} else {
$company['cid'] = $companyInfo['data']['id'];
$company['name'] = $companyInfo['data']['name'];
$company['area'] = $companyInfo['data']['area'];
$company['address'] = $companyInfo['data']['address'];
$company['yyzzid'] = $companyInfo['data']['yyzzid'];
$company['logo'] = $companyInfo['data']['logo'];
$company['skin'] = $companyInfo['data']['skin'];
$company['layout'] = $companyInfo['data']['layout'] ? unserialize($companyInfo['data']['layout']) : [];
}
}
$serial = date('YmdHis',time()).rand(0,10000);
$userInfo = [
'uid' => $rstUser['data']['id'],
'username' => Input::get('username'),
'email' => $rstUser['data']['email'],
'userType' => $rstUser['data']['isuser'],
'serial' => $serial,
'area' => $rstUser['data']['area'],
'address' => $rstUser['data']['address'],
'cid' => isset($companyInfo['data'])?$companyInfo['data']['id']:0,
'loginTime' => time(),
'person' => isset($person) ? $person : [],
'company' => isset($company) ? $company : [],
];
$userInfo['cookie'] = $_COOKIE;
Session::put('user',$userInfo);
//将session放入redis
\Redis::setex('cul_session', $this->redisTime, serialize($userInfo));
return redirect(DOMAIN.'member');
}
public function dologout()
{
//更新用户日志表
$rstLog = ApiLog::logout(Session::get('user.serial'));
if ($rstLog['code']!=0) {
echo "<script>alert('".$rstLog['msg']."');history.go(-1);</script>";exit;
}
//去除session
Session::forget('user');
\Redis::del('cul_session');
return redirect(DOMAIN.'login');
}
}