Common.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(',', strtolower($upload['mimetype']));
  68. $typeArr = explode('/', $fileInfo['type']);
  69. //验证文件后缀
  70. if ($upload['mimetype'] !== '*' &&
  71. (
  72. !in_array($suffix, $mimetypeArr)
  73. || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
  74. )
  75. ) {
  76. $this->error(__('Uploaded file format is limited'));
  77. }
  78. $replaceArr = [
  79. '{year}' => date("Y"),
  80. '{mon}' => date("m"),
  81. '{day}' => date("d"),
  82. '{hour}' => date("H"),
  83. '{min}' => date("i"),
  84. '{sec}' => date("s"),
  85. '{random}' => Random::alnum(16),
  86. '{random32}' => Random::alnum(32),
  87. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  88. '{suffix}' => $suffix,
  89. '{.suffix}' => $suffix ? '.' . $suffix : '',
  90. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  91. ];
  92. $savekey = $upload['savekey'];
  93. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  94. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  95. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  96. //
  97. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  98. if ($splInfo)
  99. {
  100. $imagewidth = $imageheight = 0;
  101. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']))
  102. {
  103. $imgInfo = getimagesize($splInfo->getPathname());
  104. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  105. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  106. }
  107. $params = array(
  108. 'filesize' => $fileInfo['size'],
  109. 'imagewidth' => $imagewidth,
  110. 'imageheight' => $imageheight,
  111. 'imagetype' => $suffix,
  112. 'imageframes' => 0,
  113. 'mimetype' => $fileInfo['type'],
  114. 'url' => $uploadDir . $splInfo->getSaveName(),
  115. 'uploadtime' => time(),
  116. 'storage' => 'local',
  117. 'sha1' => $sha1,
  118. );
  119. $attachment = model("attachment");
  120. $attachment->data(array_filter($params));
  121. $attachment->save();
  122. \think\Hook::listen("upload_after", $attachment);
  123. $this->success(__('Upload successful'), [
  124. 'url' => $uploadDir . $splInfo->getSaveName()
  125. ]);
  126. }
  127. else
  128. {
  129. // 上传失败获取错误信息
  130. $this->error($file->getError());
  131. }
  132. }
  133. }