ContentController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\BaseAttachmentModel;
  4. use App\Models\Content;
  5. use Illuminate\Http\Request;
  6. class ContentController extends Controller
  7. {
  8. protected $redirect_index = '/admin/Content/index';
  9. protected $view_path = 'admin.contents.';
  10. protected $pre_uri = '/admin/Content/';
  11. protected $model_name = '内容';
  12. protected $model;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->model = new Content();
  17. }
  18. public function index(Request $request)
  19. {
  20. $type = $this->model->getModelType($request->input('type', 1));
  21. $list = $this->model->where('type', $type)->orderBy('sort');
  22. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  23. $keyword = '%' . trim($request->input('keyword')) . '%';
  24. $list = $list->where('title', 'like', $keyword);
  25. }
  26. $list = $list->paginate()->withPath($this->getPaginateUrl());
  27. list($pre_uri, $model_name) = array($this->pre_uri, $this->model->getModelName($type));
  28. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name', 'type'));
  29. }
  30. public function create(Request $request)
  31. {
  32. $type = $this->model->getModelType($request->input('type', 1));
  33. list($pre_uri, $model_name) = array($this->pre_uri, $this->model->getModelName($type));
  34. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'type', 'model'));
  35. }
  36. public function store(Request $request)
  37. {
  38. if(!$request->isMethod('POST')) {
  39. return $this->showWarning('访问错误');
  40. }
  41. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  42. return $this->showWarning('数据错误');
  43. }
  44. $data = $request->input('data');
  45. if($request->hasFile('video')) {
  46. $data['content'] = (new BaseAttachmentModel())->upload($request->file('video'), '视频');
  47. }
  48. $res = $this->model->create($request->input('data'));
  49. if(!$res) {
  50. return $this->showWarning('数据库保存失败!');
  51. }
  52. return $this->showMessage('操作成功', $this->redirect_index . '?type=' . $res->type);
  53. }
  54. public function edit(Request $request)
  55. {
  56. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  57. return $this->showWarning('数据错误!');
  58. }
  59. $type = $this->model->getModelType($item->type);
  60. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model->getModelName($type), $this->model);
  61. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model', 'type'));
  62. }
  63. public function update(Request $request)
  64. {
  65. if(!$request->isMethod('POST')) {
  66. return $this->showWarning('访问错误');
  67. }
  68. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  69. return $this->showWarning('数据错误');
  70. }
  71. $item = $this->model->where('id', $request->input('id'))->first();
  72. if(empty($item)) {
  73. return $this->showWarning('找不到数据');
  74. }
  75. $data = $request->input('data');
  76. if($request->hasFile('video')) {
  77. $data['content'] = (new BaseAttachmentModel())->upload($request->file('video'), '视频');
  78. }
  79. $res = $this->model->where('id', $request->input('id'))->update($data);
  80. if(!$res) {
  81. return $this->showWarning('数据库保存失败!');
  82. }
  83. return $this->showMessage('操作成功', $this->redirect_index . '?type=' . $item->type);
  84. }
  85. public function delete(Request $request)
  86. {
  87. if(!$request->isMethod('POST')) {
  88. return $this->showWarning('访问错误');
  89. }
  90. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  91. return $this->showWarning('访问错误');
  92. }
  93. $res = $item->delete();
  94. if(!$res) {
  95. return $this->showWarning('数据库删除失败');
  96. }
  97. return $this->showMessage('操作成功');
  98. }
  99. }