Files
spacestation/admin/controllers/Login.php
T
小鱼开发 f8990ce8fd hdy
2025-07-26 16:52:42 +08:00

239 lines
9.3 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: linfan
* Date: 2018/11/12
* Time: 09:24
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller
{
public function index()
{
if ($cookie = $this->input->cookie(LOGIN_COOKIE)) {
header('Location:/welcome');
}
$arr = array();
if ($this->input->get('need_code') == 'logsms'){
$arr['need_code'] = 'logsms';
}
$this->load->view('login', $arr);
}
public function post()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->library('encryption');
$check_view = false;
$ip = get_client_ip();
$ip_arr = array();
if ($this->input->post('need_code') == 'get') {
return false;
}
$log = 0;//加日志
if ($this->input->post('need_code') == 'login' || $this->input->post('need_code') == 'logsms') {
$code = $this->input->post('code');
$mobile = $this->input->post('mobile');
$this->input->post('need_code') == 'logsms' && $mobile = $this->input->post('number');
$cookie = $this->input->cookie(LOGIN_CODE_COOKIE);
$code_cookie = $this->encryption->decrypt($cookie);
if (!$code_cookie) {
return $this->show_json(0, '验证码已过期');
}
if ($code_cookie != $code) {
return $this->show_json(0, '验证码错误');
}
$this->load->model('sys/sys_admin_model');
$admin_user = $this->sys_admin_model->get(array("mobile = '{$mobile}'" => null, 'status' => 1));
if (!$admin_user['id']) {
return $this->show_json(0, '用户不存在');
}
$log = 1;
} else {
if (!$username) {
return $this->show_json(0, '请输入用户名/手机号');
}
if (!$password) {
return $this->show_json(0, '请输入密码');
}
$this->load->model('sys/sys_admin_model');
$admin_user = $this->sys_admin_model->get(array("username like '{$username}' OR mobile like '{$username}'" => null, 'status' => 1));
if (!$admin_user['id']) {
return $this->show_json(0, '用户不存在');
} elseif (!$admin_user['status']) {
return $this->show_json(0, '用户状态异常');
} elseif (!password_verify($password, $admin_user['password'])) {
return $this->show_json(0, '密码错误');
} else {
if (false !== strpos($_SERVER['HTTP_HOST'], "admin.dev.liche.cn") || false !== strpos($_SERVER['HTTP_HOST'], "admin.lc.haodian.cn")) {
//开发测试不校验IP
} elseif (filter_var($ip, FILTER_VALIDATE_IP)) {
// $this->load->model('sys/sys_config_model');
// $config_ip = $this->sys_config_model->select(array("v LIKE '%\"status\":\"1\"%'" => NULL, "k" => "site"));
// foreach ($config_ip as $key => $value) {
// $ip_arr[] = json_decode($value['v'])->ip;
// }
// $ip_arr = array_merge($ip_arr, array_column($this->sys_admin_model->select(array(), '', '', '', 'login_ip'), 'login_ip'));
// if (!in_array($ip, $ip_arr)) {
// $check_view = true;
// $log = 1;
// }
}
}
}
$admin_info = array(
'id' => $admin_user['id'],
'username' => $admin_user['username'],
'role_id' => $admin_user['role_id'],
'login_ip' => $ip,
);
$http_host = explode(':', $_SERVER['HTTP_HOST']);
$domain = $http_host[0];
// $domain = explode('.', $http_host[0]);
// array_shift($domain);
// $domain = implode('.', $domain);
$this->input->set_cookie(LOGIN_COOKIE, $this->encryption->encrypt(json_encode($admin_info)), 86400 * 30, $domain);
if ($log == 1) {
$this->load->model('sys/sys_admin_log_model');
$log = array(
'admin_id' => $admin_user['id'],
'username' => $admin_user['username'],
'target_id' => 0,
'descrip' => '外网IP登录',
'action' => '',
'ip' => $ip,
'jsondata' => json_encode($this->input->post(), JSON_UNESCAPED_UNICODE),
);
$this->sys_admin_log_model->add($log);
}
if ($check_view && $admin_user['id'] > 10) {//管理员id>10去掉验证码登录
return $this->show_json(SYS_CODE_SUCCESS, '访问IP发生变化,需要手机验证登录', '/login/check_view');
} else {
$this->sys_admin_model->update(array('login_ip' => $ip), array('id' => $admin_user['id']));
return $this->show_json(SYS_CODE_SUCCESS, ' 登 录 成 功', '/');
}
}
//获取验证码
public function get_code()
{
if ($this->input->method() == 'post') {
$mobile = $this->input->post('mobile', true);
if (!$mobile){
return false;
}
$this->load->model('sys/sys_admin_model');
$admin_user = $this->sys_admin_model->get(array("mobile = '{$mobile}'" => null, 'status' => 1));
if (!$admin_user['id']) {
return false;
}
$code = rand(100000, 999999);
$domain = explode('.', $_SERVER['HTTP_HOST']);
array_shift($domain);
$domain = implode('.', $domain);
$this->input->set_cookie(LOGIN_CODE_COOKIE, $this->encryption->encrypt(json_encode($code)), 60 * 5, $domain);
// send_sms($mobile, $code);
$content = "【好店云】" . "您的验证码为:{$code},请勿泄露于他人!";
b2m_send_sms($mobile,$content);
}
}
public function check_view()
{
$this->load->library('encryption');
$this->load->model('sys/sys_admin_model');
$info = json_decode($this->encryption->decrypt($this->input->cookie(LOGIN_COOKIE)), true);
$admin_user = $this->sys_admin_model->get(array('id' => $info['id']));
$domain = explode('.', $_SERVER['HTTP_HOST']);
array_shift($domain);
$domain = implode('.', $domain);
$this->input->set_cookie(LOGIN_COOKIE, '', 0, $domain);
$this->input->set_cookie(LOGIN_CODE_COOKIE, '', 0, $domain);
if ($admin_user) {
$arr = array(
'mobile' => $admin_user['mobile'],
'need_code' => true
);
$this->load->view('login', $arr);
} else {
header('Location:/login');
}
$this->load->view('login', $arr);
}
//重置密码
public function reset_pwd()
{
if ($this->input->method() == 'post') {
$password = $this->input->post('password', true);
$confirm_password = $this->input->post('confirm_password', true);
$old_password = $this->input->post('old_password', true);
if (!$old_password) {
return $this->show_json(SYS_CODE_FAIL, '请输入旧密码');
}
if (!$password || mb_strlen($password) < 6) {
return $this->show_json(SYS_CODE_FAIL, '请输入6个字符的新密码');
}
if (!$confirm_password) {
return $this->show_json(SYS_CODE_FAIL, '请输入确认密码');
}
if ($password != $confirm_password) {
return $this->show_json(SYS_CODE_FAIL, '新密码和确认密码不一致');
}
$this->load->model('sys/sys_admin_model');
$admin_user = $this->sys_admin_model->get(array("id" => $this->uid));
if (!password_verify($old_password, $admin_user['password'])) {
return $this->show_json(SYS_CODE_FAIL, '旧密码不正确');
}
$data['password'] = password_hash($password, PASSWORD_BCRYPT);
$this->sys_admin_model->update($data, array('id' => $this->uid));
return $this->show_json(SYS_CODE_SUCCESS, '保存成功', array(), '/login/logout', 5000);
}
$this->load->view('reset');
}
//退出
public function logout()
{
// $domain = explode('.', $_SERVER['HTTP_HOST']);
// array_shift($domain);
// $domain = implode('.', $domain);
$http_host = explode(':', $_SERVER['HTTP_HOST']);
$domain = $http_host[0];
$this->input->set_cookie(LOGIN_COOKIE, '', 0, $domain);
return $this->show_json(SYS_CODE_SUCCESS, '注销成功', '/login');
}
//返回json数据
protected function show_json($code = 0, $msg = 'success', $url = '', $wait = 0)
{
header('Content-Type:application/json; charset=utf-8');
echo json_encode(array('data' => $this->data, 'code' => $code, 'msg' => $msg, 'url' => $url, 'wait' => $wait), JSON_UNESCAPED_UNICODE);
return false;
}
}