2020-01-01 13:17:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\admin\model;
|
|
|
|
|
|
|
|
|
|
use think\Model;
|
|
|
|
|
use think\facade\Db;
|
|
|
|
|
use think\facade\Session;
|
2021-01-30 17:47:18 +08:00
|
|
|
|
use app\oil\model\Station;
|
|
|
|
|
use think\facade\Cookie;
|
|
|
|
|
use think\facade\Config;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
use think\model\concern\SoftDelete;
|
|
|
|
|
|
|
|
|
|
class Admin extends Model
|
|
|
|
|
{
|
|
|
|
|
//软删除
|
|
|
|
|
use SoftDelete;
|
|
|
|
|
protected $deleteTime = 'delete_time';
|
|
|
|
|
protected $defaultSoftDelete = 0;
|
2021-12-25 13:52:30 +08:00
|
|
|
|
protected $createTime = 'false';
|
|
|
|
|
|
|
|
|
|
//自动对password进行md5加密
|
|
|
|
|
protected function setPasswordAttr($value){
|
|
|
|
|
return md5($value);
|
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
2020-01-08 17:37:41 +08:00
|
|
|
|
//管理员关联角色
|
2020-11-13 17:24:58 +08:00
|
|
|
|
/*
|
2020-01-08 17:37:41 +08:00
|
|
|
|
public function authGroup()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo('AuthGroup','auth_group_id','id');
|
|
|
|
|
}
|
2020-11-13 17:24:58 +08:00
|
|
|
|
*/
|
2021-01-30 17:47:18 +08:00
|
|
|
|
//管理员关联站点
|
|
|
|
|
public function station()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Station::class);
|
|
|
|
|
}
|
2020-11-13 17:24:58 +08:00
|
|
|
|
|
|
|
|
|
//远程一对多管理员关联角色
|
|
|
|
|
public function adminGroup()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasManyThrough('AuthGroup', 'AuthGroupAccess','uid','id','id','group_id');
|
|
|
|
|
}
|
|
|
|
|
//管理员关联角色分配表
|
|
|
|
|
public function authGroupAccess()
|
|
|
|
|
{
|
2021-01-30 17:47:18 +08:00
|
|
|
|
return $this->hasMany(AuthGroupAccess::class,'uid');
|
2020-11-13 17:24:58 +08:00
|
|
|
|
}
|
2020-01-08 17:37:41 +08:00
|
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
|
//登陆校验
|
|
|
|
|
public function login($data)
|
2021-01-30 17:47:18 +08:00
|
|
|
|
{
|
2020-01-01 13:17:19 +08:00
|
|
|
|
//查询用户
|
2021-12-25 13:52:30 +08:00
|
|
|
|
$admin = $this->where('username',$data['username'])->where('delete_time',0)->find();
|
|
|
|
|
|
2021-07-22 17:55:10 +08:00
|
|
|
|
if(is_null($admin)){
|
2021-12-25 13:52:30 +08:00
|
|
|
|
return json(['code'=>-1,'msg'=>'用户名或密码错误']);
|
2021-07-22 17:55:10 +08:00
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
if($admin['status'] !=1){
|
2021-12-25 13:52:30 +08:00
|
|
|
|
return json(['code' => -1,'msg'=> '用户被禁用或未审核,请联系管理员']);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
|
|
|
|
//对输入的密码字段进行MD5加密,再进行数据库的查询
|
|
|
|
|
$salt = substr(md5($admin['create_time']),-6);
|
|
|
|
|
$pwd = substr_replace(md5($data['password']),$salt,0,6);
|
|
|
|
|
$data['password'] = md5($pwd);
|
2021-12-25 13:52:30 +08:00
|
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
|
if($admin['password'] == $data['password']){
|
|
|
|
|
|
|
|
|
|
//将用户数据写入Session
|
|
|
|
|
Session::set('admin_id',$admin['id']);
|
|
|
|
|
Session::set('admin_name',$admin['username']);
|
2021-01-30 17:47:18 +08:00
|
|
|
|
|
|
|
|
|
if(isset($data['remember'])){
|
|
|
|
|
$salt = Config::get('taoler.salt');
|
|
|
|
|
//加密auth存入cookie
|
|
|
|
|
$auth = md5($admin['username'].$salt).":".$admin['id'];
|
|
|
|
|
Cookie::set('adminAuth',$auth,604800);
|
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
|
|
Db::name('admin')->where('id',$admin['id'])->update(
|
|
|
|
|
[
|
|
|
|
|
'last_login_time' => time(),
|
|
|
|
|
'last_login_ip' => request()->ip(),
|
|
|
|
|
]
|
2021-01-30 17:47:18 +08:00
|
|
|
|
);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
|
|
//用户名密码正确返回1
|
2021-12-25 13:52:30 +08:00
|
|
|
|
$res = ['code'=>0,'msg'=>'登陆成功', 'url'=>(string) url('index/index')];
|
2021-11-02 15:40:58 +08:00
|
|
|
|
} else {
|
2021-12-25 13:52:30 +08:00
|
|
|
|
$res = ['code'=>-1,'msg'=>'用户名或密码错误','url'=>(string) url('admin/login')];
|
2021-11-02 15:40:58 +08:00
|
|
|
|
}
|
2021-12-25 13:52:30 +08:00
|
|
|
|
return json($res);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|
2021-12-25 13:52:30 +08:00
|
|
|
|
|
|
|
|
|
//修改密码
|
|
|
|
|
public function setpass($data)
|
|
|
|
|
{
|
|
|
|
|
$admin = $this->find($data['admin_id']);
|
|
|
|
|
$salt = substr(md5($admin['create_time']),-6);
|
|
|
|
|
$oldPassword = $this->pass($salt,$data['oldPassword']);
|
|
|
|
|
if($oldPassword != $admin['password']){
|
|
|
|
|
return json(['code'=>-1,'msg'=>'当前密码错误']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($data['password'] != $data['repassword']){
|
|
|
|
|
return json(['code'=>-1,'msg'=>'两次密码不一致']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data['password'] = substr_replace(md5($data['password']),$salt,0,6);
|
|
|
|
|
$admin->password = $data['password'];
|
|
|
|
|
$result = $admin->save();
|
|
|
|
|
|
|
|
|
|
if($result){
|
|
|
|
|
$res = ['code'=>0,'msg'=>'修改密码成功'];
|
|
|
|
|
} else {
|
|
|
|
|
$res = ['code'=>-1,'msg'=>'修改密码失败'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json($res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//加密规则 加密字符串,原始秘密
|
|
|
|
|
protected function pass($salt, $pass)
|
|
|
|
|
{
|
|
|
|
|
$pwd = substr_replace(md5($pass),$salt,0,6);
|
|
|
|
|
return md5($pwd);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
|
}
|