Plugs.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use controller\BasicAdmin;
  15. use service\FileService;
  16. /**
  17. * 插件助手控制器
  18. * Class Plugs
  19. * @package app\admin\controller
  20. * @author Anyon <zoujingli@qq.com>
  21. * @date 2017/02/21
  22. */
  23. class Plugs extends BasicAdmin
  24. {
  25. /**
  26. * 文件上传
  27. * @return \think\response\View
  28. */
  29. public function upfile()
  30. {
  31. $uptype = $this->request->get('uptype');
  32. if (!in_array($uptype, ['local', 'qiniu', 'oss'])) {
  33. $uptype = sysconf('storage_type');
  34. }
  35. $mode = $this->request->get('mode', 'one');
  36. $types = $this->request->get('type', 'jpg,png');
  37. $this->assign('mimes', FileService::getFileMine($types));
  38. $this->assign('field', $this->request->get('field', 'file'));
  39. return view('', ['mode' => $mode, 'types' => $types, 'uptype' => $uptype]);
  40. }
  41. /**
  42. * 通用文件上传
  43. * @return \think\response\Json
  44. */
  45. public function upload()
  46. {
  47. $file = $this->request->file('file');
  48. $ext = pathinfo($file->getInfo('name'), 4);
  49. $md5 = str_split($this->request->post('md5'), 16);
  50. $filename = join('/', $md5) . ".{$ext}";
  51. // 文件上传Token验证
  52. if ($this->request->post('token') !== md5($filename . session_id())) {
  53. return json(['code' => 'ERROR', '文件上传验证失败']);
  54. }
  55. // 文件上传处理
  56. if (($info = $file->move('static' . DS . 'upload' . DS . $md5[0], $md5[1], true))) {
  57. if (($site_url = FileService::getFileUrl($filename, 'local'))) {
  58. return json(['data' => ['site_url' => $site_url], 'code' => 'SUCCESS', 'msg' => '文件上传成功']);
  59. }
  60. }
  61. return json(['code' => 'ERROR', '文件上传失败']);
  62. }
  63. /**
  64. * 文件状态检查
  65. */
  66. public function upstate()
  67. {
  68. $post = $this->request->post();
  69. $filename = join('/', str_split($post['md5'], 16)) . '.' . pathinfo($post['filename'], 4);
  70. // 检查文件是否已上传
  71. if (($site_url = FileService::getFileUrl($filename))) {
  72. $this->result(['site_url' => $site_url], 'IS_FOUND');
  73. }
  74. // 需要上传文件,生成上传配置参数
  75. $config = ['uptype' => $post['uptype'], 'file_url' => $filename];
  76. switch (strtolower($post['uptype'])) {
  77. case 'qiniu':
  78. $config['server'] = FileService::getUploadQiniuUrl(true);
  79. $config['token'] = $this->_getQiniuToken($filename);
  80. break;
  81. case 'local':
  82. $config['server'] = FileService::getUploadLocalUrl();
  83. $config['token'] = md5($filename . session_id());
  84. break;
  85. case 'oss':
  86. $time = time() + 3600;
  87. $policyText = [
  88. 'expiration' => date('Y-m-d', $time) . 'T' . date('H:i:s', $time) . '.000Z',
  89. 'conditions' => [['content-length-range', 0, 1048576000]],
  90. ];
  91. $config['policy'] = base64_encode(json_encode($policyText));
  92. $config['server'] = FileService::getUploadOssUrl();
  93. $config['site_url'] = FileService::getBaseUriOss() . $filename;
  94. $config['signature'] = base64_encode(hash_hmac('sha1', $config['policy'], sysconf('storage_oss_secret'), true));
  95. $config['OSSAccessKeyId'] = sysconf('storage_oss_keyid');
  96. }
  97. $this->result($config, 'NOT_FOUND');
  98. }
  99. /**
  100. * 生成七牛文件上传Token
  101. * @param string $key
  102. * @return string
  103. */
  104. protected function _getQiniuToken($key)
  105. {
  106. $host = sysconf('storage_qiniu_domain');
  107. $bucket = sysconf('storage_qiniu_bucket');
  108. $accessKey = sysconf('storage_qiniu_access_key');
  109. $secretKey = sysconf('storage_qiniu_secret_key');
  110. $protocol = sysconf('storage_qiniu_is_https') ? 'https' : 'http';
  111. $params = [
  112. "scope" => "{$bucket}:{$key}", "deadline" => 3600 + time(),
  113. "returnBody" => "{\"data\":{\"site_url\":\"{$protocol}://{$host}/$(key)\",\"file_url\":\"$(key)\"}, \"code\": \"SUCCESS\"}",
  114. ];
  115. $data = str_replace(['+', '/'], ['-', '_'], base64_encode(json_encode($params)));
  116. return $accessKey . ':' . str_replace(['+', '/'], ['-', '_'], base64_encode(hash_hmac('sha1', $data, $secretKey, true))) . ':' . $data;
  117. }
  118. /**
  119. * 字体图标选择器
  120. * @return \think\response\View
  121. */
  122. public function icon()
  123. {
  124. $field = $this->request->get('field', 'icon');
  125. return view('', ['field' => $field]);
  126. }
  127. }