From b969881638f62d1fc2356c631796ea2ffe714dd9 Mon Sep 17 00:00:00 2001 From: Dev Date: Sat, 10 Oct 2026 15:23:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AC=E8=B4=A6=E5=8A=9F=E8=83=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/TransferAuthorizationService.php | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/backend/application/common/service/TransferAuthorizationService.php b/backend/application/common/service/TransferAuthorizationService.php index 3d59484..99b5825 100644 --- a/backend/application/common/service/TransferAuthorizationService.php +++ b/backend/application/common/service/TransferAuthorizationService.php @@ -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])); + } } /**