ProjectUserRoleController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Project;
  4. use App\Models\ProjectRole;
  5. use App\Models\ProjectUser;
  6. use App\Models\User;
  7. use Illuminate\Http\Request;
  8. class ProjectUserRoleController extends BaseController
  9. {
  10. protected $model;
  11. protected $department;
  12. protected $model_name = '角色';
  13. protected $pre_uri = '/admin/ProjectUserRole/';
  14. protected $view_path = 'admin.project-user-roles.';
  15. protected $redirect_index = '/admin/ProjectUserRole/index';
  16. public function __construct()
  17. {
  18. $this->model = new ProjectUser();
  19. }
  20. public function index(Request $request)
  21. {
  22. $user = User::find($request->input('user_id'));
  23. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  24. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'user'));
  25. }
  26. public function get(Request $request)
  27. {
  28. $items = $this->model->with('user', 'projectRole');
  29. if($request->input('name')) {
  30. $user_ids = User::where('name', 'like', '%' . $request->input('name') . '')->pluck('id')->unique();
  31. if(count($user_ids) > 0) {
  32. $items = $items->whereIn('user_id', $user_ids);
  33. }
  34. }
  35. $select_items = collect(['project_id', 'user_id']);
  36. foreach($select_items as $select_item) {
  37. if($request->has($select_item) && !empty($request->input($select_item))) {
  38. $items = $items->where($select_item, '=', $request->input($select_item));
  39. }
  40. }
  41. $items = $items->paginate();
  42. foreach($items as $item) {
  43. $item->user_name = $item->user ? $item->user->name : '';
  44. $item->role_name = $item->projectRole ? $item->projectRole->name : '';
  45. $item->project_name = $item->project ? $item->project->name : '';
  46. }
  47. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  48. }
  49. public function create()
  50. {
  51. $project_options = (new Project())->getOptions();
  52. $role_options = (new ProjectRole())->getOptions();
  53. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  54. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'role_options', 'project_options'));
  55. }
  56. public function store(Request $request)
  57. {
  58. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  59. $validator = $this->model->getValidator($request, 'store');
  60. if($validator->fails()) {
  61. return back()->withErrors($validator)->withInput();
  62. }
  63. $data = $request->input('data');
  64. $res = $this->model->create($data);
  65. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  66. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  67. }
  68. public function edit(Request $request)
  69. {
  70. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  71. $project_options = (new Project())->getOptions();
  72. $role_options = (new ProjectRole())->getOptions();
  73. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  74. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'project_options', 'role_options'));
  75. }
  76. public function update(Request $request)
  77. {
  78. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  79. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  80. $validator = $this->model->getValidator($request, 'update');
  81. if($validator->fails()) {
  82. return back()->withErrors($validator)->withInput();
  83. }
  84. $data = $request->input('data');
  85. $res = $this->model->where('id', $request->input('id'))->update($data);
  86. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  87. return back()->with(['sg_success_info' => '编辑成功']);
  88. }
  89. public function delete(Request $request)
  90. {
  91. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  92. $res = $item->delete();
  93. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  94. return response()->json(['status' => 'success', 'info' => '操作成功']);
  95. }
  96. }