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

171 lines
5.0 KiB
JavaScript

"use client";
import React, { useEffect, useState } from "react";
import { Avatar, Divider, Toast } from "antd-mobile";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons";
import { useRouter } from "next/navigation";
import { get, save } from "@/utils/storeInfo";
import { uploadImage } from "@/utils/upload";
import { getUserInfo } from "@/api/public";
import requireAPI from "@/utils/requireAPI";
export default function SelectUserProfileItem() {
const router = useRouter();
const [userInfo, setUserInfo] = useState({});
useEffect(() => {
const userInfo = get("account");
if (setUserInfo) {
setUserInfo(userInfo);
}
}, []);
const uploadHead = async (e) => {
const avatarId = await uploadImage(e.target.files[0]);
try {
const data = await requireAPI(
"POST",
"/api/account/update",
{
body: { avatar: { image_ids: [avatarId] } },
},
true
);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
//向服务器请求新的账号信息并保存到本地
const account = await getUserInfo();
save("account", JSON.stringify(account));
setUserInfo(account);
} catch (error) {
// console.error(error);
}
};
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" }}
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 justify-between items-center mt-2">
<span className="text-base font-medium">头像</span>
{/* <CustomUploadButton img={userInfo?.avatar?.images[0]?.urls[0]} /> */}
<label htmlFor="uploadAvatarBtn">
<div
className="flex items-center"
// onClick={() => setMediaPickerModalVisible(!mediaPickerModalVisible)}
>
<Avatar
src={userInfo?.avatar?.images[0]?.urls[0]}
style={{ "--border-radius": "50px", "--size": "2rem" }}
/>
<FontAwesomeIcon
icon={faAngleRight}
size="xl"
className=" ml-2"
style={{ maxWidth: "12px" }}
/>
</div>
</label>
<input
type="file"
id="uploadAvatarBtn"
style={{ display: "none" }}
accept="image/png, image/jpeg"
// capture="camera"
onChange={uploadHead}
/>
</div>
<Divider className="my-2" />
<div
className="flex justify-between items-center mt-2"
onClick={() => {
router.push("/my/editUserProfile/editUserName");
}}
>
<span className="text-base font-medium">昵称</span>
<div>
<span className="text-base font-medium">{userInfo?.name}</span>
<FontAwesomeIcon
icon={faAngleRight}
style={{ maxWidth: "12px" }}
size="xl"
className=" ml-2"
onClick={() => {
router.push("my/editUserProfile/editUserName");
}}
/>
</div>
</div>
<Divider className="my-2" />
<div className="flex justify-between items-center mt-2">
<span className="text-base font-medium">ID</span>
<span className="text-base text-[#FFFFFF80] font-medium">
{userInfo?.user_id}
</span>
</div>
<Divider className="my-2" />
</div>
</div>
);
}
// const CustomUploadButton = ({ img }) => {
// const [fileList, setFileList] = useState([
// {
// url: img,
// },
// ]);
// const uploadImg = async (file) => {
// console.log(file);
// return {
// url: URL.createObjectURL(file),
// };
// };
// return (
// <ImageUploader
// value={fileList}
// onChange={setFileList}
// upload={uploadImg}
// maxCount={1}
// preview={false}
// deletable={false}
// style={{ "--cell-size": "50px" }}
// >
// <div
// style={{
// width: 50,
// height: 50,
// borderRadius: 40,
// backgroundColor: "#f5f5f5",
// display: "flex",
// justifyContent: "center",
// alignItems: "center",
// color: "#999999",
// }}
// >
// <Avatar
// src={img}
// style={{ "--border-radius": "50px", "--size": "2rem" }}
// />
// </div>
// </ImageUploader>
// );
// };