InfoController.php 4.4 KB

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