Common.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. {
  30. $lng = $this->request->request('lng');
  31. $lat = $this->request->request('lat');
  32. $content = [
  33. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  34. 'versiondata' => Version::check($version),
  35. 'uploaddata' => Config::get('upload'),
  36. 'coverdata' => Config::get("cover"),
  37. ];
  38. $this->success('', $content);
  39. }
  40. else
  41. {
  42. $this->error(__('Invalid parameters'));
  43. }
  44. }
  45. /**
  46. * 上传文件
  47. *
  48. * @param File $file 文件流
  49. */
  50. public function upload()
  51. {
  52. $file = $this->request->file('file');
  53. if (empty($file))
  54. {
  55. $this->error(__('No file upload or server upload limit exceeded'));
  56. }
  57. //判断是否已经存在附件
  58. $sha1 = $file->hash();
  59. $upload = Config::get('upload');
  60. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  61. $type = strtolower($matches[2]);
  62. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  63. $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  64. $fileInfo = $file->getInfo();
  65. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  66. $suffix = $suffix ? $suffix : 'file';
  67. $mimetypeArr = explode(',', $upload['mimetype']);
  68. $typeArr = explode('/', $fileInfo['type']);
  69. //验证文件后缀
  70. if ($upload['mimetype'] !== '*' && !in_array($suffix, $mimetypeArr) && !in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))
  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. {
  96. $imagewidth = $imageheight = 0;
  97. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']))
  98. {
  99. $imgInfo = getimagesize($splInfo->getPathname());
  100. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  101. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  102. }
  103. $params = array(
  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. }
  123. else
  124. {
  125. // 上传失败获取错误信息
  126. $this->error($file->getError());
  127. }
  128. }
  129. }