Question.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class Question extends Api
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = new \app\common\model\Question();
  15. }
  16. /**
  17. * 列表
  18. * @return void
  19. */
  20. public function getList()
  21. {
  22. try {
  23. $field = 'id,title,desc';
  24. $where['status'] = 1;
  25. $result = $this->model->field($field)->with(['questionItem'=>function($query){
  26. $query->field('id,title,content')->where(['status'=>1]);
  27. }])->where($where)->select();
  28. $rows = [];
  29. if (!empty($result)) {
  30. foreach ($result as $key => &$value) {
  31. $itemData = isset($value['question_item']) ? $value['question_item'] : '';
  32. $questionItem = [];
  33. foreach ($itemData as $itemKey => $itemVal) {
  34. $questionItem[] = [
  35. 'id' => $itemVal['id'],
  36. 'title' => $itemVal['title'],
  37. 'content' => $itemVal['content'],
  38. ];
  39. }
  40. $rows[] = [
  41. 'id' => $value['id'],
  42. 'title' => $value['title'],
  43. 'desc' => $value['desc'],
  44. 'question_item' => $questionItem,
  45. ];
  46. }
  47. }
  48. $this->success('获取成功',$rows);
  49. } catch (Exception $e) {
  50. $this->error($e->getMessage());
  51. }
  52. }
  53. /**
  54. * 详情
  55. * @return void
  56. */
  57. public function getInfo()
  58. {
  59. try {
  60. $id = $this->request->param('id',0);
  61. $field = 'id,title,content';
  62. $where['id'] = $id;
  63. $result = model('QuestionItem')->field($field)->where($where)->find();
  64. $this->success('获取成功',$result);
  65. } catch (Exception $e) {
  66. $this->error($e->getMessage());
  67. }
  68. }
  69. }