Index.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_uid',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_uid'] != 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. ->select();
  101. foreach($papers as $key => &$val){
  102. $val['image'] = localpath_to_netpath($val['image']);
  103. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  104. }
  105. $this->success('', $papers);
  106. }
  107. //培训,更多,所有培训列表
  108. public function all_trainactive_list(){
  109. $type = input('type',1);
  110. $now = time();
  111. $where = [];
  112. if($type == 3){
  113. $where['pingjia_uid'] = ['NEQ',0];
  114. }elseif($type == 1){
  115. $where['starttime'] = ['gt',$now];
  116. }else{
  117. $where['starttime'] = ['lt',$now];
  118. $where['endtime'] = ['gt',$now];
  119. }
  120. //非实名,只能看一部分
  121. $where_auth = "";
  122. if($this->auth->idcard_status != 1){
  123. $where_auth = "(userauth_status = 1) or (find_in_set('".$this->auth->id."',user_ids) )";
  124. }
  125. $papers = Db::name('train_active')->field('id,name as title,logo_image,starttime,endtime,pingjia_time,pingjia_uid')
  126. ->where('deletetime', NULL)
  127. ->where($where)
  128. ->where($where_auth)
  129. ->autopage()
  130. ->order('starttime desc')
  131. ->select();
  132. foreach($papers as $key => &$val){
  133. $val['logo_image'] = localpath_to_netpath($val['logo_image']);
  134. if($val['pingjia_uid'] != 0){ //时间或评价,都行
  135. $val['show_status'] = 3;
  136. $val['show_status_text'] = '已结束';
  137. }elseif($now < $val['starttime']){
  138. $val['show_status'] = 1;
  139. $val['show_status_text'] = '待开始';
  140. }else{
  141. $val['show_status'] = 2;
  142. $val['show_status_text'] = '进行中';
  143. }
  144. }
  145. $this->success(1, $papers);
  146. }
  147. //在线考试
  148. //我的考试
  149. //可参加考试
  150. //我的试卷
  151. public function my_paper_list(){
  152. $now = time();
  153. $papers = Db::name('exam_paper')->field('id,title,start_time,end_time,total_score,limit_time')
  154. ->where('status', 'NORMAL')
  155. ->where('deletetime', NULL)
  156. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
  157. ->where('find_in_set(:user_ids,user_ids)', ['user_ids' => $this->auth->id])
  158. ->autopage()
  159. ->select();
  160. foreach($papers as $key => &$val){
  161. $val['limit_time'] = $val['limit_time']/60; //秒转换分种
  162. $val['is_start'] = 1;
  163. }
  164. $this->success('', $papers);
  165. }
  166. //历史考试
  167. public function my_grade_list(){
  168. $field = [
  169. 'grade.score','grade.is_pass','grade.total_score',
  170. 'grade.total_count','grade.right_count','grade.error_count',
  171. 'grade.grade_time','grade.start_time',
  172. 'paper.title'
  173. ];
  174. $list = Db::name('exam_grade')->alias('grade')
  175. ->join('exam_paper paper','grade.paper_id = paper.id','LEFT')
  176. ->field($field)
  177. ->where('grade.user_id',$this->auth->id)
  178. ->autopage()
  179. ->select();
  180. foreach($list as $key => &$val){
  181. $val['grade_time_text'] = Sec2Time($val['grade_time']);
  182. }
  183. $this->success('', $list);
  184. }
  185. }