Common.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Upload;
  6. use app\common\model\Area;
  7. use app\common\model\Version;
  8. use fast\Random;
  9. use think\captcha\Captcha;
  10. use think\Config;
  11. use think\Hook;
  12. /**
  13. * 公共接口
  14. */
  15. class Common extends Api
  16. {
  17. protected $noNeedLogin = ['init', 'captcha','upload'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. if (isset($_SERVER['HTTP_ORIGIN'])) {
  22. header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
  23. }
  24. //跨域检测
  25. check_cors_request();
  26. if (!isset($_COOKIE['PHPSESSID'])) {
  27. Config::set('session.id', $this->request->server("HTTP_SID"));
  28. }
  29. parent::_initialize();
  30. }
  31. /**
  32. * 加载初始化
  33. *
  34. * @param string $version 版本号
  35. * @param string $lng 经度
  36. * @param string $lat 纬度
  37. */
  38. public function init()
  39. {
  40. if ($version = $this->request->request('version')) {
  41. $lng = $this->request->request('lng');
  42. $lat = $this->request->request('lat');
  43. //配置信息
  44. $upload = Config::get('upload');
  45. //如果非服务端中转模式需要修改为中转
  46. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  47. //临时修改上传模式为服务端中转
  48. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  49. $upload = \app\common\model\Config::upload();
  50. // 上传信息配置后
  51. Hook::listen("upload_config_init", $upload);
  52. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  53. }
  54. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  55. $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  56. $content = [
  57. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  58. 'versiondata' => Version::check($version),
  59. 'uploaddata' => $upload,
  60. 'coverdata' => Config::get("cover"),
  61. ];
  62. $this->success('', $content);
  63. } else {
  64. $this->error(__('Invalid parameters'));
  65. }
  66. }
  67. /**
  68. * 上传文件
  69. * @ApiMethod (POST)
  70. * @param File $file 文件流
  71. */
  72. public function upload()
  73. {
  74. Config::set('default_return_type', 'json');
  75. //必须设定cdnurl为空,否则cdnurl函数计算错误
  76. Config::set('upload.cdnurl', '');
  77. $chunkid = $this->request->post("chunkid");
  78. if ($chunkid) {
  79. if (!Config::get('upload.chunking')) {
  80. $this->error(__('Chunk file disabled'));
  81. }
  82. $action = $this->request->post("action");
  83. $chunkindex = $this->request->post("chunkindex/d");
  84. $chunkcount = $this->request->post("chunkcount/d");
  85. $filename = $this->request->post("filename");
  86. $method = $this->request->method(true);
  87. if ($action == 'merge') {
  88. $attachment = null;
  89. //合并分片文件
  90. try {
  91. $upload = new Upload();
  92. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  93. } catch (UploadException $e) {
  94. $this->error($e->getMessage());
  95. }
  96. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  97. } elseif ($method == 'clean') {
  98. //删除冗余的分片文件
  99. try {
  100. $upload = new Upload();
  101. $upload->clean($chunkid);
  102. } catch (UploadException $e) {
  103. $this->error($e->getMessage());
  104. }
  105. $this->success();
  106. } else {
  107. //上传分片文件
  108. //默认普通上传文件
  109. $file = $this->request->file('file');
  110. try {
  111. $upload = new Upload($file);
  112. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  113. } catch (UploadException $e) {
  114. $this->error($e->getMessage());
  115. }
  116. $this->success();
  117. }
  118. } else {
  119. $attachment = null;
  120. //默认普通上传文件
  121. $file = $this->request->file('file');
  122. try {
  123. $upload = new Upload($file);
  124. $attachment = $upload->upload();
  125. } catch (UploadException $e) {
  126. $this->error($e->getMessage());
  127. } catch (\Exception $e) {
  128. $this->error($e->getMessage());
  129. }
  130. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  131. }
  132. }
  133. /**
  134. * 验证码
  135. * @param $id
  136. * @return \think\Response
  137. */
  138. public function captcha($id = "")
  139. {
  140. \think\Config::set([
  141. 'captcha' => array_merge(config('captcha'), [
  142. 'fontSize' => 44,
  143. 'imageH' => 150,
  144. 'imageW' => 350,
  145. ])
  146. ]);
  147. $captcha = new Captcha((array)Config::get('captcha'));
  148. return $captcha->entry($id);
  149. }
  150. }