Ajax.php 4.1 KB

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