Common.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // 处理banner cdn url
  32. if (!empty($data['system']['banner'])) {
  33. $banners = explode(',', $data['system']['banner']);
  34. foreach ($banners as &$banner) {
  35. $banner = cdnurl($banner, true);
  36. }
  37. $data['system']['banner'] = implode(',', $banners);
  38. }
  39. // cdn域名
  40. $data['cdn_url'] = cdnurl('', true);
  41. $this->success('请求成功', $data);
  42. }
  43. /**
  44. * 上传文件
  45. */
  46. public function upload()
  47. {
  48. Config::set('default_return_type', 'json');
  49. //必须设定cdnurl为空,否则cdnurl函数计算错误
  50. Config::set('upload.cdnurl', '');
  51. $chunkid = $this->request->post("chunkid");
  52. if ($chunkid) {
  53. if (!Config::get('upload.chunking')) {
  54. $this->error(__('Chunk file disabled'));
  55. }
  56. $action = $this->request->post("action");
  57. $chunkindex = $this->request->post("chunkindex/d");
  58. $chunkcount = $this->request->post("chunkcount/d");
  59. $filename = $this->request->post("filename");
  60. $method = $this->request->method(true);
  61. if ($action == 'merge') {
  62. $attachment = null;
  63. //合并分片文件
  64. try {
  65. $upload = new Upload();
  66. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  67. } catch (UploadException $e) {
  68. $this->error($e->getMessage());
  69. }
  70. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  71. } elseif ($method == 'clean') {
  72. //删除冗余的分片文件
  73. try {
  74. $upload = new Upload();
  75. $upload->clean($chunkid);
  76. } catch (UploadException $e) {
  77. $this->error($e->getMessage());
  78. }
  79. $this->success();
  80. } else {
  81. //上传分片文件
  82. //默认普通上传文件
  83. $file = $this->request->file('file');
  84. try {
  85. $upload = new Upload($file);
  86. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  87. } catch (UploadException $e) {
  88. $this->error($e->getMessage());
  89. }
  90. $this->success();
  91. }
  92. } else {
  93. $attachment = null;
  94. //默认普通上传文件
  95. $file = $this->request->file('file');
  96. try {
  97. $upload = new Upload($file);
  98. $attachment = $upload->upload();
  99. } catch (UploadException $e) {
  100. $this->error($e->getMessage());
  101. }
  102. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  103. }
  104. }
  105. /**
  106. * 首页试卷列表
  107. * @return array
  108. */
  109. protected function indexPaperList()
  110. {
  111. $now = time();
  112. $papers = PaperModel::where('status', CommonStatus::NORMAL)
  113. ->where('is_only_room', 0)
  114. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
  115. ->order('join_count', 'desc')
  116. ->limit(6)->select();
  117. foreach ($papers as &$paper) {
  118. // 试卷参与人员
  119. $users = PaperModel::getJoinUsers($paper['id'], -4);
  120. // 参与人员头像
  121. $user_avatars = [];
  122. foreach ($users as $user) {
  123. $user_avatars[] = [
  124. 'src' => $user['avatar']
  125. ];
  126. }
  127. $paper['users'] = $user_avatars;
  128. }
  129. return $papers;
  130. }
  131. /**
  132. * 首页考场列表
  133. * @return array
  134. */
  135. protected function indexRoomList()
  136. {
  137. $rooms = RoomModel::where('status', CommonStatus::NORMAL)->order('grade_count', 'desc')->limit(6)->select();
  138. foreach ($rooms as &$room) {
  139. // 考场考试人员
  140. $users = RoomGradeModel::getJoinUsers($room['id'], -4);
  141. // 参与人员头像
  142. $user_avatars = [];
  143. foreach ($users as $user) {
  144. $user_avatars[] = [
  145. 'src' => $user['avatar']
  146. ];
  147. }
  148. $room['users'] = $user_avatars;
  149. }
  150. return $rooms;
  151. }
  152. }