DictionaryController.php 4.2 KB

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