Common.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Area;
  5. use app\common\model\Version;
  6. use fast\Random;
  7. use think\Config;
  8. /**
  9. * 公共接口
  10. */
  11. class Common extends Api
  12. {
  13. protected $noNeedLogin = ['init'];
  14. protected $noNeedRight = '*';
  15. /**
  16. * 加载初始化
  17. *
  18. * @param string $version 版本号
  19. * @param string $lng 经度
  20. * @param string $lat 纬度
  21. */
  22. public function init()
  23. {
  24. if ($version = $this->request->request('version')) {
  25. $lng = $this->request->request('lng');
  26. $lat = $this->request->request('lat');
  27. $content = [
  28. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  29. 'versiondata' => Version::check($version),
  30. 'uploaddata' => Config::get('upload'),
  31. 'coverdata' => Config::get("cover"),
  32. ];
  33. $this->success('', $content);
  34. } else {
  35. $this->error(__('Invalid parameters'));
  36. }
  37. }
  38. /**
  39. * 上传文件
  40. * @ApiMethod (POST)
  41. * @param File $file 文件流
  42. */
  43. public function upload()
  44. {
  45. $file = $this->request->file('file');
  46. if (empty($file)) {
  47. $this->error(__('No file upload or server upload limit exceeded'));
  48. }
  49. //判断是否已经存在附件
  50. $sha1 = $file->hash();
  51. $upload = Config::get('upload');
  52. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  53. $type = strtolower($matches[2]);
  54. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  55. $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  56. $fileInfo = $file->getInfo();
  57. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  58. $suffix = $suffix ? $suffix : 'file';
  59. $mimetypeArr = explode(',', strtolower($upload['mimetype']));
  60. $typeArr = explode('/', $fileInfo['type']);
  61. //验证文件后缀
  62. if ($upload['mimetype'] !== '*' &&
  63. (
  64. !in_array($suffix, $mimetypeArr)
  65. || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
  66. )
  67. ) {
  68. $this->error(__('Uploaded file format is limited'));
  69. }
  70. $replaceArr = [
  71. '{year}' => date("Y"),
  72. '{mon}' => date("m"),
  73. '{day}' => date("d"),
  74. '{hour}' => date("H"),
  75. '{min}' => date("i"),
  76. '{sec}' => date("s"),
  77. '{random}' => Random::alnum(16),
  78. '{random32}' => Random::alnum(32),
  79. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  80. '{suffix}' => $suffix,
  81. '{.suffix}' => $suffix ? '.' . $suffix : '',
  82. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  83. ];
  84. $savekey = $upload['savekey'];
  85. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  86. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  87. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  88. //
  89. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  90. if ($splInfo) {
  91. $imagewidth = $imageheight = 0;
  92. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'])) {
  93. $imgInfo = getimagesize($splInfo->getPathname());
  94. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  95. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  96. }
  97. $params = array(
  98. 'admin_id' => 0,
  99. 'user_id' => (int)$this->auth->id,
  100. 'filesize' => $fileInfo['size'],
  101. 'imagewidth' => $imagewidth,
  102. 'imageheight' => $imageheight,
  103. 'imagetype' => $suffix,
  104. 'imageframes' => 0,
  105. 'mimetype' => $fileInfo['type'],
  106. 'url' => $uploadDir . $splInfo->getSaveName(),
  107. 'uploadtime' => time(),
  108. 'storage' => 'local',
  109. 'sha1' => $sha1,
  110. );
  111. $attachment = model("attachment");
  112. $attachment->data(array_filter($params));
  113. $attachment->save();
  114. \think\Hook::listen("upload_after", $attachment);
  115. $this->success(__('Upload successful'), [
  116. 'url' => $uploadDir . $splInfo->getSaveName()
  117. ]);
  118. } else {
  119. // 上传失败获取错误信息
  120. $this->error($file->getError());
  121. }
  122. }
  123. }