Ajax.php 4.0 KB

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