import { View, Text, TextInput, TouchableWithoutFeedback, Keyboard, ScrollView, } from "react-native"; import React, { useState, useEffect, useContext } from "react"; import { useTailwind } from "tailwind-rn"; import { Button } from "@rneui/themed"; import Toast from "react-native-toast-message"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { get } from "../../../utils/storeInfo"; import { JSEncrypt } from "jsencrypt"; import { cryptoPassword, generateSignature } from "../../../utils/crypto"; import { AuthContext } from "../../../App"; import MyDivider from "../../../components/MyDivider"; import baseRequest from "../../../utils/baseRequest"; export default function EditPassword({ navigation }) { const { signOut } = useContext(AuthContext); const insets = useSafeAreaInsets(); const tailwind = useTailwind(); //获取环境变量 const apiUrl = process.env.EXPO_PUBLIC_API_URL; //保存区号、手机号、验证码、旧密码、新密码 const [regionCode, setRegionCode] = useState(""); const [mobilePhone, setMobilePhone] = useState(""); const [veriCode, setVeriCode] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); //获取当前用户的手机号 useEffect(() => { async function getMobilePhone() { setMobilePhone(await get("mobile_phone")); setRegionCode(await get("region_code")); } getMobilePhone(); }, []); //重新获取验证码的计时器 const [isCounting, setIsCounting] = useState(false); const [seconds, setSeconds] = useState(60); useEffect(() => { let interval; if (isCounting && seconds > 0) { interval = setInterval(() => { setSeconds(seconds - 1); }, 1000); } else { setIsCounting(false); setSeconds(60); clearInterval(interval); } return () => { clearInterval(interval); }; }, [isCounting, seconds]); //点击获取验证码 const handleVerification = async () => { //手机号校验 if (!mobilePhone.match(/^1[3456789]\d{9}$/)) { Toast.show({ type: "error", text1: "手机号码格式错误", topOffset: 60, }); return; } //开始倒计时 setIsCounting(true); //对手机号进行RSA加密 const encrypt = new JSEncrypt(); encrypt.setPublicKey(process.env.EXPO_PUBLIC_RSA_KEY); const mobile_phone = encrypt.encrypt(mobilePhone); //发送短信验证码 const base = await baseRequest(); const signature = await generateSignature({ mobile_phone: mobile_phone, region_code: regionCode, ...base, }); try { await fetch(`${apiUrl}/api/veri_code/send?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mobile_phone: mobile_phone, region_code: regionCode, ...base, }), }); } catch (error) { console.error(error); } }; //点击修改密码 const handleUpdatePassword = async () => { //验证数据格式 if (!mobilePhone.match(/^1[3456789]\d{9}$/)) { Toast.show({ type: "error", text1: "手机号码格式错误", topOffset: 60, }); return; } if (!veriCode) { Toast.show({ type: "error", text1: "请输入验证码", topOffset: 60, }); return; } if (!confirmPassword) { Toast.show({ type: "error", text1: "请再次输入您的密码", topOffset: 60, }); return; } if (newPassword != confirmPassword) { Toast.show({ type: "error", text1: "两次输入密码不一致", topOffset: 60, }); return; } if (newPassword.length < 8) { Toast.show({ type: "error", text1: "新密码不得小于8位", topOffset: 60, }); return; } if (newPassword.length > 15) { Toast.show({ type: "error", text1: "新密码不得大于15位", topOffset: 60, }); return; } //对手机号进行RSA加密 const encrypt = new JSEncrypt(); encrypt.setPublicKey(process.env.EXPO_PUBLIC_RSA_KEY); const mobile_phone = encrypt.encrypt(mobilePhone); //MD5加密新旧密码 const encryptedNewPassword = await cryptoPassword(newPassword); //发送修改密码请求 const base = await baseRequest(); const signature = await generateSignature({ mobile_phone: mobile_phone, region_code: regionCode, veri_code: veriCode, new_password: encryptedNewPassword, ...base, }); try { const response = await fetch( `${apiUrl}/api/login/reset_password?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mobile_phone: mobile_phone, region_code: regionCode, veri_code: veriCode, new_password: encryptedNewPassword, ...base, }), } ); const data = await response.json(); if (data.ret === -1) { Toast.show({ type: "error", text1: data.msg, topOffset: 60, }); return; } Toast.show({ type: "success", text1: "修改成功,请重新登录", topOffset: 60, }); signOut(); } catch (error) { console.error(error); } }; return ( Keyboard.dismiss()}> 修改密码 请牢记密码 +{regionCode} setMobilePhone(value)} value={mobilePhone} style={tailwind("flex-1 text-white")} /> 验证码 setVeriCode(value)} value={veriCode} style={tailwind("flex-1 text-white")} /> 新密码 setNewPassword(value)} value={newPassword} style={tailwind("flex-1 text-white")} /> 确认密码 setConfirmPassword(value)} value={confirmPassword} style={tailwind("flex-1 text-white")} /> ); }