UserAuthController.php 5.2 KB

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