Common.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  11. * 公共接口
  12. */
  13. class Common extends Api
  14. {
  15. protected $noNeedLogin = ['init'];
  16. protected $noNeedRight = '*';
  17. /**
  18. * 加载初始化
  19. *
  20. * @param string $version 版本号
  21. * @param string $lng 经度
  22. * @param string $lat 纬度
  23. */
  24. public function init()
  25. {
  26. if ($version = $this->request->request('version')) {
  27. $lng = $this->request->request('lng');
  28. $lat = $this->request->request('lat');
  29. $content = [
  30. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  31. 'versiondata' => Version::check($version),
  32. 'uploaddata' => Config::get('upload'),
  33. 'coverdata' => Config::get("cover"),
  34. ];
  35. $this->success('', $content);
  36. } else {
  37. $this->error(__('Invalid parameters'));
  38. }
  39. }
  40. /**
  41. * 上传文件
  42. * @ApiMethod (POST)
  43. * @param File $file 文件流
  44. */
  45. public function upload()
  46. {
  47. Config::set('default_return_type', 'json');
  48. //必须设定cdnurl为空,否则cdnurl函数计算错误
  49. Config::set('upload.cdnurl', '');
  50. $chunkid = $this->request->post("chunkid");
  51. if ($chunkid) {
  52. if (!Config::get('upload.chunking')) {
  53. $this->error(__('Chunk file disabled'));
  54. }
  55. $action = $this->request->post("action");
  56. $chunkindex = $this->request->post("chunkindex/d");
  57. $chunkcount = $this->request->post("chunkcount/d");
  58. $filename = $this->request->post("filename");
  59. $method = $this->request->method(true);
  60. if ($action == 'merge') {
  61. $attachment = null;
  62. //合并分片文件
  63. try {
  64. $upload = new Upload();
  65. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  66. } catch (UploadException $e) {
  67. $this->error($e->getMessage());
  68. }
  69. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  70. } elseif ($method == 'clean') {
  71. //删除冗余的分片文件
  72. try {
  73. $upload = new Upload();
  74. $upload->clean($chunkid);
  75. } catch (UploadException $e) {
  76. $this->error($e->getMessage());
  77. }
  78. $this->success();
  79. } else {
  80. //上传分片文件
  81. //默认普通上传文件
  82. $file = $this->request->file('file');
  83. try {
  84. $upload = new Upload($file);
  85. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  86. } catch (UploadException $e) {
  87. $this->error($e->getMessage());
  88. }
  89. $this->success();
  90. }
  91. } else {
  92. $attachment = null;
  93. //默认普通上传文件
  94. $file = $this->request->file('file');
  95. try {
  96. $upload = new Upload($file);
  97. $attachment = $upload->upload();
  98. } catch (UploadException $e) {
  99. $this->error($e->getMessage());
  100. }
  101. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  102. }
  103. }
  104. }