VodService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use think\Request;
  13. use service\JsonService;
  14. class VodService
  15. {
  16. protected static $method = 'GET'; //获取方式
  17. protected static $AccessKeyId = ''; //阿里云AccessKeyId
  18. protected static $accessKeySecret = ''; //阿里云AccessKeySecret
  19. final static function init()
  20. {
  21. self::$AccessKeyId = SystemConfigService::get('accessKeyId');//阿里云AccessKeyId
  22. self::$accessKeySecret = SystemConfigService::get('accessKeySecret');//阿里云AccessKeySecret
  23. if (self::$AccessKeyId == '' || self::$accessKeySecret == '') return JsonService::fail('阿里云AccessKeyId或阿里云AccessKeySecret没有配置');
  24. }
  25. /**获取视频上传地址和凭证
  26. * @param string $videoId
  27. * @param string $FileName
  28. * @param string $type 1=获取视频上传凭证 2=获取视频播放凭证 3=获取视频播放地址 4=删除完整视频
  29. */
  30. public static function videoUploadAddressVoucher($FileName = '', $type = 1, $videoId = '', $image = '')
  31. {
  32. self::init();
  33. $apiParams = [];
  34. $site_url = SystemConfigService::get('site_url');
  35. $region = SystemConfigService::get('configuration_item_region');
  36. $cate_id = SystemConfigService::get('cate_id');
  37. if ($site_url) {
  38. $arr = parse_url($site_url);
  39. if ($arr['scheme']) {
  40. $scheme = $arr['scheme'];
  41. } else {
  42. $scheme = 'http';
  43. }
  44. } else {
  45. $scheme = 'http';
  46. }
  47. $requestUrl = $scheme . '://vod.' . $region . '.aliyuncs.com/?';
  48. if ($videoId != '' && $type == 1) {
  49. $apiParams['Action'] = 'RefreshUploadVideo';
  50. $apiParams['VideoId'] = $videoId;
  51. } else if ($videoId != '' && $type == 2) {
  52. $apiParams['Action'] = 'GetVideoPlayAuth';
  53. $apiParams['VideoId'] = $videoId;
  54. } else if ($videoId != '' && $type == 3) {
  55. $apiParams['Action'] = 'GetPlayInfo';
  56. $apiParams['VideoId'] = $videoId;
  57. } else if ($videoId != '' && $type == 4) {
  58. $apiParams['Action'] = 'DeleteVideo';
  59. $apiParams['VideoIds'] = $videoId;
  60. } else if ($videoId == '' && $type == 1) {
  61. $apiParams['Action'] = 'CreateUploadVideo';
  62. $apiParams['Title'] = self::video_name($FileName);
  63. $apiParams['FileName'] = $FileName;
  64. $apiParams['CoverURL'] = $image;
  65. $apiParams['CateId'] = $cate_id;
  66. }
  67. $apiParams['AccessKeyId'] = self::$AccessKeyId;
  68. $apiParams['Format'] = 'JSON';
  69. $apiParams['SignatureMethod'] = 'HMAC-SHA1';
  70. $apiParams['SignatureVersion'] = '1.0';
  71. $apiParams['SignatureNonce'] = md5(uniqid(mt_rand(), true));
  72. $apiParams['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  73. $apiParams['Version'] = '2017-03-21';
  74. $apiParams['Signature'] = self::computeSignature($apiParams, self::$accessKeySecret);
  75. foreach ($apiParams as $apiParamKey => $apiParamValue) {
  76. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  77. }
  78. return substr($requestUrl, 0, -1);
  79. }
  80. public static function signString($source, $accessSecret)
  81. {
  82. return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
  83. }
  84. /**
  85. * 视频名称
  86. */
  87. public static function video_name($FileName)
  88. {
  89. return mb_substr(substr($FileName, strrpos($FileName, '.') + 1), 0, 128, 'utf8');
  90. }
  91. /**签名机制
  92. * @param $parameters
  93. * @param $accessKeySecret
  94. * @return mixed
  95. */
  96. public static function computeSignature($parameters, $accessKeySecret)
  97. {
  98. ksort($parameters);
  99. $canonicalizedQueryString = '';
  100. foreach ($parameters as $key => $value) {
  101. $canonicalizedQueryString .= '&' . self::percentEncode($key) . '=' . self::percentEncode($value);
  102. }
  103. $stringToBeSigned =
  104. self::$method . '&%2F&' . self::percentEncode(substr($canonicalizedQueryString, 1));
  105. return self::signString($stringToBeSigned, $accessKeySecret . '&');
  106. }
  107. /**
  108. * @param $str
  109. * @return string|string[]|null
  110. */
  111. public static function percentEncode($str)
  112. {
  113. $res = urlencode($str);
  114. $res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
  115. $res = preg_replace('/%7E/', '~', $res);
  116. return $res;
  117. }
  118. }