Cos.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace addons\cos;
  3. use addons\cos\library\Auth;
  4. use app\common\library\Menu;
  5. use fast\Http;
  6. use Qcloud\Cos\Client;
  7. use think\Addons;
  8. use think\App;
  9. use think\Loader;
  10. /**
  11. * COS插件
  12. */
  13. class Cos extends Addons
  14. {
  15. /**
  16. * 插件安装方法
  17. * @return bool
  18. */
  19. public function install()
  20. {
  21. return true;
  22. }
  23. /**
  24. * 插件卸载方法
  25. * @return bool
  26. */
  27. public function uninstall()
  28. {
  29. return true;
  30. }
  31. /**
  32. * 插件启用方法
  33. * @return bool
  34. */
  35. public function enable()
  36. {
  37. return true;
  38. }
  39. /**
  40. * 插件禁用方法
  41. * @return bool
  42. */
  43. public function disable()
  44. {
  45. return true;
  46. }
  47. /**
  48. * 渲染命名空间配置信息
  49. */
  50. public function appInit()
  51. {
  52. if (!class_exists('\Qcloud\Cos\Client')) {
  53. Loader::addNamespace('Qcloud\Cos', $this->addons_path . str_replace('/', DS, 'library/Qcloud/Cos/'));
  54. }
  55. if (!class_exists('\GuzzleHttp\Command\Command')) {
  56. Loader::addNamespace('GuzzleHttp\Command', $this->addons_path . str_replace('/', DS, 'library/Guzzle/command/src/'));
  57. }
  58. if (!class_exists('\GuzzleHttp\Command\Guzzle\Description')) {
  59. Loader::addNamespace('GuzzleHttp\Command\Guzzle', $this->addons_path . str_replace('/', DS, 'library/Guzzle/guzzle-services/src/'));
  60. }
  61. }
  62. /**
  63. * 判断是否来源于API上传
  64. */
  65. public function moduleInit($request)
  66. {
  67. $config = $this->getConfig();
  68. $module = strtolower($request->module());
  69. if ($module == 'api' && ($config['apiupload'] ?? 0) && in_array($module, explode(',', $config['uploadmodule'] ?? '')) &&
  70. strtolower($request->controller()) == 'common' &&
  71. strtolower($request->action()) == 'upload') {
  72. request()->param('isApi', true);
  73. App::invokeMethod(["\\addons\\cos\\controller\\Index", "upload"], ['isApi' => true]);
  74. }
  75. }
  76. /**
  77. *
  78. */
  79. public function uploadConfigInit(&$upload)
  80. {
  81. $config = $this->getConfig();
  82. $module = request()->module();
  83. $module = $module ? strtolower($module) : 'index';
  84. $uploadModule = array_filter(explode(',', $config['uploadmodule'] ?? ''));
  85. if (!in_array($module, $uploadModule)) {
  86. return $upload;
  87. }
  88. $data = ['deadline' => time() + $config['expire']];
  89. $signature = hash_hmac('sha1', json_encode($data), $config['secretKey'], true);
  90. $token = '';
  91. $noNeedLogin = array_filter(explode(',', $config['noneedlogin'] ?? ''));
  92. if (in_array($module, $noNeedLogin) || ($module == 'admin' && \app\admin\library\Auth::instance()->id) || ($module != 'admin' && \app\common\library\Auth::instance()->id)) {
  93. $token = $config['appId'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data));
  94. }
  95. $multipart = [
  96. 'costoken' => $token
  97. ];
  98. $upload = array_merge($upload, [
  99. 'cdnurl' => $config['cdnurl'],
  100. 'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('cos/index/upload', [], false, true),
  101. 'uploadmode' => $config['uploadmode'],
  102. 'bucket' => $config['bucket'],
  103. 'maxsize' => $config['maxsize'],
  104. 'mimetype' => $config['mimetype'],
  105. 'savekey' => $config['savekey'],
  106. 'chunking' => (bool)($config['chunking'] ?? $upload['chunking']),
  107. 'chunksize' => (int)($config['chunksize'] ?? $upload['chunksize']),
  108. 'multipart' => $multipart,
  109. 'storage' => $this->getName(),
  110. 'multiple' => $config['multiple'] ? true : false,
  111. ]);
  112. }
  113. /**
  114. * 附件删除后
  115. */
  116. public function uploadDelete($attachment)
  117. {
  118. $config = $this->getConfig();
  119. if ($attachment['storage'] == 'cos' && isset($config['syncdelete']) && $config['syncdelete']) {
  120. $cosConfig = array(
  121. 'region' => $config['region'],
  122. 'schema' => 'https', //协议头部,默认为http
  123. 'credentials' => array(
  124. 'secretId' => $config['secretId'],
  125. 'secretKey' => $config['secretKey']
  126. )
  127. );
  128. $oss = new Client($cosConfig);
  129. $ret = $oss->deleteObject(array('Bucket' => $config['bucket'], 'Key' => ltrim($attachment->url, '/')));
  130. }
  131. return true;
  132. }
  133. }