ProjectController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminRoleModel;
  4. use App\Models\Department;
  5. use App\Models\Project;
  6. use App\Models\Road;
  7. use App\Models\Role;
  8. use Illuminate\Http\Request;
  9. class ProjectController extends BaseController
  10. {
  11. protected $model;
  12. protected $department;
  13. protected $model_name = '项目';
  14. protected $pre_uri = '/admin/Project/';
  15. protected $view_path = 'admin.projects.';
  16. protected $redirect_index = '/admin/Project/index';
  17. public function __construct()
  18. {
  19. $this->model = new Project();
  20. }
  21. public function index()
  22. {
  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'));
  25. }
  26. public function get(Request $request)
  27. {
  28. $items = $this->model->orderBy('created_at', 'desc');
  29. $tmp_items = collect(['name']);
  30. foreach($tmp_items as $tmp_item) {
  31. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  32. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  33. }
  34. }
  35. $select_items = collect([]);
  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->active_label = $item->getNameOrLabel('active', 'label');
  44. }
  45. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  46. }
  47. public function create()
  48. {
  49. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  50. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  51. }
  52. public function store(Request $request)
  53. {
  54. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  55. $validator = $this->model->getValidator($request, 'store');
  56. if($validator->fails()) {
  57. return back()->withErrors($validator)->withInput();
  58. }
  59. $data = $request->input('data');
  60. $res = $this->model->create($data);
  61. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  62. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  63. }
  64. public function edit(Request $request)
  65. {
  66. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  67. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  68. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  69. }
  70. public function update(Request $request)
  71. {
  72. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  73. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  74. $validator = $this->model->getValidator($request, 'update');
  75. if($validator->fails()) {
  76. return back()->withErrors($validator)->withInput();
  77. }
  78. $data = $request->input('data');
  79. $res = $this->model->where('id', $request->input('id'))->update($data);
  80. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  81. return back()->with(['sg_success_info' => '编辑成功']);
  82. }
  83. public function delete(Request $request)
  84. {
  85. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  86. $res = $item->delete();
  87. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  88. return response()->json(['status' => 'success', 'info' => '操作成功']);
  89. }
  90. public function active(Request $request)
  91. {
  92. $this->model->where('id', $request->input('id'))->update(['active' => 1]);
  93. return response()->json(['status' => 'success', 'info' => '操作成功']);
  94. }
  95. }