Plugs.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. use think\View;
  17. /**
  18. * 插件助手控制器
  19. * Class Plugs
  20. * @package app\admin\controller
  21. * @author Anyon <zoujingli@qq.com>
  22. * @date 2017/02/21
  23. */
  24. class Plugs extends BasicAdmin {
  25. /**
  26. * 默认检查用户登录状态
  27. * @var bool
  28. */
  29. public $checkLogin = false;
  30. /**
  31. * 默认检查节点访问权限
  32. * @var bool
  33. */
  34. public $checkAuth = false;
  35. /**
  36. * 文件上传
  37. * @return View
  38. */
  39. public function upfile() {
  40. $types = $this->request->get('type', 'jpg,png');
  41. $mode = $this->request->get('mode', 'one');
  42. $this->assign('mode', $mode);
  43. $this->assign('types', $types);
  44. if (!in_array(($uptype = $this->request->get('uptype')), ['local', 'qiniu'])) {
  45. $uptype = sysconf('storage_type');
  46. }
  47. $this->assign('uptype', $uptype);
  48. $this->assign('mimes', FileService::getFileMine($types));
  49. $this->assign('field', $this->request->get('field', 'file'));
  50. return view();
  51. }
  52. /**
  53. * 通用文件上传
  54. * @return string
  55. */
  56. public function upload() {
  57. if ($this->request->isPost()) {
  58. $md5s = str_split($this->request->post('md5'), 16);
  59. if (($info = $this->request->file('file')->move('static' . DS . 'upload' . DS . $md5s[0], $md5s[1], true))) {
  60. $filename = join('/', $md5s) . '.' . $info->getExtension();
  61. $site_url = FileService::getFileUrl($filename, 'local');
  62. if ($site_url) {
  63. return json(['data' => ['site_url' => $site_url], 'code' => 'SUCCESS']);
  64. }
  65. }
  66. }
  67. return json(['code' => 'ERROR']);
  68. }
  69. /**
  70. * 文件状态检查
  71. */
  72. public function upstate() {
  73. $post = $this->request->post();
  74. $filename = join('/', str_split($post['md5'], 16)) . '.' . pathinfo($post['filename'], PATHINFO_EXTENSION);
  75. // 检查文件是否已上传
  76. if (($site_url = FileService::getFileUrl($filename))) {
  77. $this->result(['site_url' => $site_url], 'IS_FOUND');
  78. }
  79. // 需要上传文件,生成上传配置参数
  80. $config = ['uptype' => $post['uptype'], 'file_url' => $filename];
  81. switch (strtolower($post['uptype'])) {
  82. case 'qiniu':
  83. $config['server'] = FileService::getUploadQiniuUrl(true);
  84. $config['token'] = $this->_getQiniuToken($filename);
  85. break;
  86. case 'local':
  87. $config['server'] = FileService::getUploadLocalUrl();
  88. break;
  89. case 'oss':
  90. $time = time() + 3600;
  91. $policyText = [
  92. 'expiration' => date('Y-m-d', $time) . 'T' . date('H:i:s', $time) . '.000Z',
  93. 'conditions' => [
  94. ['content-length-range', 0, 1048576000]
  95. ]
  96. ];
  97. $config['policy'] = base64_encode(json_encode($policyText));
  98. $config['server'] = FileService::getUploadOssUrl();
  99. $config['site_url'] = FileService::getBaseUriOss() . $filename;
  100. $config['signature'] = base64_encode(hash_hmac('sha1', $config['policy'], sysconf('storage_oss_secret'), true));
  101. $config['OSSAccessKeyId'] = sysconf('storage_oss_keyid');
  102. }
  103. $this->result($config, 'NOT_FOUND');
  104. }
  105. /**
  106. * 生成七牛文件上传Token
  107. * @param string $key
  108. * @return string
  109. */
  110. protected function _getQiniuToken($key) {
  111. $accessKey = sysconf('storage_qiniu_access_key');
  112. $secretKey = sysconf('storage_qiniu_secret_key');
  113. $bucket = sysconf('storage_qiniu_bucket');
  114. $host = sysconf('storage_qiniu_domain');
  115. $protocol = sysconf('storage_qiniu_is_https') ? 'https' : 'http';
  116. $params = [
  117. "scope" => "{$bucket}:{$key}",
  118. "deadline" => 3600 + time(),
  119. "returnBody" => "{\"data\":{\"site_url\":\"{$protocol}://{$host}/$(key)\",\"file_url\":\"$(key)\"}, \"code\": \"SUCCESS\"}",
  120. ];
  121. $data = str_replace(['+', '/'], ['-', '_'], base64_encode(json_encode($params)));
  122. return $accessKey . ':' . str_replace(['+', '/'], ['-', '_'], base64_encode(hash_hmac('sha1', $data, $secretKey, true))) . ':' . $data;
  123. }
  124. /**
  125. * 字体图标
  126. */
  127. public function icon() {
  128. $this->assign('field', $this->request->get('field', 'icon'));
  129. return view();
  130. }
  131. }