CscProblem.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/9/25
  6. * Time: 10:14
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Base;
  10. use think\Db;
  11. class CscProblem extends Admin
  12. {
  13. private $_type = [
  14. '1' => '账号问题',
  15. '2' => '充值问题',
  16. '3' => '申诉问题',
  17. '4' => '人工服务',
  18. '5' => '其他',
  19. ];
  20. protected function _initialize()
  21. {
  22. parent::_initialize(); // TODO: Change the autogenerated stub
  23. $this->cscModel = model('CpsProblem');
  24. }
  25. protected function getParam(){
  26. // 搜索条件
  27. $where = [];
  28. $title = $this->request->param('title', '', 'trim');
  29. if (!empty($title)){
  30. $where['title'] = ['like','%'.$title.'%'];
  31. }
  32. $type = $this->request->param('type', 0, 'intval');
  33. if (!empty($type)){
  34. $where['type'] = $type;
  35. }
  36. $start_time = input('request.cr_start');
  37. //开始时间和结束时间不为空时
  38. if ($start_time != '' && input('request.cr_end') != '') {
  39. $where['create_time'] = [
  40. ['>=', strtotime($start_time)],
  41. ['<=', strtotime(input('request.cr_end').' 23:59:59')],
  42. ];
  43. } //开始时间不为空时
  44. elseif ($start_time!= '') {
  45. $where['create_time'] = ['>=', strtotime($start_time)];
  46. } //结束时间不为空时
  47. elseif (input('request.cr_end') != '') {
  48. $where['create_time'] = ['<=', strtotime(input('request.cr_end').' 23:59:59')];
  49. }
  50. return $where;
  51. }
  52. /**
  53. * 列表
  54. */
  55. public function index(){
  56. // 排序条件
  57. $orderStr = 'create_time desc';
  58. $orderUrl = 'asc';
  59. $order = $this->request->param('order');
  60. if ($order == 'desc'){
  61. $orderUrl = 'asc';
  62. $orderStr = 'create_time desc';
  63. }elseif ($order == 'asc'){
  64. $orderStr = 'order asc,create_time asc';
  65. $orderUrl = 'desc';
  66. }
  67. $where = $this->getParam();
  68. $list = $this->cscModel
  69. ->where($where)
  70. ->order($orderStr)
  71. ->paginate(10, false, array('query' => input('get.')));
  72. $this->assign('orderUrl',$orderUrl);
  73. $this->assign('typeList',$this->_type);
  74. $this->assign('list',$list);
  75. $this->assign('page',$list->render());
  76. return $this->fetch();
  77. }
  78. /**
  79. * 查看问题答案
  80. */
  81. public function seeAnswer(){
  82. $id = $this->request->param('id',0,'intval');
  83. if (empty($id)) $this->error('参数错误!');
  84. $info = $this->cscModel->where('id',$id)->find();
  85. if (empty($info)) $this->error('查无数据');
  86. $this->success($info);
  87. }
  88. /**
  89. * 删除
  90. */
  91. public function del(){
  92. $id = $this->request->param('id',0,'intval');
  93. if (empty($id)) $this->error('参数错误!');
  94. $res = $this->cscModel->where('id',$id)->delete();
  95. if ($res) $this->success('删除成功');
  96. $this->error('删除失败');
  97. }
  98. /**
  99. * 添加
  100. */
  101. public function add(){
  102. if ($this->request->isPost()){
  103. $data = [
  104. 'title'=> input('post.title'),
  105. 'answer'=> input('post.answer'),
  106. 'order'=> input('post.order'),
  107. 'type'=> input('post.type'),
  108. ];
  109. $result = $this->validate($data,'CscProblem.add');
  110. if(true !== $result){
  111. // 验证失败 输出错误信息
  112. $this->error($result);
  113. }
  114. $data['create_time'] = NOW_TIMESTAMP;
  115. $res = $this->cscModel->insertGetId($data);
  116. if ($res) $this->success('添加成功!',url('index'));
  117. $this->error('添加失败!');
  118. }
  119. $this->assign('typeList',$this->_type);
  120. return $this->fetch();
  121. }
  122. /**
  123. * 编辑
  124. */
  125. public function edit(){
  126. $id = $this->request->param('id',0,'intval');
  127. if (empty($id)) $this->error('参数错误!');
  128. if ($this->request->isPost()){
  129. $data = [
  130. 'title'=> input('post.title'),
  131. 'answer'=> input('post.answer'),
  132. 'order'=> input('post.order'),
  133. 'type'=> input('post.type'),
  134. ];
  135. $result = $this->validate($data,'CscProblem.edit');
  136. if(true !== $result){
  137. // 验证失败 输出错误信息
  138. $this->error($result);
  139. }
  140. if ($this->cscModel->allowField(true)->save($data, ['id' => $id]) !== false ) {
  141. $this->success('编辑成功',url('index'));
  142. }
  143. $this->error($this->cscModel->getError() ?: '编辑失败');
  144. }
  145. $info = $this->cscModel->where('id',$id)->find();
  146. $this->assign('info',$info);
  147. $this->assign('typeList',$this->_type);
  148. return $this->fetch();
  149. }
  150. }