tiefen_space_h5/app/my/editUserProfile/editUserName/page.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-07-02 23:08:38 +08:00
"use client";
2024-07-10 16:50:53 +08:00
import React, { useState,useEffect } from "react";
import { Input, Button, Toast } from "antd-mobile";
2024-07-02 23:08:38 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2024-07-22 16:07:41 +08:00
import { faAngleLeft } from "@fortawesome/free-solid-svg-icons";
2024-07-02 23:08:38 +08:00
import { useRouter } from "next/navigation";
2024-07-10 16:50:53 +08:00
import {getUserInfo} from "@/api/public";
import {get,save} from "@/utils/storeInfo";
2024-07-22 16:07:41 +08:00
import requireAPI from "@/utils/requireAPI";
2024-07-10 16:50:53 +08:00
const account = get("account");
2024-07-02 23:08:38 +08:00
export default function EditUserName() {
const router = useRouter();
const [name, setName] = useState();
2024-07-10 16:50:53 +08:00
useEffect(() => {
const getName = async () => {
setName(account.name);
};
getName();
}, []);
const handleSubmit = async () => {
if (!name) {
Toast.show({
icon: "fail",
content: "昵称不得为空",
position: "top",
});
return;
} else if (name.length > 10) {
Toast.show({
icon: "fail",
content: "昵称不得超过10个字",
position: "top",
});
return;
} else if (name === account.name) {
router.back();
return;
}
try {
2024-07-22 16:07:41 +08:00
const data = await requireAPI("POST", "/api/account/update", {
2024-07-10 16:50:53 +08:00
body: {
name: name,
},
},true);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
//向服务器请求新的账号信息并保存到本地
const account = await getUserInfo()
save("account", JSON.stringify(account));
} catch (error) {
console.error(error);
}
router.back();
};
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}
size="xl"
onClick={() => {
router.back();
}}
/>
</div>
<p className="text-base text-center leading-9">修改资料</p>
</div>
{/* 内容 */}
<div className="pt-16 px-4">
<div className="flex items-center rounded-full bg-[#FFFFFF1A] text-white h-12 px-4">
<Input
placeholder="请输入新昵称"
max={8}
onChange={(value) => setName(value)}
2024-07-10 16:50:53 +08:00
value={name}
2024-07-02 23:08:38 +08:00
style={{ "--placeholder-color": "#FFFFFF80" }}
/>
</div>
<div className="mt-16">
<Button
shape="rounded"
size="middle"
block
2024-07-10 16:50:53 +08:00
onClick={handleSubmit}
2024-07-02 23:08:38 +08:00
style={{"--background-color": "#FF669E","color": "#FFFFFF"}}
>
确认
</Button>
</div>
</div>
</div>
);
}