Index.php 6.2 KB

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