轻松聘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

227 lines
9.0 KiB

import { useState } from 'react';
import { Ionicons } from '@expo/vector-icons';
import { Pressable, StyleSheet, Text, TextInput, View } from 'react-native';
import { colors } from '../../../src/theme';
import { useAgreementGate, useLogin, useOnboarding, useSmsCode, useWechatBindPhone } from '../../../src/hooks';
import { isValidPhone, SMS_CODE_MAX_LENGTH, SMS_CODE_MIN_LENGTH } from '../../../src/utils';
import { useAuth } from '../../../src/context/AuthContext';
import { ScreenShell } from '../ui/ScreenShell';
import { CountdownButton } from '../ui/CountdownButton';
import { AgreementCheckbox } from '../ui/AgreementCheckbox';
import { PrimaryButton } from '../ui/PrimaryButton';
import { AgreementModal } from '../ui/AgreementModal';
/** 登录阶段:手机号 → 验证码 */
type Stage = 'phone' | 'code';
/** P2 登录/注册:阶段1 手机号(对齐参考样式)→ 阶段2 验证码 + 60s 倒计时 */
export function P2PhoneCode() {
const { data, setData, navigate, back } = useOnboarding();
const { gate, modalProps } = useAgreementGate();
const { login, loading: logging, error: loginError } = useLogin();
const { send: sendCode, error: smsError } = useSmsCode();
/** 登录态写入:此前本页只跳 P3 不写 token,导致登录态始终为空 */
const { login: writeAuth } = useAuth();
const { bind, loading: binding, error: bindError } = useWechatBindPhone();
const [code, setCode] = useState('');
const [stage, setStage] = useState<Stage>('phone');
const agreed = data.agreed;
const phone = data.phone;
const phoneValid = isValidPhone(phone);
/** 有值说明本页是从「微信第一次登录」进来的,提交时需完成微信绑定 */
const wechatTicket = data.wechatTicket;
/** 阶段1 → 阶段2:发送验证码;发送失败则停在阶段1,不误导用户去填验证码 */
const requestCode = async () => {
if (!phoneValid) return;
try {
await sendCode(phone);
setStage('code');
} catch {
// 失败原因由 useSmsCode 记录,页面下方统一展示
}
};
/** 阶段2 重新获取(不改变阶段) */
const resendCode = () => {
void sendCode(phone).catch(() => {
// 同上,错误已记录
});
};
/*
* 提交验证码:
* - 微信第一次登录(data.wechatTicket 有值):调绑定注册接口把 openid 与手机号关联,
* hook 内部已写入登录态,再进 P3 选择身份;失败则留在当前页并提示。
* - 手机号登录:换取令牌并写入登录态(本次修复的缺口),成功后才进 P3。
*/
const handleLogin = async () => {
if (code.length < SMS_CODE_MIN_LENGTH) return;
setData({ phone, code });
if (wechatTicket) {
try {
await bind({ token: wechatTicket.token, openid: wechatTicket.openid, phone, code });
} catch {
return;
}
} else {
try {
const result = await login(phone, code);
writeAuth(result.token);
} catch {
return;
}
}
navigate('P3');
};
/** 掩码手机号:138****1234 */
const maskedPhone = phone.length === 11 ? `${phone.slice(0, 3)}****${phone.slice(7)}` : phone;
return (
<ScreenShell showBack onBack={stage === 'code' ? () => setStage('phone') : back}>
{stage === 'phone' ? (
/* ---------- 阶段1:手机号 ---------- */
<View>
<Text style={styles.h1}>登录/注册</Text>
<Text style={styles.subtitle}>首次验证通过即注册有职账号</Text>
<View style={styles.phoneRow}>
<Text style={styles.prefix}>+86</Text>
<Ionicons name="caret-down" size={14} color={colors.textSecondary} />
<View style={styles.divider} />
<TextInput
style={styles.phoneInput}
value={phone}
onChangeText={(t) => setData({ phone: t.replace(/[^\d]/g, '').slice(0, 11) })}
placeholder="请输入手机号"
placeholderTextColor={colors.textHint}
keyboardType="phone-pad"
maxLength={11}
/>
{phone.length > 0 ? (
<Pressable onPress={() => setData({ phone: '' })} hitSlop={8}>
<Ionicons name="close-circle" size={18} color={colors.textDisabled} />
</Pressable>
) : null}
</View>
<View style={styles.underline} />
<Text style={styles.smsHelp}>接收不到短信</Text>
<View style={styles.btnArea}>
<PrimaryButton label="下一步" active={phoneValid} onPress={() => gate(requestCode)} />
</View>
{smsError ? <Text style={styles.formError}>{smsError.message}</Text> : null}
<View style={styles.agree}>
<AgreementCheckbox
checked={agreed}
onToggle={() => setData({ agreed: !agreed })}
text={
<Text style={styles.agreeText}>
已阅读并同意
<Text style={styles.link} onPress={() => navigate('B2')}>《有职用户协议》</Text>
和
<Text style={styles.link} onPress={() => navigate('B2')}>《隐私政策》</Text>
,允许有职统一管理本人账号信息
</Text>
}
/>
</View>
{/* 其他登录方式:密码登录(与 P2P 互跳,保证两条通道都有入口) */}
<View style={styles.orRow}>
<View style={styles.orLine} />
<Text style={styles.orText}>或</Text>
<View style={styles.orLine} />
</View>
<Pressable onPress={() => gate(() => navigate('P2P'))} hitSlop={8}>
<Text style={styles.switchLink}>密码登录</Text>
</Pressable>
</View>
) : (
/* ---------- 阶段2:验证码 ---------- */
<View>
<Text style={styles.h1}>输入验证码</Text>
<Text style={styles.subtitle}>验证码已发送至 {maskedPhone}</Text>
<View style={styles.phoneRow}>
<TextInput
style={styles.phoneInput}
value={code}
onChangeText={(t) => setCode(t.replace(/[^\d]/g, '').slice(0, SMS_CODE_MAX_LENGTH))}
placeholder="请输入验证码"
placeholderTextColor={colors.textHint}
keyboardType="number-pad"
maxLength={SMS_CODE_MAX_LENGTH}
/>
<CountdownButton disabled={!phoneValid} onSend={resendCode} />
</View>
<View style={styles.underline} />
<View style={styles.btnArea}>
<PrimaryButton
label={binding ? '绑定中…' : '登录'}
active={code.length >= SMS_CODE_MIN_LENGTH && !binding && !logging}
onPress={() => gate(handleLogin)}
/>
</View>
{bindError ? <Text style={styles.formError}>{bindError.message}</Text> : null}
{loginError ? <Text style={styles.formError}>{loginError.message}</Text> : null}
{smsError ? <Text style={styles.formError}>{smsError.message}</Text> : null}
<View style={styles.agree}>
<AgreementCheckbox
checked={agreed}
onToggle={() => setData({ agreed: !agreed })}
text={
<Text style={styles.agreeText}>
已阅读并同意
<Text style={styles.link} onPress={() => navigate('B2')}>《有职用户协议》</Text>
和
<Text style={styles.link} onPress={() => navigate('B2')}>《隐私政策》</Text>
,允许有职统一管理本人账号信息
</Text>
}
/>
</View>
</View>
)}
<AgreementModal {...modalProps} />
</ScreenShell>
);
}
const styles = StyleSheet.create({
h1: { fontSize: 26, fontWeight: '800', color: colors.textPrimary, marginTop: 12 },
subtitle: { fontSize: 14, color: colors.textSecondary, marginTop: 10 },
phoneRow: {
flexDirection: 'row',
alignItems: 'center',
height: 56,
marginTop: 28,
gap: 6,
},
prefix: { fontSize: 17, fontWeight: '700', color: colors.textPrimary },
divider: { width: 1, height: 20, backgroundColor: colors.border, marginHorizontal: 10 },
phoneInput: { flex: 1, fontSize: 17, color: colors.textPrimary, padding: 0 },
underline: { height: 1, backgroundColor: colors.borderLight },
smsHelp: { fontSize: 13, color: colors.textHint, marginTop: 16 },
btnArea: { marginTop: 28 },
agree: { marginTop: 18 },
formError: { marginTop: 10, textAlign: 'center', fontSize: 12, color: colors.danger },
agreeText: { fontSize: 12, color: colors.textSecondary, lineHeight: 18 },
link: { color: colors.primary, fontWeight: '600' },
orRow: { flexDirection: 'row', alignItems: 'center', marginTop: 36, gap: 12 },
orLine: { flex: 1, height: 1, backgroundColor: colors.borderLight },
orText: { fontSize: 12, color: colors.textHint },
switchLink: { marginTop: 20, textAlign: 'center', fontSize: 14, color: colors.primaryText, fontWeight: '600' },
});