InfoController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers\Admin\User;
  3. use App\Http\Controllers\Admin\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Repositories\Base\Criteria\OrderBy;
  6. use App\Repositories\User\Criteria\MultiWhere;
  7. use App\Repositories\User\InfoRepository;
  8. class InfoController extends Controller
  9. {
  10. private $repository;
  11. public function __construct(InfoRepository $repository)
  12. {
  13. if (!$this->repository) $this->repository = $repository;
  14. }
  15. function index(Request $request)
  16. {
  17. $search['keyword'] = $request->input('keyword');
  18. $query = $this->repository->pushCriteria(new MultiWhere($search));
  19. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  20. $query = $query->pushCriteria(new OrderBy($request['sort_field'], $request['sort_field_by']));
  21. } else {
  22. $query = $query->pushCriteria(new OrderBy('id', 'DESC'));
  23. }
  24. $list = $query->paginate(16);
  25. return view('admin.user.info.index', compact('list'));
  26. }
  27. /**
  28. * 添加
  29. *
  30. */
  31. public function create(Request $request)
  32. {
  33. if ($request->method() == 'POST') {
  34. return $this->_createSave();
  35. }
  36. return view('admin.user.info.edit');
  37. }
  38. /**
  39. * 保存修改
  40. */
  41. private function _createSave()
  42. {
  43. $data = (array)request('data');
  44. $id = $this->repository->create($data);
  45. if ($id) {
  46. $url[] = array('url' => U('User/Info/index'), 'title' => '返回列表');
  47. $url[] = array('url' => U('User/Info/create'), 'title' => '继续添加');
  48. $this->showMessage('添加成功', $url);
  49. } else {
  50. $url[] = array('url' => U('User/Info/index'), 'title' => '返回列表');
  51. return $this->showWarning('添加失败', $url);
  52. }
  53. }
  54. /**
  55. *
  56. * 修改
  57. *
  58. */
  59. public function update(Request $request)
  60. {
  61. if ($request->method() == 'POST') {
  62. return $this->_updateSave();
  63. }
  64. $data = $this->repository->find($request->get('id'));
  65. return view('admin.user.info.edit', compact('data'));
  66. }
  67. /**
  68. * 保存修改
  69. */
  70. private function _updateSave()
  71. {
  72. $data = (array)request('data');
  73. $ok = $this->repository->update(request('id'), $data);
  74. if ($ok) {
  75. $url[] = array('url' => U('User/Info/index'), 'title' => '返回列表');
  76. return $this->showMessage('操作成功', urldecode(request('_referer')));
  77. } else {
  78. $url[] = array('url' => U('User/Info/index'), 'title' => '返回列表');
  79. return $this->showWarning('操作失败', $url);
  80. }
  81. }
  82. public function view(Request $request)
  83. {
  84. $data = $this->repository->find(request('id'));
  85. return view('admin.user.info.view', compact('data'));
  86. }
  87. /**
  88. *
  89. * 状态改变
  90. *
  91. */
  92. public function status(Request $request)
  93. {
  94. $ok = $this->repository->updateStatus(request('id'), request('status'));
  95. if ($ok) {
  96. return $this->showMessage('操作成功');
  97. } else {
  98. return $this->showWarning('操作失败');
  99. }
  100. }
  101. /**
  102. * 删除
  103. */
  104. public function destroy(Request $request)
  105. {
  106. $bool = $this->repository->destroy($request->get('id'));
  107. if ($bool) {
  108. return $this->showMessage('操作成功');
  109. } else {
  110. return $this->showWarning("操作失败");
  111. }
  112. }
  113. }