一、登录成功发送邮件
在根目录下app文件夹下controller文件夹下common文件夹下,修改Login.php,代码如下
<?php
/*** 登录退出操作* User: 龙哥·三年风水* Date: 2024/10/29* Time: 15:53*/
namespace app\controller\common;
use app\controller\Emptys;
use app\model\permission\Admin;
use app\model\param\System as SystemModel;
use Email\EmailSender;
use Redis\Redis;
use app\model\common\Token as TokenModel;
use Encipher\Encrypt;
class Login extends Emptys
{//验证码展示public function getCode(){$re = app()->make(\Other\Captcha::class)->create();$base64 = 'data:image/png;base64,' . base64_encode($re->getData());return succ('SUCCESS',$base64);}//登录public function doLogin(){$param = $this->request->param();$validate = new \app\validate\common\Login;$result = $validate->check($param);//参数验证if (!$result) return err($validate->getError());//验证码的正确性if (!app()->make(\Other\Captcha::class)->check($param['captcha_code'])) return err('验证码错误');if (time() + 5 < strtotime($param['login_time'])) return err('非法登录');$resData = Admin::dataFind(['email' => trim($param['username'])], 'id,realname,password,ip,status', true);//用户信息的正确性if (empty($resData) && empty($resData['id'])) return err('用户不存在');if ($resData['status'] !== 1) return err('该账号已被禁用');$systemParam = SystemModel::dataFind(['id' => 1],'security_password,platform_token_expira');if ($resData['password'] !== sha1($param['password'] . $systemParam['security_password'])) return err('账号对应的密码错误');if(!empty($resData['ip'])){if($resData['ip'] != $this->request->header('x-real-ip'))return err('禁止访问,不在IP白名单中');}//写入Token日志$data['token_type'] = 1;$data['menu_name'] = 'CommonLoginDoLogin';$data['admin_id'] = $resData['id'];$data['random_number'] = alnum();$data['create_time'] = date('Y-m-d',strtotime($param['login_time']));$data['login_time'] = $param['login_time'];$data['expire_time'] = strtotime($param['login_time']) + $systemParam['platform_token_expira'];$token = $data['admin_id'] . $data['random_number'];$data['token'] = sha1(sha1($token) . strtotime($data['login_time']));TokenModel::save($data,[]);//加入跨站攻击验证队列Redis::select(config('cache.stores.redis.token_db'))->setex('token_' . $data['token'],$systemParam['platform_token_expira'],$resData['id']);Redis::select(config('cache.stores.redis.token_db'))->setex('token_' . $resData['id'],$systemParam['platform_token_expira'],Encrypt::encryptRsa($data['token']));$emailSender = new EmailSender();$emailSender::send($param['username'],'登录系统',$resData['realname'].'于'.$param['login_time'].'登录系统');return succ('登录成功',Encrypt::encryptRsa($token));}
}
二、退出成功发送邮件
1、更改前端apijs文件
在根目录下src文件夹下api文件夹下,修改common.js文件,代码如下:
// 退出
export function logout() {return request({url: '/permission/member/logout',method: 'post'})
}
2、后端api更改路由
在根目录下route文件夹下,修改app.php文件,代码如下:
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;Route::get('think', function () {return 'hello,ThinkPHP6!';
});
Route::post('index/index','Index/index');// 测试使用
Route::post('index/generate_rsa','Index/generateRsa');// 生成rsa公私钥文件使用
// 特殊操作
Route::get('login/get_code', 'common.Login/getCode');// 获取验证码使用
Route::post('login/do_login', 'common.Login/doLogin');// 登录提交接口
Route::post('upload/file','common.Upload/file');// 上传文件接口
Route::post('publics/get_info','common.Publics/getInfo');// 获取公有数据接口
Route::group('permission',function (){// 个人资料Route::post('member/personal_data','permission.Member/personalData');// 获取个人信息接口Route::post('member/personal_menu','permission.Member/personalMenu');// 获取个人菜单接口Route::post('member/update_personal','permission.Member/updatePersonal');// 修改个人信息接口Route::post('member/logout', 'permission.Member/logout');// 退出登录接口// 管理员操作Route::get('admin/get_list','permission.Admin/getList');// 获取管理员列表Route::post('admin/get_all','permission.Admin/getAll');// 获取管理员所有数据Route::post('admin/get_info','permission.Admin/getInfo');// 获取管理员单条数据Route::post('admin/save_info','permission.Admin/saveInfo');// 保存管理员数据Route::post('admin/status_info','permission.Admin/statusInfo');// 启禁用管理员Route::post('admin/delete_info','permission.Admin/deleteInfo');// 删除管理员// 角色操作Route::get('role/get_list','permission.Role/getList');// 获取角色列表Route::post('role/get_all','permission.Role/getAll');// 获取所有角色数据Route::post('role/get_info','permission.Role/getInfo');// 获取单个角色数据Route::post('role/save_info','permission.Role/saveInfo');// 保存角色数据Route::post('role/status_info','permission.Role/statusInfo');// 启禁用角色数据Route::post('role/delete_info','permission.Role/deleteInfo');// 删除角色数据// 菜单操作Route::post('menu/get_all','permission.Menu/getAll');// 获取所有菜单数据Route::post('menu/get_info','permission.Menu/getInfo');// 获取单个菜单数据Route::post('menu/save_info','permission.Menu/saveInfo');// 保存菜单数据Route::post('menu/status_info','permission.Menu/statusInfo');// 启禁用菜单数据Route::post('menu/delete_info','permission.Menu/deleteInfo');// 删除菜单数据});
Route::group('param',function (){//系统配置Route::post('system/get_info','param.System/getInfo');// 获取系统参数数据Route::post('system/save_info','param.System/saveInfo');// 保存系统参数数据//上传配置Route::post('upload/get_info','param.Upload/getInfo');// 获取上传参数数据Route::post('upload/save_info','param.Upload/saveInfo');// 保存上传参数数据//短信配置Route::post('channel_sms/get_info','param.ChannelSms/getInfo');// 获取邮箱短信参数数据Route::post('channel_sms/save_info','param.ChannelSms/saveInfo');// 保存邮箱短信参数数据//邮件配置Route::post('channel_email/get_info','param.ChannelEmail/getInfo');// 获取邮箱短信参数数据Route::post('channel_email/save_info','param.ChannelEmail/saveInfo');// 保存邮箱短信参数数据// 图片格式操作Route::get('image/get_list','param.Image/getList');// 获取图片格式列表Route::post('image/get_all','param.Image/getAll');// 获取所有图片格式数据Route::post('image/get_info','param.Image/getInfo');// 获取单个图片格式数据Route::post('image/save_info','param.Image/saveInfo');// 保存图片格式数据Route::post('image/status_info','param.Image/statusInfo');// 启禁用图片格式数据Route::post('image/delete_info','param.Image/deleteInfo');// 删除图片格式数据// 视频格式操作Route::get('video/get_list','param.Video/getList');// 获取视频格式列表Route::post('video/get_all','param.Video/getAll');// 获取所有视频格式数据Route::post('video/get_info','param.Video/getInfo');// 获取单个视频格式数据Route::post('video/save_info','param.Video/saveInfo');// 保存视频格式数据Route::post('video/status_info','param.Video/statusInfo');// 启禁用视频格式数据Route::post('video/delete_info','param.Video/deleteInfo');// 删除视频格式数据// 文件格式操作Route::get('filedoc/get_list','param.Filedoc/getList');// 获取文件格式列表Route::post('filedoc/get_all','param.Filedoc/getAll');// 获取所有文件格式数据Route::post('filedoc/get_info','param.Filedoc/getInfo');// 获取单个文件格式数据Route::post('filedoc/save_info','param.Filedoc/saveInfo');// 保存文件格式数据Route::post('filedoc/status_info','param.Filedoc/statusInfo');// 启禁用文件格式数据Route::post('filedoc/delete_info','param.Filedoc/deleteInfo');// 删除文件格式数据// 部门配置操作Route::get('department/get_list','param.Department/getList');// 获取部门配置列表Route::post('department/get_all','param.Department/getAll');// 获取所有部门配置数据Route::post('department/get_info','param.Department/getInfo');// 获取单个部门配置数据Route::post('department/save_info','param.Department/saveInfo');// 保存部门配置数据Route::post('department/status_info','param.Department/statusInfo');// 启禁用部门配置数据Route::post('department/delete_info','param.Department/deleteInfo');// 删除部门配置数据// 级别配置操作Route::get('grade/get_list','param.Grade/getList');// 获取级别配置列表Route::post('grade/get_all','param.Grade/getAll');// 获取所有级别配置数据Route::post('grade/get_info','param.Grade/getInfo');// 获取单个级别配置数据Route::post('grade/save_info','param.Grade/saveInfo');// 保存级别配置数据Route::post('grade/status_info','param.Grade/statusInfo');// 启禁用级别配置数据Route::post('grade/delete_info','param.Grade/deleteInfo');// 删除级别配置数据// 邮箱配置操作Route::get('sms/get_list','param.Sms/getList');// 获取短信配置列表Route::post('sms/get_all','param.Sms/getAll');// 获取所有短信配置数据Route::post('sms/get_info','param.Sms/getInfo');// 获取单个短信配置数据Route::post('sms/save_info','param.Sms/saveInfo');// 保存短信配置数据Route::post('sms/status_info','param.Sms/statusInfo');// 启禁用短信配置数据Route::post('sms/delete_info','param.Sms/deleteInfo');// 删除短信配置数据
});
Route::miss('Emptys/index');
3、添加退出方法
在根目录下app文件夹下controller文件夹下permission文件夹下,修改Member.php文件,代码如下:
<?php
/*** 个人信息控制-也就是登录者信息及角色对外方法* User: 龙哥·三年风水* Date: 2024/10/30* Time: 14:18*/
namespace app\controller\permission;
use app\controller\Base;
use app\model\param\Department as PDModel;
use app\model\param\Grade as PGModel;
use app\model\permission\Role as PRModel;
use app\model\param\System as PSModel;
use app\model\permission\Admin as PAModel;
use Other\Tree;
use Redis\Redis;
use app\model\permission\Menu as PMModel;
use Email\EmailSender;
use app\model\common\Token as TokenModel;
class Member extends Base
{/*** 获取个人信息* User: 龙哥·三年风水* Date: 2024/10/30* Time: 14:23* @ return \think\response\Json*/public function personalData(){$data = [];$data['username'] = $this->username;$data['avatar'] = $this->avatar;$data['email'] = $this->email;$data['realname'] = $this->realname;$data['ip'] = empty($this->ip) ? '' : $this->ip;$departmentTitle = PDModel::dataFind(['id' => $this->departmentId],'title',false);$data['department_title'] = empty($departmentTitle) ? '' : $departmentTitle;$gradeTitle = PGModel::dataFind(['id' => $this->gradeId],'title',false);$data['grade_title'] = empty($gradeTitle) ? '' : $gradeTitle;$data['rolename'] = PRModel::dataFind(['id' => $this->roleId],'rolename',false);$res = $this->getRoleMenu();$data['key'] = $res['key'];$data['butt'] = $res['butts'];return succ('操作成功',$data);}/*** 登录者菜单权限* User: 龙哥·三年风水* Date: 2024/11/5* Time: 17:32* @ return \think\response\Json*/public function personalMenu(){$listMenu = unserialize(Redis::select(config('cache.stores.redis.cache_db'))->get('menu_list'.$this->userId));if($listMenu === false){if(count($this->rules) == 0)$this->setAuth();$listMenu = PMModel::getAll([['id', 'in', $this->rules], ['status', '=', 1], ['is_menu', '=', 1]], 'id,menuname,pid,title,path,component,icon,redirect,always_show,is_hidden,is_icon,is_cache','sort DESC,id DESC');if(empty($listMenu)) return err("该用户没有权限");$seconds = rand(60,120);Redis::select(config('cache.stores.redis.cache_db'))->setex('menu_list'.$this->userId,$seconds,serialize($listMenu));}$data['menu'] = Tree::list_to_tree($listMenu,0, 'id', 'pid', 'children');return succ('操作成功',$data);}/*** 修改个人资料* User: 龙哥·三年风水* Date: 2024/11/5* Time: 9:31*/public function updatePersonal(){$param = $this->request->param();$validate = new \app\validate\permission\UpdateAdmin;$param['id'] = $this->userId;if (!$validate->check($param)) return err($validate->getError());if(!empty($param['password'])){$securityPassword = PSModel::dataFind(['id' => 1],'security_password')['security_password'];$data['password'] = sha1($param['password'] . $securityPassword);}$data['username'] = $param['username'];$data['avatar'] = empty($param['avatar']) ? $this->avatar : $param['avatar'];$data['realname'] = $param['realname'];$data['email'] = $param['email'];PAModel::redisSave($data, $this->userId);return succ('操作成功',[]);}//退出public function logout(){$redisToken = Redis::select(config('cache.stores.redis.token_db'))->get('token_' . $this->token);if(!empty($redisToken)){$emailSender = new EmailSender();$emailSender::send($this->email,'退出系统',$this->realname.'于'.date('Y-m-d H:i:s',time()).'退出系统');TokenModel::save(['expire_time' => time()], ['token' => $this->token]);Redis::select(config('cache.stores.redis.token_db'))->del('token_' . $redisToken);Redis::select(config('cache.stores.redis.token_db'))->del('token_' . $this->token);}return succ('退出成功');}
}
4、删除登录文件里面的退出方法
在根目录下app文件夹下controller文件夹下common文件夹下,修改Login.php文件,代码如下:
<?php
/*** 登录退出操作* User: 龙哥·三年风水* Date: 2024/10/29* Time: 15:53*/
namespace app\controller\common;
use app\controller\Emptys;
use app\model\permission\Admin;
use app\model\param\System as SystemModel;
use Email\EmailSender;
use Redis\Redis;
use app\model\common\Token as TokenModel;
use Encipher\Encrypt;
class Login extends Emptys
{//验证码展示public function getCode(){$re = app()->make(\Other\Captcha::class)->create();$base64 = 'data:image/png;base64,' . base64_encode($re->getData());return succ('SUCCESS',$base64);}//登录public function doLogin(){$param = $this->request->param();$validate = new \app\validate\common\Login;$result = $validate->check($param);//参数验证if (!$result) return err($validate->getError());//验证码的正确性if (!app()->make(\Other\Captcha::class)->check($param['captcha_code'])) return err('验证码错误');if (time() + 5 < strtotime($param['login_time'])) return err('非法登录');$resData = Admin::dataFind(['email' => trim($param['username'])], 'id,realname,password,ip,status', true);//用户信息的正确性if (empty($resData) && empty($resData['id'])) return err('用户不存在');if ($resData['status'] !== 1) return err('该账号已被禁用');$systemParam = SystemModel::dataFind(['id' => 1],'security_password,platform_token_expira');if ($resData['password'] !== sha1($param['password'] . $systemParam['security_password'])) return err('账号对应的密码错误');if(!empty($resData['ip'])){if($resData['ip'] != $this->request->header('x-real-ip'))return err('禁止访问,不在IP白名单中');}//写入Token日志$data['token_type'] = 1;$data['menu_name'] = 'CommonLoginDoLogin';$data['admin_id'] = $resData['id'];$data['random_number'] = alnum();$data['create_time'] = date('Y-m-d',strtotime($param['login_time']));$data['login_time'] = $param['login_time'];$data['expire_time'] = strtotime($param['login_time']) + $systemParam['platform_token_expira'];$token = $data['admin_id'] . $data['random_number'];$data['token'] = sha1(sha1($token) . strtotime($data['login_time']));TokenModel::save($data,[]);//加入跨站攻击验证队列Redis::select(config('cache.stores.redis.token_db'))->setex('token_' . $data['token'],$systemParam['platform_token_expira'],$resData['id']);Redis::select(config('cache.stores.redis.token_db'))->setex('token_' . $resData['id'],$systemParam['platform_token_expira'],Encrypt::encryptRsa($data['token']));$emailSender = new EmailSender();$emailSender::send($param['username'],'登录系统',$resData['realname'].'于'.$param['login_time'].'登录系统');return succ('登录成功',Encrypt::encryptRsa($token));}
}