* @version : HUOSDK 8.5 */ namespace huoIdentify\identifyDriver\driver; use huoIdentify\identifyDriver\Driver; use huolib\status\CommonStatus; use TencentCloud\Common\Credential; use TencentCloud\Faceid\V20180301\FaceidClient; use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest; use think\Config; use think\Log; class Wxpay extends Driver { /** * 构造函数 * * @param array $config */ public function __construct($config = []) { if (empty($config)) { $config = (array)Config::get('identify_conf.weixin'); } $_config = array( 'secretId' => get_val($config, 'SECRET_ID', ''), 'secretKey' => get_val($config, 'SECRET_KEY', ''), 'apiFrom' => get_val($config, 'API_FROM', 2), //认证api 来源 1 官方 2 市场 ); parent::__construct($_config); } /** * 获取认证 * * @return array */ public function identify() { $_api_from = $this->config['apiFrom']; if (1 == $_api_from) { return $this->fromOfficial(); } return $this->fromMarket(); } /*** * 腾讯云官方认证接口 * https://cloud.tencent.com/document/api/1007/33188 */ public function fromOfficial() { require_once 'wxpay/TCloudAutoLoader.php'; // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey $cred = new Credential($this->config['secretId'], $this->config['secretKey']); // # 实例化要请求产品(以cvm为例)的client对象 $client = new FaceidClient($cred, "ap-guangzhou"); // 实例化一个请求对象 $req = new IdCardVerificationRequest(); $req->IdCard = $this->id_card; $req->Name = $this->real_name; // 通过client对象调用想要访问的接口,需要传入请求对象 $resp = $client->IdCardVerification($req); $_rs = $resp->toJsonString(); $_rs_arr = json_decode($_rs, true); if ('0' != $_rs_arr['Result']) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name) ).'&rs='.$_rs.'[微信实名认证api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, $_rs_arr['Description']); } $_code = CommonStatus::NO_ERROR; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } /*** * 腾讯云市场认证接口 * https://market.cloud.tencent.com/products/15838 */ public function fromMarket() { // 云市场分配的密钥Id $secretId = $this->config['secretId']; // 云市场分配的密钥Key $secretKey = $this->config['secretKey']; $source = 'market'; // 签名 $datetime = gmdate('D, d M Y H:i:s T'); $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source); $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true)); $auth = sprintf( 'hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign ); // 请求方法 $method = 'GET'; // 请求头 $headers = array( 'X-Source' => $source, 'X-Date' => $datetime, 'Authorization' => $auth, ); // 查询参数 $queryParams = array( 'cardNo' => $this->id_card, 'realName' => $this->real_name, ); // body参数(POST方法下) $bodyParams = array(); // url参数拼接 $url = 'https://service-d7fobmw9-1253662630.ap-shanghai.apigateway.myqcloud.com/release/VerifyIdcard'; if (count($queryParams) > 0) { $url .= '?'.http_build_query($queryParams); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt( $ch, CURLOPT_HTTPHEADER, array_map( function ($v, $k) { return $k.': '.$v; }, array_values($headers), array_keys($headers) ) ); if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams)); } $_rs_json = curl_exec($ch); curl_close($ch); $_rs = json_decode($_rs_json, true); if (!isset($_rs['error_code']) && isset($_rs['message'])) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name) ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, $_rs['message']); } if ((isset($_rs['error_code']) && '0' != $_rs['error_code']) || empty($_rs['result'])) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name) ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, CommonStatus::getMsg($_code)); } if (!isset($_rs['result']['isok']) || false === $_rs['result']['isok']) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name) ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, CommonStatus::getMsg($_code)); } $_code = CommonStatus::NO_ERROR; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } }