Common.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Config;
  10. use think\Hook;
  11. /**
  12. * 公共接口
  13. */
  14. class Common extends Api
  15. {
  16. protected $noNeedLogin = ['init','upload'];
  17. protected $noNeedRight = '*';
  18. /**
  19. * 加载初始化
  20. *
  21. * @param string $version 版本号
  22. * @param string $lng 经度
  23. * @param string $lat 纬度
  24. */
  25. public function init()
  26. {
  27. if ($version = $this->request->request('version')) {
  28. $lng = $this->request->request('lng');
  29. $lat = $this->request->request('lat');
  30. //配置信息
  31. $upload = Config::get('upload');
  32. //如果非服务端中转模式需要修改为中转
  33. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  34. //临时修改上传模式为服务端中转
  35. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  36. $upload = \app\common\model\Config::upload();
  37. // 上传信息配置后
  38. Hook::listen("upload_config_init", $upload);
  39. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  40. }
  41. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  42. $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  43. $content = [
  44. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  45. 'versiondata' => Version::check($version),
  46. 'uploaddata' => $upload,
  47. 'coverdata' => Config::get("cover"),
  48. ];
  49. $this->success('', $content);
  50. } else {
  51. $this->error(__('Invalid parameters'));
  52. }
  53. }
  54. /**
  55. * 上传文件
  56. * @ApiMethod (POST)
  57. * @param File $file 文件流
  58. */
  59. public function upload()
  60. {
  61. Config::set('default_return_type', 'json');
  62. //必须设定cdnurl为空,否则cdnurl函数计算错误
  63. Config::set('upload.cdnurl', '');
  64. $chunkid = $this->request->post("chunkid");
  65. if ($chunkid) {
  66. if (!Config::get('upload.chunking')) {
  67. $this->error(__('Chunk file disabled'));
  68. }
  69. $action = $this->request->post("action");
  70. $chunkindex = $this->request->post("chunkindex/d");
  71. $chunkcount = $this->request->post("chunkcount/d");
  72. $filename = $this->request->post("filename");
  73. $method = $this->request->method(true);
  74. if ($action == 'merge') {
  75. $attachment = null;
  76. //合并分片文件
  77. try {
  78. $upload = new Upload();
  79. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  80. } catch (UploadException $e) {
  81. $this->error($e->getMessage());
  82. }
  83. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  84. } elseif ($method == 'clean') {
  85. //删除冗余的分片文件
  86. try {
  87. $upload = new Upload();
  88. $upload->clean($chunkid);
  89. } catch (UploadException $e) {
  90. $this->error($e->getMessage());
  91. }
  92. $this->success();
  93. } else {
  94. //上传分片文件
  95. //默认普通上传文件
  96. $file = $this->request->file('file');
  97. try {
  98. $upload = new Upload($file);
  99. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  100. } catch (UploadException $e) {
  101. $this->error($e->getMessage());
  102. }
  103. $this->success();
  104. }
  105. } else {
  106. $attachment = null;
  107. //默认普通上传文件
  108. $file = $this->request->file('file');
  109. try {
  110. $upload = new Upload($file);
  111. $attachment = $upload->upload();
  112. } catch (UploadException $e) {
  113. $this->error($e->getMessage());
  114. }
  115. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  116. }
  117. }
  118. }