Index.php 7.1 KB

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