ProjectController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 applyIndex()
  27. {
  28. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  29. return view($this->view_path . 'apply-index', compact('model', 'model_name','pre_uri'));
  30. }
  31. public function get(Request $request)
  32. {
  33. $items = $this->model->orderBy('created_at', 'desc');
  34. $tmp_items = collect(['name']);
  35. foreach($tmp_items as $tmp_item) {
  36. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  37. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  38. }
  39. }
  40. $select_items = collect([]);
  41. foreach($select_items as $select_item) {
  42. if($request->has($select_item) && !empty($request->input($select_item))) {
  43. $items = $items->where($select_item, '=', $request->input($select_item));
  44. }
  45. }
  46. if($request->input('type') == 'apply') {
  47. $items = $items->whereNotNull('user_id');
  48. }
  49. $items = $items->paginate();
  50. foreach($items as $item) {
  51. $item->active_label = $item->getNameOrLabel('active', 'label');
  52. $item->manager = $item->getManager();
  53. $item->manager_name = $item->manager ? $item->manager->name : '';
  54. $item->manager_phone = $item->manager ? $item->manager->phone : '';
  55. $item->user_name = $item->user ? $item->user->name : '';
  56. $item->user_avatar = $item->user ? $item->user->getAvatar() : '';
  57. $item->user_phone = $item->user ? $item->user->phone : '';
  58. }
  59. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  60. }
  61. public function create()
  62. {
  63. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  64. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  65. }
  66. public function store(Request $request)
  67. {
  68. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  69. $validator = $this->model->getValidator($request, 'store');
  70. if($validator->fails()) {
  71. return back()->withErrors($validator)->withInput();
  72. }
  73. $data = $request->input('data');
  74. $res = $this->model->create($data);
  75. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  76. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  77. }
  78. public function edit(Request $request)
  79. {
  80. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  81. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  82. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  83. }
  84. public function update(Request $request)
  85. {
  86. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  87. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  88. $validator = $this->model->getValidator($request, 'update');
  89. if($validator->fails()) {
  90. return back()->withErrors($validator)->withInput();
  91. }
  92. $data = $request->input('data');
  93. $res = $this->model->where('id', $request->input('id'))->update($data);
  94. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  95. return back()->with(['sg_success_info' => '编辑成功']);
  96. }
  97. public function delete(Request $request)
  98. {
  99. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  100. $res = $item->delete();
  101. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  102. return response()->json(['status' => 'success', 'info' => '操作成功']);
  103. }
  104. public function change(Request $request)
  105. {
  106. $this->model->where('id', $request->input('id'))->update(['active' => $request->input('active')]);
  107. return response()->json(['status' => 'success', 'info' => '操作成功']);
  108. }
  109. public function active(Request $request)
  110. {
  111. $this->model->where('id', $request->input('id'))->update(['active' => 1]);
  112. return response()->json(['status' => 'success', 'info' => '操作成功']);
  113. }
  114. }