ConfigController.php 4.3 KB

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