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

178 lines
5.2 KiB
JavaScript
Raw Normal View History

2024-07-02 23:08:38 +08:00
"use client";
2024-07-10 00:48:37 +08:00
import React, { useEffect, useState } from "react";
import { Avatar, Divider, Input } from "antd-mobile";
2024-07-02 23:08:38 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons";
import { useRouter } from "next/navigation";
2024-07-10 00:48:37 +08:00
import { get, save } from "@/utils/storeInfo";
import { uploadImage } from "@/utils/upload";
import { getUserInfo } from "@/api/public";
import require from "@/utils/require";
2024-07-02 23:08:38 +08:00
export default function SelectUserProfileItem() {
const router = useRouter();
2024-07-10 00:48:37 +08:00
const [userInfo, setUserInfo] = useState({});
useEffect(() => {
const userInfo = get("account");
if (setUserInfo) {
setUserInfo(userInfo);
}
}, []);
const uploadImg = async (e) => {
console.log(e.target.files[0]);
const file = e.target.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
if (file) {
reader.onload = (e) => {
uploadHead();
};
}
// return {
// url: URL.createObjectURL(file),
// };
};
const uploadHead = async () => {
const avatarId = await uploadImage(userInfo?.avatar?.images[0]?.urls[0]);
try {
const data = await require("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);
}
};
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 justify-between items-center mt-2">
<span className="text-base font-medium">头像</span>
2024-07-10 00:48:37 +08:00
{/* <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"
/>
</div>
</label>
<input
type="file"
id="uploadAvatarBtn"
style={{ display: "none" }}
accept="image/png, image/jpeg"
capture="camera"
onChange={uploadImg}
/>
2024-07-02 23:08:38 +08:00
</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>
2024-07-10 00:48:37 +08:00
<span className="text-base font-medium">{userInfo?.name}</span>
2024-07-02 23:08:38 +08:00
<FontAwesomeIcon
icon={faAngleRight}
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">
2024-07-10 00:48:37 +08:00
{userInfo?.user_id}
2024-07-02 23:08:38 +08:00
</span>
</div>
<Divider className="my-2" />
</div>
</div>
);
}
2024-07-10 00:48:37 +08:00
// 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>
// );
// };