MayCurServerService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\Redis;
  5. class MayCurServerService extends Service
  6. {
  7. protected $param = "";
  8. protected $domain_url = "";
  9. public function __construct()
  10. {
  11. $this->param = config("maycur");
  12. $this->domain_url = $this->param['zs_url'];
  13. }
  14. public function getToken($data){
  15. //每刻所有配置
  16. $url_array = $this->param;
  17. //redis 存储的token
  18. $key = $this->domain_url . $url_array['login_redis_topic'];
  19. $token = Redis::get($key);
  20. if(! empty($token)) return [true, json_decode($token,true)];
  21. //获取token的参数
  22. $appcode = env('maycur_appcode');
  23. $appsercet = env('maycur_appsercet');
  24. if(empty($appcode) || empty($appsercet)) return [false, '每刻鉴权认证参数不能为空'];
  25. //组织获取参数
  26. $url = $this->domain_url . $url_array['login'];
  27. $time = microtime(true);
  28. $timeStamp = round($time * 1000);
  29. //生成secret
  30. $secret = $this->getSecret($appcode, $appsercet, $timeStamp);
  31. $post = [
  32. "secret" => $secret,
  33. "appCode" => $appcode,
  34. "timestamp" => $timeStamp
  35. ];
  36. $header = ['Content-Type:application/json'];
  37. list($status, $result) = $this->post_helper($url,$post, $header);
  38. if(! $status) return [$status, $result];
  39. if(empty($result['data']['entCode']) || empty($result['data']['tokenId'])) return [false, $result['message'] ?? '鉴权失败,请联系开发者'];
  40. $token_array = [
  41. 'entCode' => $result['data']['entCode'],
  42. 'tokenId' => $result['data']['tokenId'],
  43. ];
  44. Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array));
  45. return [true, $token_array];
  46. }
  47. private function getSecret($appCode, $appSecret, $timeStamp) {
  48. // 拼接字符串并计算 SHA-256 哈希值
  49. $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp;
  50. $hashedString = hash('sha256', $stringToHash);
  51. return $hashedString;
  52. }
  53. public function post_helper($url, $data, $header = [], $timeout = 20){
  54. Log::channel('apiLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]);
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, $url);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58. curl_setopt($ch, CURLOPT_ENCODING, '');
  59. curl_setopt($ch, CURLOPT_POST, 1);
  60. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  61. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  62. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  63. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  64. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  65. $r = curl_exec($ch);
  66. if ($r === false) {
  67. // 获取错误号
  68. $errorNumber = curl_errno($ch);
  69. // 获取错误信息
  70. $errorMessage = curl_error($ch);
  71. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  72. Log::channel('apiLog')->info('每刻POST结果', ["message" => $message]);
  73. return [false, $message];
  74. }
  75. curl_close($ch);
  76. $return = json_decode($r, true);
  77. Log::channel('apiLog')->info('每刻POST结果', ["message" => $return]);
  78. return [true, $return];
  79. }
  80. public function get_helper($url,$header=[],$timeout = 20){
  81. Log::channel('apiLog')->info('每刻GET', ["api" => $url ,"header" => $header]);
  82. $ch = curl_init();
  83. curl_setopt_array($ch, array(
  84. CURLOPT_URL => $url,
  85. CURLOPT_RETURNTRANSFER => true,
  86. CURLOPT_ENCODING => '',
  87. CURLOPT_MAXREDIRS => 10,
  88. CURLOPT_TIMEOUT => $timeout,
  89. CURLOPT_FOLLOWLOCATION => true,
  90. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  91. CURLOPT_CUSTOMREQUEST => 'GET',
  92. CURLOPT_SSL_VERIFYPEER => false,
  93. CURLOPT_HTTPHEADER => $header,
  94. ));
  95. $r = curl_exec($ch);
  96. if ($r === false) {
  97. // 获取错误号
  98. $errorNumber = curl_errno($ch);
  99. // 获取错误信息
  100. $errorMessage = curl_error($ch);
  101. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  102. Log::channel('apiLog')->info('每刻GET', ["message" => $message]);
  103. return [false, $message];
  104. }
  105. curl_close($ch);
  106. $return = json_decode($r, true);
  107. Log::channel('apiLog')->info('每刻GET', ["message" => $return]);
  108. return [true, $return];
  109. }
  110. }