tiefen_space_h5/app/my/setting/editPassword/page.js

300 lines
9.0 KiB
JavaScript
Raw Normal View History

2024-07-02 23:08:38 +08:00
"use client";
2024-07-16 20:20:12 +08:00
import React, { useState, useEffect } from "react";
2024-07-31 18:16:25 +08:00
import { Button, Input, Divider, Toast } from "antd-mobile";
2024-07-02 23:08:38 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft } from "@fortawesome/free-solid-svg-icons";
2024-07-17 20:30:33 +08:00
import { useRouter, useSearchParams } from "next/navigation";
2024-07-16 20:20:12 +08:00
import { get } from "@/utils/storeInfo";
import { handleVerification } from "@/api/public";
import { JSEncrypt } from "jsencrypt";
import { cryptoPassword } from "@/utils/crypto";
2024-07-22 16:07:41 +08:00
import requireAPI from "@/utils/requireAPI";
2024-07-16 20:20:12 +08:00
import { signOut } from "@/utils/auth";
2024-07-02 23:08:38 +08:00
export default function EditPassword() {
const searchParams = useSearchParams();
const [regionCode] = useState("86");
const [mobilePhone, setMobilePhone] = useState(
searchParams.get("phone") || ""
);
2024-07-02 23:08:38 +08:00
const [veriCode, setVeriCode] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [isCounting, setIsCounting] = useState(false);
const [seconds, setSeconds] = useState(60);
const router = useRouter();
2024-07-16 20:20:12 +08:00
// useEffect(() => {
// const mobile_phone = get("mobile_phone");
// console.log("mobile_phone",mobile_phone)
// setMobilePhone(mobile_phone);
// }, []);
//获取之前缓存的用户的手机号
useEffect(() => {
const mobile_phone = get("mobile_phone");
const region_code = get("region_code");
if (mobile_phone && region_code) {
setMobilePhone(mobile_phone);
2024-07-17 16:58:27 +08:00
// setRegionCode(region_code);
2024-07-16 20:20:12 +08:00
}
}, []);
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 getVerification = async () => {
//开始倒计时
setIsCounting(true);
handleVerification(mobilePhone, regionCode);
};
//点击修改密码
const handleUpdatePassword = async () => {
//验证数据格式
if (!mobilePhone.match(/^1[3456789]\d{9}$/)) {
Toast.show({
icon: "fail",
content: "手机号码格式错误",
position: "top",
});
return;
}
2024-07-31 18:16:25 +08:00
if (!veriCode && searchParams.get("is_enabled") != "0") {
2024-07-16 20:20:12 +08:00
Toast.show({
icon: "fail",
content: "请输入验证码",
position: "top",
});
return;
}
if (!confirmPassword) {
Toast.show({
icon: "fail",
content: "请再次输入您的密码",
position: "top",
});
return;
}
if (newPassword != confirmPassword) {
Toast.show({
icon: "fail",
content: "两次输入密码不一致",
position: "top",
});
return;
}
if (newPassword.length < 8) {
Toast.show({
icon: "fail",
content: "新密码不得小于8位",
position: "top",
});
return;
}
if (newPassword.length > 15) {
Toast.show({
icon: "fail",
content: "新密码不得大于15位",
position: "top",
});
return;
}
//对手机号进行RSA加密
const encrypt = new JSEncrypt();
encrypt.setPublicKey(process.env.NEXT_PUBLIC_RSA_KEY);
const mobile_phone = encrypt.encrypt(mobilePhone);
//MD5加密新旧密码
const encryptedNewPassword = cryptoPassword(newPassword);
//发送修改密码请求
2024-07-31 18:16:25 +08:00
const body = {
mobile_phone: mobile_phone,
region_code: regionCode,
new_password: encryptedNewPassword,
};
if (searchParams.get("is_enabled") != "0") {
body.veri_code = veriCode;
2024-07-31 18:16:25 +08:00
}
2024-07-16 20:20:12 +08:00
try {
const data = await requireAPI(
"POST",
`/api/login/${
searchParams.get("is_enabled") == "0"
? "set_password"
: "reset_password"
}`,
{
body,
}
);
2024-07-16 20:20:12 +08:00
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
Toast.show({
icon: "success",
content:
searchParams.get("is_enabled") == "0"
? "密码设置成功"
: "密码修改成功",
2024-07-16 20:20:12 +08:00
position: "top",
});
if (searchParams.get("is_enabled") == "0") {
router.push("/");
} else {
router.back();
2024-07-31 18:16:25 +08:00
}
2024-07-16 20:20:12 +08:00
} catch (error) {
2024-09-09 15:34:31 +08:00
// console.error(error);
2024-07-16 20:20:12 +08:00
}
};
2024-07-31 18:16:25 +08:00
2024-07-02 23:08:38 +08:00
return (
<div>
<div className="p-4 fixed top-0 z-10 w-full">
<div className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full absolute">
<FontAwesomeIcon
icon={faAngleLeft}
style={{ maxWidth: "12px" }}
2024-07-02 23:08:38 +08:00
size="xl"
onClick={() => {
router.back();
}}
/>
</div>
<p className="text-base text-center leading-9"></p>
</div>
{/* 内容 */}
<div className="pt-32 px-4">
2024-07-17 20:30:33 +08:00
<p className="text-3xl text-white font-medium">
2024-07-19 14:14:40 +08:00
{searchParams.get("is_enabled") == "0"
? "设置密码"
2024-07-19 16:22:43 +08:00
: mobilePhone
2024-07-19 14:14:40 +08:00
? "修改密码"
: "重置密码"}
2024-07-17 20:30:33 +08:00
</p>
2024-07-02 23:08:38 +08:00
<p className="text-base text-white mb-10">请牢记密码</p>
<div className="border-2 border-[#2c2b2f] rounded-2xl p-4">
<div className="flex flex-row flex-nowrap items-center mb-4">
<p className="text-base text-white mr-4">+{regionCode}</p>
<Input
2024-07-17 20:30:33 +08:00
placeholder="请输入手机号"
2024-07-31 18:16:25 +08:00
disabled={!searchParams.get("forgetPassword")}
2024-07-02 23:08:38 +08:00
type="number"
maxLength={11}
2024-07-17 20:30:33 +08:00
onChange={(value) => {
2024-08-05 18:59:30 +08:00
// console.log("value", value);
2024-07-17 20:30:33 +08:00
setMobilePhone(value);
2024-07-16 20:20:12 +08:00
}}
2024-07-17 20:30:33 +08:00
value={mobilePhone}
2024-07-19 14:14:40 +08:00
style={{
"--font-size": "16px",
"--placeholder-color": "#FFFFFF80",
}}
2024-07-02 23:08:38 +08:00
/>
</div>
2024-07-17 20:30:33 +08:00
{searchParams.get("is_enabled") == "0" ? null : (
<>
<Divider />
<div className="flex flex-row flex-nowrap items-center my-4">
<p className="text-base text-white mr-4 whitespace-nowrap">
验证码
</p>
<Input
autoComplete="off"
onCompositionEnd={() => setVeriCode("")}
2024-07-17 20:30:33 +08:00
placeholder="请输入验证码"
onChange={(value) => setVeriCode(value)}
value={veriCode}
type="number"
style={{
"--placeholder-color": "#FFFFFF80",
"--font-size": "16px",
}}
/>
<Button
shape="rounded"
size="mini"
disabled={isCounting}
onClick={getVerification}
style={{ "--background-color": "#FF669E", color: "#FFFFFF" }}
className="whitespace-nowrap"
>
{isCounting ? `(${seconds})重新发送` : "获取验证码"}
</Button>
</div>
</>
)}
2024-07-02 23:08:38 +08:00
<Divider />
<div className="flex flex-row flex-nowrap items-center my-4">
2024-07-16 20:20:12 +08:00
<p className="text-base text-white mr-4 whitespace-nowrap">
新密码
</p>
2024-07-02 23:08:38 +08:00
<Input
name="password"
autocomplete="new-password"
2024-07-02 23:08:38 +08:00
placeholder="请输入8-15位新密码"
2024-07-17 20:30:33 +08:00
onChange={(value) => setNewPassword(value)}
2024-07-02 23:08:38 +08:00
value={newPassword}
2024-07-23 20:56:25 +08:00
type="password"
2024-07-16 20:20:12 +08:00
style={{
"--placeholder-color": "#FFFFFF80",
"--font-size": "16px",
}}
2024-07-02 23:08:38 +08:00
/>
</div>
<Divider />
<div className="flex flex-row flex-nowrap items-center mt-4">
2024-07-16 20:20:12 +08:00
<p className="text-base text-white mr-4 whitespace-nowrap">
确认密码
</p>
2024-07-02 23:08:38 +08:00
<Input
placeholder="请再次输入新密码"
2024-07-23 20:56:25 +08:00
onChange={(value) => setConfirmPassword(value)}
2024-07-02 23:08:38 +08:00
value={confirmPassword}
2024-07-23 20:56:25 +08:00
type="password"
autoComplete="off"
2024-07-16 20:20:12 +08:00
style={{
"--placeholder-color": "#FFFFFF80",
"--font-size": "16px",
}}
2024-07-02 23:08:38 +08:00
/>
</div>
</div>
<div className="mt-16">
<Button
shape="rounded"
size="middle"
block
2024-07-23 20:56:25 +08:00
onClick={handleUpdatePassword}
2024-07-02 23:08:38 +08:00
style={{ "--background-color": "#FF669E", color: "#FFFFFF" }}
>
确认修改
</Button>
</div>
</div>
2024-07-19 14:14:40 +08:00
{searchParams.get("is_enabled") == "0" && (
<div
onClick={() => router.push(`/`)}
2024-08-13 12:34:50 +08:00
className="mt-4 w-full text-[#FFFFFF80] text-xs text-center"
2024-07-19 14:14:40 +08:00
>
2024-08-13 12:34:50 +08:00
跳过
2024-07-19 14:14:40 +08:00
</div>
)}
2024-07-02 23:08:38 +08:00
</div>
);
}