fix(gpc): preserve auto mode after stop refresh

This commit is contained in:
朴圣佑
2026-05-11 12:23:07 +08:00
parent ace716838e
commit 7552b40c70
2 changed files with 161 additions and 17 deletions
+60 -17
View File
@@ -2167,22 +2167,55 @@ function getGpcHelperAutoModeEnabled(state = latestState) {
return Boolean(state?.gopayHelperAutoModeEnabled);
}
function hasGpcAutoModePermissionField(payload = {}) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
function normalizeGpcAutoModePermissionValue(value) {
if (typeof value === 'boolean') {
return value;
}
if (typeof value === 'number') {
if (value === 1) return true;
if (value === 0) return false;
}
const normalized = String(value ?? '').trim().toLowerCase();
if (!normalized) {
return null;
}
if (['true', '1', 'yes', 'y', 'on', 'enabled', 'enable'].includes(normalized)) {
return true;
}
if (['false', '0', 'no', 'n', 'off', 'disabled', 'disable'].includes(normalized)) {
return false;
}
return payload.auto_mode_enabled !== undefined
|| payload.autoModeEnabled !== undefined
|| payload.auto_enabled !== undefined
|| payload.autoEnabled !== undefined
|| (payload.data && typeof payload.data === 'object' && !Array.isArray(payload.data) && hasGpcAutoModePermissionField(payload.data));
return null;
}
function getGpcAutoModePermissionFromPayload(payload = {}) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
return null;
}
for (const key of ['auto_mode_enabled', 'autoModeEnabled', 'auto_enabled', 'autoEnabled']) {
if (payload[key] !== undefined) {
return normalizeGpcAutoModePermissionValue(payload[key]);
}
}
if (payload.data && typeof payload.data === 'object' && !Array.isArray(payload.data)) {
return getGpcAutoModePermissionFromPayload(payload.data);
}
return null;
}
function shouldPreserveSelectedGpcAutoMode(state = latestState) {
const payloadPermission = getGpcAutoModePermissionFromPayload(state?.gopayHelperBalancePayload);
return normalizeGpcHelperPhoneModeValue(state?.gopayHelperPhoneMode) === GPC_HELPER_PHONE_MODE_AUTO
&& (Boolean(state?.gopayHelperAutoModeEnabled) || payloadPermission === true);
}
function hasGpcAutoModePermissionField(payload = {}) {
return getGpcAutoModePermissionFromPayload(payload) !== null;
}
function isGpcAutoModePermissionDenied(state = latestState) {
if (getGpcHelperAutoModeEnabled(state)) {
return false;
}
return hasGpcAutoModePermissionField(state?.gopayHelperBalancePayload);
const payloadPermission = getGpcAutoModePermissionFromPayload(state?.gopayHelperBalancePayload);
return payloadPermission === false;
}
function normalizeGpcRemainingUsesValue(value) {
@@ -3389,7 +3422,10 @@ function collectSettingsPayload() {
? selectGpcHelperPhoneMode.value
: (latestState?.gopayHelperPhoneMode || 'manual')
);
const effectiveGpcPhoneMode = (typeof isGpcAutoModePermissionDenied === 'function' && isGpcAutoModePermissionDenied(latestState))
const preserveSelectedGpcAutoMode = typeof shouldPreserveSelectedGpcAutoMode === 'function'
? shouldPreserveSelectedGpcAutoMode(latestState)
: false;
const effectiveGpcPhoneMode = (!preserveSelectedGpcAutoMode && typeof isGpcAutoModePermissionDenied === 'function' && isGpcAutoModePermissionDenied(latestState))
? 'manual'
: selectedGpcPhoneMode;
const selectedGpcOtpChannel = normalizeGpcOtpChannelSafe(
@@ -7370,7 +7406,11 @@ function updatePlusModeUI() {
);
const gpcAutoModeDenied = isGpcAutoModePermissionDenied(latestState);
const gpcAutoModeEnabled = getGpcHelperAutoModeEnabled(latestState);
const isGpcAutoMode = !gpcAutoModeDenied && gpcPhoneMode === GPC_HELPER_PHONE_MODE_AUTO;
const preserveSelectedGpcAutoMode = typeof shouldPreserveSelectedGpcAutoMode === 'function'
? shouldPreserveSelectedGpcAutoMode(latestState)
: false;
const effectiveGpcAutoModeDenied = gpcAutoModeDenied && !preserveSelectedGpcAutoMode;
const isGpcAutoMode = !effectiveGpcAutoModeDenied && gpcPhoneMode === GPC_HELPER_PHONE_MODE_AUTO;
const gpcOtpChannel = normalizeGpcOtpChannelValue(
typeof selectGpcHelperOtpChannel !== 'undefined' && selectGpcHelperOtpChannel
? selectGpcHelperOtpChannel.value
@@ -7385,7 +7425,7 @@ function updatePlusModeUI() {
? normalizePlusPaymentMethod(selectPlusPaymentMethod.value)
: method;
const gpcRowsVisible = enabled && selectedMethod === gpcValue;
const canShowGpcModeSelector = gpcRowsVisible && (gpcAutoModeEnabled || !gpcAutoModeDenied);
const canShowGpcModeSelector = gpcRowsVisible && (gpcAutoModeEnabled || !effectiveGpcAutoModeDenied);
const localSmsControlsVisible = gpcRowsVisible && !isGpcAutoMode;
const effectiveLocalSmsEnabled = !isGpcAutoMode && localSmsEnabled;
if (typeof selectPlusPaymentMethod !== 'undefined' && selectPlusPaymentMethod) {
@@ -7430,7 +7470,7 @@ function updatePlusModeUI() {
rowGpcHelperPhoneMode.style.display = canShowGpcModeSelector ? '' : 'none';
}
if (typeof selectGpcHelperPhoneMode !== 'undefined' && selectGpcHelperPhoneMode) {
selectGpcHelperPhoneMode.value = gpcAutoModeDenied ? GPC_HELPER_PHONE_MODE_MANUAL : gpcPhoneMode;
selectGpcHelperPhoneMode.value = effectiveGpcAutoModeDenied ? GPC_HELPER_PHONE_MODE_MANUAL : gpcPhoneMode;
}
[
typeof rowGpcHelperCountryCode !== 'undefined' ? rowGpcHelperCountryCode : null,
@@ -11765,8 +11805,9 @@ btnGpcHelperBalance?.addEventListener('click', async () => {
gopayHelperAutoModeEnabled: getGpcAutoModeEnabledFromResponse(response),
gopayHelperApiKeyStatus: response?.apiKeyStatus || response?.data?.status || response?.payload?.data?.status || response?.payload?.status || '',
};
const selectedModeBeforeBalanceState = getSelectedGpcHelperPhoneMode();
syncLatestState(nextState);
if (!nextState.gopayHelperAutoModeEnabled && getSelectedGpcHelperPhoneMode() === GPC_HELPER_PHONE_MODE_AUTO) {
if (!nextState.gopayHelperAutoModeEnabled && selectedModeBeforeBalanceState === GPC_HELPER_PHONE_MODE_AUTO) {
selectGpcHelperPhoneMode.value = GPC_HELPER_PHONE_MODE_MANUAL;
syncLatestState({ gopayHelperPhoneMode: GPC_HELPER_PHONE_MODE_MANUAL });
await saveSettings({ silent: true, force: true }).catch(() => {});
@@ -13728,7 +13769,9 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.payload.gopayHelperPhoneMode !== undefined && selectGpcHelperPhoneMode) {
selectGpcHelperPhoneMode.value = normalizeGpcHelperPhoneModeValue(message.payload.gopayHelperPhoneMode);
}
if (message.payload.gopayHelperAutoModeEnabled === false && selectGpcHelperPhoneMode?.value === GPC_HELPER_PHONE_MODE_AUTO) {
if (message.payload.gopayHelperAutoModeEnabled === false
&& selectGpcHelperPhoneMode?.value === GPC_HELPER_PHONE_MODE_AUTO
&& isGpcAutoModePermissionDenied(latestState)) {
selectGpcHelperPhoneMode.value = GPC_HELPER_PHONE_MODE_MANUAL;
syncLatestState({ gopayHelperPhoneMode: GPC_HELPER_PHONE_MODE_MANUAL });
showToast('当前 API Key 未开通自动模式,已切回手动模式。', 'warn', 2200);