Paypal增加轻量级号池
This commit is contained in:
@@ -35,8 +35,10 @@
|
||||
finalizeStep3Completion,
|
||||
finalizeIcloudAliasAfterSuccessfulFlow,
|
||||
findHotmailAccount,
|
||||
findPayPalAccount,
|
||||
flushCommand,
|
||||
getCurrentLuckmailPurchase,
|
||||
getCurrentPayPalAccount,
|
||||
getCurrentMail2925Account,
|
||||
getPendingAutoRunTimerPlan,
|
||||
getSourceLabel,
|
||||
@@ -62,6 +64,7 @@
|
||||
refreshIpProxyPool,
|
||||
normalizeHotmailAccounts,
|
||||
normalizeMail2925Accounts,
|
||||
normalizePayPalAccounts,
|
||||
normalizeRunCount,
|
||||
AUTO_RUN_TIMER_KIND_SCHEDULED_START,
|
||||
notifyStepComplete,
|
||||
@@ -79,6 +82,7 @@
|
||||
selectLuckmailPurchase,
|
||||
switchIpProxy,
|
||||
changeIpProxyExit,
|
||||
setCurrentPayPalAccount,
|
||||
setCurrentMail2925Account,
|
||||
setCurrentHotmailAccount,
|
||||
setContributionMode,
|
||||
@@ -99,7 +103,9 @@
|
||||
deleteMail2925Account,
|
||||
deleteMail2925Accounts,
|
||||
syncHotmailAccounts,
|
||||
syncPayPalAccounts,
|
||||
testHotmailAccountMailAccess,
|
||||
upsertPayPalAccount,
|
||||
upsertMail2925Account,
|
||||
upsertHotmailAccount,
|
||||
verifyHotmailAccount,
|
||||
@@ -779,6 +785,16 @@
|
||||
return { ok: true, account };
|
||||
}
|
||||
|
||||
case 'UPSERT_PAYPAL_ACCOUNT': {
|
||||
const account = await upsertPayPalAccount(message.payload || {});
|
||||
return { ok: true, account };
|
||||
}
|
||||
|
||||
case 'SELECT_PAYPAL_ACCOUNT': {
|
||||
const account = await setCurrentPayPalAccount(String(message.payload?.accountId || ''));
|
||||
return { ok: true, account };
|
||||
}
|
||||
|
||||
case 'DELETE_HOTMAIL_ACCOUNT': {
|
||||
await deleteHotmailAccount(String(message.payload?.accountId || ''));
|
||||
return { ok: true };
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
(function attachBackgroundPayPalAccountStore(root, factory) {
|
||||
root.MultiPageBackgroundPayPalAccountStore = factory();
|
||||
})(typeof self !== 'undefined' ? self : globalThis, function createBackgroundPayPalAccountStoreModule() {
|
||||
function createPayPalAccountStore(deps = {}) {
|
||||
const {
|
||||
broadcastDataUpdate,
|
||||
findPayPalAccount,
|
||||
getState,
|
||||
normalizePayPalAccount,
|
||||
normalizePayPalAccounts,
|
||||
setPersistentSettings,
|
||||
setState,
|
||||
upsertPayPalAccountInList,
|
||||
} = deps;
|
||||
|
||||
async function syncSelectedPayPalAccountState(account = null) {
|
||||
const updates = account
|
||||
? {
|
||||
currentPayPalAccountId: account.id,
|
||||
paypalEmail: String(account.email || '').trim(),
|
||||
paypalPassword: String(account.password || ''),
|
||||
}
|
||||
: {
|
||||
currentPayPalAccountId: '',
|
||||
paypalEmail: '',
|
||||
paypalPassword: '',
|
||||
};
|
||||
|
||||
await setPersistentSettings(updates);
|
||||
await setState({
|
||||
currentPayPalAccountId: updates.currentPayPalAccountId || null,
|
||||
paypalEmail: updates.paypalEmail,
|
||||
paypalPassword: updates.paypalPassword,
|
||||
});
|
||||
broadcastDataUpdate({
|
||||
currentPayPalAccountId: updates.currentPayPalAccountId || null,
|
||||
paypalEmail: updates.paypalEmail,
|
||||
paypalPassword: updates.paypalPassword,
|
||||
});
|
||||
}
|
||||
|
||||
async function syncPayPalAccounts(accounts) {
|
||||
const normalized = normalizePayPalAccounts(accounts);
|
||||
await setPersistentSettings({ paypalAccounts: normalized });
|
||||
await setState({ paypalAccounts: normalized });
|
||||
broadcastDataUpdate({ paypalAccounts: normalized });
|
||||
|
||||
const state = await getState();
|
||||
if (state.currentPayPalAccountId && !findPayPalAccount(normalized, state.currentPayPalAccountId)) {
|
||||
await syncSelectedPayPalAccountState(null);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function getCurrentPayPalAccount(state = {}) {
|
||||
return findPayPalAccount(state.paypalAccounts, state.currentPayPalAccountId) || null;
|
||||
}
|
||||
|
||||
async function upsertPayPalAccount(input = {}) {
|
||||
const state = await getState();
|
||||
const accounts = normalizePayPalAccounts(state.paypalAccounts);
|
||||
const normalizedEmail = String(input?.email || '').trim().toLowerCase();
|
||||
const existing = input?.id
|
||||
? findPayPalAccount(accounts, input.id)
|
||||
: accounts.find((account) => account.email === normalizedEmail) || null;
|
||||
const normalized = normalizePayPalAccount({
|
||||
...(existing || {}),
|
||||
...input,
|
||||
id: input?.id || existing?.id || crypto.randomUUID(),
|
||||
createdAt: existing?.createdAt || Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
|
||||
const nextAccounts = typeof upsertPayPalAccountInList === 'function'
|
||||
? upsertPayPalAccountInList(accounts, normalized)
|
||||
: accounts.concat(normalized);
|
||||
|
||||
await syncPayPalAccounts(nextAccounts);
|
||||
|
||||
if (state.currentPayPalAccountId === normalized.id) {
|
||||
await syncSelectedPayPalAccountState(normalized);
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
async function setCurrentPayPalAccount(accountId) {
|
||||
const state = await getState();
|
||||
const accounts = normalizePayPalAccounts(state.paypalAccounts);
|
||||
const account = findPayPalAccount(accounts, accountId);
|
||||
if (!account) {
|
||||
throw new Error('未找到对应的 PayPal 账号。');
|
||||
}
|
||||
|
||||
await syncSelectedPayPalAccountState(account);
|
||||
return account;
|
||||
}
|
||||
|
||||
return {
|
||||
getCurrentPayPalAccount,
|
||||
setCurrentPayPalAccount,
|
||||
syncPayPalAccounts,
|
||||
upsertPayPalAccount,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
createPayPalAccountStore,
|
||||
};
|
||||
});
|
||||
@@ -99,17 +99,30 @@
|
||||
return result || {};
|
||||
}
|
||||
|
||||
function resolvePayPalCredentials(state = {}) {
|
||||
const currentId = String(state?.currentPayPalAccountId || '').trim();
|
||||
const accounts = Array.isArray(state?.paypalAccounts) ? state.paypalAccounts : [];
|
||||
const selectedAccount = currentId
|
||||
? accounts.find((account) => String(account?.id || '').trim() === currentId) || null
|
||||
: null;
|
||||
return {
|
||||
email: String(selectedAccount?.email || state?.paypalEmail || '').trim(),
|
||||
password: String(selectedAccount?.password || state?.paypalPassword || ''),
|
||||
};
|
||||
}
|
||||
|
||||
async function submitLogin(tabId, state = {}) {
|
||||
if (!state.paypalPassword) {
|
||||
throw new Error('步骤 8:未配置 PayPal 密码,请先在侧边栏填写。');
|
||||
const credentials = resolvePayPalCredentials(state);
|
||||
if (!credentials.password) {
|
||||
throw new Error('步骤 8:未配置可用的 PayPal 账号,请先在侧边栏添加并选择账号。');
|
||||
}
|
||||
await addLog('步骤 8:正在填写 PayPal 登录信息并提交...', 'info');
|
||||
const result = await sendTabMessageUntilStopped(tabId, PAYPAL_SOURCE, {
|
||||
type: 'PAYPAL_SUBMIT_LOGIN',
|
||||
source: 'background',
|
||||
payload: {
|
||||
email: state.paypalEmail || '',
|
||||
password: state.paypalPassword || '',
|
||||
email: credentials.email,
|
||||
password: credentials.password,
|
||||
},
|
||||
});
|
||||
if (result?.error) {
|
||||
|
||||
Reference in New Issue
Block a user