Common.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace addons\exam\controller;
  3. use addons\exam\enum\CommonStatus;
  4. use addons\exam\model\PaperModel;
  5. use addons\exam\model\RoomGradeModel;
  6. use addons\exam\model\RoomModel;
  7. use app\admin\model\exam\NoticeModel;
  8. use app\common\exception\UploadException;
  9. use app\common\library\Upload;
  10. use think\Config;
  11. /**
  12. * 公共接口
  13. */
  14. class Common extends Base
  15. {
  16. protected $noNeedLogin = ['index', 'login'];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 读取配置
  20. */
  21. public function index()
  22. {
  23. $data['system'] = getConfig('system_config');
  24. $data['page'] = getConfig('page_config');
  25. $data['ad'] = getConfig('ad_config');
  26. $data['notice'] = NoticeModel::where('status', CommonStatus::NORMAL)->order('weigh desc')->column('name');
  27. $data['notices'] = NoticeModel::where('status', CommonStatus::NORMAL)->order('weigh desc')->field('id,name')->limit(5)->select();
  28. $data['papers'] = $this->indexPaperList();
  29. $data['rooms'] = $this->indexRoomList();
  30. // $data['version'] = $this->getAppVersion();
  31. $this->success('请求成功', $data);
  32. }
  33. /**
  34. * 上传文件
  35. */
  36. public function upload()
  37. {
  38. Config::set('default_return_type', 'json');
  39. //必须设定cdnurl为空,否则cdnurl函数计算错误
  40. Config::set('upload.cdnurl', '');
  41. $chunkid = $this->request->post("chunkid");
  42. if ($chunkid) {
  43. if (!Config::get('upload.chunking')) {
  44. $this->error(__('Chunk file disabled'));
  45. }
  46. $action = $this->request->post("action");
  47. $chunkindex = $this->request->post("chunkindex/d");
  48. $chunkcount = $this->request->post("chunkcount/d");
  49. $filename = $this->request->post("filename");
  50. $method = $this->request->method(true);
  51. if ($action == 'merge') {
  52. $attachment = null;
  53. //合并分片文件
  54. try {
  55. $upload = new Upload();
  56. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  57. } catch (UploadException $e) {
  58. $this->error($e->getMessage());
  59. }
  60. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  61. } elseif ($method == 'clean') {
  62. //删除冗余的分片文件
  63. try {
  64. $upload = new Upload();
  65. $upload->clean($chunkid);
  66. } catch (UploadException $e) {
  67. $this->error($e->getMessage());
  68. }
  69. $this->success();
  70. } else {
  71. //上传分片文件
  72. //默认普通上传文件
  73. $file = $this->request->file('file');
  74. try {
  75. $upload = new Upload($file);
  76. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  77. } catch (UploadException $e) {
  78. $this->error($e->getMessage());
  79. }
  80. $this->success();
  81. }
  82. } else {
  83. $attachment = null;
  84. //默认普通上传文件
  85. $file = $this->request->file('file');
  86. try {
  87. $upload = new Upload($file);
  88. $attachment = $upload->upload();
  89. } catch (UploadException $e) {
  90. $this->error($e->getMessage());
  91. }
  92. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  93. }
  94. }
  95. /**
  96. * 首页试卷列表
  97. * @return array
  98. */
  99. protected function indexPaperList()
  100. {
  101. $now = time();
  102. $papers = PaperModel::where('status', CommonStatus::NORMAL)
  103. ->where('is_only_room', 0)
  104. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
  105. ->order('join_count', 'desc')
  106. ->limit(6)->select();
  107. foreach ($papers as &$paper) {
  108. // 试卷参与人员
  109. $users = PaperModel::getJoinUsers($paper['id'], -4);
  110. // 参与人员头像
  111. $user_avatars = [];
  112. foreach ($users as $user) {
  113. $user_avatars[] = [
  114. 'src' => $user['avatar']
  115. ];
  116. }
  117. $paper['users'] = $user_avatars;
  118. }
  119. return $papers;
  120. }
  121. /**
  122. * 首页考场列表
  123. * @return array
  124. */
  125. protected function indexRoomList()
  126. {
  127. $rooms = RoomModel::where('status', CommonStatus::NORMAL)->order('grade_count', 'desc')->limit(6)->select();
  128. foreach ($rooms as &$room) {
  129. // 考场考试人员
  130. $users = RoomGradeModel::getJoinUsers($room['id'], -4);
  131. // 参与人员头像
  132. $user_avatars = [];
  133. foreach ($users as $user) {
  134. $user_avatars[] = [
  135. 'src' => $user['avatar']
  136. ];
  137. }
  138. $room['users'] = $user_avatars;
  139. }
  140. return $rooms;
  141. }
  142. }