Question.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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')->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. ];
  38. }
  39. $rows[] = [
  40. 'id' => $value['id'],
  41. 'title' => $value['title'],
  42. 'desc' => $value['desc'],
  43. 'question_item' => $questionItem,
  44. ];
  45. }
  46. }
  47. $this->success('获取成功',$rows);
  48. } catch (Exception $e) {
  49. $this->error($e->getMessage());
  50. }
  51. }
  52. /**
  53. * 详情
  54. * @return void
  55. */
  56. public function getInfo()
  57. {
  58. try {
  59. $id = $this->request->param('id',0);
  60. $field = 'id,title,content';
  61. $where['id'] = $id;
  62. $result = model('QuestionItem')->field($field)->where($where)->find();
  63. $this->success('获取成功',$result);
  64. } catch (Exception $e) {
  65. $this->error($e->getMessage());
  66. }
  67. }
  68. }