webman_duanju/app/utils/AliUtils.php

94 lines
2.9 KiB
PHP

<?php
namespace app\utils;
use app\exception\SysException;
use support\Log;
class AliUtils
{
public static function verify($name, $idCard, $accountNo, $bankPreMobile)
{
Log::info("阿里云四要素认证请求参数: {$name}, {$idCard}, {$accountNo}, {$bankPreMobile}");
// 参数校验
if (empty($name)) {
throw new SysException("持卡人姓名不能为空");
}
if (empty($idCard)) {
throw new SysException("身份证号码不能为空");
}
if (empty($accountNo)) {
throw new SysException("银行卡卡号不能为空");
}
if (empty($bankPreMobile)) {
throw new SysException("银行预留手机号码不能为空");
}
if (!preg_match('/^[\x{4e00}-\x{9fa5}]{2,}$/u', $name)) {
throw new SysException("持卡人姓名不合法");
}
if (!self::isValidIdCard($idCard)) {
throw new SysException("身份证号码不合法");
}
if (!preg_match('/^1[3-9]\d{9}$/', $bankPreMobile)) {
throw new SysException("银行预留手机号码格式不正确");
}
$appcode = 'f98606b602564d209f37fc02b0bd590c';
$headers = [
"Authorization: APPCODE {$appcode}",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8"
];
$url = 'https://bkvip.market.alicloudapi.com/v3/bcheck';
$body = http_build_query([
'accountNo' => $accountNo,
'name' => $name,
'idCardCode' => $idCard,
'bankPreMobile' => $bankPreMobile
]);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15
]);
$response = curl_exec($ch);
curl_close($ch);
Log::info("阿里云四要素: {$idCard}, {$name}, {$accountNo}, {$bankPreMobile}, 结果: {$response}");
$ret = json_decode($response, true);
if (!isset($ret['error_code']) || !isset($ret['result'])) {
throw new SysException("阿里云接口返回格式错误".$response);
}
$errorCode = $ret['error_code'];
$result = $ret['result'];
$respCode = $result['respCode'] ?? '';
if ($errorCode == 0 && $respCode === "0") {
// 验证通过
} else {
throw new SysException($result['respMsg'] ?? '认证失败');
}
$bankName = $result['bancardInfor']['bankName'] ?? null;
return [
'bankName' => $bankName,
'respJson' => $response
];
}
private static function isValidIdCard($idCard)
{
// 简单校验身份证号
return preg_match('/^\d{17}[\dXx]$/', $idCard);
}
}