Common.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 公共接口
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\common\controller\Api;
  16. use app\common\exception\UploadException;
  17. use app\common\library\Upload as UploadLib;
  18. use think\facade\Config;
  19. class Common extends Api
  20. {
  21. protected $noNeedLogin = ['init', 'captcha'];
  22. protected $noNeedRight = '*';
  23. /**
  24. * 加载初始化
  25. *
  26. * @param string $version 版本号
  27. * @param string $lng 经度
  28. * @param string $lat 纬度
  29. */
  30. public function init()
  31. {
  32. }
  33. /**
  34. * 上传文件
  35. */
  36. public function upload()
  37. {
  38. $dir = $this->request->param("dir");
  39. $from = $this->request->param("from");
  40. if ($dir == '') {
  41. $this->error('没有指定上传目录');
  42. }
  43. $chunkid = $this->request->post("chunkid");
  44. if ($chunkid) {
  45. if (!config::get('upload.chunking')) {
  46. $this->error('未开启分片上传功能');
  47. }
  48. //分片
  49. $action = $this->request->post("action");
  50. $chunkindex = $this->request->post("chunkindex/d", 0);
  51. $chunkcount = $this->request->post("chunkcount/d", 1);
  52. $filename = $this->request->post("filename");
  53. $method = $this->request->method(true);
  54. if ($action == 'merge') {
  55. $attachment = null;
  56. //合并分片文件
  57. try {
  58. $upload = new UploadLib();
  59. $attachment = $upload->merge($chunkid, $chunkcount, $filename, $dir, $from);
  60. } catch (UploadException $e) {
  61. $this->error($e->getMessage());
  62. }
  63. $this->success('上传成功', $attachment->getBaseData());
  64. } elseif ($method == 'clean') {
  65. //删除冗余的分片文件
  66. try {
  67. $upload = new UploadLib();
  68. $upload->clean($chunkid);
  69. } catch (UploadException $e) {
  70. $this->error($e->getMessage());
  71. }
  72. $this->success();
  73. } else {
  74. //上传分片文件
  75. $file = $this->request->file('file');
  76. try {
  77. $upload = new UploadLib($file);
  78. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  79. } catch (UploadException $e) {
  80. $this->error($e->getMessage());
  81. }
  82. $this->success();
  83. }
  84. }
  85. $attachment = null;
  86. try {
  87. //默认普通上传文件
  88. $file = $this->request->file('file');
  89. $upload = new UploadLib($file);
  90. $attachment = $upload->upload($dir);
  91. } catch (UploadException | Exception $e) {
  92. $this->error($e->getMessage());
  93. }
  94. $this->success('上传成功', $attachment->getBaseData());
  95. }
  96. }