Topic.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Topic\Type as Request;
  3. use App\Models\Topic\Type as Model;
  4. /**
  5. * 帖子管理
  6. *
  7. * @author 刘相欣
  8. *
  9. */
  10. class Topic extends Auth{
  11. protected function _initialize(){
  12. parent::_initialize();
  13. $this->assign('breadcrumb1','社区管理');
  14. $this->assign('breadcrumb2','社区内容');
  15. }
  16. /**
  17. * 列表页
  18. *
  19. * */
  20. public function index(Model $Model){
  21. // 查询条件
  22. $map = [];
  23. // 编码ID
  24. // 查询数据
  25. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  26. // 分配数据
  27. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  28. $this->assign('list',$list);
  29. // 加载模板
  30. return $this->fetch();
  31. }
  32. /**
  33. * 添加
  34. *
  35. * */
  36. public function add(Request $request,Model $Model){
  37. if( request()->isMethod('post') ){
  38. // 验证参数
  39. $request->scene('add')->validate();
  40. // 接收数据
  41. $data['name'] = request('name','');
  42. // 写入数据表
  43. $id = $Model->add($data);
  44. // 如果操作失败
  45. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  46. // 记录行为
  47. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  48. // 告知结果
  49. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  50. }
  51. // 分配数据
  52. $this->assign('crumbs','新增');
  53. // 加载模板
  54. return $this->fetch();
  55. }
  56. /**
  57. * 修改
  58. *
  59. * */
  60. public function edit(Request $request,Model $Model){
  61. // 接收参数
  62. $id = request('id',0);
  63. // 查询用户
  64. $oldData = $Model->where(['id'=>$id])->first();
  65. // 修改
  66. if(request()->isMethod('post')){
  67. // 验证参数
  68. $request->scene('edit')->validate();
  69. // 接收数据
  70. $data['name'] = request('name','');
  71. // 写入数据表
  72. $result = $Model->edit($id,$data);
  73. // 如果操作失败
  74. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  75. // 记录行为
  76. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  77. // 告知结果
  78. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  79. }
  80. // 错误告知
  81. if( !$oldData ) return $this->error('查无数据');
  82. // 分配数据
  83. $this->assign('oldData',$oldData);
  84. $this->assign('crumbs','修改');
  85. // 加载模板
  86. return $this->fetch();
  87. }
  88. /**
  89. * 修改状态
  90. *
  91. * */
  92. public function set_status(Request $request,Model $Model){
  93. // 验证参数
  94. $request->scene('set_status')->validate();
  95. // 设置状态
  96. $id = request('id',0);
  97. $status = request('status',0);
  98. // 查询用户
  99. $oldData = $Model->where(['id'=>$id])->first();
  100. // 如果用户不存在
  101. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  102. // 执行修改
  103. $result = $Model->edit($id,['status'=>$status]);
  104. // 提示新增失败
  105. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  106. // 记录行为
  107. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  108. // 告知结果
  109. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  110. }
  111. }