Common.php 4.9 KB

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