$data = request()->param();
if(empty($data['role_id'])){
return json(['code'=>1,'data'=>'','msg'=>'请先选择角色']);
}
$role = roles::find($data['role_id']);
$ids = explode(',',$data['auth_ids']);
$arr = YyRulerAuth::where("role_id",$data['role_id'])->select();
if(!empty($arr)){
$strs="";//空的字符串
foreach($arr as $k =>$v){
$strs .= $v['auth_id'].',';
}
$xian = substr($strs,0,-1);
$strs = explode(',',$xian);
$zeng=array_values(array_diff($ids,$strs));
if(!empty($zeng[0])){
$zeng=array_values(array_diff($ids,$strs));
for($i=0;$i<count($zeng);$i++){
$data = [
'role_id' =>$data['role_id'],
'auth_id' => $zeng[$i],
'create_time'=>date("Y-m-d H:i:s"),
];
$result = YyRulerAuth::insert($data);
$auth = YyAuth::where("id",$ids[$i])->find();
}
}
$jian = array_values(array_diff($strs,$ids)); //array(1) {[0] => string(0) "" }
if(!empty($jian[0])){
if (is_array($jian))
$id = implode(',', $jian);
$op = is_numeric($id) ? '=' : 'in';
$ids = explode(",",$id);
for($i=0;$i<count($ids);$i++){
$auth = YyAuth::where('id', $ids[$i])->find();
}
YyRulerAuth::where('auth_id', $op, $id)->where("role_id",$data['role_id'])->delete(true);
}
}else{
for($i=0;$i<count($ids);$i++){
$data = [
'role_id' => $data['role_id'],
'auth_id' => $ids[$i],
'create_time'=>date("Y-m-d H:i:s"),
];
$result = YyRulerAuth::insert($data);
$auth = YyAuth::where("id",$ids[$i])->find();
}
}