param = config("maycur"); $this->domain_url = $this->param['zs_url']; } public function getToken($data){ //每刻所有配置 $url_array = $this->param; //redis 存储的token $key = $this->domain_url . $url_array['login_redis_topic']; $token = Redis::get($key); if(! empty($token)) return [true, json_decode($token,true)]; //获取token的参数 $appcode = env('maycur_appcode'); $appsercet = env('maycur_appsercet'); if(empty($appcode) || empty($appsercet)) return [false, '每刻鉴权认证参数不能为空']; //组织获取参数 $url = $this->domain_url . $url_array['login']; $time = microtime(true); $timeStamp = round($time * 1000); //生成secret $secret = $this->getSecret($appcode, $appsercet, $timeStamp); $post = [ "secret" => $secret, "appCode" => $appcode, "timestamp" => $timeStamp ]; $header = ['Content-Type:application/json']; list($status, $result) = $this->post_helper($url,$post, $header); if(! $status) return [$status, $result]; if(empty($result['data']['entCode']) || empty($result['data']['tokenId'])) return [false, $result['message'] ?? '鉴权失败,请联系开发者']; $token_array = [ 'entCode' => $result['data']['entCode'], 'tokenId' => $result['data']['tokenId'], ]; Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array)); return [true, $token_array]; } private function getSecret($appCode, $appSecret, $timeStamp) { // 拼接字符串并计算 SHA-256 哈希值 $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp; $hashedString = hash('sha256', $stringToHash); return $hashedString; } public function post_helper($url, $data, $header = [], $timeout = 20){ Log::channel('apiLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $r = curl_exec($ch); if ($r === false) { // 获取错误号 $errorNumber = curl_errno($ch); // 获取错误信息 $errorMessage = curl_error($ch); $message = "cURL Error #{$errorNumber}: {$errorMessage}"; Log::channel('apiLog')->info('每刻POST结果', ["message" => $message]); return [false, $message]; } curl_close($ch); $return = json_decode($r, true); Log::channel('apiLog')->info('每刻POST结果', ["message" => $return]); return [true, $return]; } public function get_helper($url,$header=[],$timeout = 20){ Log::channel('apiLog')->info('每刻GET', ["api" => $url ,"header" => $header]); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => $timeout, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTPHEADER => $header, )); $r = curl_exec($ch); if ($r === false) { // 获取错误号 $errorNumber = curl_errno($ch); // 获取错误信息 $errorMessage = curl_error($ch); $message = "cURL Error #{$errorNumber}: {$errorMessage}"; Log::channel('apiLog')->info('每刻GET', ["message" => $message]); return [false, $message]; } curl_close($ch); $return = json_decode($r, true); Log::channel('apiLog')->info('每刻GET', ["message" => $return]); return [true, $return]; } }