自动读取短信

This commit is contained in:
2026-05-31 01:22:29 +08:00
parent 557931892f
commit eea21cd33b
6 changed files with 64 additions and 112 deletions

32
utils/smsUserConsent.ts Normal file
View File

@@ -0,0 +1,32 @@
import { Platform } from 'react-native';
import {
retrieveVerificationCode,
startSmsHandling,
} from '@eabdullazyanov/react-native-sms-user-consent';
/** 用户点允许后,从短信正文取数字(优先 preferLength 位,否则取第一段数字) */
export function digitsFromSms(message: string, preferLength?: number): string {
if (preferLength) {
const exact = retrieveVerificationCode(message, preferLength);
if (exact) return exact;
}
const m = message.match(/\d+/);
return m ? m[0] : '';
}
/**
* SMS User Consent系统弹窗用户同意后才回调无需 READ_SMS / app hash。
*/
export function startSmsUserConsentListener(
preferLength: number,
onDigits: (digits: string) => void,
): () => void {
if (Platform.OS !== 'android') return () => {};
return startSmsHandling((event) => {
const sms = event?.sms ?? '';
if (!sms) return;
const digits = digitsFromSms(sms, preferLength);
if (digits) onDigits(digits);
});
}