Ajax.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $file = $this->request->file('file');
  35. //判断是否已经存在附件
  36. $sha1 = $file->hash();
  37. $uploaded = model("attachment")->where('sha1', $sha1)->find();
  38. if ($uploaded)
  39. {
  40. $this->success('', null, [
  41. 'url' => $uploaded['url']
  42. ]);
  43. }
  44. $upload = Config::get('upload');
  45. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  46. $type = strtolower($matches[2]);
  47. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  48. $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  49. $fileInfo = $file->getInfo();
  50. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  51. $suffix = $suffix ? $suffix : 'file';
  52. $replaceArr = [
  53. '{year}' => date("Y"),
  54. '{mon}' => date("m"),
  55. '{day}' => date("d"),
  56. '{hour}' => date("H"),
  57. '{min}' => date("i"),
  58. '{sec}' => date("s"),
  59. '{random}' => Random::alnum(16),
  60. '{random32}' => Random::alnum(32),
  61. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  62. '{suffix}' => $suffix,
  63. '{.suffix}' => $suffix ? '.' . $suffix : '',
  64. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  65. ];
  66. $savekey = $upload['savekey'];
  67. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  68. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  69. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  70. //
  71. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  72. if ($splInfo)
  73. {
  74. $imagewidth = $imageheight = 0;
  75. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']))
  76. {
  77. $imgInfo = getimagesize($splInfo->getPathname());
  78. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  79. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  80. }
  81. $params = array(
  82. 'filesize' => $fileInfo['size'],
  83. 'imagewidth' => $imagewidth,
  84. 'imageheight' => $imageheight,
  85. 'imagetype' => $suffix,
  86. 'imageframes' => 0,
  87. 'mimetype' => $fileInfo['type'],
  88. 'url' => $uploadDir . $splInfo->getSaveName(),
  89. 'uploadtime' => time(),
  90. 'sha1' => $sha1,
  91. );
  92. model("attachment")->create(array_filter($params));
  93. $this->success('', null, [
  94. 'url' => $uploadDir . $splInfo->getSaveName()
  95. ]);
  96. }
  97. else
  98. {
  99. // 上传失败获取错误信息
  100. $this->error($file->getError());
  101. }
  102. }
  103. }