feat: 初始化雄戈商城服务端代码与Webhook自动部署配置
这个提交包含在:
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use think\Db;
|
||||
|
||||
class HuifuOnboardAdminView
|
||||
{
|
||||
public static function enrichListRow($row)
|
||||
{
|
||||
if (!$row) {
|
||||
return $row;
|
||||
}
|
||||
$data = is_array($row) ? $row : $row->toArray();
|
||||
$payload = self::parsePayload($data['payload'] ?? '');
|
||||
$user = self::fetchUser($data['user_id']);
|
||||
$biz = self::businessStatus($data);
|
||||
|
||||
$data['user_nickname'] = $user['nickname'] ?? '';
|
||||
$data['user_mobile'] = $user['mobile'] ?? '';
|
||||
$data['payload_name'] = $payload['name'] ?? '';
|
||||
$data['payload_mobile'] = $payload['mobile_no'] ?? '';
|
||||
$data['payload_cert_no'] = self::maskCertNo($payload['cert_no'] ?? '');
|
||||
$data['payload_card_tail'] = self::maskCardTail($payload['card_no'] ?? '');
|
||||
$data['business_status'] = $biz['status'];
|
||||
$data['business_status_text'] = $biz['status_text'];
|
||||
$data['audit_url'] = $biz['audit_url'];
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function buildDetail($row)
|
||||
{
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
$data = is_array($row) ? $row : $row->toArray();
|
||||
$payload = self::parsePayload($data['payload'] ?? '');
|
||||
$user = self::fetchUser($data['user_id']);
|
||||
$biz = self::businessStatus($data);
|
||||
$fileMap = self::parsePayload($data['file_map'] ?? '');
|
||||
|
||||
$validityMap = ['0' => '非长期有效', '1' => '长期有效'];
|
||||
$form = [
|
||||
'account_type' => $payload['account_type'] ?? 'indv',
|
||||
'account_type_text' => ($payload['account_type'] ?? 'indv') === 'ent' ? '个体户/企业' : '个人',
|
||||
'reg_name' => $payload['reg_name'] ?? '',
|
||||
'license_code' => $payload['license_code'] ?? '',
|
||||
'legal_name' => $payload['legal_name'] ?? '',
|
||||
'branch_name' => $payload['branch_name'] ?? '',
|
||||
'name' => $payload['name'] ?? '',
|
||||
'cert_no' => $payload['cert_no'] ?? '',
|
||||
'cert_no_masked' => self::maskCertNo($payload['cert_no'] ?? ''),
|
||||
'cert_validity_type' => $payload['cert_validity_type'] ?? '',
|
||||
'cert_validity_text' => $validityMap[$payload['cert_validity_type'] ?? ''] ?? '-',
|
||||
'cert_begin_date' => self::formatYmd($payload['cert_begin_date'] ?? ''),
|
||||
'cert_end_date' => self::formatYmd($payload['cert_end_date'] ?? ''),
|
||||
'mobile_no' => $payload['mobile_no'] ?? '',
|
||||
'card_no' => $payload['card_no'] ?? '',
|
||||
'card_no_masked' => self::maskCardNo($payload['card_no'] ?? ''),
|
||||
'card_name' => $payload['card_name'] ?? '',
|
||||
'branch_code' => $payload['branch_code'] ?? '',
|
||||
'prov_id' => $payload['prov_id'] ?? '',
|
||||
'area_id' => $payload['area_id'] ?? '',
|
||||
'id_card_front_url' => self::fullUrl($payload['id_card_front_url'] ?? ''),
|
||||
'id_card_back_url' => self::fullUrl($payload['id_card_back_url'] ?? ''),
|
||||
'bank_card_url' => self::fullUrl($payload['bank_card_url'] ?? ''),
|
||||
];
|
||||
|
||||
return [
|
||||
'record' => [
|
||||
'id' => $data['id'],
|
||||
'user_id' => $data['user_id'],
|
||||
'role_type' => $data['role_type'],
|
||||
'role_ref_id' => $data['role_ref_id'],
|
||||
'onboard_status' => $data['onboard_status'],
|
||||
'huifu_id' => $data['huifu_id'] ?? '',
|
||||
'apply_no' => $data['apply_no'] ?? '',
|
||||
'reject_reason' => $data['reject_reason'] ?? '',
|
||||
'createtime' => $data['createtime'] ?? 0,
|
||||
'updatetime' => $data['updatetime'] ?? 0,
|
||||
],
|
||||
'user' => [
|
||||
'id' => $user['id'] ?? $data['user_id'],
|
||||
'nickname' => $user['nickname'] ?? '',
|
||||
'mobile' => $user['mobile'] ?? '',
|
||||
'avatar' => self::fullUrl($user['avatar'] ?? ''),
|
||||
],
|
||||
'business' => $biz,
|
||||
'form' => $form,
|
||||
'file_map' => $fileMap,
|
||||
'can_submit_huifu' => ($data['onboard_status'] ?? '') === 'draft',
|
||||
'can_retry_huifu' => in_array($data['onboard_status'] ?? '', ['rejected', 'failed'], true),
|
||||
'can_poll' => !in_array($data['onboard_status'] ?? '', ['draft', 'approved'], true),
|
||||
];
|
||||
}
|
||||
|
||||
protected static function fetchUser($userId)
|
||||
{
|
||||
$userId = intval($userId);
|
||||
if ($userId <= 0) {
|
||||
return [];
|
||||
}
|
||||
try {
|
||||
return Db::name('user')->where('id', $userId)->field('id,nickname,mobile,avatar')->find() ?: [];
|
||||
} catch (\Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
protected static function businessStatus(array $data)
|
||||
{
|
||||
$roleType = $data['role_type'] ?? '';
|
||||
$userId = intval($data['user_id'] ?? 0);
|
||||
$roleRefId = intval($data['role_ref_id'] ?? 0);
|
||||
$statusMap = [
|
||||
'pending' => '待审核',
|
||||
'normal' => '已通过',
|
||||
'needinfo' => '待完善资料',
|
||||
'reject' => '已驳回',
|
||||
'forbidden' => '已禁用',
|
||||
'freeze' => '已冻结',
|
||||
];
|
||||
|
||||
if ($roleType === 'regional_agent') {
|
||||
$agent = Db::name('shopro_regional_agent')->where('id', $roleRefId)->find();
|
||||
$status = $agent['status'] ?? '';
|
||||
return [
|
||||
'type' => 'regional_agent',
|
||||
'type_text' => '区域公司',
|
||||
'status' => $status,
|
||||
'status_text' => $statusMap[$status] ?? ($status ?: '未知'),
|
||||
'area_name' => $agent['area_name'] ?? '',
|
||||
'audit_url' => $roleRefId ? ('shopro/regional_agent/agent/index') : '',
|
||||
];
|
||||
}
|
||||
|
||||
$agent = Db::name('shopro_commission_agent')->where('user_id', $userId)->find();
|
||||
$status = $agent['status'] ?? '';
|
||||
return [
|
||||
'type' => 'promoter',
|
||||
'type_text' => '推广员/分销商',
|
||||
'status' => $status,
|
||||
'status_text' => $statusMap[$status] ?? ($status ?: '未成为分销商'),
|
||||
'area_name' => '',
|
||||
'audit_url' => 'shopro/commission/agent/index',
|
||||
];
|
||||
}
|
||||
|
||||
protected static function parsePayload($raw)
|
||||
{
|
||||
if (!$raw) {
|
||||
return [];
|
||||
}
|
||||
if (is_array($raw)) {
|
||||
return $raw;
|
||||
}
|
||||
$data = json_decode($raw, true);
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
protected static function fullUrl($url)
|
||||
{
|
||||
$url = trim((string) $url);
|
||||
if ($url === '') {
|
||||
return '';
|
||||
}
|
||||
if (preg_match('/^https?:\/\//i', $url)) {
|
||||
return $url;
|
||||
}
|
||||
return cdnurl($url, true);
|
||||
}
|
||||
|
||||
protected static function maskCertNo($certNo)
|
||||
{
|
||||
$certNo = trim((string) $certNo);
|
||||
if (strlen($certNo) < 8) {
|
||||
return $certNo;
|
||||
}
|
||||
return substr($certNo, 0, 6) . '********' . substr($certNo, -4);
|
||||
}
|
||||
|
||||
protected static function maskCardNo($cardNo)
|
||||
{
|
||||
$cardNo = preg_replace('/\s+/', '', (string) $cardNo);
|
||||
$len = strlen($cardNo);
|
||||
if ($len <= 4) {
|
||||
return $cardNo;
|
||||
}
|
||||
return str_repeat('*', max(0, $len - 4)) . substr($cardNo, -4);
|
||||
}
|
||||
|
||||
protected static function maskCardTail($cardNo)
|
||||
{
|
||||
$cardNo = preg_replace('/\s+/', '', (string) $cardNo);
|
||||
if ($cardNo === '') {
|
||||
return '';
|
||||
}
|
||||
return '****' . substr($cardNo, -4);
|
||||
}
|
||||
|
||||
protected static function formatYmd($value)
|
||||
{
|
||||
$value = preg_replace('/\D/', '', (string) $value);
|
||||
if (strlen($value) !== 8) {
|
||||
return $value;
|
||||
}
|
||||
return substr($value, 0, 4) . '-' . substr($value, 4, 2) . '-' . substr($value, 6, 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
class HuifuOnboardAreaHelper
|
||||
{
|
||||
/**
|
||||
* 将小程序 shopro 省市区 ID 转为汇付 prov_id / area_id / district_id
|
||||
*/
|
||||
public static function applyRegionCodes(array &$data)
|
||||
{
|
||||
if (empty($data['prov_id']) && !empty($data['shopro_province_id'])) {
|
||||
$data['prov_id'] = self::toCode($data['shopro_province_id']);
|
||||
}
|
||||
if (empty($data['area_id']) && !empty($data['shopro_city_id'])) {
|
||||
$data['area_id'] = self::toCode($data['shopro_city_id']);
|
||||
}
|
||||
if (empty($data['district_id']) && !empty($data['shopro_district_id'])) {
|
||||
$data['district_id'] = self::toCode($data['shopro_district_id']);
|
||||
}
|
||||
}
|
||||
|
||||
public static function toCode($id)
|
||||
{
|
||||
$id = trim((string) $id);
|
||||
if ($id === '') {
|
||||
return '';
|
||||
}
|
||||
if (strlen($id) >= 6) {
|
||||
return substr($id, 0, 6);
|
||||
}
|
||||
return str_pad($id, 6, '0', STR_PAD_RIGHT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use think\Db;
|
||||
|
||||
class HuifuOnboardBankBranch
|
||||
{
|
||||
protected static $jsonCache;
|
||||
|
||||
/**
|
||||
* @return array<int, array{branch_code:string,branch_name:string,bank_name?:string,prov_id?:string,area_id?:string}>
|
||||
*/
|
||||
public static function search($keyword, $cityCode = '', $limit = 20)
|
||||
{
|
||||
$keyword = trim((string) $keyword);
|
||||
if ($keyword === '') {
|
||||
return [];
|
||||
}
|
||||
$limit = max(1, min(50, (int) $limit));
|
||||
$cityCode = HuifuOnboardAreaHelper::toCode($cityCode);
|
||||
|
||||
$rows = self::searchDatabase($keyword, $cityCode, $limit);
|
||||
if (count($rows) >= $limit) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
$jsonRows = self::searchJson($keyword, $cityCode, $limit - count($rows));
|
||||
$existing = array_column($rows, 'branch_code');
|
||||
foreach ($jsonRows as $row) {
|
||||
if (in_array($row['branch_code'], $existing, true)) {
|
||||
continue;
|
||||
}
|
||||
$rows[] = $row;
|
||||
if (count($rows) >= $limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
protected static function searchDatabase($keyword, $cityCode, $limit)
|
||||
{
|
||||
try {
|
||||
$query = Db::name('shopro_huifu_bank_branch')->where('status', 1);
|
||||
if ($cityCode !== '') {
|
||||
$query->where('area_id', $cityCode);
|
||||
}
|
||||
$query->where(function ($q) use ($keyword) {
|
||||
$q->whereLike('branch_name', '%' . $keyword . '%')
|
||||
->whereOr('branch_code', 'like', '%' . $keyword . '%')
|
||||
->whereOr('bank_name', 'like', '%' . $keyword . '%');
|
||||
});
|
||||
$list = $query->limit($limit)->select();
|
||||
if (!$list) {
|
||||
return [];
|
||||
}
|
||||
$rows = [];
|
||||
foreach ($list as $item) {
|
||||
$rows[] = [
|
||||
'branch_code' => (string) $item['branch_code'],
|
||||
'branch_name' => (string) $item['branch_name'],
|
||||
'bank_name' => (string) ($item['bank_name'] ?? ''),
|
||||
'prov_id' => (string) ($item['prov_id'] ?? ''),
|
||||
'area_id' => (string) ($item['area_id'] ?? ''),
|
||||
];
|
||||
}
|
||||
return $rows;
|
||||
} catch (\Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
protected static function searchJson($keyword, $limit)
|
||||
{
|
||||
if ($limit <= 0) {
|
||||
return [];
|
||||
}
|
||||
$all = self::loadJson();
|
||||
$keywordLower = mb_strtolower($keyword, 'UTF-8');
|
||||
$rows = [];
|
||||
foreach ($all as $item) {
|
||||
$hay = mb_strtolower(
|
||||
($item['branch_name'] ?? '') . ' ' . ($item['bank_name'] ?? '') . ' ' . ($item['branch_code'] ?? ''),
|
||||
'UTF-8'
|
||||
);
|
||||
if (mb_strpos($hay, $keywordLower, 0, 'UTF-8') === false) {
|
||||
continue;
|
||||
}
|
||||
$rows[] = $item;
|
||||
if (count($rows) >= $limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
protected static function loadJson()
|
||||
{
|
||||
if (self::$jsonCache !== null) {
|
||||
return self::$jsonCache;
|
||||
}
|
||||
$file = ADDON_PATH . 'shopro_huifu/data/bank_branches.json';
|
||||
if (!is_file($file)) {
|
||||
self::$jsonCache = [];
|
||||
return self::$jsonCache;
|
||||
}
|
||||
$decoded = json_decode(file_get_contents($file), true);
|
||||
self::$jsonCache = is_array($decoded) ? $decoded : [];
|
||||
return self::$jsonCache;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\library\SdkLoader;
|
||||
|
||||
class HuifuOnboardBasicEnt
|
||||
{
|
||||
protected $sdk;
|
||||
|
||||
public function __construct(HuifuOnboardSdk $sdk = null)
|
||||
{
|
||||
$this->sdk = $sdk ?: new HuifuOnboardSdk();
|
||||
}
|
||||
|
||||
public function submit(array $form, array $fileMap)
|
||||
{
|
||||
SdkLoader::requireRequest('V2UserBasicdataEntRequest.php');
|
||||
$meta = $this->sdk->newReqMeta();
|
||||
$request = new \BsPaySdk\request\V2UserBasicdataEntRequest();
|
||||
$request->setReqSeqId($meta['req_seq_id']);
|
||||
$request->setReqDate($meta['req_date']);
|
||||
$request->setRegName($form['reg_name']);
|
||||
$request->setLicenseCode($form['license_code']);
|
||||
$request->setLicenseValidityType($form['license_validity_type']);
|
||||
$request->setLicenseBeginDate($form['license_begin_date']);
|
||||
if ($form['license_validity_type'] === '0' && !empty($form['license_end_date'])) {
|
||||
$request->setLicenseEndDate($form['license_end_date']);
|
||||
}
|
||||
$request->setRegProvId($form['prov_id']);
|
||||
$request->setRegAreaId($form['area_id']);
|
||||
if (!empty($form['district_id'])) {
|
||||
$request->setRegDistrictId($form['district_id']);
|
||||
}
|
||||
$request->setRegDetail($form['reg_detail']);
|
||||
$request->setLegalName($form['legal_name']);
|
||||
$request->setLegalCertType('00');
|
||||
$request->setLegalCertNo($form['legal_cert_no']);
|
||||
$request->setLegalCertValidityType($form['legal_cert_validity_type']);
|
||||
$request->setLegalCertBeginDate($form['legal_cert_begin_date']);
|
||||
if ($form['legal_cert_validity_type'] === '0' && !empty($form['legal_cert_end_date'])) {
|
||||
$request->setLegalCertEndDate($form['legal_cert_end_date']);
|
||||
}
|
||||
$request->setContactName($form['legal_name']);
|
||||
$request->setContactMobile($form['legal_mobile']);
|
||||
$request->setLoginName($form['login_name'] ?? ('HFENT' . date('YmdHis') . mt_rand(100, 999)));
|
||||
|
||||
$fileList = [];
|
||||
if (!empty($fileMap['F01'])) {
|
||||
$fileList[] = [
|
||||
'file_type' => 'F01',
|
||||
'file_id' => $fileMap['F01'],
|
||||
'file_name' => 'license.jpg',
|
||||
];
|
||||
}
|
||||
if (!empty($fileMap['F02'])) {
|
||||
$fileList[] = [
|
||||
'file_type' => 'F02',
|
||||
'file_id' => $fileMap['F02'],
|
||||
'file_name' => 'legal_id_front.jpg',
|
||||
];
|
||||
}
|
||||
if (!empty($fileMap['F03'])) {
|
||||
$fileList[] = [
|
||||
'file_type' => 'F03',
|
||||
'file_id' => $fileMap['F03'],
|
||||
'file_name' => 'legal_id_back.jpg',
|
||||
];
|
||||
}
|
||||
|
||||
$extend = $this->sdk->mergeSkillHuifuId([
|
||||
'short_name' => $form['reg_name'],
|
||||
'sms_send_flag' => '1',
|
||||
]);
|
||||
if ($fileList) {
|
||||
$extend['file_list'] = json_encode($fileList, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
$request->setExtendInfo($extend);
|
||||
|
||||
$result = $this->sdk->getClient()->postRequest($request);
|
||||
$data = $this->sdk->getClient()->parseResponse($result);
|
||||
$huifuId = $data['huifu_id'] ?? '';
|
||||
if ($huifuId === '') {
|
||||
throw new \RuntimeException('企业开户未返回 huifu_id');
|
||||
}
|
||||
|
||||
return [
|
||||
'req_date' => $meta['req_date'],
|
||||
'req_seq_id' => $meta['req_seq_id'],
|
||||
'huifu_id' => $huifuId,
|
||||
'resp' => $data,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\library\SdkLoader;
|
||||
|
||||
class HuifuOnboardBasicIndv
|
||||
{
|
||||
protected $sdk;
|
||||
|
||||
public function __construct(HuifuOnboardSdk $sdk = null)
|
||||
{
|
||||
$this->sdk = $sdk ?: new HuifuOnboardSdk();
|
||||
}
|
||||
|
||||
public function submit(array $form, array $fileMap)
|
||||
{
|
||||
SdkLoader::requireRequest('V2UserBasicdataIndvRequest.php');
|
||||
$meta = $this->sdk->newReqMeta();
|
||||
$request = new \BsPaySdk\request\V2UserBasicdataIndvRequest();
|
||||
$request->setReqSeqId($meta['req_seq_id']);
|
||||
$request->setReqDate($meta['req_date']);
|
||||
$request->setName($form['name']);
|
||||
$request->setCertType('00');
|
||||
$request->setCertNo($form['cert_no']);
|
||||
$request->setCertValidityType($form['cert_validity_type']);
|
||||
$request->setCertBeginDate($form['cert_begin_date']);
|
||||
$request->setMobileNo($form['mobile_no']);
|
||||
if (!empty($form['reg_detail'])) {
|
||||
$request->setAddress($form['reg_detail']);
|
||||
}
|
||||
|
||||
$fileList = [];
|
||||
if (!empty($fileMap['F02'])) {
|
||||
$fileList[] = [
|
||||
'file_type' => 'F02',
|
||||
'file_id' => $fileMap['F02'],
|
||||
'file_name' => 'id_front.jpg',
|
||||
];
|
||||
}
|
||||
if (!empty($fileMap['F03'])) {
|
||||
$fileList[] = [
|
||||
'file_type' => 'F03',
|
||||
'file_id' => $fileMap['F03'],
|
||||
'file_name' => 'id_back.jpg',
|
||||
];
|
||||
}
|
||||
|
||||
$extend = [];
|
||||
if ($form['cert_validity_type'] === '0' && !empty($form['cert_end_date'])) {
|
||||
$extend['cert_end_date'] = $form['cert_end_date'];
|
||||
}
|
||||
if ($fileList) {
|
||||
$extend['file_list'] = json_encode($fileList, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
if ($extend) {
|
||||
$request->setExtendInfo($extend);
|
||||
}
|
||||
|
||||
$result = $this->sdk->getClient()->postRequest($request);
|
||||
$data = $this->sdk->getClient()->parseResponse($result);
|
||||
$huifuId = $data['huifu_id'] ?? '';
|
||||
if ($huifuId === '') {
|
||||
throw new \RuntimeException('个人开户未返回 huifu_id');
|
||||
}
|
||||
|
||||
return [
|
||||
'req_date' => $meta['req_date'],
|
||||
'req_seq_id' => $meta['req_seq_id'],
|
||||
'huifu_id' => $huifuId,
|
||||
'resp' => $data,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
|
||||
|
||||
use addons\shopro_huifu\library\HuifuConfig;
|
||||
|
||||
use addons\shopro_huifu\library\SdkLoader;
|
||||
|
||||
|
||||
|
||||
class HuifuOnboardBusiOpen
|
||||
|
||||
{
|
||||
|
||||
protected $sdk;
|
||||
|
||||
protected $config;
|
||||
|
||||
|
||||
|
||||
public function __construct(HuifuOnboardSdk $sdk = null)
|
||||
|
||||
{
|
||||
|
||||
$this->sdk = $sdk ?: new HuifuOnboardSdk();
|
||||
|
||||
$this->config = HuifuConfig::getInstance();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function submit($huifuId, array $form, array $fileMap)
|
||||
|
||||
{
|
||||
|
||||
SdkLoader::requireRequest('V2UserBusiOpenRequest.php');
|
||||
|
||||
$meta = $this->sdk->newReqMeta();
|
||||
|
||||
$request = new \BsPaySdk\request\V2UserBusiOpenRequest();
|
||||
|
||||
$request->setHuifuId($huifuId);
|
||||
|
||||
$request->setReqSeqId($meta['req_seq_id']);
|
||||
|
||||
$request->setReqDate($meta['req_date']);
|
||||
|
||||
$request->setUpperHuifuId($this->sdk->getUpperHuifuId());
|
||||
|
||||
|
||||
|
||||
$settleCycle = $this->config->get('onboard_settle_cycle', 'D1');
|
||||
|
||||
$settleBatch = $this->config->get('onboard_settle_batch_no', '300');
|
||||
|
||||
$cashType = $this->config->get('onboard_cash_type', 'D1');
|
||||
|
||||
$cashFix = $this->config->get('onboard_cash_fix_amt', '0.03');
|
||||
|
||||
$cashRate = $this->config->get('onboard_cash_fee_rate', '0');
|
||||
|
||||
|
||||
|
||||
$settleConfigList = [[
|
||||
|
||||
'settle_cycle' => $settleCycle,
|
||||
|
||||
'settle_batch_no' => $settleBatch,
|
||||
|
||||
'min_amt' => '0.01',
|
||||
|
||||
'remained_amt' => '0.00',
|
||||
|
||||
'settle_abstract' => '分账结算',
|
||||
|
||||
'out_settle_flag' => '2',
|
||||
|
||||
'fixed_ratio' => '0.00',
|
||||
|
||||
]];
|
||||
|
||||
|
||||
|
||||
$cardType = (string) ($form['bank_account_type'] ?? '0');
|
||||
|
||||
$cardInfo = [
|
||||
|
||||
'card_type' => $cardType,
|
||||
|
||||
'card_name' => $form['card_name'],
|
||||
|
||||
'card_no' => $form['card_no'],
|
||||
|
||||
'prov_id' => $form['prov_id'],
|
||||
|
||||
'area_id' => $form['area_id'],
|
||||
|
||||
'mp' => $form['mp'],
|
||||
|
||||
];
|
||||
|
||||
if (!empty($form['branch_code'])) {
|
||||
|
||||
$cardInfo['branch_code'] = $form['branch_code'];
|
||||
|
||||
}
|
||||
|
||||
if (!empty($form['branch_name'])) {
|
||||
|
||||
$cardInfo['branch_name'] = $form['branch_name'];
|
||||
|
||||
}
|
||||
|
||||
if ($cardType === '0') {
|
||||
|
||||
$cardInfo['cert_type'] = '00';
|
||||
|
||||
$cardInfo['cert_no'] = $form['cert_no'];
|
||||
|
||||
$cardInfo['cert_validity_type'] = $form['cert_validity_type'];
|
||||
|
||||
$cardInfo['cert_begin_date'] = $form['cert_begin_date'];
|
||||
|
||||
if ($form['cert_validity_type'] === '0' && !empty($form['cert_end_date'])) {
|
||||
|
||||
$cardInfo['cert_end_date'] = $form['cert_end_date'];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$cashConfig = [[
|
||||
|
||||
'cash_type' => $cashType,
|
||||
|
||||
'fix_amt' => $cashFix,
|
||||
|
||||
'fee_rate' => $cashRate,
|
||||
|
||||
]];
|
||||
|
||||
|
||||
|
||||
$fileList = [];
|
||||
|
||||
if (!empty($fileMap['F13'])) {
|
||||
|
||||
$fileList[] = [
|
||||
|
||||
'file_type' => 'F13',
|
||||
|
||||
'file_id' => $fileMap['F13'],
|
||||
|
||||
'file_name' => 'bank_card.jpg',
|
||||
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
if ($cardType === '1' && !empty($fileMap['F08'])) {
|
||||
|
||||
$fileList[] = [
|
||||
|
||||
'file_type' => 'F08',
|
||||
|
||||
'file_id' => $fileMap['F08'],
|
||||
|
||||
'file_name' => 'account_permit.jpg',
|
||||
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$extend = [
|
||||
|
||||
'settle_config_list' => json_encode($settleConfigList, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
|
||||
'card_info' => json_encode($cardInfo, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
|
||||
'cash_config' => json_encode($cashConfig, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
|
||||
'async_return_url' => $this->sdk->getOnboardNotifyUrl(),
|
||||
|
||||
];
|
||||
|
||||
if ($fileList) {
|
||||
|
||||
$extend['file_list'] = json_encode($fileList, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
}
|
||||
|
||||
$request->setExtendInfo($extend);
|
||||
|
||||
|
||||
|
||||
$result = $this->sdk->getClient()->postRequest($request);
|
||||
|
||||
$data = $this->sdk->getClient()->parseResponse($result);
|
||||
|
||||
$applyNo = $data['apply_no'] ?? ($data['apply_id'] ?? '');
|
||||
|
||||
|
||||
|
||||
return [
|
||||
|
||||
'req_date' => $meta['req_date'],
|
||||
|
||||
'req_seq_id' => $meta['req_seq_id'],
|
||||
|
||||
'apply_no' => $applyNo,
|
||||
|
||||
'resp' => $data,
|
||||
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
class HuifuOnboardError
|
||||
{
|
||||
/**
|
||||
* 将汇付/系统异常转为用户可读提示(去掉 resp_code 等技术字段)
|
||||
*/
|
||||
public static function translate($message)
|
||||
{
|
||||
$msg = trim((string) $message);
|
||||
if ($msg === '') {
|
||||
return '进件提交失败,请稍后重试';
|
||||
}
|
||||
|
||||
$lower = strtolower($msg);
|
||||
if (strpos($msg, 'certEndDate') !== false
|
||||
|| strpos($msg, '证件有效期') !== false
|
||||
|| strpos($msg, '到期日') !== false) {
|
||||
return '身份证/证件有效期须晚于今天;长期有效请选择 2099-12-31';
|
||||
}
|
||||
if (strpos($msg, 'certBeginDate') !== false || strpos($msg, '开始日期') !== false) {
|
||||
return '证件开始日期填写有误,请与身份证一致';
|
||||
}
|
||||
if (strpos($msg, 'branch') !== false || strpos($msg, '联行') !== false) {
|
||||
return '开户银行支行信息有误,请重新搜索选择支行';
|
||||
}
|
||||
if (strpos($msg, 'branch_code') !== false) {
|
||||
return '请选择正确的开户银行支行';
|
||||
}
|
||||
if (strpos($msg, '图片') !== false || strpos($lower, 'picture') !== false || strpos($lower, 'file_id') !== false) {
|
||||
return '证件或银行卡图片上传失败,请重新上传 jpg/png 清晰照片';
|
||||
}
|
||||
|
||||
if (preg_match('/基础参数校验失败[::](.+?)(?:,resp_code|$)/u', $msg, $m)) {
|
||||
return '资料校验未通过:' . trim($m[1]);
|
||||
}
|
||||
|
||||
$msg = preg_replace('/,resp_code=[^,]+/u', '', $msg);
|
||||
$msg = preg_replace('/,trans_stat=[^,]+/u', '', $msg);
|
||||
$msg = str_replace('汇付业务失败,', '', $msg);
|
||||
$msg = str_replace('汇付 SDK 请求异常:', '', $msg);
|
||||
|
||||
return $msg !== '' ? $msg : '进件提交失败,请检查资料后重试';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\library\Bridge;
|
||||
use addons\shopro_huifu\library\HuifuConfig;
|
||||
use addons\shopro_huifu\model\Onboard;
|
||||
use think\Db;
|
||||
|
||||
class HuifuOnboardGate
|
||||
{
|
||||
/**
|
||||
* 与 shopro split Config::isHuifuMode 同口径,但不依赖核心是否已升级该方法
|
||||
*/
|
||||
public static function isHuifuSplitActive()
|
||||
{
|
||||
try {
|
||||
$split = Db::name('shopro_split_config')->column('value', 'name');
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
if (($split['split_enable'] ?? '0') !== '1') {
|
||||
return false;
|
||||
}
|
||||
$mode = $split['split_mode'] ?? 'wechat';
|
||||
if ($mode === 'huifu' || $mode === 'both') {
|
||||
return true;
|
||||
}
|
||||
$provider = $split['split_provider'] ?? 'auto';
|
||||
if ($provider === 'huifu') {
|
||||
return true;
|
||||
}
|
||||
if ($provider === 'auto') {
|
||||
return Bridge::isHuifuMiniProgramChannel('wxMiniProgram');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isOnboardRequired()
|
||||
{
|
||||
if (!Bridge::isEnabled()) {
|
||||
return false;
|
||||
}
|
||||
if (HuifuConfig::getInstance()->get('onboard_required', '1') === '0') {
|
||||
return false;
|
||||
}
|
||||
return self::isHuifuSplitActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台业务审核通过后触发汇付进件(先审后汇付)
|
||||
*/
|
||||
public static function submitOnBusinessApproved($userId, $roleType, $roleRefId)
|
||||
{
|
||||
if (!self::isOnboardRequired()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
$service = new HuifuOnboardService();
|
||||
return $service->submitFromDraft($userId, $roleType, $roleRefId);
|
||||
} catch (\Throwable $e) {
|
||||
\think\Log::error('[shopro_huifu] onboard after admin approve fail uid=' . $userId . ' ' . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户端是否可重新提交汇付(仅汇付驳回/失败后)
|
||||
*/
|
||||
public static function canSubmitOnboard($userId, $roleType, $roleRefId, $agentStatus = '')
|
||||
{
|
||||
if (!self::isOnboardRequired()) {
|
||||
return false;
|
||||
}
|
||||
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
||||
if (!$row) {
|
||||
return false;
|
||||
}
|
||||
return in_array($row['onboard_status'], [
|
||||
Onboard::STATUS_REJECTED,
|
||||
Onboard::STATUS_FAILED,
|
||||
], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户端是否可保存/更新进件草稿(不调用汇付 API)
|
||||
*/
|
||||
public static function canSaveHuifuDraft($userId, $roleType, $roleRefId, $agentStatus = '')
|
||||
{
|
||||
if (!self::isOnboardRequired()) {
|
||||
return false;
|
||||
}
|
||||
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
||||
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
||||
return false;
|
||||
}
|
||||
if ($row && in_array($row['onboard_status'], [
|
||||
Onboard::STATUS_SUBMITTING,
|
||||
Onboard::STATUS_BASIC_OK,
|
||||
Onboard::STATUS_BUSI_PENDING,
|
||||
], true)) {
|
||||
return false;
|
||||
}
|
||||
if ($row && $row['onboard_status'] === Onboard::STATUS_DRAFT
|
||||
&& in_array($agentStatus, ['pending', 'needinfo', 'reject'], true)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function publicStatus($row, $userId = 0, $roleType = 'promoter', $roleRefId = 0, $agentStatus = '')
|
||||
{
|
||||
$required = self::isOnboardRequired();
|
||||
$canSubmit = $required && $userId && self::canSubmitOnboard($userId, $roleType, $roleRefId, $agentStatus);
|
||||
$canSaveDraft = $required && $userId && self::canSaveHuifuDraft($userId, $roleType, $roleRefId, $agentStatus);
|
||||
$awaitAdmin = $required && $row && $row['onboard_status'] === Onboard::STATUS_DRAFT
|
||||
&& in_array($agentStatus, ['pending', 'needinfo', 'reject'], true);
|
||||
if (!$row) {
|
||||
return [
|
||||
'required' => $required,
|
||||
'can_submit' => $canSubmit,
|
||||
'can_save_draft' => $canSaveDraft,
|
||||
'await_admin' => false,
|
||||
'audit_first' => true,
|
||||
'status' => null,
|
||||
'huifu_id' => '',
|
||||
'reject_reason' => '',
|
||||
];
|
||||
}
|
||||
return [
|
||||
'required' => $required,
|
||||
'can_submit' => $canSubmit,
|
||||
'can_save_draft' => $canSaveDraft,
|
||||
'await_admin' => $awaitAdmin,
|
||||
'audit_first' => true,
|
||||
'status' => $row['onboard_status'],
|
||||
'huifu_id' => $row['huifu_id'] ? substr($row['huifu_id'], 0, 4) . '****' . substr($row['huifu_id'], -4) : '',
|
||||
'reject_reason' => $row['reject_reason'] ?? '',
|
||||
'apply_no' => $row['apply_no'] ?? '',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\library\HuifuConfig;
|
||||
use addons\shopro_huifu\library\SdkLoader;
|
||||
use think\Log;
|
||||
|
||||
class HuifuOnboardPicture
|
||||
{
|
||||
protected $sdk;
|
||||
|
||||
public function __construct(HuifuOnboardSdk $sdk = null)
|
||||
{
|
||||
$this->sdk = $sdk ?: new HuifuOnboardSdk();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileType F02/F03/F13
|
||||
* @param string $imageUrl 站点可访问图片 URL
|
||||
*/
|
||||
public function uploadByUrl($fileType, $imageUrl)
|
||||
{
|
||||
SdkLoader::requireRequest('V2SupplementaryPictureRequest.php');
|
||||
$meta = $this->sdk->newReqMeta();
|
||||
$request = new \BsPaySdk\request\V2SupplementaryPictureRequest();
|
||||
$request->setReqSeqId($meta['req_seq_id']);
|
||||
$request->setReqDate($meta['req_date']);
|
||||
$request->setFileType($fileType);
|
||||
|
||||
list($sourcePath, $sourceTemp) = $this->resolveUploadFile($imageUrl);
|
||||
list($uploadPath, $uploadTemp) = $this->normalizeImageForHuifu($sourcePath, $sourceTemp);
|
||||
|
||||
$filename = $this->buildUploadFilename($fileType, $uploadPath);
|
||||
$mime = $this->detectMime($uploadPath);
|
||||
$merchantHuifuId = $this->sdk->getPictureMerchantHuifuId();
|
||||
|
||||
$extend = $this->sdk->mergeSkillHuifuId([
|
||||
'huifu_id' => $merchantHuifuId,
|
||||
'picture' => $filename,
|
||||
]);
|
||||
$request->setExtendInfo($extend);
|
||||
|
||||
$uploadTag = new \CURLFile($uploadPath, $mime, $filename);
|
||||
|
||||
try {
|
||||
$result = $this->sdk->getClient()->postRequest($request, $uploadTag);
|
||||
return $this->parsePictureUploadResult($result, $fileType);
|
||||
} finally {
|
||||
if ($sourceTemp && is_file($sourcePath)) {
|
||||
@unlink($sourcePath);
|
||||
}
|
||||
if ($uploadTemp && $uploadPath !== $sourcePath && is_file($uploadPath)) {
|
||||
@unlink($uploadPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function parsePictureUploadResult($result, $fileType)
|
||||
{
|
||||
if (!$result || $result->isError()) {
|
||||
$msg = \addons\shopro_huifu\library\HuifuClient::formatSdkError($result);
|
||||
Log::error('[shopro_huifu] onboard picture sdk fail type=' . $fileType . ' ' . $msg);
|
||||
throw new \RuntimeException('图片上传汇付失败:' . $msg);
|
||||
}
|
||||
|
||||
$raw = $result->getRspDatas();
|
||||
Log::info('[shopro_huifu] onboard picture resp type=' . $fileType . ' raw=' . json_encode($raw, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$layers = [];
|
||||
if (is_array($raw)) {
|
||||
$layers[] = $raw;
|
||||
if (isset($raw['data'])) {
|
||||
$layers[] = $this->decodeLayer($raw['data']);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($layers as $layer) {
|
||||
if (!is_array($layer)) {
|
||||
continue;
|
||||
}
|
||||
$this->assertPictureResponse($layer);
|
||||
$fileId = $this->extractPictureFileId($layer);
|
||||
if ($fileId !== '') {
|
||||
return $fileId;
|
||||
}
|
||||
}
|
||||
|
||||
$desc = $this->pickResponseDesc($layers);
|
||||
Log::error('[shopro_huifu] onboard picture no file_id type=' . $fileType . ' resp=' . json_encode($raw, JSON_UNESCAPED_UNICODE));
|
||||
throw new \RuntimeException('图片上传汇付失败' . ($desc ? ':' . $desc : '') . ',请使用 jpg/png 格式且大小不超过 2MB');
|
||||
}
|
||||
|
||||
protected function decodeLayer($value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
if (is_string($value) && $value !== '') {
|
||||
$decoded = json_decode($value, true);
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function pickResponseDesc(array $layers)
|
||||
{
|
||||
foreach ($layers as $layer) {
|
||||
if (!is_array($layer)) {
|
||||
continue;
|
||||
}
|
||||
foreach (['sub_resp_desc', 'resp_desc'] as $key) {
|
||||
if (!empty($layer[$key])) {
|
||||
return (string) $layer[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function assertPictureResponse(array $data)
|
||||
{
|
||||
$subCode = (string) ($data['sub_resp_code'] ?? '');
|
||||
if ($subCode !== '' && $subCode !== '00000000') {
|
||||
$desc = (string) ($data['sub_resp_desc'] ?? '业务失败');
|
||||
throw new \RuntimeException('图片上传汇付失败:' . $desc . ' [' . $subCode . ']');
|
||||
}
|
||||
$respCode = (string) ($data['resp_code'] ?? '');
|
||||
if ($respCode !== '' && !in_array($respCode, ['00000000', '10000'], true)) {
|
||||
$desc = (string) ($data['resp_desc'] ?? '请求失败');
|
||||
throw new \RuntimeException('图片上传汇付失败:' . $desc . ' [' . $respCode . ']');
|
||||
}
|
||||
}
|
||||
|
||||
protected function extractPictureFileId(array $data)
|
||||
{
|
||||
foreach (['file_id', 'jfile_id', 'fileId'] as $key) {
|
||||
if (!empty($data[$key])) {
|
||||
return trim((string) $data[$key]);
|
||||
}
|
||||
}
|
||||
foreach ($data as $value) {
|
||||
if (is_array($value)) {
|
||||
$nested = $this->extractPictureFileId($value);
|
||||
if ($nested !== '') {
|
||||
return $nested;
|
||||
}
|
||||
} elseif (is_string($value) && $value !== '' && $value[0] === '{') {
|
||||
$inner = json_decode($value, true);
|
||||
if (is_array($inner)) {
|
||||
$nested = $this->extractPictureFileId($inner);
|
||||
if ($nested !== '') {
|
||||
return $nested;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function buildUploadFilename($fileType, $localPath)
|
||||
{
|
||||
$map = [
|
||||
'F02' => 'id_front',
|
||||
'F03' => 'id_back',
|
||||
'F12' => 'bank_card',
|
||||
'F13' => 'bank_card',
|
||||
];
|
||||
$base = $map[$fileType] ?? 'upload';
|
||||
$ext = strtolower(pathinfo($localPath, PATHINFO_EXTENSION));
|
||||
if (!in_array($ext, ['jpg', 'jpeg', 'png'], true)) {
|
||||
$ext = 'jpg';
|
||||
}
|
||||
if ($ext === 'jpeg') {
|
||||
$ext = 'jpg';
|
||||
}
|
||||
return substr($base, 0, 20) . '.' . $ext;
|
||||
}
|
||||
|
||||
protected function detectMime($localPath)
|
||||
{
|
||||
if (function_exists('mime_content_type')) {
|
||||
$mime = mime_content_type($localPath);
|
||||
if ($mime && stripos($mime, 'image/') === 0) {
|
||||
return $mime;
|
||||
}
|
||||
}
|
||||
$ext = strtolower(pathinfo($localPath, PATHINFO_EXTENSION));
|
||||
if ($ext === 'png') {
|
||||
return 'image/png';
|
||||
}
|
||||
return 'image/jpeg';
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为汇付支持的 jpg/png,必要时压缩
|
||||
* @return array [uploadPath, shouldDeleteUploadPath]
|
||||
*/
|
||||
protected function normalizeImageForHuifu($localPath, $isTemp)
|
||||
{
|
||||
if (!is_file($localPath)) {
|
||||
throw new \RuntimeException('图片文件不存在');
|
||||
}
|
||||
if (filesize($localPath) > 2 * 1024 * 1024) {
|
||||
$compressed = $this->compressImage($localPath);
|
||||
if ($compressed) {
|
||||
return [$compressed, true];
|
||||
}
|
||||
throw new \RuntimeException('图片超过 2MB,请压缩后重新上传');
|
||||
}
|
||||
|
||||
$binary = file_get_contents($localPath);
|
||||
if ($binary === false || $binary === '') {
|
||||
throw new \RuntimeException('无法读取图片文件');
|
||||
}
|
||||
|
||||
$isJpeg = strncmp($binary, "\xFF\xD8\xFF", 3) === 0;
|
||||
$isPng = strncmp($binary, "\x89PNG\r\n\x1a\n", 8) === 0;
|
||||
if ($isJpeg || $isPng) {
|
||||
return [$localPath, $isTemp];
|
||||
}
|
||||
|
||||
if (!function_exists('imagecreatefromstring') || !function_exists('imagejpeg')) {
|
||||
throw new \RuntimeException('图片格式不支持,请上传 jpg 或 png 格式');
|
||||
}
|
||||
|
||||
$img = @imagecreatefromstring($binary);
|
||||
if (!$img) {
|
||||
throw new \RuntimeException('图片格式不支持,请上传 jpg 或 png 格式');
|
||||
}
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'hf_pic_conv_');
|
||||
$out = $tmp . '.jpg';
|
||||
@unlink($tmp);
|
||||
if (!imagejpeg($img, $out, 85)) {
|
||||
imagedestroy($img);
|
||||
throw new \RuntimeException('图片格式转换失败');
|
||||
}
|
||||
imagedestroy($img);
|
||||
if (filesize($out) > 2 * 1024 * 1024) {
|
||||
@unlink($out);
|
||||
throw new \RuntimeException('图片超过 2MB,请压缩后重新上传');
|
||||
}
|
||||
return [$out, true];
|
||||
}
|
||||
|
||||
protected function compressImage($localPath)
|
||||
{
|
||||
if (!function_exists('imagecreatefromstring') || !function_exists('imagejpeg')) {
|
||||
return null;
|
||||
}
|
||||
$binary = file_get_contents($localPath);
|
||||
if ($binary === false) {
|
||||
return null;
|
||||
}
|
||||
$img = @imagecreatefromstring($binary);
|
||||
if (!$img) {
|
||||
return null;
|
||||
}
|
||||
$width = imagesx($img);
|
||||
$height = imagesy($img);
|
||||
$maxSide = 1600;
|
||||
if ($width > $maxSide || $height > $maxSide) {
|
||||
$scale = min($maxSide / $width, $maxSide / $height);
|
||||
$newW = (int) round($width * $scale);
|
||||
$newH = (int) round($height * $scale);
|
||||
$resized = imagecreatetruecolor($newW, $newH);
|
||||
imagecopyresampled($resized, $img, 0, 0, 0, 0, $newW, $newH, $width, $height);
|
||||
imagedestroy($img);
|
||||
$img = $resized;
|
||||
}
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'hf_pic_zip_');
|
||||
$out = $tmp . '.jpg';
|
||||
@unlink($tmp);
|
||||
$ok = false;
|
||||
for ($quality = 85; $quality >= 55; $quality -= 10) {
|
||||
if (imagejpeg($img, $out, $quality) && filesize($out) <= 2 * 1024 * 1024) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
imagedestroy($img);
|
||||
if (!$ok) {
|
||||
@unlink($out);
|
||||
return null;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array [localPath, isTemp]
|
||||
*/
|
||||
protected function resolveUploadFile($imageUrl)
|
||||
{
|
||||
$localPath = $this->resolveLocalPath($imageUrl);
|
||||
if ($localPath && is_file($localPath)) {
|
||||
return [$localPath, false];
|
||||
}
|
||||
$absoluteUrl = $this->resolveAbsoluteUrl($imageUrl);
|
||||
$content = $this->fetchImageBinary($absoluteUrl);
|
||||
if ($content === '') {
|
||||
throw new \RuntimeException('无法读取图片:' . $absoluteUrl);
|
||||
}
|
||||
if (strlen($content) > 2 * 1024 * 1024) {
|
||||
throw new \RuntimeException('图片超过 2MB,请压缩后重新上传');
|
||||
}
|
||||
$ext = $this->guessImageExt($content, $absoluteUrl);
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'hf_pic_');
|
||||
if ($tmp === false) {
|
||||
throw new \RuntimeException('创建临时图片文件失败');
|
||||
}
|
||||
$tmpPath = $tmp . '.' . $ext;
|
||||
@unlink($tmp);
|
||||
if (file_put_contents($tmpPath, $content) === false) {
|
||||
throw new \RuntimeException('写入临时图片失败');
|
||||
}
|
||||
return [$tmpPath, true];
|
||||
}
|
||||
|
||||
protected function fetchImageBinary($url)
|
||||
{
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
$body = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
if ($body !== false && $body !== '') {
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
$body = @file_get_contents($url);
|
||||
return $body !== false ? $body : '';
|
||||
}
|
||||
|
||||
protected function guessImageExt($binary, $url)
|
||||
{
|
||||
if (strncmp($binary, "\x89PNG\r\n\x1a\n", 8) === 0) {
|
||||
return 'png';
|
||||
}
|
||||
if (strncmp($binary, "\xFF\xD8\xFF", 3) === 0) {
|
||||
return 'jpg';
|
||||
}
|
||||
$path = parse_url($url, PHP_URL_PATH);
|
||||
$ext = strtolower(pathinfo((string) $path, PATHINFO_EXTENSION));
|
||||
if (in_array($ext, ['jpg', 'jpeg', 'png'], true)) {
|
||||
return $ext === 'jpeg' ? 'jpg' : $ext;
|
||||
}
|
||||
return 'jpg';
|
||||
}
|
||||
|
||||
protected function resolveAbsoluteUrl($url)
|
||||
{
|
||||
$url = trim($url);
|
||||
if (strpos($url, 'http') === 0) {
|
||||
return $url;
|
||||
}
|
||||
$base = HuifuConfig::getInstance()->getSiteBaseUrl();
|
||||
return rtrim($base, '/') . '/' . ltrim($url, '/');
|
||||
}
|
||||
|
||||
protected function resolveLocalPath($url)
|
||||
{
|
||||
$path = parse_url($url, PHP_URL_PATH);
|
||||
if (!$path) {
|
||||
$path = $url;
|
||||
}
|
||||
if (strpos($path, '/uploads/') !== false) {
|
||||
$relative = substr($path, strpos($path, '/uploads/'));
|
||||
$local = ROOT_PATH . 'public' . str_replace('/', DS, $relative);
|
||||
if (is_file($local)) {
|
||||
return $local;
|
||||
}
|
||||
}
|
||||
$try = ROOT_PATH . 'public' . str_replace('/', DS, $path);
|
||||
return is_file($try) ? $try : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\library\SdkLoader;
|
||||
use addons\shopro_huifu\model\Onboard;
|
||||
|
||||
class HuifuOnboardQuery
|
||||
{
|
||||
protected $sdk;
|
||||
|
||||
public function __construct(HuifuOnboardSdk $sdk = null)
|
||||
{
|
||||
$this->sdk = $sdk ?: new HuifuOnboardSdk();
|
||||
}
|
||||
|
||||
public function pollAndUpdate(Onboard $row)
|
||||
{
|
||||
if (!$row['huifu_id']) {
|
||||
return $row;
|
||||
}
|
||||
if ($row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
||||
return $row;
|
||||
}
|
||||
|
||||
$applyNo = $row['apply_no'];
|
||||
if ($applyNo) {
|
||||
$status = $this->queryApply($row['huifu_id'], $applyNo);
|
||||
$row = $this->applyRemoteStatus($row, $status);
|
||||
if ($row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
|
||||
$basic = $this->queryBasic($row['huifu_id']);
|
||||
if ($this->isBasicActive($basic)) {
|
||||
if (in_array($row['onboard_status'], [Onboard::STATUS_BUSI_PENDING, Onboard::STATUS_BASIC_OK], true)) {
|
||||
$row->onboard_status = Onboard::STATUS_APPROVED;
|
||||
$row->reject_reason = '';
|
||||
$row->resp_snapshot = json_encode($basic, JSON_UNESCAPED_UNICODE);
|
||||
$row->save();
|
||||
HuifuOnboardReceiverSync::sync($row);
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function queryApply($huifuId, $applyNo)
|
||||
{
|
||||
SdkLoader::requireRequest('V2UserApplyQueryRequest.php');
|
||||
$meta = $this->sdk->newReqMeta();
|
||||
$request = new \BsPaySdk\request\V2UserApplyQueryRequest();
|
||||
$request->setHuifuId($huifuId);
|
||||
$request->setReqSeqId($meta['req_seq_id']);
|
||||
$request->setReqDate($meta['req_date']);
|
||||
$request->setApplyNo($applyNo);
|
||||
$result = $this->sdk->getClient()->postRequest($request);
|
||||
$safe = $this->sdk->getClient()->safeApiResult($result);
|
||||
return $safe['ok'] ? $safe['data'] : [];
|
||||
}
|
||||
|
||||
public function queryBasic($huifuId)
|
||||
{
|
||||
SdkLoader::requireRequest('V2UserBasicdataQueryRequest.php');
|
||||
$meta = $this->sdk->newReqMeta();
|
||||
$request = new \BsPaySdk\request\V2UserBasicdataQueryRequest();
|
||||
$request->setHuifuId($huifuId);
|
||||
$request->setReqSeqId($meta['req_seq_id']);
|
||||
$request->setReqDate($meta['req_date']);
|
||||
$result = $this->sdk->getClient()->postRequest($request);
|
||||
$safe = $this->sdk->getClient()->safeApiResult($result);
|
||||
return $safe['ok'] ? $safe['data'] : [];
|
||||
}
|
||||
|
||||
protected function applyRemoteStatus(Onboard $row, array $data)
|
||||
{
|
||||
if (!$data) {
|
||||
return $row;
|
||||
}
|
||||
$stat = strtolower((string) ($data['apply_status'] ?? $data['audit_status'] ?? $data['status'] ?? ''));
|
||||
$desc = (string) ($data['apply_desc'] ?? $data['resp_desc'] ?? $data['reject_reason'] ?? '');
|
||||
|
||||
if (in_array($stat, ['s', 'success', 'approved', '1', '02', '2'], true)) {
|
||||
$row->onboard_status = Onboard::STATUS_APPROVED;
|
||||
$row->reject_reason = '';
|
||||
$row->resp_snapshot = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
$row->save();
|
||||
HuifuOnboardReceiverSync::sync($row);
|
||||
return $row;
|
||||
}
|
||||
if (in_array($stat, ['f', 'fail', 'failed', 'rejected', '3', '03', '-1'], true)) {
|
||||
$row->onboard_status = Onboard::STATUS_REJECTED;
|
||||
$row->reject_reason = $desc ?: '汇付进件审核驳回';
|
||||
$row->resp_snapshot = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
$row->save();
|
||||
return $row;
|
||||
}
|
||||
|
||||
$row->onboard_status = Onboard::STATUS_BUSI_PENDING;
|
||||
$row->resp_snapshot = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
$row->save();
|
||||
return $row;
|
||||
}
|
||||
|
||||
protected function isBasicActive(array $data)
|
||||
{
|
||||
if (!$data) {
|
||||
return false;
|
||||
}
|
||||
$stat = strtolower((string) ($data['user_status'] ?? $data['status'] ?? $data['acct_stat'] ?? ''));
|
||||
return in_array($stat, ['n', 'normal', 's', 'success', '1'], true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro\model\split\Receiver;
|
||||
use addons\shopro_huifu\model\Onboard;
|
||||
use think\Db;
|
||||
|
||||
class HuifuOnboardReceiverSync
|
||||
{
|
||||
public static function sync(Onboard $row)
|
||||
{
|
||||
if ($row['onboard_status'] !== Onboard::STATUS_APPROVED || empty($row['huifu_id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$roleId = $row['user_id'];
|
||||
if ($row['role_type'] === 'regional_agent') {
|
||||
$roleId = $row['user_id'];
|
||||
}
|
||||
|
||||
$payload = json_decode($row['payload'] ?? '', true);
|
||||
$name = '用户' . $roleId;
|
||||
if (is_array($payload)) {
|
||||
if (($payload['account_type'] ?? 'indv') === 'ent' && !empty($payload['reg_name'])) {
|
||||
$name = $payload['reg_name'];
|
||||
} elseif (!empty($payload['name'])) {
|
||||
$name = $payload['name'];
|
||||
}
|
||||
}
|
||||
|
||||
$existing = Receiver::where('role_type', $row['role_type'])
|
||||
->where('role_id', $roleId)
|
||||
->where('channel', 'huifu')
|
||||
->find();
|
||||
|
||||
$now = time();
|
||||
if ($existing) {
|
||||
$existing->receiver_type = 'HUIFU_ID';
|
||||
$existing->receiver_account = $row['huifu_id'];
|
||||
$existing->receiver_name = $name;
|
||||
$existing->status = 'normal';
|
||||
$existing->updatetime = $now;
|
||||
$existing->save();
|
||||
return $existing;
|
||||
}
|
||||
|
||||
return Receiver::create([
|
||||
'role_type' => $row['role_type'],
|
||||
'role_id' => $roleId,
|
||||
'channel' => 'huifu',
|
||||
'receiver_type' => 'HUIFU_ID',
|
||||
'receiver_account' => $row['huifu_id'],
|
||||
'receiver_name' => $name,
|
||||
'relation_type' => 'USER',
|
||||
'wx_status' => 0,
|
||||
'status' => 'normal',
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function bindOnboardId($roleType, $roleRefId, $onboardId)
|
||||
{
|
||||
if ($roleType === 'promoter') {
|
||||
Db::name('shopro_commission_agent')->where('user_id', $roleRefId)->update(['huifu_onboard_id' => $onboardId]);
|
||||
return;
|
||||
}
|
||||
if ($roleType === 'regional_agent') {
|
||||
Db::name('shopro_regional_agent')->where('id', $roleRefId)->update(['huifu_onboard_id' => $onboardId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\library\HuifuClient;
|
||||
use addons\shopro_huifu\library\HuifuConfig;
|
||||
use addons\shopro_huifu\library\SdkLoader;
|
||||
|
||||
class HuifuOnboardSdk
|
||||
{
|
||||
protected $client;
|
||||
protected $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = HuifuConfig::getInstance();
|
||||
SdkLoader::boot();
|
||||
$this->reinitSkillSource();
|
||||
$this->client = new HuifuClient();
|
||||
}
|
||||
|
||||
protected function reinitSkillSource()
|
||||
{
|
||||
$skill = $this->config->get('onboard_skill_source', 'hfps/1.3.5;hfms/1.0.2');
|
||||
\BsPaySdk\core\BsPay::init([
|
||||
'sys_id' => $this->config->get('sys_id'),
|
||||
'product_id' => $this->config->get('product_id'),
|
||||
'rsa_merch_private_key' => $this->config->getPrivateKey(),
|
||||
'rsa_huifu_public_key' => $this->config->getPublicKey(),
|
||||
'skill_source' => $skill,
|
||||
], true);
|
||||
}
|
||||
|
||||
public function getClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
public function newReqMeta()
|
||||
{
|
||||
return [
|
||||
'req_date' => date('Ymd'),
|
||||
'req_seq_id' => 'HFOB' . date('YmdHis') . mt_rand(1000, 9999),
|
||||
];
|
||||
}
|
||||
|
||||
public function getUpperHuifuId()
|
||||
{
|
||||
$upper = trim((string) $this->config->get('upper_huifu_id'));
|
||||
if ($upper !== '') {
|
||||
return $upper;
|
||||
}
|
||||
return trim((string) $this->config->get('sys_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 为 SDK skill 请求头补齐 huifu_id(extendInfo 合并进 params 后供 jpt-x-skill-huifu_id 使用)
|
||||
*/
|
||||
public function mergeSkillHuifuId(array $extend = [])
|
||||
{
|
||||
if (empty($extend['huifu_id'])) {
|
||||
$extend['huifu_id'] = $this->getPictureMerchantHuifuId();
|
||||
}
|
||||
return $extend;
|
||||
}
|
||||
|
||||
/**
|
||||
* 进件图片上传用的商户号(个人用户尚未开户前,传平台/渠道商户 huifu_id)
|
||||
*/
|
||||
public function getPictureMerchantHuifuId()
|
||||
{
|
||||
$huifuId = trim((string) $this->config->get('huifu_id'));
|
||||
if ($huifuId !== '') {
|
||||
return $huifuId;
|
||||
}
|
||||
$upper = $this->getUpperHuifuId();
|
||||
if ($upper !== '') {
|
||||
return $upper;
|
||||
}
|
||||
throw new \RuntimeException('插件未配置 huifu_id 或 upper_huifu_id,无法上传进件图片');
|
||||
}
|
||||
|
||||
public function getOnboardNotifyUrl()
|
||||
{
|
||||
return rtrim($this->config->getSiteBaseUrl(), '/') . '/addons/shopro_huifu/onboard/notify';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
use addons\shopro_huifu\model\Onboard;
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
|
||||
class HuifuOnboardService
|
||||
{
|
||||
protected $sdk;
|
||||
protected $picture;
|
||||
protected $basic;
|
||||
protected $basicEnt;
|
||||
protected $busi;
|
||||
protected $query;
|
||||
|
||||
protected function sdk()
|
||||
{
|
||||
if (!$this->sdk) {
|
||||
$this->sdk = new HuifuOnboardSdk();
|
||||
$this->picture = new HuifuOnboardPicture($this->sdk);
|
||||
$this->basic = new HuifuOnboardBasicIndv($this->sdk);
|
||||
$this->basicEnt = new HuifuOnboardBasicEnt($this->sdk);
|
||||
$this->busi = new HuifuOnboardBusiOpen($this->sdk);
|
||||
$this->query = new HuifuOnboardQuery($this->sdk);
|
||||
}
|
||||
return $this->sdk;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户申请时仅保存进件资料,待后台业务审核通过后再提交汇付
|
||||
*/
|
||||
public function saveDraft($userId, $roleType, $roleRefId, array $formData)
|
||||
{
|
||||
if (!HuifuOnboardGate::isOnboardRequired()) {
|
||||
return null;
|
||||
}
|
||||
if (!in_array($roleType, ['promoter', 'regional_agent'], true)) {
|
||||
throw new \InvalidArgumentException('无效角色类型');
|
||||
}
|
||||
|
||||
$form = HuifuOnboardValidator::validateSubmit($formData);
|
||||
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
||||
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
||||
return $row;
|
||||
}
|
||||
if ($row && in_array($row['onboard_status'], [
|
||||
Onboard::STATUS_SUBMITTING,
|
||||
Onboard::STATUS_BASIC_OK,
|
||||
Onboard::STATUS_BUSI_PENDING,
|
||||
], true)) {
|
||||
throw new \RuntimeException('汇付进件处理中,请耐心等待');
|
||||
}
|
||||
|
||||
if (!$row) {
|
||||
$row = Onboard::create([
|
||||
'user_id' => $userId,
|
||||
'role_type' => $roleType,
|
||||
'role_ref_id' => $roleRefId,
|
||||
'onboard_status' => Onboard::STATUS_DRAFT,
|
||||
]);
|
||||
}
|
||||
|
||||
$row->onboard_status = Onboard::STATUS_DRAFT;
|
||||
$row->payload = json_encode($form, JSON_UNESCAPED_UNICODE);
|
||||
$row->reject_reason = '';
|
||||
$row->save();
|
||||
|
||||
HuifuOnboardReceiverSync::bindOnboardId($roleType, $roleRefId, $row['id']);
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台业务审核通过后,读取 draft 资料提交汇付
|
||||
*/
|
||||
public function submitFromDraft($userId, $roleType, $roleRefId)
|
||||
{
|
||||
if (!HuifuOnboardGate::isOnboardRequired()) {
|
||||
return null;
|
||||
}
|
||||
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
||||
if (!$row || $row['onboard_status'] !== Onboard::STATUS_DRAFT) {
|
||||
return null;
|
||||
}
|
||||
$payload = json_decode($row['payload'] ?? '', true);
|
||||
if (!$payload || !is_array($payload)) {
|
||||
throw new \RuntimeException('缺少汇付进件资料,请用户在小程序重新提交');
|
||||
}
|
||||
return $this->submit($userId, $roleType, $roleRefId, $payload);
|
||||
}
|
||||
|
||||
public function submit($userId, $roleType, $roleRefId, array $formData)
|
||||
{
|
||||
if (!HuifuOnboardGate::isOnboardRequired()) {
|
||||
return null;
|
||||
}
|
||||
if (!in_array($roleType, ['promoter', 'regional_agent'], true)) {
|
||||
throw new \InvalidArgumentException('无效角色类型');
|
||||
}
|
||||
|
||||
$form = HuifuOnboardValidator::validateSubmit($formData);
|
||||
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
||||
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
||||
return $row;
|
||||
}
|
||||
if ($row && in_array($row['onboard_status'], [Onboard::STATUS_SUBMITTING, Onboard::STATUS_BUSI_PENDING], true)) {
|
||||
throw new \RuntimeException('进件审核中,请耐心等待');
|
||||
}
|
||||
|
||||
if (!$row) {
|
||||
$row = Onboard::create([
|
||||
'user_id' => $userId,
|
||||
'role_type' => $roleType,
|
||||
'role_ref_id' => $roleRefId,
|
||||
'onboard_status' => Onboard::STATUS_DRAFT,
|
||||
]);
|
||||
}
|
||||
|
||||
$row->onboard_status = Onboard::STATUS_SUBMITTING;
|
||||
$row->payload = json_encode($form, JSON_UNESCAPED_UNICODE);
|
||||
$row->reject_reason = '';
|
||||
$row->save();
|
||||
|
||||
try {
|
||||
$this->sdk();
|
||||
$fileMap = $this->uploadFormPictures($form);
|
||||
$row->file_map = json_encode($fileMap, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$accountType = $form['account_type'] ?? 'indv';
|
||||
$basicResult = $accountType === 'ent'
|
||||
? $this->basicEnt->submit($form, $fileMap)
|
||||
: $this->basic->submit($form, $fileMap);
|
||||
$row->huifu_id = $basicResult['huifu_id'];
|
||||
$row->basic_req_seq_id = $basicResult['req_seq_id'];
|
||||
$row->req_date = $basicResult['req_date'];
|
||||
$row->req_seq_id = $basicResult['req_seq_id'];
|
||||
$row->onboard_status = Onboard::STATUS_BASIC_OK;
|
||||
$row->save();
|
||||
|
||||
$busiResult = $this->busi->submit($row['huifu_id'], $form, $fileMap);
|
||||
$row->busi_req_seq_id = $busiResult['req_seq_id'];
|
||||
$row->req_seq_id = $busiResult['req_seq_id'];
|
||||
$row->apply_no = $busiResult['apply_no'] ?? '';
|
||||
$row->onboard_status = Onboard::STATUS_BUSI_PENDING;
|
||||
$row->resp_snapshot = json_encode($busiResult['resp'], JSON_UNESCAPED_UNICODE);
|
||||
$row->save();
|
||||
|
||||
HuifuOnboardReceiverSync::bindOnboardId($roleType, $roleRefId, $row['id']);
|
||||
$row = $this->query->pollAndUpdate($row);
|
||||
|
||||
return $row;
|
||||
} catch (\Throwable $e) {
|
||||
$friendly = HuifuOnboardError::translate($e->getMessage());
|
||||
Log::error('[shopro_huifu] onboard submit fail uid=' . $userId . ' ' . $e->getMessage());
|
||||
$row->onboard_status = Onboard::STATUS_FAILED;
|
||||
$row->reject_reason = $friendly;
|
||||
$row->save();
|
||||
throw new \RuntimeException($friendly, 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function resubmit($userId, $roleType, $roleRefId, array $formData)
|
||||
{
|
||||
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
||||
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
||||
return $row;
|
||||
}
|
||||
if ($row && in_array($row['onboard_status'], [Onboard::STATUS_REJECTED, Onboard::STATUS_FAILED], true)) {
|
||||
$row->onboard_status = Onboard::STATUS_DRAFT;
|
||||
$row->save();
|
||||
}
|
||||
return $this->submit($userId, $roleType, $roleRefId, $formData);
|
||||
}
|
||||
|
||||
public function pollPending($limit = 50)
|
||||
{
|
||||
$list = Onboard::where('onboard_status', 'in', [
|
||||
Onboard::STATUS_BUSI_PENDING,
|
||||
Onboard::STATUS_BASIC_OK,
|
||||
])->limit($limit)->select();
|
||||
if (!$list || count($list) === 0) {
|
||||
return 0;
|
||||
}
|
||||
$this->sdk();
|
||||
$count = 0;
|
||||
foreach ($list as $row) {
|
||||
try {
|
||||
$this->query->pollAndUpdate($row);
|
||||
$count++;
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('[shopro_huifu] onboard poll fail id=' . $row['id'] . ' ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
protected function uploadFormPictures(array $form)
|
||||
{
|
||||
$fileMap = [];
|
||||
$accountType = $form['account_type'] ?? 'indv';
|
||||
if ($accountType === 'ent') {
|
||||
if (!empty($form['license_url'])) {
|
||||
$fileMap['F01'] = $this->picture->uploadByUrl('F01', $form['license_url']);
|
||||
}
|
||||
if (!empty($form['legal_id_front_url'])) {
|
||||
$fileMap['F02'] = $this->picture->uploadByUrl('F02', $form['legal_id_front_url']);
|
||||
}
|
||||
if (!empty($form['legal_id_back_url'])) {
|
||||
$fileMap['F03'] = $this->picture->uploadByUrl('F03', $form['legal_id_back_url']);
|
||||
}
|
||||
} else {
|
||||
$fileMap['F02'] = $this->picture->uploadByUrl('F02', $form['id_card_front_url']);
|
||||
$fileMap['F03'] = $this->picture->uploadByUrl('F03', $form['id_card_back_url']);
|
||||
}
|
||||
if (!empty($form['bank_card_url'])) {
|
||||
$fileMap['F13'] = $this->picture->uploadByUrl('F13', $form['bank_card_url']);
|
||||
}
|
||||
if (($form['bank_account_type'] ?? '0') === '1' && !empty($form['account_permit_url'])) {
|
||||
$fileMap['F08'] = $this->picture->uploadByUrl('F08', $form['account_permit_url']);
|
||||
}
|
||||
return $fileMap;
|
||||
}
|
||||
|
||||
public function handleNotify(array $data)
|
||||
{
|
||||
$huifuId = trim((string) ($data['huifu_id'] ?? ''));
|
||||
$applyNo = trim((string) ($data['apply_no'] ?? ''));
|
||||
if ($huifuId === '') {
|
||||
return false;
|
||||
}
|
||||
$query = Onboard::where('huifu_id', $huifuId);
|
||||
if ($applyNo !== '') {
|
||||
$query->where('apply_no', $applyNo);
|
||||
}
|
||||
$row = $query->order('id desc')->find();
|
||||
if (!$row) {
|
||||
return false;
|
||||
}
|
||||
$this->sdk();
|
||||
$this->query->pollAndUpdate($row);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_huifu\library\onboard;
|
||||
|
||||
class HuifuOnboardValidator
|
||||
{
|
||||
protected static $fieldLabels = [
|
||||
'account_type' => '结算账户类型',
|
||||
'name' => '真实姓名',
|
||||
'cert_no' => '身份证号',
|
||||
'reg_name' => '商户名称',
|
||||
'license_code' => '统一社会信用代码',
|
||||
'legal_name' => '法人姓名',
|
||||
'legal_mobile' => '法人手机号',
|
||||
'legal_cert_no' => '法人身份证号',
|
||||
'reg_detail' => '详细地址',
|
||||
'mobile_no' => '手机号',
|
||||
'card_no' => '银行卡号',
|
||||
'branch_name' => '开户银行',
|
||||
'branch_code' => '支行联行号',
|
||||
'prov_id' => '所在区域',
|
||||
'id_card_front_url' => '身份证人像面',
|
||||
'id_card_back_url' => '身份证国徽面',
|
||||
'license_url' => '营业执照照片',
|
||||
'legal_id_front_url' => '法人身份证人像面',
|
||||
'legal_id_back_url' => '法人身份证国徽面',
|
||||
];
|
||||
|
||||
public static function validateSubmit(array $data)
|
||||
{
|
||||
$type = self::normalizeAccountType($data['account_type'] ?? 'indv');
|
||||
$data['account_type'] = $type;
|
||||
HuifuOnboardAreaHelper::applyRegionCodes($data);
|
||||
self::resolveBranch($data);
|
||||
|
||||
if ($type === 'ent') {
|
||||
self::validateEnterprise($data);
|
||||
} else {
|
||||
self::validateIndividual($data);
|
||||
}
|
||||
self::validateBank($data);
|
||||
|
||||
return self::normalize($data);
|
||||
}
|
||||
|
||||
protected static function validateIndividual(array $data)
|
||||
{
|
||||
foreach (['name', 'cert_no', 'mobile_no', 'reg_detail', 'id_card_front_url', 'id_card_back_url'] as $field) {
|
||||
if (empty($data[$field])) {
|
||||
throw new \InvalidArgumentException('请填写' . self::fieldLabel($field));
|
||||
}
|
||||
}
|
||||
if (!preg_match('/^\d{17}[\dXx]$/', $data['cert_no'])) {
|
||||
throw new \InvalidArgumentException('身份证号格式不正确');
|
||||
}
|
||||
if (!preg_match('/^1\d{10}$/', $data['mobile_no'])) {
|
||||
throw new \InvalidArgumentException('手机号格式不正确');
|
||||
}
|
||||
self::assertCertDates($data, 'cert');
|
||||
}
|
||||
|
||||
protected static function validateEnterprise(array $data)
|
||||
{
|
||||
foreach ([
|
||||
'reg_name', 'license_code', 'reg_detail', 'legal_name', 'legal_mobile', 'legal_cert_no',
|
||||
'license_url', 'legal_id_front_url', 'legal_id_back_url',
|
||||
] as $field) {
|
||||
if (empty($data[$field])) {
|
||||
throw new \InvalidArgumentException('请填写' . self::fieldLabel($field));
|
||||
}
|
||||
}
|
||||
if (!preg_match('/^[0-9A-Z]{15}$|^[0-9A-Z]{18}$/', strtoupper($data['license_code']))) {
|
||||
throw new \InvalidArgumentException('统一社会信用代码格式不正确');
|
||||
}
|
||||
if (!preg_match('/^\d{17}[\dXx]$/', $data['legal_cert_no'])) {
|
||||
throw new \InvalidArgumentException('法人身份证号格式不正确');
|
||||
}
|
||||
if (!preg_match('/^1\d{10}$/', $data['legal_mobile'])) {
|
||||
throw new \InvalidArgumentException('法人手机号格式不正确');
|
||||
}
|
||||
self::assertCertDates($data, 'license');
|
||||
self::assertCertDates($data, 'legal_cert');
|
||||
}
|
||||
|
||||
protected static function validateBank(array $data)
|
||||
{
|
||||
if (empty($data['card_no'])) {
|
||||
throw new \InvalidArgumentException('请填写银行卡号');
|
||||
}
|
||||
if (!preg_match('/^\d{12,19}$/', preg_replace('/\s+/', '', $data['card_no']))) {
|
||||
throw new \InvalidArgumentException('银行卡号格式不正确');
|
||||
}
|
||||
if (empty($data['branch_code']) && empty($data['branch_name'])) {
|
||||
throw new \InvalidArgumentException('请选择开户银行支行');
|
||||
}
|
||||
if (empty($data['prov_id']) || empty($data['area_id'])) {
|
||||
throw new \InvalidArgumentException('请选择所在区域');
|
||||
}
|
||||
$bankAccountType = (string) ($data['bank_account_type'] ?? '0');
|
||||
if ($bankAccountType === '0') {
|
||||
$mp = trim((string) ($data['mp'] ?? $data['mobile_no'] ?? $data['legal_mobile'] ?? ''));
|
||||
if ($mp === '' || !preg_match('/^1\d{10}$/', $mp)) {
|
||||
throw new \InvalidArgumentException('请填写银行预留手机号');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function assertCertDates(array $data, $prefix)
|
||||
{
|
||||
$typeKey = $prefix . '_validity_type';
|
||||
$beginKey = $prefix . '_begin_date';
|
||||
$endKey = $prefix . '_end_date';
|
||||
$type = (string) ($data[$typeKey] ?? '');
|
||||
if (!in_array($type, ['0', '1'], true)) {
|
||||
throw new \InvalidArgumentException('证件有效期类型无效');
|
||||
}
|
||||
if (empty($data[$beginKey])) {
|
||||
throw new \InvalidArgumentException('请选择证件开始日期');
|
||||
}
|
||||
if ($type === '0' && empty($data[$endKey])) {
|
||||
throw new \InvalidArgumentException('请选择证件截止日期');
|
||||
}
|
||||
if ($type === '0' && !empty($data[$endKey])) {
|
||||
self::assertFutureDate($data[$endKey], '证件有效期');
|
||||
}
|
||||
}
|
||||
|
||||
protected static function assertFutureDate($ymd, $label)
|
||||
{
|
||||
$ymd = trim((string) $ymd);
|
||||
if (strlen($ymd) !== 8) {
|
||||
return;
|
||||
}
|
||||
$endTs = strtotime(substr($ymd, 0, 4) . '-' . substr($ymd, 4, 2) . '-' . substr($ymd, 6, 2));
|
||||
$todayTs = strtotime(date('Y-m-d'));
|
||||
if ($endTs !== false && $endTs <= $todayTs) {
|
||||
throw new \InvalidArgumentException($label . '须晚于今天,长期有效请选择 2099-12-31');
|
||||
}
|
||||
}
|
||||
|
||||
protected static function resolveBranch(array &$data)
|
||||
{
|
||||
if (!empty($data['branch_code'])) {
|
||||
return;
|
||||
}
|
||||
$name = trim((string) ($data['branch_name'] ?? ''));
|
||||
if ($name === '') {
|
||||
return;
|
||||
}
|
||||
$city = $data['area_id'] ?? '';
|
||||
$matches = HuifuOnboardBankBranch::search($name, $city, 1);
|
||||
if ($matches) {
|
||||
$data['branch_code'] = $matches[0]['branch_code'];
|
||||
$data['branch_name'] = $matches[0]['branch_name'];
|
||||
if (empty($data['prov_id']) && !empty($matches[0]['prov_id'])) {
|
||||
$data['prov_id'] = $matches[0]['prov_id'];
|
||||
}
|
||||
if (empty($data['area_id']) && !empty($matches[0]['area_id'])) {
|
||||
$data['area_id'] = $matches[0]['area_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function normalize(array $data)
|
||||
{
|
||||
$data['account_type'] = self::normalizeAccountType($data['account_type'] ?? 'indv');
|
||||
HuifuOnboardAreaHelper::applyRegionCodes($data);
|
||||
self::resolveBranch($data);
|
||||
|
||||
$data['bank_account_type'] = (string) ($data['bank_account_type'] ?? '0');
|
||||
if ($data['account_type'] === 'indv') {
|
||||
$data['bank_account_type'] = '0';
|
||||
}
|
||||
|
||||
$data['name'] = trim($data['name'] ?? '');
|
||||
$data['cert_no'] = strtoupper(trim($data['cert_no'] ?? ''));
|
||||
$data['mobile_no'] = trim($data['mobile_no'] ?? '');
|
||||
$data['reg_name'] = trim($data['reg_name'] ?? '');
|
||||
$data['license_code'] = strtoupper(trim($data['license_code'] ?? ''));
|
||||
$data['legal_name'] = trim($data['legal_name'] ?? '');
|
||||
$data['legal_mobile'] = trim($data['legal_mobile'] ?? '');
|
||||
$data['legal_cert_no'] = strtoupper(trim($data['legal_cert_no'] ?? ''));
|
||||
$data['reg_detail'] = trim($data['reg_detail'] ?? '');
|
||||
$data['card_no'] = preg_replace('/\s+/', '', $data['card_no'] ?? '');
|
||||
$data['branch_code'] = trim($data['branch_code'] ?? '');
|
||||
$data['branch_name'] = trim($data['branch_name'] ?? '');
|
||||
$data['prov_id'] = HuifuOnboardAreaHelper::toCode($data['prov_id'] ?? '');
|
||||
$data['area_id'] = HuifuOnboardAreaHelper::toCode($data['area_id'] ?? '');
|
||||
$data['district_id'] = HuifuOnboardAreaHelper::toCode($data['district_id'] ?? '');
|
||||
|
||||
foreach (['cert', 'license', 'legal_cert'] as $prefix) {
|
||||
$beginKey = $prefix . '_begin_date';
|
||||
$endKey = $prefix . '_end_date';
|
||||
$typeKey = $prefix . '_validity_type';
|
||||
if (!empty($data[$beginKey])) {
|
||||
$data[$beginKey] = self::toYmd($data[$beginKey]);
|
||||
}
|
||||
if (isset($data[$typeKey])) {
|
||||
$data[$typeKey] = (string) $data[$typeKey];
|
||||
}
|
||||
if (($data[$typeKey] ?? '') === '1') {
|
||||
unset($data[$endKey]);
|
||||
} elseif (!empty($data[$endKey])) {
|
||||
$data[$endKey] = self::toYmd($data[$endKey]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($data['account_type'] === 'indv') {
|
||||
$data['card_name'] = trim($data['card_name'] ?: $data['name']);
|
||||
$data['mp'] = trim($data['mp'] ?? $data['mobile_no']);
|
||||
} else {
|
||||
if ($data['bank_account_type'] === '1') {
|
||||
$data['card_name'] = trim($data['card_name'] ?: $data['reg_name']);
|
||||
} else {
|
||||
$data['card_name'] = trim($data['card_name'] ?: $data['legal_name']);
|
||||
$data['cert_no'] = $data['legal_cert_no'];
|
||||
$data['cert_validity_type'] = $data['legal_cert_validity_type'];
|
||||
$data['cert_begin_date'] = $data['legal_cert_begin_date'];
|
||||
$data['cert_end_date'] = $data['legal_cert_end_date'] ?? '';
|
||||
}
|
||||
$data['mp'] = trim($data['mp'] ?? $data['legal_mobile']);
|
||||
$data['mobile_no'] = $data['legal_mobile'];
|
||||
$data['name'] = $data['legal_name'];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function maskPayload(array $data)
|
||||
{
|
||||
$copy = $data;
|
||||
foreach (['cert_no', 'legal_cert_no'] as $key) {
|
||||
if (!empty($copy[$key])) {
|
||||
$copy[$key] = substr($copy[$key], 0, 6) . '********' . substr($copy[$key], -4);
|
||||
}
|
||||
}
|
||||
if (!empty($copy['card_no'])) {
|
||||
$len = strlen($copy['card_no']);
|
||||
$copy['card_no'] = str_repeat('*', max(0, $len - 4)) . substr($copy['card_no'], -4);
|
||||
}
|
||||
return $copy;
|
||||
}
|
||||
|
||||
protected static function normalizeAccountType($type)
|
||||
{
|
||||
$type = strtolower(trim((string) $type));
|
||||
return in_array($type, ['indv', 'ent'], true) ? $type : 'indv';
|
||||
}
|
||||
|
||||
protected static function toYmd($value)
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
$value = str_replace(['-', '/', '.'], '', $value);
|
||||
if (strlen($value) === 8 && ctype_digit($value)) {
|
||||
return $value;
|
||||
}
|
||||
throw new \InvalidArgumentException('日期格式应为 yyyyMMdd 或 yyyy-MM-dd');
|
||||
}
|
||||
|
||||
protected static function fieldLabel($field)
|
||||
{
|
||||
return self::$fieldLabels[$field] ?? $field;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户