Common.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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'];
  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. //
  116. // $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  117. // }
  118. //
  119. // }
  120. /**
  121. * 上传文件
  122. * @ApiMethod (POST)
  123. * @param File $file 文件流
  124. */
  125. public function upload()
  126. {
  127. $folder = 'avatar';
  128. $files = request()->file();
  129. if (empty($files) || $files == null) {
  130. $this->error('请上传文件');
  131. }
  132. $imags = [];
  133. $errors = [];
  134. foreach ($files as $file) {
  135. if ($folder) { //保存目录
  136. // 移动到框架应用根目录/public/uploads/ 目录下
  137. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $folder);
  138. if ($info) {
  139. // 成功上传后 获取上传信息
  140. //echo $info->getFilename();
  141. // $path = '/uploads/editor/' . $info->getSaveName();
  142. $path = $folder . DS . $info->getSaveName();
  143. $fileName = str_replace('\\', '/', $path);
  144. array_push($imags, '/' . 'uploads/'.$fileName);
  145. } else {
  146. array_push($errors, $file->getError()); // 上传失败获取错误信息
  147. }
  148. }
  149. }
  150. if (!$errors) {
  151. $msg['code'] = 1;
  152. $msg['data'] = $imags;
  153. return json($msg);
  154. } else {
  155. $msg['code'] = 0;
  156. $msg['data'] = $imags;
  157. $msg['msg'] = "上传出错";
  158. return json($msg);
  159. }
  160. }
  161. }