InfoController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 信息列表
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-07-11 06:50:54
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Messages;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\MessagesFollowerModel;
  12. use App\Models\MessagesTagModel;
  13. use App\Models\UserInfoModel;
  14. use Illuminate\Http\Request;
  15. use App\Repositories\Base\Criteria\OrderBy;
  16. use App\Repositories\Messages\Criteria\MultiWhere;
  17. use App\Repositories\Messages\InfoRepository;
  18. class InfoController extends Controller
  19. {
  20. private $repository;
  21. public function __construct(InfoRepository $repository) {
  22. if(!$this->repository) $this->repository = $repository;
  23. }
  24. function index(Request $request) {
  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. foreach ($list as $key=>$val){
  34. $list[$key]['count'] = MessagesFollowerModel::where([['messages_id',$val['id']]])->count();
  35. $user = UserInfoModel::find($val['user_id']);
  36. $list[$key]['user'] = $user->nickname;
  37. switch ($val['type']) {
  38. case '0':
  39. $list[$key]['type'] = '免费';
  40. $list[$key]['state'] = '--';
  41. break;
  42. case '1':
  43. $list[$key]['type'] = '悬赏';
  44. if($val['state'] == 0){
  45. $list[$key]['state'] = '未完成';
  46. } else {
  47. $list[$key]['state'] = '已结束';
  48. }
  49. break;
  50. case '2':
  51. $list[$key]['type'] = '收费';
  52. $list[$key]['state'] = '--';
  53. break;
  54. }
  55. }
  56. return view('admin.messages.info.index',compact('list'));
  57. }
  58. function check(Request $request) {
  59. $request = $request->all();
  60. $search['keyword'] = $request->input('keyword');
  61. $orderby = array();
  62. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  63. $orderby[$request['sort_field']] = $request['sort_field_by'];
  64. }
  65. $list = $this->repository->search($search,$orderby);
  66. return view('admin.messages.info.check',compact('list'));
  67. }
  68. /**
  69. * 添加
  70. *
  71. */
  72. public function create(Request $request)
  73. {
  74. if($request->method() == 'POST') {
  75. return $this->_createSave();
  76. }
  77. $tags = MessagesTagModel::orderBy('sort','desc')->get();
  78. return view('admin.messages.info.edit',compact('tags'));
  79. }
  80. /**
  81. * 保存修改
  82. */
  83. private function _createSave(){
  84. $data = (array) request('data');
  85. $id = $this->repository->create($data);
  86. if($id) {
  87. $url[] = array('url'=>U( 'Messages/Info/index'),'title'=>'返回列表');
  88. $url[] = array('url'=>U( 'Messages/Info/create'),'title'=>'继续添加');
  89. $this->showMessage('添加成功',$url);
  90. }else{
  91. $url[] = array('url'=>U( 'Messages/Info/index'),'title'=>'返回列表');
  92. return $this->showWarning('添加失败',$url);
  93. }
  94. }
  95. /**
  96. *
  97. * 修改
  98. *
  99. *
  100. */
  101. public function update(Request $request) {
  102. if($request->method() == 'POST') {
  103. return $this->_updateSave();
  104. }
  105. $data = $this->repository->find($request->get('id'));
  106. $tags = MessagesTagModel::orderBy('sort','desc')->get();
  107. return view('admin.messages.info.edit',compact('data','tags'));
  108. }
  109. /**
  110. * 保存修改
  111. */
  112. private function _updateSave() {
  113. $data = (array) request('data');
  114. $ok = $this->repository->update(request('id'),$data);
  115. if($ok) {
  116. $url[] = array('url'=>U( 'Messages/Info/index'),'title'=>'返回列表');
  117. return $this->showMessage('操作成功',urldecode(request('_referer')));
  118. }else{
  119. $url[] = array('url'=>U( 'Messages/Info/index'),'title'=>'返回列表');
  120. return $this->showWarning('操作失败',$url);
  121. }
  122. }
  123. public function view(Request $request) {
  124. $data = $this->repository->find(request('id'));
  125. $pic = json_decode($data['pic_url']);
  126. $data['pic'] = $pic;
  127. return view('admin.messages.info.view',compact('data'));
  128. }
  129. /**
  130. *
  131. * 状态改变
  132. *
  133. */
  134. public function status(Request $request) {
  135. $ok = $this->repository->updateStatus(request('id'),request('status'));
  136. if($ok) {
  137. return $this->showMessage('操作成功');
  138. }else{
  139. return $this->showWarning('操作失败');
  140. }
  141. }
  142. /**
  143. * 删除
  144. */
  145. public function destroy(Request $request) {
  146. $bool = $this->repository->destroy($request->get('id'));
  147. if($bool) {
  148. return $this->showMessage('操作成功');
  149. }else{
  150. return $this->showWarning("操作失败");
  151. }
  152. }
  153. }