Index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 首页接口
  7. */
  8. class Index extends Api
  9. {
  10. protected $noNeedLogin = ['index'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 首页
  14. * 热门试卷3个
  15. * 未开始的 + 进行中的
  16. * 我能参与的
  17. */
  18. public function index()
  19. {
  20. $now = time();
  21. $papers = Db::name('exam_paper')->field('id,image,title,start_time,end_time,total_score,limit_time,user_ids')
  22. ->where('status', 'NORMAL')
  23. ->where('deletetime', NULL)
  24. ->whereRaw("((end_time = 0) or (end_time > {$now}))")
  25. ->limit(2)
  26. ->order('start_time desc')
  27. ->select();
  28. if(empty($papers)){
  29. $papers = Db::name('exam_paper')->field('id,image,title,start_time,end_time,total_score,limit_time,user_ids')
  30. ->where('status', 'NORMAL')
  31. ->where('deletetime', NULL)
  32. ->limit(2)
  33. ->order('start_time desc')
  34. ->select();
  35. }
  36. foreach($papers as $key => &$val){
  37. $val['image'] = localpath_to_netpath($val['image']);
  38. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  39. $val['is_start'] = 0;
  40. if($val['start_time'] < $now && $now < $val['end_time'] && in_array($this->auth->id,explode(',',$val['user_ids'])) ){
  41. $val['is_start'] = 1;
  42. }
  43. unset($val['user_ids']);
  44. }
  45. $this->success('', $papers);
  46. }
  47. //考试,更多,所有考试列表
  48. public function all_paper_list(){
  49. //非实名,只能看一部分
  50. $where_auth = "";
  51. if($this->auth->idcard_status != 1){
  52. $where_auth = "(find_in_set('".$this->auth->id."',user_ids) )";
  53. }
  54. $papers = Db::name('exam_paper')->field('id,image,title,start_time,end_time,total_score,limit_time')
  55. ->where('status', 'NORMAL')
  56. ->where('deletetime', NULL)
  57. ->where($where_auth)
  58. ->autopage()
  59. ->order('start_time desc')
  60. ->select();
  61. foreach($papers as $key => &$val){
  62. $val['image'] = localpath_to_netpath($val['image']);
  63. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  64. }
  65. $this->success('', $papers);
  66. }
  67. //在线考试
  68. //我的考试
  69. //可参加考试
  70. //我的试卷
  71. public function my_paper_list(){
  72. $now = time();
  73. $papers = Db::name('exam_paper')->field('id,title,start_time,end_time,total_score,limit_time')
  74. ->where('status', 'NORMAL')
  75. ->where('deletetime', NULL)
  76. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
  77. ->where('find_in_set(:user_ids,user_ids)', ['user_ids' => $this->auth->id])
  78. ->order('start_time desc')
  79. ->autopage()
  80. ->select();
  81. foreach($papers as $key => &$val){
  82. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  83. $val['is_start'] = 1;
  84. }
  85. $this->success('', $papers);
  86. }
  87. //历史考试
  88. public function my_grade_list(){
  89. $field = [
  90. 'grade.score','grade.is_pass','grade.total_score',
  91. 'grade.total_count','grade.right_count','grade.error_count',
  92. 'grade.grade_time','grade.start_time',
  93. 'paper.title'
  94. ];
  95. $list = Db::name('exam_grade')->alias('grade')
  96. ->join('exam_paper paper','grade.paper_id = paper.id','LEFT')
  97. ->field($field)
  98. ->where('grade.user_id',$this->auth->id)
  99. ->order('grade.id desc')
  100. ->autopage()
  101. ->select();
  102. foreach($list as $key => &$val){
  103. $val['grade_time_text'] = Sec2Time($val['grade_time']);
  104. }
  105. $this->success('', $list);
  106. }
  107. }