Index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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')
  22. ->where('status', 'NORMAL')
  23. ->where('deletetime', NULL)
  24. // ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
  25. ->whereRaw("((end_time = 0) or (end_time > {$now}))")
  26. // ->where('find_in_set(:user_ids,user_ids)', ['user_ids' => $this->auth->id])
  27. ->limit(3)
  28. ->order('start_time desc')
  29. ->select();
  30. foreach($papers as $key => &$val){
  31. $val['image'] = localpath_to_netpath($val['image']);
  32. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  33. $val['is_start'] = 1;
  34. }
  35. $this->success('', $papers);
  36. }
  37. /**
  38. * 首页
  39. * 全部培训3个
  40. * 未开始的 + 进行中的
  41. */
  42. public function index_trainactive(){
  43. $now = time();
  44. $lists = Db::name('train_active')->field('id,title,logo_image,starttime,endtime,pingjia_time')
  45. ->where('status', 1)
  46. ->where('deletetime', NULL)
  47. ->whereRaw("((endtime = 0) or (endtime > {$now}))")
  48. //->where("(userauth_status = 1) or (find_in_set('".$this->auth->id."',user_ids) )")
  49. ->limit(3)
  50. ->order('starttime desc')
  51. ->select();
  52. foreach($lists as $key => &$val){
  53. $val['logo_image'] = localpath_to_netpath($val['logo_image']);
  54. if($now < $val['starttime']){
  55. $val['show_status'] = 1;
  56. $val['show_status_text'] = '待开始';
  57. }
  58. if($val['starttime'] < $now && $now < $val['endtime']){
  59. $val['show_status'] = 2;
  60. $val['show_status_text'] = '进行中';
  61. }
  62. if($val['endtime'] < $now || $val['pingjia_time'] > 0){ //时间或评价,都行
  63. $val['show_status'] = 3;
  64. $val['show_status_text'] = '已结束';
  65. }
  66. }
  67. $this->success('', $lists);
  68. }
  69. //考试,更多,所有考试列表
  70. public function all_paper_list(){
  71. $papers = Db::name('exam_paper')->field('id,image,title,start_time,end_time,total_score,limit_time')
  72. ->where('status', 'NORMAL')
  73. ->where('deletetime', NULL)
  74. ->autopage()
  75. ->select();
  76. foreach($papers as $key => &$val){
  77. $val['image'] = localpath_to_netpath($val['image']);
  78. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  79. $val['is_start'] = 1;
  80. }
  81. $this->success('', $papers);
  82. }
  83. //培训,更多,所有培训列表
  84. public function all_trainactive_list(){
  85. $type = input('type',1);
  86. $now = time();
  87. $where = [];
  88. if($type == 1){
  89. $where['starttime'] = ['gt',$now];
  90. }
  91. if($type == 2){
  92. $where['starttime'] = ['lt',$now];
  93. $where['endtime'] = ['gt',$now];
  94. }
  95. if($type == 3){
  96. $where = 'endtime < '.$now.' or pingjia_time > 0';
  97. }
  98. $papers = Db::name('train_active')->field('id,title,logo_image,starttime,endtime,pingjia_time')
  99. ->where('status', 1)
  100. ->where('deletetime', NULL)
  101. ->where($where)
  102. ->autopage()
  103. ->order('starttime desc')
  104. ->select();
  105. foreach($papers as $key => &$val){
  106. $val['logo_image'] = localpath_to_netpath($val['logo_image']);
  107. if($now < $val['starttime']){
  108. $val['show_status'] = 1;
  109. $val['show_status_text'] = '待开始';
  110. }
  111. if($val['starttime'] < $now && $now < $val['endtime']){
  112. $val['show_status'] = 2;
  113. $val['show_status_text'] = '进行中';
  114. }
  115. if($val['endtime'] < $now || $val['pingjia_time'] > 0){ //时间或评价,都行
  116. $val['show_status'] = 3;
  117. $val['show_status_text'] = '已结束';
  118. }
  119. }
  120. $this->success('', $papers);
  121. }
  122. //在线考试
  123. //我的考试
  124. //可参加考试
  125. //我的试卷
  126. public function my_paper_list(){
  127. $now = time();
  128. $papers = Db::name('exam_paper')->field('id,title,start_time,end_time,total_score,limit_time')
  129. ->where('status', 'NORMAL')
  130. ->where('deletetime', NULL)
  131. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
  132. ->where('find_in_set(:user_ids,user_ids)', ['user_ids' => $this->auth->id])
  133. ->autopage()
  134. ->select();
  135. foreach($papers as $key => &$val){
  136. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  137. $val['is_start'] = 1;
  138. }
  139. $this->success('', $papers);
  140. }
  141. //历史考试
  142. public function my_grade_list(){
  143. $field = [
  144. 'grade.score','grade.is_pass','grade.total_score',
  145. 'grade.total_count','grade.right_count','grade.error_count',
  146. 'grade.grade_time','grade.start_time',
  147. 'paper.title'
  148. ];
  149. $list = Db::name('exam_grade')->alias('grade')
  150. ->join('exam_paper paper','grade.paper_id = paper.id','LEFT')
  151. ->field($field)
  152. ->where('grade.user_id',$this->auth->id)
  153. ->autopage()
  154. ->select();
  155. foreach($list as $key => &$val){
  156. $val['grade_time_text'] = Sec2Time($val['grade_time']);
  157. }
  158. $this->success('', $list);
  159. }
  160. }