Common.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $chunkid = $this->request->post("chunkid");
  49. if ($chunkid) {
  50. if (!Config::get('upload.chunking')) {
  51. $this->error(__('Chunk file disabled'));
  52. }
  53. $action = $this->request->post("action");
  54. $chunkindex = $this->request->post("chunkindex/d");
  55. $chunkcount = $this->request->post("chunkcount/d");
  56. $filename = $this->request->post("filename");
  57. $method = $this->request->method(true);
  58. if ($action == 'merge') {
  59. $attachment = null;
  60. //合并分片文件
  61. try {
  62. $upload = new Upload();
  63. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  64. } catch (UploadException $e) {
  65. $this->error($e->getMessage());
  66. }
  67. $this->success(__('Uploaded successful'), ['url' => $attachment->url]);
  68. } elseif ($method == 'clean') {
  69. //删除冗余的分片文件
  70. try {
  71. $upload = new Upload();
  72. $upload->clean($chunkid);
  73. } catch (UploadException $e) {
  74. $this->error($e->getMessage());
  75. }
  76. $this->success();
  77. } else {
  78. //上传分片文件
  79. //默认普通上传文件
  80. $file = $this->request->file('file');
  81. try {
  82. $upload = new Upload($file);
  83. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  84. } catch (UploadException $e) {
  85. $this->error($e->getMessage());
  86. }
  87. $this->success();
  88. }
  89. } else {
  90. $attachment = null;
  91. //默认普通上传文件
  92. $file = $this->request->file('file');
  93. try {
  94. $upload = new Upload($file);
  95. $attachment = $upload->upload();
  96. } catch (UploadException $e) {
  97. $this->error($e->getMessage());
  98. }
  99. $this->success(__('Uploaded successful'), ['url' => $attachment->url]);
  100. }
  101. }
  102. }