RiddleQuestion.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Riddle\Question as Request;
  3. use App\Models\Riddle\Question as Model;
  4. /**
  5. * 灯谜活动题目
  6. *
  7. * @author 刘相欣
  8. *
  9. */
  10. class RiddleQuestion 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. $name = request('title','');
  23. // 查询条件
  24. $map = [];
  25. // 组合条件
  26. if( $name ) $map[] = ['title','=',$name];
  27. // 查询数据
  28. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
  29. // 循环处理数据
  30. foreach ($list as $key => $value) {
  31. // 重组
  32. $list[$key] = $value;
  33. }
  34. // 分配数据
  35. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  36. $this->assign('list',$list);
  37. // 加载模板
  38. return $this->fetch();
  39. }
  40. /**
  41. * 添加
  42. *
  43. * */
  44. public function add(Request $request,Model $Model){
  45. if( request()->isMethod('post') ){
  46. // 验证参数
  47. $request->scene('add')->validate();
  48. // 接收数据
  49. $data['title'] = request('title','');
  50. $data['status'] = 1;
  51. // 写入数据表
  52. $id = $Model->add($data);
  53. // 如果操作失败
  54. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  55. // 记录行为
  56. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  57. // 告知结果
  58. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  59. }
  60. // 分配数据
  61. $this->assign('crumbs','新增');
  62. // 加载模板
  63. return $this->fetch();
  64. }
  65. /**
  66. * 修改
  67. *
  68. * */
  69. public function edit(Request $request,Model $Model){
  70. // 接收参数
  71. $id = request('id',0);
  72. // 查询用户
  73. $oldData = $Model->where(['id'=>$id])->first();
  74. // 修改
  75. if(request()->isMethod('post')){
  76. // 验证参数
  77. $request->scene('edit')->validate();
  78. // 接收数据
  79. $data['title'] = request('title','');
  80. // 写入数据表
  81. $result = $Model->edit($id,$data);
  82. // 如果操作失败
  83. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  84. // 记录行为
  85. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  86. // 告知结果
  87. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  88. }
  89. // 错误告知
  90. if( !$oldData ) return $this->error('查无数据');
  91. // 分配数据
  92. $this->assign('oldData',$oldData);
  93. $this->assign('crumbs','修改');
  94. // 加载模板
  95. return $this->fetch();
  96. }
  97. /**
  98. * 修改状态
  99. *
  100. * */
  101. public function set_status(Request $request,Model $Model){
  102. // 验证参数
  103. $request->scene('set_status')->validate();
  104. // 设置状态
  105. $id = request('id',0);
  106. $status = request('status',0);
  107. // 查询用户
  108. $oldData = $Model->where(['id'=>$id])->first();
  109. // 如果用户不存在
  110. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  111. // 执行修改
  112. $result = $Model->edit($id,['status'=>$status]);
  113. // 提示新增失败
  114. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  115. // 记录行为
  116. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  117. // 告知结果
  118. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  119. }
  120. }