转账功能优化

This commit is contained in:
Dev
2026-10-10 15:23:55 +08:00
parent 352b633a5c
commit b969881638
@@ -111,9 +111,9 @@ class TransferAuthorizationService
}
//未生效:发起授权申请(未确认的单号可复用重新发起)
//CLOSED(用户或商户已关闭) 时复用原单号存在发起失败风险,改用全新单号并新建记录
$forceCreate = ($queryState === self::STATE_CLOSED);
if ($forceCreate) {
//CLOSED(用户或商户已关闭) 时复用原单号存在发起失败风险,换新单号并覆盖当前记录
$renew = ($queryState === self::STATE_CLOSED);
if ($renew) {
$authNo = self::generateAuthNoUnique($userId);
}
$displayName = mb_substr($displayName ?: ($user->nickname ?: ('用户' . $userId)), 0, 32);
@@ -131,7 +131,7 @@ class TransferAuthorizationService
$res['state'] ?? self::STATE_WAIT_USER_CONFIRM,
'',
$res['package_info'],
$forceCreate
$renew
);
return [
@@ -151,14 +151,19 @@ class TransferAuthorizationService
* @param string $state 授权状态
* @param string $authorizationId 微信侧授权单号(可为空)
* @param string $packageInfo 拉起授权页的 package(可为空)
* @param bool $forceCreate 是否强制新建记录(授权单已关闭换新单号时使用)
* @param bool $renew 是否解除后重新授权(覆盖 out_authorization_no 并清空旧 authorization_id)
* @return void
*/
private static function saveAuthorization($userId, string $authNo, string $state, string $authorizationId = '', string $packageInfo = '', bool $forceCreate = false)
private static function saveAuthorization($userId, string $authNo, string $state, string $authorizationId = '', string $packageInfo = '', bool $renew = false)
{
$record = $forceCreate ? null : self::getByUserId($userId);
//表上存在 uniq_user_id 唯一索引,一个用户只允许一条授权记录,只能更新不能新增
$record = self::getByUserId($userId);
$data = ['state' => $state];
if ($authorizationId !== '') {
if ($renew) {
//授权已被关闭/解除:换新单号重新发起,旧的 authorization_id 已失效需清空
$data['out_authorization_no'] = $authNo;
$data['authorization_id'] = '';
} elseif ($authorizationId !== '') {
$data['authorization_id'] = $authorizationId;
}
if ($packageInfo !== '') {
@@ -171,12 +176,21 @@ class TransferAuthorizationService
return;
}
self::create(array_merge([
'user_id' => $userId,
'out_authorization_no' => $authNo,
'authorization_id' => $authorizationId,
'package_info' => $packageInfo,
], $data));
try {
self::create(array_merge([
'user_id' => $userId,
'out_authorization_no' => $authNo,
'authorization_id' => $authorizationId,
'package_info' => $packageInfo,
], $data));
} catch (\Exception $e) {
//并发下可能撞 uniq_user_id / uniq_out_auth_no,回退为更新已有记录
$exists = self::getByUserId($userId);
if (!$exists) {
throw $e;
}
$exists->save(array_merge($data, ['out_authorization_no' => $authNo]));
}
}
/**