hookehuyr

feat(家庭): 添加家庭列表可添加字段并优化按钮显示逻辑

添加返回字段 can_add 以控制是否显示创建家庭按钮
移除基于年龄的按钮显示逻辑,改为使用 can_add 字段
/*
* @Date: 2024-01-01 00:00:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-22 15:35:23
* @LastEditTime: 2025-09-23 09:37:24
* @FilePath: /lls_program/src/api/family.js
* @Description: 家庭相关接口
*/
......@@ -43,6 +43,7 @@ export const searchFamilyByPassphraseAPI = (params) => fn(fetch.get(Api.SEARCH_B
* @returns {string} response.code - 响应状态码
* @returns {string} response.msg - 响应消息
* @returns {Array} response.data.families - 家庭列表
* @returns {boolean} response.data.can_add - 是否可以添加家庭
* @returns {number} response.data.families[].id - 家庭ID
* @returns {string} response.data.families[].name - 家庭名称
* @returns {string} response.data.families[].avatar_url - 家庭头像
......
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-20 22:26:29
* @LastEditTime: 2025-09-23 09:45:30
* @FilePath: /lls_program/src/pages/MyFamily/index.vue
* @Description: 我的家庭页面 - 展示用户加入的家庭列表
-->
......@@ -114,9 +114,9 @@
</view>
<!-- 底部固定按钮 -->
<view v-if="isShowBtn" class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 p-4 z-10">
<!-- 双按钮布局:大于60岁且未创建过家庭 -->
<view v-if="showDoubleButtons" class="flex gap-3">
<view v-if="familyList.length > 0" class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 p-4 z-10">
<!-- 双按钮布局:可以创建家庭时 -->
<view v-if="canAddFamily" class="flex gap-3">
<view
@tap="createNewFamily"
class="flex-1 bg-green-500 text-white text-center py-3 rounded-lg font-medium text-sm"
......@@ -131,7 +131,7 @@
</view>
</view>
<!-- 单按钮布局:其他情况 -->
<!-- 单按钮布局:只能加入家庭时 -->
<view
v-else
@tap="joinNewFamily"
......@@ -242,7 +242,6 @@ import { Home, Check } from '@nutui/icons-vue-taro';
import './index.less';
// 获取接口信息
import { getMyFamiliesAPI, switchCurrentFamilyAPI, deleteFamilyMemberAPI } from '@/api/family';
import { getUserProfileAPI } from '@/api/user';
//
const defaultFamilyCoverSvg = 'https://cdn.ipadbiz.cn/lls_prog/images/default-family-cover.png';
// 默认头像
......@@ -251,8 +250,6 @@ const defaultAvatar = 'https://cdn.ipadbiz.cn/mlaj/images/icon_1.jpeg'
// 响应式数据
const familyList = ref([]);
const userProfile = ref(null); // 用户资料信息
const isOver60 = ref(false); // 是否大于60岁
// 成员管理相关数据
const showMemberPopup = ref(false);
......@@ -261,61 +258,19 @@ const selectedMembers = ref([]);
const currentMembers = ref([]);
/**
* 计算年龄
* @param {string} birthDate - 生日日期字符串
* @returns {number} 年龄
*/
const calculateAge = (birthDate) => {
if (!birthDate) return 0;
const birth = new Date(birthDate);
const today = new Date();
let age = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age--;
}
return age;
};
/**
* 初始化页面数据
*/
const canAddFamily = ref(false);
const initPageData = async () => {
// 获取家庭列表
const { code, data } = await getMyFamiliesAPI();
if (code) {
familyList.value = data?.families || [];
console.warn(data);
}
// 获取用户资料
const userRes = await getUserProfileAPI();
if (userRes.code && userRes.data?.user) {
userProfile.value = userRes.data.user;
const age = calculateAge(userRes.data.user.birth_date);
isOver60.value = age > 60;
console.log('用户年龄:', age, '是否大于60岁:', isOver60.value);
canAddFamily.value = data?.can_add || false;
}
};
// 如果家庭列表存在时, 才显示加入新家庭的按钮
const isShowBtn = computed(() => {
return familyList.value.length > 0;
});
// 判断用户是否创建过家庭
const hasCreatedFamily = computed(() => {
return familyList.value.some(family => family.is_my);
});
// 判断是否显示双按钮(创建家庭 + 加入家庭)
const showDoubleButtons = computed(() => {
return isOver60.value && !hasCreatedFamily.value;
});
/**
* 切换到指定家庭
* @param {number} familyId - 家庭ID
......