DictionaryController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public function __construct(DictionaryRepository $repository) {
  19. if(!$this->repository) $this->repository = $repository;
  20. }
  21. function index(Request $request) {
  22. $search['keyword'] = $request->input('keyword');
  23. $query = $this->repository->pushCriteria(new MultiWhere($search));
  24. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  25. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  26. }else{
  27. $query = $query->pushCriteria(new OrderBy('id','DESC'));
  28. }
  29. $list = $query->paginate();
  30. return view('admin.base.dictionary.index',compact('list'));
  31. }
  32. function check(Request $request) {
  33. $request = $request->all();
  34. $search['keyword'] = $request->input('keyword');
  35. $orderby = array();
  36. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  37. $orderby[$request['sort_field']] = $request['sort_field_by'];
  38. }
  39. $list = $this->repository->search($search,$orderby);
  40. return view('admin.base.dictionary.check',compact('list'));
  41. }
  42. /**
  43. * 添加
  44. *
  45. */
  46. public function create(Request $request)
  47. {
  48. if($request->method() == 'POST') {
  49. return $this->_createSave();
  50. }
  51. return view('admin.base.dictionary.edit');
  52. }
  53. /**
  54. * 保存修改
  55. */
  56. private function _createSave(){
  57. $data = (array) request('data');
  58. $id = $this->repository->create($data);
  59. if($id) {
  60. $url[] = array('url'=>U( 'Base/Dictionary/index'),'title'=>'返回列表');
  61. $url[] = array('url'=>U( 'Base/Dictionary/create'),'title'=>'继续添加');
  62. $this->showMessage('添加成功',$url);
  63. }else{
  64. $url[] = array('url'=>U( 'Base/Dictionary/index'),'title'=>'返回列表');
  65. return $this->showWarning('添加失败',$url);
  66. }
  67. }
  68. /**
  69. *
  70. * 修改
  71. *
  72. *
  73. */
  74. public function update(Request $request) {
  75. if($request->method() == 'POST') {
  76. return $this->_updateSave();
  77. }
  78. $data = $this->repository->find($request->get('id'));
  79. return view('admin.base.dictionary.edit',compact('data'));
  80. }
  81. /**
  82. * 保存修改
  83. */
  84. private function _updateSave() {
  85. $data = (array) request('data');
  86. $ok = $this->repository->update(request('id'),$data);
  87. if($ok) {
  88. $url[] = array('url'=>U( 'Base/Dictionary/index'),'title'=>'返回列表');
  89. return $this->showMessage('操作成功',urldecode(request('_referer')));
  90. }else{
  91. $url[] = array('url'=>U( 'Base/Dictionary/index'),'title'=>'返回列表');
  92. return $this->showWarning('操作失败',$url);
  93. }
  94. }
  95. public function view(Request $request) {
  96. $data = $this->repository->find(request('id'));
  97. return view('admin.base.dictionary.view',compact('data'));
  98. }
  99. /**
  100. *
  101. * 状态改变
  102. *
  103. */
  104. public function status(Request $request) {
  105. $ok = $this->repository->updateStatus(request('id'),request('status'));
  106. if($ok) {
  107. return $this->showMessage('操作成功');
  108. }else{
  109. return $this->showWarning('操作失败');
  110. }
  111. }
  112. /**
  113. * 删除
  114. */
  115. public function destroy(Request $request) {
  116. $bool = $this->repository->destroy($request->get('id'));
  117. if($bool) {
  118. return $this->showMessage('操作成功');
  119. }else{
  120. return $this->showWarning("操作失败");
  121. }
  122. }
  123. }