MayCurVouchersServerService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\Redis;
  5. class MayCurVouchersServerService extends Service
  6. {
  7. protected $param = "";
  8. protected $domain_url = "";
  9. protected $is_formal_vouch = false;
  10. protected $head = null;
  11. public function __construct($data)
  12. {
  13. $param = config("maycur");
  14. if(isset($data['is_voucher']) && ! $data['is_voucher']){ // 测试
  15. $this->param = $param['voucher_cs'] ?? [];
  16. $this->domain_url = $this->param['ip'] ?? "";
  17. }else{
  18. $this->param = $param['voucher_zs'] ?? [];
  19. $this->domain_url = $this->param['ip'] ?? "";
  20. $this->is_formal_vouch = true;
  21. }
  22. }
  23. private function getSecret($appCode, $appSecret, $timeStamp) {
  24. // 拼接字符串并计算 SHA-256 哈希值
  25. $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp;
  26. $hashedString = hash('sha256', $stringToHash);
  27. return $hashedString;
  28. }
  29. public function getToken(){
  30. //每刻所有配置
  31. $url_array = $this->param;
  32. //环境
  33. $is_voucher_formal = $this->is_formal_vouch;
  34. $name = $is_voucher_formal ? "每刻凭证正试环境" : "每刻凭证测试环境";
  35. //redis 存储的token
  36. $key = $this->domain_url . $url_array['login_expire_time'];
  37. $token = Redis::get($key);
  38. if(! empty($token)) {
  39. $this->head = json_decode($token,true);
  40. return [true, ''];
  41. }
  42. //获取token的参数
  43. $appcode = $url_array['appkey'];
  44. $appsercet = $url_array['appsecret'];
  45. if(empty($appcode) || empty($appsercet)) return [false, $name . '鉴权认证参数不能为空'];
  46. //组织获取参数
  47. $url = $this->domain_url . $url_array['login'];
  48. $time = microtime(true);
  49. $timeStamp = intval($time * 1000);
  50. //生成secret
  51. $secret = $this->getSecret($appcode, $appsercet, $timeStamp);
  52. $post = [
  53. "secretToken" => $secret,
  54. "userCode" => $appcode,
  55. "timestamp" => $timeStamp
  56. ];
  57. $header = ['Content-Type:application/json'];
  58. list($status, $result) = $this->post_helper($url,$post, $header);
  59. if(! $status) return [$status, $result];
  60. if(isset($result['code']) && $result['code'] != '200') return [false, $result['errMsg'] ?? $name . '鉴权失败,请联系开发者'];
  61. $token_array = [
  62. 'Authorization:Bearer ' . $result['data']['token'],
  63. ];
  64. $this->head = $token_array;
  65. Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array));
  66. return [true, ''];
  67. }
  68. public function voucher($data){
  69. if(empty($data['body'])) return [false, '凭证同步入参请放入body内'];
  70. //获取token
  71. list($status, $token) = $this->getToken();
  72. if(! $status) return [false, $token];
  73. $header = $this->head;
  74. //每刻所有配置
  75. $url_array = $this->param;
  76. //组织获取参数
  77. $url = $this->domain_url . $url_array['voucher_insert'];
  78. $post = $data['body'] ?? [];
  79. $header = array_merge(['Content-Type:application/json'], $header);
  80. list($status, $result) = $this->post_helper($url,$post, $header);
  81. if(! $status) return [$status, $result];
  82. if(isset($result['code']) && $result['code'] == 1) return [false, $result['errData'][0]['errorMsg'] ?? ""];
  83. if(isset($result['code']) && $result['code'] != 0 && ! empty($result['errMsg'])) return [false, $result['errMsg']];
  84. return [true, ''];
  85. }
  86. public function post_helper($url, $data, $header = [], $timeout = 20){
  87. Log::channel('apiMcLog')->info('每刻凭证POST', ["api" => $url , "param" => $data ,"header" => $header]);
  88. $ch = curl_init();
  89. curl_setopt($ch, CURLOPT_URL, $url);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  91. curl_setopt($ch, CURLOPT_ENCODING, '');
  92. curl_setopt($ch, CURLOPT_POST, 1);
  93. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  94. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  95. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  96. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  97. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  98. $r = curl_exec($ch);
  99. if ($r === false) {
  100. // 获取错误号
  101. $errorNumber = curl_errno($ch);
  102. // 获取错误信息
  103. $errorMessage = curl_error($ch);
  104. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  105. Log::channel('apiMcLog')->info('每刻凭证POST结果', ["message" => $message]);
  106. return [false, $message];
  107. }
  108. curl_close($ch);
  109. $return = json_decode($r, true);
  110. Log::channel('apiMcLog')->info('每刻凭证POST结果', ["message" => $return]);
  111. return [true, $return];
  112. }
  113. public function get_helper($url,$header=[],$timeout = 20){
  114. Log::channel('apiMcLog')->info('每刻凭证GET', ["api" => $url ,"header" => $header]);
  115. $ch = curl_init();
  116. curl_setopt_array($ch, array(
  117. CURLOPT_URL => $url,
  118. CURLOPT_RETURNTRANSFER => true,
  119. CURLOPT_ENCODING => '',
  120. CURLOPT_MAXREDIRS => 10,
  121. CURLOPT_TIMEOUT => $timeout,
  122. CURLOPT_FOLLOWLOCATION => true,
  123. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  124. CURLOPT_CUSTOMREQUEST => 'GET',
  125. CURLOPT_SSL_VERIFYPEER => false,
  126. CURLOPT_HTTPHEADER => $header,
  127. ));
  128. $r = curl_exec($ch);
  129. if ($r === false) {
  130. // 获取错误号
  131. $errorNumber = curl_errno($ch);
  132. // 获取错误信息
  133. $errorMessage = curl_error($ch);
  134. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  135. Log::channel('apiMcLog')->info('每刻凭证GET', ["message" => $message]);
  136. return [false, $message];
  137. }
  138. curl_close($ch);
  139. $return = json_decode($r, true);
  140. Log::channel('apiMcLog')->info('每刻凭证GET', ["message" => $return]);
  141. return [true, $return];
  142. }
  143. }