RemoteUploadApi.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/5/17
  6. * Time: 下午2:44
  7. */
  8. namespace common\helpers\http;
  9. use common\models\Uploads;
  10. use yii\base\BaseObject;
  11. use yii\base\StaticInstanceTrait;
  12. use yii\httpclient\Client;
  13. class RemoteUploadApi extends BaseObject
  14. {
  15. use StaticInstanceTrait;
  16. /**
  17. * 通讯密钥
  18. */
  19. public $authKey;
  20. /**
  21. * 上传地址
  22. * @var
  23. */
  24. public $uploadUrl;
  25. /**
  26. * action地址
  27. * @var
  28. */
  29. public $actionUrl;
  30. public $filePath;
  31. /**
  32. * 上传的用户ID
  33. * @var
  34. */
  35. public $id;
  36. const CATEGORY_EXPORT = 'export';
  37. const FORBIDDEN_NOTIFY_URL = 'forbidden';
  38. /**
  39. * 初始化
  40. * @throws \yii\base\Exception
  41. */
  42. public function init()
  43. {
  44. parent::init();
  45. $this->authKey = \Yii::$app->params['http']['remoteUploadApi']['authKey'];
  46. $this->uploadUrl = \Yii::$app->params['http']['remoteUploadApi']['host'].'/upload';
  47. $this->actionUrl = \Yii::$app->params['http']['remoteUploadApi']['host'].'/action';
  48. $this->filePath = \Yii::$app->params['http']['remoteUploadApi']['host'].'/files/';
  49. $this->id = \Yii::$app->security->generateRandomString(32);
  50. }
  51. /**
  52. * @param $uid
  53. * @param array $params
  54. * @return array
  55. */
  56. public function generateToken($uid, $params = []){
  57. if(!isset($params['date']) || !$params['date']){
  58. $params['date'] = date('YmdH');
  59. }
  60. if(!isset($params['authKey']) || !$params['authKey']){
  61. $params['authKey'] = \Yii::$app->params['http']['remoteUploadApi']['authKey'];
  62. }
  63. $hash = 'uid:'.$uid.'&secretkey:'.(string)$params['authKey'].'&datetime:'.(string)$params['date'];
  64. if(!isset($params['notifyUrl']) || !$params['notifyUrl']){
  65. $params['notifyUrl'] = \Yii::$app->params['http']['remoteUploadApi']['remoteUploadNotifyUrl'];
  66. }elseif($params['notifyUrl'] == self::FORBIDDEN_NOTIFY_URL){
  67. $params['notifyUrl'] = '';
  68. }
  69. $hash .= '&notifyurl:' . (string)$params['notifyUrl'];
  70. $md5 = md5($hash);
  71. return [
  72. 'date' => $params['date'],
  73. 'token' => $md5,
  74. 'notifyUrl' => $params['notifyUrl'],
  75. ];
  76. }
  77. /**
  78. * 上传
  79. * @param $filePath
  80. * @return bool|mixed
  81. * @throws \yii\base\InvalidConfigException
  82. * @throws \yii\httpclient\Exception
  83. */
  84. public function upload($filePath){
  85. $token = $this->generateToken($this->id);
  86. // 上传
  87. $client = new Client();
  88. $response = $client->createRequest()
  89. ->setMethod('POST')
  90. ->addHeaders([
  91. 'UPLOAD-SERVER-TOKEN' => $token['token'],
  92. 'UPLOAD-SERVER-USER' => $this->id,
  93. 'UPLOAD-SERVER-NOTIFY-URL'=>$token['notifyUrl'],
  94. 'UPLOAD-SERVER-DATE'=>$token['date'],
  95. ])
  96. ->setUrl($this->uploadUrl)
  97. ->addFile('ATTACHMENT', $filePath)
  98. ->send();
  99. if($response->isOk && isset($response->data['url'])){
  100. return $response->data;
  101. } else {
  102. return false;
  103. }
  104. }
  105. /**
  106. * 删除
  107. * @param $filePath
  108. * @return bool
  109. * @throws \yii\base\InvalidConfigException
  110. */
  111. public function delete($filePath){
  112. // 处理$filePath
  113. if(strpos($filePath, $this->filePath) !== false){
  114. $filePath = str_replace($this->filePath, '', $filePath);
  115. }
  116. $token = $this->generateToken($this->id);
  117. $client = new Client();
  118. $response = $client->createRequest()
  119. ->setMethod('POST')
  120. ->addHeaders([
  121. 'UPLOAD-SERVER-TOKEN' => $token['token'],
  122. 'UPLOAD-SERVER-USER' => $this->id,
  123. 'UPLOAD-SERVER-NOTIFY-URL'=>$token['notifyUrl'],
  124. 'UPLOAD-SERVER-DATE'=>$token['date'],
  125. ])
  126. ->setUrl($this->actionUrl)
  127. ->setData([
  128. 'action' => 'delete',
  129. 'post_data' => $filePath,
  130. ])
  131. ->send();
  132. if($response->isOk && isset($response->data['success']) && $response->data['success'] == 1){
  133. return $response->data;
  134. } else {
  135. return false;
  136. }
  137. }
  138. }