123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Http\Controllers\Role;
- use App\Http\Controllers\AdminBaseController;
- use App\Models\Menu;
- use App\Models\Role;
- use App\Repositories\Eloquent\RoleRepositoryEloquent;
- use App\Servers\MenuServer;
- use App\Validators\RoleValidator;
- use Illuminate\Contracts\Routing\ResponseFactory;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- /**
- * Class RolesController.
- *
- * @package namespace App\Http\Controllers;
- */
- class RolesController extends AdminBaseController
- {
- /**
- * RolesController constructor.
- *
- * @param RoleRepositoryEloquent $repository
- * @param RoleValidator $validator
- */
- public function __construct(RoleRepositoryEloquent $repository, RoleValidator $validator)
- {
- parent::__construct($repository, $validator);
- }
- protected function _indexPost($datum)
- {
- foreach ( $datum as $value ) {
- if ($value->{'id'} ==1) {
- $value->skip = 'skip';
- } else {
- $value->skip = '';
- }
- }
- return $datum;
- }
- protected function _storePost($request)
- {
- $name = $request->input('name', '');
- $status = $request->input('status', '');
- if (empty($name)) {
- $this->errorMsg = "角色名称必填";
- return false;
- }
- // 查询角色名称是否存在
- $count = Role::where('name', $name)->count();
- if ($count > 0) {
- $this->errorMsg = "角色名称已存在";
- return false;
- }
- Role::create(['name' => $name,'status'=>$status]);
- return true;
- }
- protected function _updatePost($request, $id)
- {
- $name = $request->input('name', '');
- $status = $request->input('status', '');
- if (empty($name)) {
- $this->errorMsg = "角色名称必填";
- return false;
- }
- $model = $this->repository->find($id);
- if (empty($model)) {
- $this->errorMsg = "角色不存在";
- return false;
- }
- // 查询角色名称是否存在
- $count = Role::where('name', $name)->where('id','<>',$id)->count();
- if ($count > 0) {
- $this->errorMsg = "角色名称已存在";
- return false;
- }
- Role::where('id',$id)->update(['name' => $name,'status'=>$status]);
- return true;
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @param $id
- * @return \Illuminate\Http\JsonResponse
- *
- * 删除一行数据
- */
- protected function destroy(Request $request, ResponseFactory $response, $id = null)
- {
- if ($request->isMethod('post') && $request->ajax()) {
- $count = DB::table('users')->where('roles_id', $id)->count();
- if ($count >= 1) {
- $data = [
- 'status' => 1,
- 'message' => '当前角色已绑定账号不允许删除'
- ];
- } else {
- $deleted = $this->repository->delete($id);
- $deleted === false ? $data = [
- 'status' => 1,
- 'message' => self::ERROR_MSG
- ] : $data = [
- 'status' => 0,
- 'message' => ''
- ];
- }
- if ($request->wantsJson()) {
- return $response->json($data);
- }
- }
- }
- function getPermissionsView()
- {
- $id = request()->input('id');
- $role = Role::where('id', $id)->first();
- $permission=MenuServer::creatServer()->getPermissionsMenu($role->{'role_p_ids'});
- $permission_ids= explode(',', $role->{'role_p_ids'});
- if(empty($permission_ids))$permission_ids=[0];
- return view('admins.roles.permissions',['permission'=>$permission,'permission_ids'=>$permission_ids,'id'=>$id]);
- }
- function setPermissions(){
- $permission_ids=request()->input('permission_ids');
- $id=request()->input('id');
- if(empty($permission_ids)){
- return $this->response('请选择权限', 'error');
- }
- $role_p_ids=$permission_ids;
- if(is_string($permission_ids))$permission_ids=explode(',',$permission_ids);
- $p_permission_ids= Menu::where('level', 2)->whereIn('id',$permission_ids)->pluck('p_ids');
- foreach ($p_permission_ids as $v){
- $p_ids= explode(',',$v);
- $permission_ids=array_merge($permission_ids,$p_ids);
- }
- $permission_ids=array_unique($permission_ids);
- $permission_ids=implode(',',$permission_ids);
- Role::where('id',$id)->update(['permission_ids'=>$permission_ids,'role_p_ids'=>$role_p_ids]);
- return $this->response(self::SUCCESS_MSG, 'success', '', '');
- }
- }
|