sign-up.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import CaptchaBox, { CaptchaRes } from '@/components/captcha-box';
  2. import { site } from '@/config.json';
  3. import { useInterval } from '@/hooks/hooks';
  4. import api, { ApiError } from '@/utils/api';
  5. import { signUp, useAuth } from '@/utils/auth';
  6. import { Button, Toast } from '@ant-design/react-native';
  7. import { Ionicons } from '@expo/vector-icons';
  8. import { Link, router, useLocalSearchParams } from 'expo-router';
  9. import { openBrowserAsync } from 'expo-web-browser';
  10. import React, { useRef, useState } from 'react';
  11. import {
  12. KeyboardAvoidingView,
  13. Platform,
  14. Pressable,
  15. ScrollView,
  16. StyleSheet,
  17. Text,
  18. TextInput,
  19. View,
  20. } from 'react-native';
  21. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  22. function FieldLabel({ children }: { children: React.ReactNode }) {
  23. return (
  24. <Text className="mb-2 ml-1 text-xs font-bold uppercase tracking-widest text-outline">
  25. {children}
  26. </Text>
  27. );
  28. }
  29. export default function SignUpScreen() {
  30. const [mobile, setMobile] = useState('');
  31. const [code, setCode] = useState('');
  32. const [password, setPassword] = useState('');
  33. const [rePass, setRepass] = useState('');
  34. const [email, setEmail] = useState('');
  35. const [name, setName] = useState('');
  36. const [organization, setOrganization] = useState('');
  37. const [agreed, setAgreed] = useState(false);
  38. const [flushAgree, setFlushAgree] = useState(false);
  39. const [loading, setLoading] = useState(false);
  40. const scrollView = useRef<ScrollView>(null);
  41. const [captchaVisible, setCaptchaVisible] = useState<boolean>(false);
  42. const [smsTtl, setSmsTtl] = useState(0);
  43. const { setToken } = useAuth();
  44. const { signIn, } = useLocalSearchParams<{ signIn: string; redirectTo?: string }>();
  45. const waitCaptcha = useRef<(res: CaptchaRes) => void | null>(null);
  46. const needsCaptchaRef = useRef(false);
  47. const handleCaptcha = (res: CaptchaRes) => {
  48. let fun = waitCaptcha.current;
  49. waitCaptcha.current = null;
  50. alert(1)
  51. setCaptchaVisible(false);
  52. fun?.(res);
  53. }
  54. const handleSendCode = async () => {
  55. if (mobile.trim().length !== 11) {
  56. Toast.fail('请先输入 11 位手机号');
  57. return;
  58. }
  59. let captcha: CaptchaRes = null!;
  60. while (true) {
  61. if (needsCaptchaRef.current) {
  62. setCaptchaVisible(true);
  63. captcha = await new Promise<CaptchaRes>((resolve) => {
  64. waitCaptcha.current = resolve;
  65. });
  66. if (!captcha || !captcha.ok) {
  67. return;
  68. }
  69. }
  70. try {
  71. const res = await api.post<{
  72. needsCaptcha?: boolean;
  73. timerout: number;
  74. }>("sms/send?__session_id=" + captcha?.sid || '', {
  75. mobile: mobile,
  76. event: 'register',
  77. captcha_code: captcha?.code,
  78. });
  79. if (res.needsCaptcha) {
  80. needsCaptchaRef.current = true;
  81. setCaptchaVisible(true);
  82. captcha = await new Promise<CaptchaRes>((resolve) => {
  83. waitCaptcha.current = resolve;
  84. });
  85. alert(JSON.stringify(captcha));
  86. if (!captcha || !captcha.ok) {
  87. return;
  88. }
  89. continue;
  90. }
  91. setSmsTtl(res.timerout);
  92. break;
  93. } catch (e) {
  94. if (ApiError.isApiError(e)) {
  95. if (e.code === -1) {
  96. Toast.fail("该手机已被注册");
  97. return;
  98. }
  99. if (e.code === -99) {
  100. Toast.fail("图形验证码无效");
  101. return;
  102. }
  103. }
  104. Toast.fail("发送验证码失败");
  105. return;
  106. }
  107. }
  108. };
  109. // useEffect(() => {
  110. // let isMount = true;
  111. // setTimeout(()=> {
  112. // if (!isMount) {
  113. // return;
  114. // }
  115. // setSmsTtl((pre)=> pre --);
  116. // })
  117. // return ()=> {
  118. // isMount = false;
  119. // }
  120. // }, [])
  121. useInterval(() => {
  122. setSmsTtl((pre) => pre - 1)
  123. }, smsTtl > 0 ? 1000 : null);
  124. const handleRegister = async () => {
  125. setFlushAgree(false);
  126. if (mobile.trim().length !== 11) {
  127. Toast.fail('请输入正确的手机号');
  128. return;
  129. }
  130. if (code.trim().length !== 6) {
  131. Toast.fail('请输入 6 位验证码');
  132. return;
  133. }
  134. if (password.length < 6) {
  135. Toast.fail('请输入不少于 6 位的登录密码');
  136. return;
  137. }
  138. if (rePass !== password) {
  139. Toast.fail("两次密码输入不一至");
  140. return;
  141. }
  142. if (!email.includes('@')) {
  143. Toast.fail('请输入正确的邮箱');
  144. return;
  145. }
  146. if (!name.trim()) {
  147. Toast.fail('请输入姓名');
  148. return;
  149. }
  150. if (!agreed) {
  151. Toast.fail('请先阅读并同意协议');
  152. setFlushAgree(true);
  153. scrollView.current?.scrollToEnd({ animated: true });
  154. return;
  155. }
  156. setLoading(true);
  157. const toastKey = Toast.loading('正在创建账号...');
  158. try {
  159. const token = await signUp({
  160. mobile, password, captcha: code, name, email,
  161. });
  162. setToken(token);
  163. Toast.success('登录成功');
  164. if (signIn) {
  165. router.dismissTo("/");
  166. } else {
  167. router.dismiss();
  168. }
  169. } catch (error) {
  170. console.error('登录失败:', error);
  171. Toast.fail('登录失败,请稍后重试');
  172. } finally {
  173. setLoading(false);
  174. Toast.remove(toastKey);
  175. }
  176. };
  177. const insets = useSafeAreaInsets();
  178. return (
  179. <View className="flex-1 bg-surface">
  180. <KeyboardAvoidingView
  181. className="flex-1"
  182. behavior={Platform.OS === 'ios' ? 'padding' : undefined}
  183. >
  184. <ScrollView
  185. ref={scrollView}
  186. className="flex-1"
  187. contentContainerClassName="px-8"
  188. contentInset={{ top: (insets.top ?? 10) + 2, bottom: (insets.bottom ?? 8) + 4 }}
  189. contentContainerStyle={{ flexGrow: 1 }}
  190. keyboardShouldPersistTaps="handled"
  191. showsVerticalScrollIndicator={false}
  192. >
  193. <View className="absolute -top-24 -right-24 h-96 w-96 rounded-full bg-primary-container/10" />
  194. <View className="absolute top-32 -left-20 h-64 w-64 rounded-full bg-secondary-container/20" />
  195. <View className="absolute -bottom-16 right-0 h-48 w-48 rounded-full bg-primary-fixed/50" />
  196. <View className="flex-1 justify-between">
  197. <View>
  198. <View className="mb-12">
  199. <View className="mb-6 h-16 w-16 items-center justify-center rounded-2xl bg-primary-container shadow-lg">
  200. <Ionicons name="sparkles" size={30} color="#ffffff" />
  201. </View>
  202. <Text className="mb-2 text-4xl font-extrabold tracking-tight text-on-surface">
  203. 创建账号
  204. </Text>
  205. <Text className="text-base font-medium leading-7 text-on-surface-variant">
  206. 完善基础信息,开启您的智能借贷助手工作台
  207. </Text>
  208. </View>
  209. <View className="mb-8 rounded-2xl bg-surface-container-lowest p-3 shadow-sm">
  210. <View className="px-2 pb-2">
  211. <FieldLabel>手机号码</FieldLabel>
  212. <View className="mb-5 flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  213. <Text className="text-2xl font-bold text-on-surface">+86</Text>
  214. <View
  215. className="mx-4 h-5 bg-outline-variant/40"
  216. style={{ width: StyleSheet.hairlineWidth }}
  217. />
  218. <TextInput
  219. value={mobile}
  220. onChangeText={setMobile}
  221. editable={!loading}
  222. keyboardType="phone-pad"
  223. maxLength={11}
  224. placeholder="请输入手机号"
  225. placeholderTextColor="#9ca3af"
  226. className="flex-1 p-0 text-xl font-medium text-on-surface"
  227. />
  228. <Ionicons
  229. name="phone-portrait-outline"
  230. size={22}
  231. color="#c3c6d7"
  232. />
  233. </View>
  234. <FieldLabel>验证码</FieldLabel>
  235. <View className="mb-5 flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  236. <TextInput
  237. value={code}
  238. onChangeText={setCode}
  239. editable={!loading}
  240. keyboardType="number-pad"
  241. maxLength={6}
  242. placeholder="请输入 6 位验证码"
  243. placeholderTextColor="#9ca3af"
  244. className="flex-1 p-0 text-xl font-medium text-on-surface"
  245. />
  246. <Pressable
  247. disabled={loading || smsTtl > 0}
  248. hitSlop={8}
  249. onPress={handleSendCode}
  250. >
  251. <Text className={`text-lg font-bold leading-6 ${loading || smsTtl > 0 ? 'text-on-secondary-fixed' : 'text-primary'}`}>
  252. {smsTtl > 0 ? `${smsTtl}s` : '发送验证码'}
  253. </Text>
  254. </Pressable>
  255. </View>
  256. <FieldLabel>登录密码</FieldLabel>
  257. <View className="mb-5 flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  258. <TextInput
  259. value={password}
  260. onChangeText={setPassword}
  261. editable={!loading}
  262. secureTextEntry
  263. maxLength={20}
  264. placeholder="请输入不少于 6 位的密码"
  265. placeholderTextColor="#9ca3af"
  266. className="flex-1 p-0 text-xl font-medium text-on-surface"
  267. />
  268. <Ionicons name="lock-closed-outline" size={22} color="#c3c6d7" />
  269. </View>
  270. <FieldLabel>重复密码</FieldLabel>
  271. <View className="mb-5 flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  272. <TextInput
  273. value={rePass}
  274. onChangeText={setRepass}
  275. editable={!loading}
  276. secureTextEntry
  277. maxLength={20}
  278. placeholder="请再输入一次密码"
  279. placeholderTextColor="#9ca3af"
  280. className="flex-1 p-0 text-xl font-medium text-on-surface"
  281. />
  282. <Ionicons name="lock-closed-outline" size={22} color="#c3c6d7" />
  283. </View>
  284. <FieldLabel>邮箱</FieldLabel>
  285. <View className="mb-5 flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  286. <TextInput
  287. value={email}
  288. onChangeText={setEmail}
  289. editable={!loading}
  290. keyboardType="email-address"
  291. autoCapitalize="none"
  292. placeholder="请输入常用邮箱"
  293. placeholderTextColor="#9ca3af"
  294. className="flex-1 p-0 text-xl font-medium text-on-surface"
  295. />
  296. <Ionicons name="mail-outline" size={22} color="#c3c6d7" />
  297. </View>
  298. <FieldLabel>姓名</FieldLabel>
  299. <View className="mb-5 flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  300. <TextInput
  301. value={name}
  302. onChangeText={setName}
  303. editable={!loading}
  304. placeholder="请输入姓名"
  305. placeholderTextColor="#9ca3af"
  306. className="flex-1 p-0 text-xl font-medium text-on-surface"
  307. />
  308. <Ionicons name="person-outline" size={22} color="#c3c6d7" />
  309. </View>
  310. <FieldLabel>所属机构</FieldLabel>
  311. <View className="flex-row items-center rounded-2xl bg-surface-container-low px-5 py-4">
  312. <TextInput
  313. value={organization}
  314. onChangeText={setOrganization}
  315. editable={!loading}
  316. placeholder="请输入所属机构"
  317. placeholderTextColor="#9ca3af"
  318. className="flex-1 p-0 text-xl font-medium text-on-surface"
  319. />
  320. <Ionicons name="business-outline" size={22} color="#c3c6d7" />
  321. </View>
  322. </View>
  323. </View>
  324. <View className="mb-8 gap-4">
  325. <Button type="primary" loading={loading} onPress={handleRegister}>
  326. 注册账号
  327. </Button>
  328. <View className="flex-row items-center justify-between px-3">
  329. <Pressable hitSlop={8} onPress={() => router.back()}>
  330. <Text className="text-lg font-medium text-on-surface-variant">
  331. 返回登录
  332. </Text>
  333. </Pressable>
  334. <Pressable hitSlop={8}>
  335. <Text className="text-lg font-medium text-on-surface-variant">
  336. 遇到问题?
  337. </Text>
  338. </Pressable>
  339. </View>
  340. </View>
  341. </View>
  342. <View className="pt-4">
  343. <View className={`flex-row items-start gap-3 px-2 rounded-md border-2 transition-colors duration-700 ${flushAgree ? ' border-primary/50' : 'border-transparent'}`}>
  344. <Pressable
  345. onPress={() => setAgreed((value) => !value)}
  346. hitSlop={8}
  347. className="pt-1"
  348. >
  349. <View
  350. className={`h-6 w-6 mt-2 items-center justify-center border-2 border-primary/50 rounded-full cursor-pointer ${agreed
  351. ? 'border-primary bg-primary'
  352. : 'border-outline-variant bg-surface-container-low'
  353. }`}
  354. >
  355. {agreed ? (
  356. <Ionicons name="checkmark" size={14} color="#ffffff" />
  357. ) : null}
  358. </View>
  359. </Pressable>
  360. <Text className="flex-1 text-sm leading-6 text-on-surface-variant">
  361. 注册即代表您已阅读并同意
  362. <Link href={{
  363. pathname: '/web',
  364. params: { uri: site }
  365. }}>
  366. <Text className="font-semibold text-primary">《用户服务协议》</Text>
  367. </Link>
  368. <Text className="font-semibold text-primary cursor-pointer" onPress={() => openBrowserAsync(`${site}`)}>《隐私政策》</Text>
  369. ,以及授权该应用获取您的公开信息。
  370. </Text>
  371. </View>
  372. </View>
  373. </View>
  374. </ScrollView>
  375. </KeyboardAvoidingView>
  376. <CaptchaBox visible={captchaVisible} onClose={handleCaptcha} />
  377. </View>
  378. );
  379. }