Common.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. use Endroid\QrCode\QrCode;
  12. /**
  13. * 公共接口
  14. */
  15. class Common extends Api
  16. {
  17. protected $noNeedLogin = ['init'];
  18. protected $noNeedRight = '*';
  19. /**
  20. * 加载初始化
  21. *
  22. * @param string $version 版本号
  23. * @param string $lng 经度
  24. * @param string $lat 纬度
  25. */
  26. public function init()
  27. {
  28. if ($version = $this->request->request('version')) {
  29. $lng = $this->request->request('lng');
  30. $lat = $this->request->request('lat');
  31. //配置信息
  32. $upload = Config::get('upload');
  33. //如果非服务端中转模式需要修改为中转
  34. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  35. //临时修改上传模式为服务端中转
  36. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  37. $upload = \app\common\model\Config::upload();
  38. // 上传信息配置后
  39. Hook::listen("upload_config_init", $upload);
  40. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  41. }
  42. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  43. $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  44. $content = [
  45. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  46. 'versiondata' => Version::check($version),
  47. 'uploaddata' => $upload,
  48. 'coverdata' => Config::get("cover"),
  49. ];
  50. $this->success('', $content);
  51. } else {
  52. $this->error(__('Invalid parameters'));
  53. }
  54. }
  55. /**
  56. * 上传文件
  57. * @ApiMethod (POST)
  58. * @param File $file 文件流
  59. */
  60. public function upload()
  61. {
  62. Config::set('default_return_type', 'json');
  63. //必须设定cdnurl为空,否则cdnurl函数计算错误
  64. Config::set('upload.cdnurl', '');
  65. $chunkid = $this->request->post("chunkid");
  66. if ($chunkid) {
  67. if (!Config::get('upload.chunking')) {
  68. $this->error(__('Chunk file disabled'));
  69. }
  70. $action = $this->request->post("action");
  71. $chunkindex = $this->request->post("chunkindex/d");
  72. $chunkcount = $this->request->post("chunkcount/d");
  73. $filename = $this->request->post("filename");
  74. $method = $this->request->method(true);
  75. if ($action == 'merge') {
  76. $attachment = null;
  77. //合并分片文件
  78. try {
  79. $upload = new Upload();
  80. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  81. } catch (UploadException $e) {
  82. $this->error($e->getMessage());
  83. }
  84. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  85. } elseif ($method == 'clean') {
  86. //删除冗余的分片文件
  87. try {
  88. $upload = new Upload();
  89. $upload->clean($chunkid);
  90. } catch (UploadException $e) {
  91. $this->error($e->getMessage());
  92. }
  93. $this->success();
  94. } else {
  95. //上传分片文件
  96. //默认普通上传文件
  97. $file = $this->request->file('file');
  98. try {
  99. $upload = new Upload($file);
  100. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  101. } catch (UploadException $e) {
  102. $this->error($e->getMessage());
  103. }
  104. $this->success();
  105. }
  106. } else {
  107. $attachment = null;
  108. //默认普通上传文件
  109. $file = $this->request->file('file');
  110. try {
  111. $upload = new Upload($file);
  112. $attachment = $upload->upload();
  113. } catch (UploadException $e) {
  114. $this->error($e->getMessage());
  115. }
  116. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  117. }
  118. }
  119. /**
  120. * 上传文件
  121. * @ApiMethod (POST)
  122. * @param File $file 文件流
  123. */
  124. public function uploads($file) {
  125. if (empty($file)) {
  126. $this->error(__('No file upload or server upload limit exceeded'));
  127. }
  128. //判断是否已经存在附件
  129. $sha1 = $file->hash();
  130. $upload = Config::get('upload');
  131. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  132. $type = strtolower($matches[2]);
  133. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  134. $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  135. $fileInfo = $file->getInfo();
  136. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  137. $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
  138. $mimetypeArr = explode(',', strtolower($upload['mimetype']));
  139. $typeArr = explode('/', $fileInfo['type']);
  140. //禁止上传PHP和HTML文件
  141. if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm'])) {
  142. $this->error(__('Uploaded file format is limited'));
  143. }
  144. //验证文件后缀
  145. if ($upload['mimetype'] !== '*' &&
  146. (
  147. !in_array($suffix, $mimetypeArr)
  148. || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
  149. )
  150. ) {
  151. $this->error(__('Uploaded file format is limited'));
  152. }
  153. // //验证是否为图片文件
  154. // $imagewidth = $imageheight = 0;
  155. // if (in_array($fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
  156. // $imgInfo = getimagesize($fileInfo['tmp_name']);
  157. // if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
  158. // $this->error(__('Uploaded file is not a valid image'));
  159. // }
  160. // $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  161. // $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  162. // }
  163. $replaceArr = [
  164. '{year}' => date("Y"),
  165. '{mon}' => date("m"),
  166. '{day}' => date("d"),
  167. '{hour}' => date("H"),
  168. '{min}' => date("i"),
  169. '{sec}' => date("s"),
  170. '{random}' => Random::alnum(16),
  171. '{random32}' => Random::alnum(32),
  172. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  173. '{suffix}' => $suffix,
  174. '{.suffix}' => $suffix ? '.' . $suffix : '',
  175. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  176. ];
  177. $savekey = $upload['savekey'];
  178. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  179. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  180. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  181. //
  182. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  183. if ($splInfo) {
  184. $params = array(
  185. 'admin_id' => 0,
  186. 'user_id' => (int)$this->auth->id,
  187. 'filesize' => $fileInfo['size'],
  188. 'imagetype' => $suffix,
  189. 'imageframes' => 0,
  190. 'mimetype' => $fileInfo['type'],
  191. 'url' => $uploadDir . $splInfo->getSaveName(),
  192. 'uploadtime' => time(),
  193. 'storage' => 'local',
  194. 'sha1' => $sha1,
  195. );
  196. $attachment = model("attachment");
  197. $attachment->data(array_filter($params));
  198. $attachment->save();
  199. \think\Hook::listen("upload_after", $attachment);
  200. return $uploadDir . $splInfo->getSaveName();
  201. } else {
  202. // 上传失败获取错误信息
  203. $this->error($file->getError());
  204. }
  205. }
  206. // 返回执行日期所在周的第一天(周一)日期
  207. public function firstOfWeek($date) {
  208. $now = strtotime($date); //当时的时间戳
  209. $number = date("w",$now); //当时是周几
  210. $number = $number == 0 ? 7 : $number; //如遇周末,将0换成7
  211. $diff_day = $number - 1; //求到周一差几天
  212. return date("Ymd",$now - ($diff_day * 60 * 60 * 24));
  213. }
  214. /**
  215. * 数字转化
  216. */
  217. public function changeW($val) {
  218. return $val > 10000 ? round($val/10000,2)."w":$val;
  219. }
  220. public function getModelName($type)
  221. {
  222. return "app\common\model\\" . ucfirst($type);
  223. }
  224. }