Commit aaee822f authored by hujun's avatar hujun

登录

parent 1bc9186e
...@@ -6,6 +6,8 @@ use app\api\untils\JwtUntils; ...@@ -6,6 +6,8 @@ use app\api\untils\JwtUntils;
use app\api_broker\service\BrokerService; use app\api_broker\service\BrokerService;
use app\index\extend\Basic; use app\index\extend\Basic;
use app\model\AAgents; use app\model\AAgents;
use app\model\AStore;
use app\model\AuthGroup;
use app\model\AuthRule; use app\model\AuthRule;
use think\Session; use think\Session;
...@@ -19,16 +21,20 @@ use think\Session; ...@@ -19,16 +21,20 @@ use think\Session;
class Login extends Basic class Login extends Basic
{ {
protected $loginAgent; protected $m_agent;
protected $authRule; protected $m_authRule;
protected $brokerService; protected $brokerService;
protected $m_store;
protected $m_auth_group;
public function __construct($request = null) public function __construct($request = null)
{ {
parent::__construct($request); parent::__construct($request);
$this->loginAgent = new AAgents(); $this->m_agent = new AAgents();
$this->authRule = new AuthRule(); $this->m_authRule = new AuthRule();
$this->brokerService = new BrokerService(); $this->brokerService = new BrokerService();
$this->m_auth_group = new AuthGroup();
$this->m_store = new AStore();
} }
public function login() public function login()
...@@ -37,61 +43,60 @@ class Login extends Basic ...@@ -37,61 +43,60 @@ class Login extends Basic
} }
/** /**
* 登录接口 * 登录
* *
* @return \think\Response|void * @return \think\Response
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function loginVerify() public function loginVerify()
{ {
$name = $this->request->param("username"); $name = $this->params["username"];
$password = $this->request->param("passwd"); $password = $this->params["password"];
if (!isset($name) || !isset($password)) { if (empty($name) || empty($password)) {
return $this->response('100', '用户名或密码不能为空'); return $this->response(101, '用户名或密码不能为空');
} }
$filed = " a.id,a.store_id,a.auth_group_id,a.district_id,a.level,a.name,a.phone,a.sex,a.img,a.status,a.admin_off, $fields = 'a.id,a.store_id,a.auth_group_id,a.district_id,a.level,a.name,a.phone,a.sex,a.img,a.status,a.admin_off
ifNull(s.store_name,'')store_name,ifNull(d.district_name,'')district_name,g.rules,g.status as g_status"; ,b.store_name,c.district_name';
$join = [ try {
[ 'a_store s', ' a.store_id=a.id', 'left' ], $where['password'] = md5($password);
[ 'a_district d', 'a.district_id=d.id', 'left' ], $where['phone'] = trim($name);
[ 'auth_group g', 'a.auth_group_id=g.id', 'left' ] $user_data = $this->m_agent->getStoreDistrict($fields, $where);
];
$params["name"] = $name; if (empty($user_data)) {
$password = md5($password); return $this->response(101, '用户名或密码错误');
$where = "( phone='{$name}') and password='$password' "; }
$list = $this->loginAgent->verifyUser($filed, $join, $where);
if (!$list) { $auth_group_id = '';
return $this->response('101', '用户名或密码错误'); if (!empty($user_data['auth_group_id'])) {
} $auth_group_id = $this->m_auth_group->getAuthGroupById($user_data['auth_group_id'], 'rules');
}
if ($list['g_status'] == '1') { if ($user_data['status'] != 0 || $user_data['admin_off'] != '0' || empty($auth_group_id)) {
return $this->response('102', '用户所在角色组已冻结'); return $this->response('101', '无后台访问权限');
} }
$last_login_ip = ip2long($this->request->ip());
if ($user_data['id'] != 1) {
//判断设备id是否存在
$is_login = $this->brokerService->judgeBand($last_login_ip, $user_data['id'], "浏览器需前端传", 0, $user_data['phone'], 1);
if (!$is_login) {
return $this->response("102", "该账号没有绑定该手机,请致电人事进行绑定。");
}
}
if ($list['admin_off'] != '0' || empty($list['rules']) || $list['status'] != '0') {
return $this->response('103', '无后台访问权限');
} else {
$where_rule['status'] = 0; $where_rule['status'] = 0;
if ($list['id'] == 1) { if ($user_data['id'] == 1) {
//超级管理员 //超级管理员
$nav = $this->authRule->getRule('', $where_rule); $nav = $this->m_authRule->getRule('', $where_rule);
} else { } else {
$where_rule['id'] = [ 'in', $list['rules'] ]; $where_rule['id'] = [ 'in', $auth_group_id ];
//查询后台菜单 //查询后台菜单
$nav = $this->authRule->getRule('', $where_rule); $nav = $this->m_authRule->getRule('', $where_rule);
} }
$menu_data = $auth_data = [];
$menu_data = [];
$auth_data = [];
foreach ($nav as $k => $v) { foreach ($nav as $k => $v) {
if ($v['is_menu'] == 1) { if ($v['is_menu'] == 1) {
$menu_data[$k]['id'] = $v['id']; $menu_data[$k]['id'] = $v['id'];
...@@ -106,43 +111,31 @@ class Login extends Basic ...@@ -106,43 +111,31 @@ class Login extends Basic
} }
} }
$list['menu'] = list_to_tree($menu_data); $user_data['menu'] = list_to_tree($menu_data);
$list['auth'] = $auth_data; $user_data['auth'] = $auth_data;
$list = $list->toArray();//转化arr $user_data = $user_data->toArray();//转化arr
}
$last_login_ip = ip2long($this->request->ip()); $update_data['last_login_time'] = date('Y-m-d H:i:s');
//判断设备id是否存在 $update_data['last_login_ip'] = $last_login_ip;
$is_login = $this->brokerService->judgeBand($last_login_ip, $list['id'],"浏览器需前端传", 0, $list['phone'], 1); $this->m_agent->updateData($user_data['id'], $update_data);
if (!$is_login) { } catch (\Exception $e) {
return $this->response("102", "该账号没有绑定该手机,请致电人事进行绑定。"); return $this->response(101, '内部错误,请联系运营!'. $e->getMessage());
} }
$jwt = new JwtUntils(); $jwt = new JwtUntils();
$jwt_data['id'] = $list['id']; $jwt_data['id'] = $user_data['id'];
$jwt_data['name'] = $list['name']; $jwt_data['name'] = $user_data['name'];
$jwt_data['phone'] = $list['phone']; $jwt_data['phone'] = $user_data['phone'];
$jwt_data['level'] = $list['level']; $jwt_data['level'] = $user_data['level'];
$list['AuthToken'] = $jwt->createToken($jwt_data); $list['AuthToken'] = $jwt->createToken($jwt_data);
Session::set("userName", $list["name"]); Session::set("userName", $user_data["name"]);
Session::set("userId", $list["id"]); Session::set("userId", $user_data["id"]);
Session::set("lastLoginTime", time()); Session::set("lastLoginTime", time());
Session::set("user_info", $list); Session::set("user_info", $user_data);
$this->operating_records($list["id"], 1, '后台登陆'); //记录操作日志 $this->operating_records($user_data["id"], 1, '后台登陆'); //记录操作日志
if ($this->request->isAjax()) { return $this->response('200', '登录成功', $user_data);
return $this->response('200', '登录成功', $list);
} else {
$this->redirect('/admin.php');
}
return;
}
public function userVerify()
{
//todo
} }
/** /**
......
...@@ -924,4 +924,20 @@ class AAgents extends BaseModel ...@@ -924,4 +924,20 @@ class AAgents extends BaseModel
return $data; return $data;
} }
/**
* 更新数据
*
* @param $id
* @param $data
* @return AAgents|bool
*/
public function updateData($id, $data) {
if ($id) {
$result = $this->where('id', $id)->update($data);
} else {
$result = false;
}
return $result;
}
} }
\ No newline at end of file
...@@ -5,174 +5,6 @@ namespace app\model; ...@@ -5,174 +5,6 @@ namespace app\model;
class AuthGroup extends BaseModel class AuthGroup extends BaseModel
{ {
const TYPE_ADMIN = 1; // 管理员用户组类型标识
const MEMBER = 'agents';
const AUTH_GROUP_ACCESS = 'auth_group_access'; // 关系表表名
const AUTH_GROUP = 'auth_group'; // 用户组表名
const AUTH_EXTEND_CATEGORY_TYPE = 1; // 分类权限标识
const AUTH_EXTEND_MODEL_TYPE = 2; //分类权限标识
protected $insert =['status'=>1];
/**
* 返回用户组列表
* 默认返回正常状态的管理员用户组列表
* @param array $where 查询条件,供where()方法使用
*
*/
public function getGroups($where=array()){
$map = array('status'=>1);
$map = array_merge($map,$where);
return $this->where($map)->select();
}
/**
* 把用户添加到用户组,支持批量添加用户到用户组
*
* 示例: 把uid=1的用户添加到group_id为1,2的组 `AuthGroupModel->addToGroup(1,'1,2');`
*/
public function addToGroup($uid, $gid){
$uid = is_array($uid)? implode(',',$uid) : trim($uid,',');
$gid = is_array($gid)? $gid:explode( ',',trim($gid,',') );
$Access = model(self::AUTH_GROUP_ACCESS);
$del = true;
if( isset($_REQUEST['batch']) ){
//为单个用户批量添加用户组时,先删除旧数据
$del = $Access->where(['uid'=>['in',$uid]])->delete();
}
$uid_arr = explode(',',$uid);
$uid_arr = array_diff($uid_arr,get_administrators());
$add = [];
if( $del!==false ){
foreach ($uid_arr as $u){
foreach ($gid as $g){
if( is_numeric($u) && is_numeric($g) ){
//防止重复添加
if (!$Access->where(['group_id'=>$g,'uid'=>$u])->count()) {
$add[] = ['group_id'=>$g,'uid'=>$u];
}
}
}
}
if (!empty($add) && is_array($add)) {
$Access->saveAll($add);
} else{
$this->error = "添加失败,可能有重复添加操作";
return false;
}
}
if ($Access->getError()) {
if( count($uid_arr)==1 && count($gid)==1 ){
//单个添加时定制错误提示
$this->error = "不能重复添加";
}
return false;
}
return true;
}
/**
* 返回用户所属用户组信息
* @param int $uid 用户id
* @return array 用户所属的用户组 array(
* array('uid'=>'用户id','group_id'=>'用户组id','title'=>'用户组名称','rules'=>'用户组拥有的规则id,多个,号隔开'),
* ...)
*/
static public function getUserGroup($uid){
static $groups = array();
if (isset($groups[$uid]))
return $groups[$uid];
$prefix = config('database.prefix');
$user_groups = model()
->field('uid,group_id,title,description,rules')
->table($prefix.self::AUTH_GROUP_ACCESS.' a')
->join ($prefix.self::AUTH_GROUP." g on a.group_id=g.id")
->where("a.uid='$uid' and g.status='1'")
->select();
$groups[$uid]=$user_groups?$user_groups:array();
return $groups[$uid];
}
/**
* 将用户从用户组中移除
* @param int|string|array $gid 用户组id
* @param int|string|array $cid 分类id
*/
public function removeFromGroup($uid,$gid){
$del_result = model(self::AUTH_GROUP_ACCESS)->where( array( 'uid'=>$uid,'group_id'=>$gid) )->delete();
if ($del_result) {
$user_auth_role = db('users')->where(array('uid'=>$uid))->value('auth_groups');
if ($user_auth_role) {
$user_auth_role=array_merge(array_diff(explode(',', $user_auth_role), array($gid)));
model('user')->where(array('uid'=>$uid))->setField('auth_groups',$user_auth_role);//同时将用户角色关联删除
}
}
return $del_result;
}
/**
* 获取某个用户组的用户列表
*
* @param $group_id 用户组id
* @return mixed
*/
static public function userInGroup($group_id){
$prefix = config('database.prefix');
$l_table = $prefix.self::MEMBER;
$r_table = $prefix.self::AUTH_GROUP_ACCESS;
$list = model() ->field('m.uid,u.username,m.last_login_time,m.last_login_ip,m.status')
->table($l_table.' m')
->join($r_table.' a ON m.uid=a.uid')
->where(array('a.group_id'=>$group_id))
->select();
return $list;
}
/**
* 检查id是否全部存在
*
* @param $modelname
* @param $mid
* @param string $msg
* @return bool
*/
public function checkId($modelname,$mid,$msg = '以下id不存在:'){
if(is_array($mid)){
$count = count($mid);
$ids = implode(',',$mid);
}else{
$mid = explode(',',$mid);
$count = count($mid);
$ids = $mid;
}
$s = model($modelname)->where(array('id'=>array('in',$ids)))->column('id');
if(count($s)===$count){
return true;
}else{
$diff = implode(',',array_diff($mid,$s));
$this->error = $msg.$diff;
return false;
}
}
/**
* 检查用户组是否全部存在
*
* @param $gid
* @return bool
*/
public function checkGroupId($gid){
return $this->checkId('AuthGroup',$gid, '以下用户组id不存在:');
}
/** /**
* @param string $order_ * @param string $order_
* @param string $field * @param string $field
...@@ -238,11 +70,13 @@ class AuthGroup extends BaseModel ...@@ -238,11 +70,13 @@ class AuthGroup extends BaseModel
* *
* @param $id * @param $id
* @param $fields * @param $fields
* @param int $status
* @return mixed * @return mixed
*/ */
public function getAuthGroupById($id, $fields) public function getAuthGroupById($id, $fields, $status = 0)
{ {
$return = $this->where('id', $id)->value($fields); return $this->where('id', $id)
return $return; ->where('status', $status)
->value($fields);
} }
} }
...@@ -42,7 +42,7 @@ $(function(){ ...@@ -42,7 +42,7 @@ $(function(){
url: '/admin.php/index/loginVerify', url: '/admin.php/index/loginVerify',
data: { data: {
'username': _userName, 'username': _userName,
'passwd': _passWord 'password': _passWord
}, },
timeout: 30000, timeout: 30000,
dataType: 'json', dataType: 'json',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment