修复分享页面不能访问问题
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import { View, Text, Image, ScrollView, Button } from "@tarojs/components";
|
||||
import { View, Text, Image, Button } from "@tarojs/components";
|
||||
import { PopupPicker } from "@/components/Picker/index";
|
||||
import Taro from "@tarojs/taro";
|
||||
import "./index.scss";
|
||||
@@ -19,18 +19,22 @@ const EditProfilePage: React.FC = () => {
|
||||
const user_info = useUserInfo();
|
||||
|
||||
// 表单状态,基于store中的用户信息初始化
|
||||
const [form_data, setFormData] = useState({
|
||||
nickname: (user_info as UserInfoType)?.nickname || "",
|
||||
personal_profile: (user_info as UserInfoType)?.personal_profile || "",
|
||||
occupation: (user_info as UserInfoType)?.occupation || "",
|
||||
ntrp_level: (user_info as UserInfoType)?.ntrp_level || "4.0",
|
||||
phone: (user_info as UserInfoType)?.phone || "",
|
||||
gender: (user_info as UserInfoType)?.gender || "",
|
||||
birthday: (user_info as UserInfoType)?.birthday || "2000-01-01",
|
||||
country: (user_info as UserInfoType)?.country || "",
|
||||
province: (user_info as UserInfoType)?.province || "",
|
||||
city: (user_info as UserInfoType)?.city || "",
|
||||
});
|
||||
const getInitialFormData = () => {
|
||||
const info = user_info as UserInfoType;
|
||||
return {
|
||||
nickname: info?.nickname ?? "",
|
||||
personal_profile: info?.personal_profile ?? "",
|
||||
occupation: info?.occupation ?? "",
|
||||
ntrp_level: info?.ntrp_level ?? "4.0",
|
||||
phone: info?.phone ?? "",
|
||||
gender: info?.gender ?? "",
|
||||
birthday: info?.birthday ?? "2000-01-01",
|
||||
country: info?.country ?? "",
|
||||
province: info?.province ?? "",
|
||||
city: info?.city ?? "",
|
||||
};
|
||||
};
|
||||
const [form_data, setFormData] = useState(getInitialFormData());
|
||||
|
||||
// 加载状态
|
||||
const [loading, setLoading] = useState(false);
|
||||
@@ -55,17 +59,18 @@ const EditProfilePage: React.FC = () => {
|
||||
// 监听store中的用户信息变化,同步到表单状态
|
||||
useEffect(() => {
|
||||
if (user_info && Object.keys(user_info).length > 0) {
|
||||
const info = user_info as UserInfoType;
|
||||
setFormData({
|
||||
nickname: (user_info as UserInfoType)?.nickname || "",
|
||||
personal_profile: (user_info as UserInfoType)?.personal_profile || "",
|
||||
occupation: (user_info as UserInfoType)?.occupation || "",
|
||||
ntrp_level: (user_info as UserInfoType)?.ntrp_level || "4.0",
|
||||
phone: (user_info as UserInfoType)?.phone || "",
|
||||
gender: (user_info as UserInfoType)?.gender || "",
|
||||
birthday: (user_info as UserInfoType)?.birthday || "2000-01-01",
|
||||
country: (user_info as UserInfoType)?.country || "",
|
||||
province: (user_info as UserInfoType)?.province || "",
|
||||
city: (user_info as UserInfoType)?.city || "",
|
||||
nickname: info?.nickname ?? "",
|
||||
personal_profile: info?.personal_profile ?? "",
|
||||
occupation: info?.occupation ?? "",
|
||||
ntrp_level: info?.ntrp_level ?? "4.0",
|
||||
phone: info?.phone ?? "",
|
||||
gender: info?.gender ?? "",
|
||||
birthday: info?.birthday ?? "2000-01-01",
|
||||
country: info?.country ?? "",
|
||||
province: info?.province ?? "",
|
||||
city: info?.city ?? "",
|
||||
});
|
||||
}
|
||||
}, [user_info]);
|
||||
@@ -183,6 +188,14 @@ const EditProfilePage: React.FC = () => {
|
||||
|
||||
const handle_edit_modal_save = async (value: string) => {
|
||||
try {
|
||||
// 验证值不能是 undefined 或 null
|
||||
if (value === undefined || value === null) {
|
||||
Taro.showToast({
|
||||
title: "数据不完整,请重新输入",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 调用更新用户信息接口,只传递修改的字段
|
||||
const update_data = { [editing_field]: value };
|
||||
await updateUserInfo(update_data);
|
||||
@@ -224,11 +237,30 @@ const EditProfilePage: React.FC = () => {
|
||||
field !== null &&
|
||||
!Array.isArray(field)
|
||||
) {
|
||||
// 验证对象中的值不能是 undefined
|
||||
const hasUndefined = Object.values(field).some(
|
||||
(v) => v === undefined || v === null
|
||||
);
|
||||
if (hasUndefined) {
|
||||
Taro.showToast({
|
||||
title: "数据不完整,请重新选择",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
await updateUserInfo({ ...field });
|
||||
|
||||
// 更新表单状态(store会自动更新)
|
||||
setFormData((prev) => ({ ...prev, ...field }));
|
||||
} else {
|
||||
// 验证值不能是 undefined
|
||||
if (value === undefined || value === null) {
|
||||
Taro.showToast({
|
||||
title: "数据不完整,请重新选择",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 调用更新用户信息接口,只传递修改的字段
|
||||
const update_data = { [field as string]: value };
|
||||
await updateUserInfo(update_data);
|
||||
@@ -252,12 +284,26 @@ const EditProfilePage: React.FC = () => {
|
||||
|
||||
// 处理性别选择
|
||||
const handle_gender_change = (e: any) => {
|
||||
if (!Array.isArray(e) || e.length === 0 || e[0] === undefined) {
|
||||
Taro.showToast({
|
||||
title: "请选择性别",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const gender_value = e[0];
|
||||
handle_field_edit("gender", gender_value);
|
||||
handle_field_edit("gender", String(gender_value));
|
||||
};
|
||||
|
||||
// 处理生日选择
|
||||
const handle_birthday_change = (e: any) => {
|
||||
if (!Array.isArray(e) || e.length < 3 || e.some((v) => v === undefined)) {
|
||||
Taro.showToast({
|
||||
title: "请完整选择生日",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const [year, month, day] = e;
|
||||
handle_field_edit(
|
||||
"birthday",
|
||||
@@ -270,20 +316,46 @@ const EditProfilePage: React.FC = () => {
|
||||
|
||||
// 处理地区选择
|
||||
const handle_location_change = (e: any) => {
|
||||
if (!Array.isArray(e) || e.length < 3 || e.some((v) => v === undefined || v === null)) {
|
||||
Taro.showToast({
|
||||
title: "请完整选择地区",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const [country, province, city] = e;
|
||||
handle_field_edit({ country, province, city });
|
||||
handle_field_edit({
|
||||
country: String(country ?? ""),
|
||||
province: String(province ?? ""),
|
||||
city: String(city ?? "")
|
||||
});
|
||||
};
|
||||
|
||||
// 处理NTRP水平选择
|
||||
const handle_ntrp_level_change = (e: any) => {
|
||||
if (!Array.isArray(e) || e.length === 0 || e[0] === undefined) {
|
||||
Taro.showToast({
|
||||
title: "请选择NTRP水平",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const ntrp_level_value = e[0];
|
||||
handle_field_edit("ntrp_level", ntrp_level_value);
|
||||
handle_field_edit("ntrp_level", String(ntrp_level_value));
|
||||
};
|
||||
|
||||
// 处理职业选择
|
||||
const handle_occupation_change = (e: any) => {
|
||||
const [country, province, city] = e;
|
||||
handle_field_edit("occupation", `${country} ${province} ${city}`);
|
||||
if (!Array.isArray(e) || e.length === 0 || e.some((v) => v === undefined || v === null)) {
|
||||
Taro.showToast({
|
||||
title: "请完整选择职业",
|
||||
icon: "none",
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 职业可能是多级联动,将所有选中的值用空格连接
|
||||
const occupation_value = e.map((v) => String(v ?? "")).filter(Boolean).join(" ");
|
||||
handle_field_edit("occupation", occupation_value);
|
||||
};
|
||||
|
||||
// 处理退出登录
|
||||
@@ -375,7 +447,7 @@ const EditProfilePage: React.FC = () => {
|
||||
}}
|
||||
/>
|
||||
{/* 主要内容 */}
|
||||
<ScrollView className="edit_main_content" scrollY>
|
||||
<View className="edit_main_content">
|
||||
{loading ? (
|
||||
<View className="loading_container">
|
||||
<Text className="loading_text">加载中...</Text>
|
||||
@@ -635,7 +707,7 @@ const EditProfilePage: React.FC = () => {
|
||||
</View>
|
||||
</>
|
||||
)}
|
||||
</ScrollView>
|
||||
</View>
|
||||
|
||||
{/* 编辑弹窗 */}
|
||||
<EditModal
|
||||
|
||||
Reference in New Issue
Block a user