You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.9 KiB
PHP
131 lines
4.9 KiB
PHP
<?php
|
|
namespace App\Services\Admin;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\Login\LoginService;
|
|
|
|
class UserService
|
|
{
|
|
public function GetInfoList($arr){
|
|
$q=DB::select("select a.id, a.cn_name as cname,a.username as uname,a.status,a.created_at,b.group_name from users as a left join `group` as b on a.`group` =b.id where a.status in(0,1) limit ?,?",[($arr['page']-1)*$arr['pagesize'],$arr['pagesize']]);
|
|
$count=DB::select("select count(1) as c from users as a left join `group` as b on a.`group` =b.id where a.status in(0,1)");
|
|
$result['list']=$q;
|
|
$result['count']=$count[0]->c;
|
|
return $result;
|
|
}
|
|
public function Save($arr){
|
|
$result=[];
|
|
if($arr['info']['id']){
|
|
$query=DB::table('users')->where(['id'=>$arr['info']['id']])->update([
|
|
'group' => $arr['info']['groupId'],
|
|
'cn_name' => $arr['info']['cname'],
|
|
'username' => $arr['info']['uname'],
|
|
'status'=>$arr['info']['status'],
|
|
'department_id'=> isset($arr['info']['department_id']) ?$arr['info']['department_id']: 0
|
|
]);
|
|
if($query){
|
|
$result['status']='ok';
|
|
$result['msg']='操作成功';
|
|
}else{
|
|
$result['status']='no';
|
|
$result['msg']='操作失败,没有记录被更新';
|
|
}
|
|
}else{
|
|
$hash = password_hash('111111', PASSWORD_DEFAULT);
|
|
$c=DB::table('users')->where(['username'=>$arr['info']['uname']])->get();
|
|
if(count($c)){
|
|
$result['status']='no';
|
|
$result['msg']='用户名已存在';
|
|
return $result;
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$id=DB::table('users')->insertGetId([
|
|
'group' => $arr['info']['groupId'],
|
|
'cn_name' => $arr['info']['cname'],
|
|
'username' => $arr['info']['uname'],
|
|
'pwd' => $hash,
|
|
'status'=>1,
|
|
'department_id'=> isset($arr['info']['department_id']) ?$arr['info']['department_id']: 0
|
|
]);
|
|
|
|
DB::commit(); // 手动提交事务
|
|
if($id){
|
|
$result['status']='ok';
|
|
$result['msg']='操作成功';
|
|
}
|
|
} catch (\Exception $e) {
|
|
DB::rollback(); // 发生异常时手动回滚事务
|
|
$result['status']='no';
|
|
$result['msg']='操作失败';
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
public function GetDetail($arr){
|
|
$c=DB::table('users')->select(['id','cn_name','username','status','group','img',])->where(['id'=>$arr['id']])->whereIn('status',[0,1])->get();
|
|
if(count($c)){
|
|
$result['info']=$c;
|
|
$result['status']='ok';
|
|
$result['msg']='成功';
|
|
}else{
|
|
$result['status']='no';
|
|
$result['msg']='获取详情失败';
|
|
}
|
|
return $result;
|
|
}
|
|
public function ChangePwd($arr){
|
|
$result=array();
|
|
$s=app()->make(LoginService::class);
|
|
$check=$s->CheckPwd(['userid'=>$arr['id'],'password'=>$arr['oldpwd']]);
|
|
if($check['status']){
|
|
$hash = password_hash($arr['newpwd'], PASSWORD_DEFAULT);
|
|
$u=DB::table('users')->where(['id'=>$arr['id'],'status'=>1])->update(['pwd'=>$hash]);
|
|
if($u){
|
|
$result['status']='ok';
|
|
}else{
|
|
$result['status']='no';
|
|
$result['msg']='修改密码失败';
|
|
}
|
|
}else{
|
|
$result=$check;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
//检查用户是否有某个目录的权限
|
|
//参数['userid'=>$userid,'group'=>$group,'url'=>$url]
|
|
public function CheckMenuAuth($arr){
|
|
$list=['index','dashboard'];
|
|
$q=DB::select("select * from users where id=? and `group` =?",[$arr['userid'],$arr['group']]);
|
|
if(count($q)==1){
|
|
if(in_array($arr['url'],$list)){
|
|
return \Yz::Return(true,'',[]);
|
|
}
|
|
$check=DB::select("select * from (select menu_id from group_menu where group_id=? ) as a inner JOIN (select id from menu where url = ? ) as b on a.menu_id=b.id
|
|
",[$arr['group'],$arr['url']]);
|
|
if(count($check)>0){
|
|
return \Yz::Return(true,'',[]);
|
|
}else{
|
|
return \Yz::echoError1('暂无权限');
|
|
}
|
|
}else{
|
|
return \Yz::echoError1('权限不匹配');
|
|
}
|
|
}
|
|
|
|
//修改自身信息
|
|
public function ChangInfo($arr){
|
|
$u=DB::table('users')->where(['id'=>$arr['userid']])->update([
|
|
'cn_name'=>$arr['name'],
|
|
'img'=>$arr['headimg'],
|
|
]);
|
|
if($u>0){
|
|
return \Yz::Return(true,'操作成功',[]);
|
|
}else{
|
|
return \Yz::echoError1('更新失败');
|
|
}
|
|
}
|
|
}
|