Ajax.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use fast\Random;
  5. use think\Config;
  6. use think\Lang;
  7. /**
  8. * Ajax异步请求接口
  9. * @internal
  10. */
  11. class Ajax extends Frontend
  12. {
  13. protected $noNeedLogin = ['lang'];
  14. protected $noNeedRight = ['*'];
  15. protected $layout = '';
  16. /**
  17. * 加载语言包
  18. */
  19. public function lang()
  20. {
  21. header('Content-Type: application/javascript');
  22. $callback = $this->request->get('callback');
  23. $controllername = input("controllername");
  24. $this->loadlang($controllername);
  25. //强制输出JSON Object
  26. $result = 'define(' . json_encode(Lang::get(), JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE) . ');';
  27. return $result;
  28. }
  29. /**
  30. * 上传文件
  31. */
  32. public function upload()
  33. {
  34. Config::set('default_return_type', 'json');
  35. $file = $this->request->file('file');
  36. if (empty($file))
  37. {
  38. $this->error(__('No file upload or server upload limit exceeded'));
  39. }
  40. //判断是否已经存在附件
  41. $sha1 = $file->hash();
  42. $upload = Config::get('upload');
  43. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  44. $type = strtolower($matches[2]);
  45. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  46. $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  47. $fileInfo = $file->getInfo();
  48. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  49. $suffix = $suffix ? $suffix : 'file';
  50. $mimetypeArr = explode(',', $upload['mimetype']);
  51. $typeArr = explode('/', $fileInfo['type']);
  52. //验证文件后缀
  53. if ($upload['mimetype'] !== '*' && !in_array($suffix, $mimetypeArr) && !in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))
  54. {
  55. $this->error(__('Uploaded file format is limited'));
  56. }
  57. $replaceArr = [
  58. '{year}' => date("Y"),
  59. '{mon}' => date("m"),
  60. '{day}' => date("d"),
  61. '{hour}' => date("H"),
  62. '{min}' => date("i"),
  63. '{sec}' => date("s"),
  64. '{random}' => Random::alnum(16),
  65. '{random32}' => Random::alnum(32),
  66. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  67. '{suffix}' => $suffix,
  68. '{.suffix}' => $suffix ? '.' . $suffix : '',
  69. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  70. ];
  71. $savekey = $upload['savekey'];
  72. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  73. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  74. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  75. //
  76. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  77. if ($splInfo)
  78. {
  79. $imagewidth = $imageheight = 0;
  80. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']))
  81. {
  82. $imgInfo = getimagesize($splInfo->getPathname());
  83. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  84. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  85. }
  86. $params = array(
  87. 'filesize' => $fileInfo['size'],
  88. 'imagewidth' => $imagewidth,
  89. 'imageheight' => $imageheight,
  90. 'imagetype' => $suffix,
  91. 'imageframes' => 0,
  92. 'mimetype' => $fileInfo['type'],
  93. 'url' => $uploadDir . $splInfo->getSaveName(),
  94. 'uploadtime' => time(),
  95. 'storage' => 'local',
  96. 'sha1' => $sha1,
  97. );
  98. $attachment = model("attachment");
  99. $attachment->data(array_filter($params));
  100. $attachment->save();
  101. \think\Hook::listen("upload_after", $attachment);
  102. $this->success(__('Upload successful'), null, [
  103. 'url' => $uploadDir . $splInfo->getSaveName()
  104. ]);
  105. }
  106. else
  107. {
  108. // 上传失败获取错误信息
  109. $this->error($file->getError());
  110. }
  111. }
  112. }