Plugs.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~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. * @var bool
  27. */
  28. protected $checkLogin = false;
  29. /**
  30. * 默认检查节点访问权限
  31. * @var bool
  32. */
  33. protected $checkAuth = false;
  34. /**
  35. * 文件上传
  36. */
  37. public function upfile($mode = 'one') {
  38. $types = $this->request->get('type', 'jpg,png');
  39. $this->assign('mode', $mode);
  40. $this->assign('types', $types);
  41. $this->assign('uptype', sysconf('storage_type'));
  42. $this->assign('mimes', FileService::getFileMine($types));
  43. $this->assign('field', $this->request->get('field', 'file'));
  44. return view();
  45. }
  46. /**
  47. * 通用文件上传
  48. * @return string
  49. */
  50. public function upload() {
  51. if ($this->request->isPost()) {
  52. $md5s = str_split($this->request->post('md5'), 16);
  53. if (($info = $this->request->file('file')->move('upload' . DS . $md5s[0], $md5s[1], true))) {
  54. $site_url = FileService::getFileUrl(join('/', $md5s) . '.' . $info->getExtension());
  55. return json(['data' => ['site_url' => $site_url], 'code' => 'SUCCESS']);
  56. }
  57. }
  58. return json(['code' => 'ERROR']);
  59. }
  60. /**
  61. * 文件状态检查
  62. * @return string
  63. */
  64. public function upstate() {
  65. $post = $this->request->post();
  66. $filename = join('/', str_split($post['md5'], 16)) . '.' . pathinfo($post['filename'], PATHINFO_EXTENSION);
  67. // 检查文件是否已上传
  68. if (($site_url = FileService::getFileUrl($filename))) {
  69. return $this->result(['site_url' => $site_url], 'IS_FOUND');
  70. }
  71. // 需要上传文件,生成上传配置参数
  72. $config = ['uptype' => $post['uptype'], 'file_url' => $filename, 'server' => url('admin/plugs/upload')];
  73. switch (strtolower(sysconf('storage_type'))) {
  74. case 'qiniu':
  75. $config['server'] = sysconf('storage_qiniu_is_https') ? 'https://up.qbox.me' : 'http://upload.qiniu.com';
  76. $config['token'] = $this->_getQiniuToken($filename);
  77. break;
  78. case 'local':
  79. $config['server'] = url('admin/plugs/upload');
  80. break;
  81. }
  82. return $this->result($config, 'NOT_FOUND');
  83. }
  84. /**
  85. * 生成七牛文件上传Token
  86. * @param string $key
  87. * @return string
  88. */
  89. protected function _getQiniuToken($key) {
  90. empty($key) && exit('param error');
  91. $accessKey = sysconf('storage_qiniu_access_key');
  92. $secretKey = sysconf('storage_qiniu_secret_key');
  93. $bucket = sysconf('storage_qiniu_bucket');
  94. $host = sysconf('storage_qiniu_domain');
  95. $protocol = sysconf('storage_qiniu_is_https') ? 'https' : 'http';
  96. $params = [
  97. "scope" => "{$bucket}:{$key}",
  98. "deadline" => 3600 + time(),
  99. "returnBody" => "{\"data\":{\"site_url\":\"{$protocol}://{$host}/$(key)\",\"file_url\":\"$(key)\"}, \"code\": \"SUCCESS\"}",
  100. ];
  101. $data = str_replace(['+', '/'], ['-', '_'], base64_encode(json_encode($params)));
  102. return $accessKey . ':' . str_replace(['+', '/'], ['-', '_'], base64_encode(hash_hmac('sha1', $data, $secretKey, true))) . ':' . $data;
  103. }
  104. /**
  105. * 字体图标
  106. */
  107. public function icon() {
  108. $this->assign('field', $this->request->get('field', 'icon'));
  109. return view();
  110. }
  111. }