StorageController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace api\modules\v1\controllers\common;
  3. use Yii;
  4. use Qiniu\Auth;
  5. use linslin\yii2\curl\Curl;
  6. use common\helpers\ResultHelper;
  7. use common\enums\AttachmentDriveEnum;
  8. use yii\web\Response;
  9. use api\controllers\OnAuthController;
  10. /**
  11. * Class StorageController
  12. * @package api\modules\v1\controllers\common
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class StorageController extends OnAuthController
  16. {
  17. /**
  18. * @var string[]
  19. */
  20. public $authOptional = ['oss'];
  21. /**
  22. * @var string
  23. */
  24. public $modelClass = '';
  25. /**
  26. *
  27. * 建议增加字段 ip,type,host,merchant_id
  28. *
  29. * @return array
  30. * @throws \yii\web\NotFoundHttpException
  31. * @throws \Exception
  32. */
  33. public function actionOss()
  34. {
  35. if ($this->ossSignVerify() === false) {
  36. return ResultHelper::json(422, '签名校验失败');
  37. }
  38. $data = Yii::$app->request->post();
  39. $baseUrlArr = explode('/', $data['filename']);
  40. $fileName = end($baseUrlArr);
  41. $fileName = explode('.', $fileName);
  42. unset($fileName[count($fileName) - 1]);
  43. $name = implode('.', $fileName);
  44. $baseInfo = [
  45. 'drive' => AttachmentDriveEnum::OSS,
  46. 'upload_type' => $data['type'],
  47. 'specific_type' => $data['mimeType'],
  48. 'size' => $data['size'],
  49. 'extension' => $data['format'],
  50. 'name' => $name,
  51. 'width' => $data['width'],
  52. 'height' => $data['height'],
  53. 'url' => urldecode($data['host']) . '/' . $data['filename'],
  54. 'path' => $data['filename'],
  55. 'ip' => $data['ip'] ?? '',
  56. 'md5' => $data['md5'] ?? '',
  57. 'format_size' => Yii::$app->formatter->asShortSize($data['size'], 2),
  58. ];
  59. Yii::$app->services->merchant->setId($data['merchant_id'] ?? 0);
  60. $attachment = Yii::$app->services->attachment->create($baseInfo);
  61. // 百度编辑器返回
  62. if (isset($data['is_ueditor']) && $data['is_ueditor'] == 'ueditor') {
  63. Yii::$app->response->format = Response::FORMAT_JSON;
  64. return [
  65. "state" => 'SUCCESS',
  66. "url" => $attachment['url'],
  67. ];
  68. }
  69. return ResultHelper::json(200, '获取成功', $attachment);
  70. }
  71. /**
  72. * @return array|bool
  73. * @throws \Exception
  74. */
  75. protected function ossSignVerify()
  76. {
  77. // 1.获取OSS的签名header和公钥url header
  78. $authorizationBase64 = Yii::$app->request->headers->get('authorization');
  79. $pubKeyUrlBase64 = Yii::$app->request->headers->get('x-oss-pub-key-url');
  80. if (!$authorizationBase64 || !$pubKeyUrlBase64) {
  81. return false;
  82. }
  83. // 2.获取OSS的签名
  84. $authorization = base64_decode($authorizationBase64);
  85. // 3.获取公钥
  86. $pubKeyUrl = base64_decode($pubKeyUrlBase64);
  87. $curl = new Curl();
  88. $pubKey = $curl->get($pubKeyUrl);
  89. if ($pubKey == "") {
  90. return false;
  91. }
  92. // 4.获取回调body
  93. $body = file_get_contents('php://input');
  94. // 5.拼接待签名字符串
  95. $path = $_SERVER['REQUEST_URI'];
  96. $pos = strpos($path, '?');
  97. if ($pos === false) {
  98. $authStr = urldecode($path) . "\n" . $body;
  99. } else {
  100. $authStr = urldecode(substr($path, 0, $pos)) . substr($path, $pos, strlen($path) - $pos) . "\n" . $body;
  101. }
  102. // 6.验证签名
  103. $res = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5);
  104. if ($res == 1) {
  105. return true;
  106. }
  107. return false;
  108. }
  109. /**
  110. * 七牛回调
  111. */
  112. public function actionQiNiu()
  113. {
  114. // $accessKey = getenv('QINIU_ACCESS_KEY');
  115. // $secretKey = getenv('QINIU_SECRET_KEY');
  116. // $bucket = getenv('QINIU_TEST_BUCKET');
  117. // $auth = new Auth($accessKey, $secretKey);
  118. // //获取回调的body信息
  119. // $callbackBody = file_get_contents('php://input');
  120. // //回调的contentType
  121. // $contentType = 'application/x-www-form-urlencoded';
  122. // //回调的签名信息,可以验证该回调是否来自七牛
  123. // $authorization = $_SERVER['HTTP_AUTHORIZATION'];
  124. // //七牛回调的url,具体可以参考:https://developer.qiniu.com/kodo/manual/1206/put-policy
  125. // $url = 'http://172.30.251.210/upload_verify_callback.php';
  126. // $isQiniuCallback = $auth->verifyCallback($contentType, $authorization, $url, $callbackBody);
  127. // if ($isQiniuCallback) {
  128. // $resp = array('ret' => 'success');
  129. // } else {
  130. // $resp = array('ret' => 'failed');
  131. // }
  132. // echo json_encode($resp);
  133. }
  134. }
粤ICP备19079148号