index.vue
15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<template>
<view class="min-h-screen flex flex-col bg-white">
<view class="flex-1 px-4 pt-3 pb-6 flex flex-col">
<!-- Title -->
<h2 class="text-xl font-bold text-center mb-2">
输入家训口令
</h2>
<!-- Description -->
<view class="text-gray-600 text-left text-sm mb-6">
请输入家人提供的家训口令,加入家庭一起参与健康挑战
</view>
<!-- Input boxes -->
<view class="motto-input-container">
<view
v-for="(char, index) in mottoChars"
:key="index"
class="motto-input-box"
:style="{
borderColor: focusedIndex === index ? THEME_COLORS.PRIMARY : '#d1d5db'
}"
>
<input
:ref="(el) => (inputRefs[index] = el)"
type="text"
v-model="mottoChars[index]"
@input="(e) => handleInputChange(index, e.target.value)"
@keydown="(e) => handleKeyDown(index, e)"
@focus="focusedIndex = index"
@blur="handleBlur(index)"
class="motto-input"
:cursorSpacing="100"
/>
</view>
</view>
<!-- Help text -->
<view class="text-gray-500 text-center text-sm mb-4">
没有口令?请联系您的大家长获取
</view>
<!-- Role selection -->
<view class="mb-6">
<h3 class="identity-title">
选择您的身份
</h3>
<view class="flex gap-2 flex-wrap">
<view
v-for="role in familyRoles"
:key="role.id"
@tap="selectedRole = role.id"
:class="[
'w-[calc(49%-4rpx)] py-3 rounded-lg border text-center flex flex-col items-center gap-1',
selectedRole === role.id
? 'border-blue-500 bg-blue-50 text-blue-500'
: 'border-gray-200 text-gray-700'
]"
>
<IconFont :name="role.type" size="25" />
<span class="text-sm">{{ role.label }}</span>
</view>
</view>
</view>
<!-- Submit Button -->
<view
@tap="handleJoinFamily"
:disabled="!isComplete"
:class="[
'w-full py-3 text-white text-lg font-medium rounded-lg mt-auto text-center',
isComplete ? 'bg-blue-500' : 'bg-gray-300'
]"
>
加入家庭
</view>
</view>
<!-- 家庭选择弹窗 -->
<nut-popup
v-model:visible="showFamilySelector"
position="bottom"
:style="{ height: '80vh' }"
round
closeable
@close="closeFamilySelector"
>
<view class="family-selector-container h-full flex flex-col">
<!-- 标题 -->
<view class="text-lg font-bold text-center py-4 border-b border-gray-100 flex-shrink-0">
选择要加入的家庭
</view>
<!-- 搜索框 -->
<view class="p-4 flex-shrink-0">
<nut-searchbar
v-model="searchKeyword"
placeholder="搜索家庭名称"
@search="handleSearch"
@clear="handleClearSearch"
/>
</view>
<!-- 家庭列表 -->
<view class="flex-1 px-4 pb-4 overflow-hidden">
<view
ref="familyListContainer"
class="h-full space-y-3 overflow-y-auto"
>
<view
v-for="family in filteredFamilies"
:key="family.id"
@tap="selectFamily(family.id)"
:class="[
'family-item p-4 border rounded-lg flex items-center space-x-3',
family.is_kicked
? 'border-gray-300 bg-gray-100 opacity-60 cursor-not-allowed'
: selectedFamilyId === family.id
? 'border-blue-500 bg-blue-50'
: 'border-gray-200 bg-white'
]"
>
<!-- 家庭头像 -->
<view class="w-12 h-12 rounded-full bg-gray-200 flex items-center justify-center overflow-hidden">
<image
:src="family.avatar_url || defaultAvatar"
class="w-full h-full object-cover"
/>
</view>
<!-- 家庭信息 -->
<view class="flex-1">
<view class="font-medium text-gray-900 mb-1">
{{ family.name }}
</view>
<text v-if="family.is_kicked" class="text-xs text-red-500">被大家长移除后无法再加入</text>
<view class="text-sm text-gray-600 line-clamp-2">{{ family.note }}</view>
</view>
<!-- 选中状态 -->
<view v-if="selectedFamilyId === family.id" class="text-blue-500">
<Check size="20" />
</view>
</view>
<!-- 加载更多按钮 -->
<view v-if="hasMoreData && !searchKeyword" class="text-center py-4">
<nut-button
@click="loadMoreFamilies"
:loading="isLoadingMore"
type="default"
size="small"
plain
>
{{ isLoadingMore ? '加载中...' : '加载更多' }}
</nut-button>
</view>
<!-- 没有更多数据提示 -->
<view v-if="!hasMoreData && totalFamilies.length > 0 && !searchKeyword" class="text-center py-4 text-gray-500 text-sm">
没有更多家庭了
</view>
</view>
</view>
<!-- 底部按钮 -->
<view class="flex gap-3 p-4 border-t border-gray-100 flex-shrink-0">
<nut-button
@click="closeFamilySelector"
class="flex-1"
type="default"
size="large"
plain
>
关闭
</nut-button>
<nut-button
@click="confirmJoinFamily"
class="flex-1"
:color="selectedFamilyId ? THEME_COLORS.PRIMARY : 'gray'"
:disabled="!selectedFamilyId"
size="large"
>
确认
</nut-button>
</view>
</view>
</nut-popup>
</view>
</template>
<script setup>
import { ref, computed, nextTick, onMounted, watch } from 'vue';
import Taro from '@tarojs/taro';
import { My, Check, IconFont } from '@nutui/icons-vue-taro';
// 获取接口信息
import { searchFamilyByPassphraseAPI, joinFamilyAPI } from '@/api/family';
// 导入主题颜色
import { THEME_COLORS } from '@/utils/config';
// 默认头像
const defaultAvatar = 'https://cdn.ipadbiz.cn/lls_prog/images/%E5%85%A8%E5%AE%B6%E7%A6%8F3_%E5%89%AF%E6%9C%AC.jpg?imageMogr2/strip/quality/60'
const mottoChars = ref(['', '', '', '']);
const selectedRole = ref('');
const inputRefs = ref([]);
const focusedIndex = ref(-1);
// 弹窗相关数据
const showFamilySelector = ref(false);
const searchKeyword = ref('');
const selectedFamilyId = ref('');
const mockFamilies = ref([]);
const familyListContainer = ref(null);
// 移除不再需要的familyListHeight变量,因为现在使用flexbox布局
// 分页相关数据
const currentPage = ref(0);
const pageSize = ref(10);
const hasMoreData = ref(true);
const isLoadingMore = ref(false);
const totalFamilies = ref([]);
const handleInputChange = (index, value) => {
// 允许输入多个字符,但只保留第一个有效字符(汉字、数字、大小写字母),兼容输入法
if (value) {
// 提取第一个有效字符(汉字、数字、大小写字母)
const firstChar = value.match(/[\u4e00-\u9fa5a-zA-Z0-9]/)?.[0] || '';
mottoChars.value[index] = firstChar;
// 如果输入了有效字符且不是最后一个输入框,自动聚焦下一个
if (firstChar && index < 3) {
focusedIndex.value = index + 1;
// 使用 nextTick 确保 DOM 更新后再聚焦
nextTick(() => {
if (inputRefs.value[index + 1]) {
inputRefs.value[index + 1].focus();
}
});
}
} else {
mottoChars.value[index] = '';
}
};
const handleKeyDown = (index, e) => {
if (e.key === 'Backspace' && !mottoChars.value[index] && index > 0) {
// 同样,在Taro中处理光标移动需要不同的方式
}
};
/**
* 处理输入框失焦事件
* @param {number} index - 输入框索引
*/
const handleBlur = (index) => {
// 重置焦点状态
focusedIndex.value = -1;
// 失焦时再次验证输入值,确保只保留有效字符(汉字、数字、大小写字母)
const currentValue = mottoChars.value[index];
if (currentValue) {
const firstChar = currentValue.match(/[\u4e00-\u9fa5a-zA-Z0-9]/)?.[0] || '';
mottoChars.value[index] = firstChar;
}
};
const familyRoles = [
{ id: '丈夫', label: '丈夫', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E7%88%B7%E7%88%B7.png' },
{ id: '妻子', label: '妻子', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E5%A5%B6%E5%A5%B6.png' },
{ id: '儿子', label: '儿子', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E7%94%B7%E8%81%8C%E5%91%98.png' },
{ id: '女儿', label: '女儿', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E5%A5%B3%E8%81%8C%E5%91%98.png' },
{ id: '女婿', label: '女婿', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E7%94%B7%E8%81%8C%E5%91%98.png' },
{ id: '儿媳', label: '儿媳', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E5%A5%B3%E8%81%8C%E5%91%98.png' },
{ id: '孙子', label: '孙子', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E7%94%B7%E5%AD%A9.png' },
{ id: '外孙', label: '外孙', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E7%94%B7%E5%AD%A9.png' },
{ id: '孙女', label: '孙女', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E5%A5%B3%E5%AD%A9.png' },
{ id: '外孙女', label: '外孙女', type: 'https://cdn.ipadbiz.cn/lls_prog/icon/create/%E5%A5%B3%E5%AD%A9.png' }
];
const isComplete = computed(() => {
return mottoChars.value.every((char) => char) && selectedRole.value;
});
// 过滤后的家庭列表
const filteredFamilies = computed(() => {
if (!searchKeyword.value) {
return mockFamilies.value
}
return mockFamilies.value.filter(family =>
family.name?.includes(searchKeyword.value) ||
family.description?.includes(searchKeyword.value)
)
});
// 处理搜索
const handleSearch = (value) => {
searchKeyword.value = value
// 搜索时重置选中的家庭
selectedFamilyId.value = ''
}
// 清除搜索
const handleClearSearch = () => {
searchKeyword.value = ''
// 清除搜索时重置选中的家庭
selectedFamilyId.value = ''
};
// 选择家庭
const selectFamily = (familyId) => {
// 查找对应的家庭信息
const family = mockFamilies.value.find(f => f.id === familyId);
// 如果家庭被禁用,则不允许选中
if (family && family.is_kicked) {
return;
}
selectedFamilyId.value = familyId
};
// 关闭家庭选择器
const closeFamilySelector = () => {
showFamilySelector.value = false
selectedFamilyId.value = ''
searchKeyword.value = ''
}
// 监听弹窗显示状态,重置选中状态
watch(showFamilySelector, async (newVal) => {
if (newVal) {
await nextTick()
// 移除高度计算逻辑,现在使用flexbox自动布局
}
})
// 监听搜索关键词变化,重置选中的家庭
watch(searchKeyword, () => {
selectedFamilyId.value = ''
})
// 确认加入家庭
const confirmJoinFamily = async () => {
if (!selectedFamilyId.value) {
Taro.showToast({
title: '请选择一个家庭',
icon: 'none'
})
return
}
const selectedFamily = mockFamilies.value.find(f => f.id === selectedFamilyId.value)
console.log('确认加入家庭:', selectedFamily)
const joinFamily = await joinFamilyAPI({
family_id: selectedFamily.id,
role: selectedRole.value
})
if (joinFamily.code) {
// 关闭弹窗
closeFamilySelector()
Taro.showToast({
title: '加入成功',
icon: 'success'
})
setTimeout(() => {
Taro.redirectTo({
url: '/pages/Dashboard/index'
})
}, 1500)
}
};
const handleJoinFamily = async () => {
if (!isComplete.value) return
const motto = mottoChars.value.join('')
try {
// 重置分页数据
currentPage.value = 0;
hasMoreData.value = true;
totalFamilies.value = [];
// 调用API查询家庭(第一页)
const { code, data } = await searchFamilyByPassphraseAPI({
passphrase: motto,
page: currentPage.value,
limit: pageSize.value
})
let families = [];
if (code) {
families = data;
totalFamilies.value = families;
// 检查是否还有更多数据
hasMoreData.value = families.length === pageSize.value;
console.log('查询家庭:', { motto, role: selectedRole.value, families, hasMore: hasMoreData.value })
if (families.length === 0) {
Taro.showToast({
title: '未找到匹配的家庭',
icon: 'none'
})
return
}
if (families.length === 1) {
// 只有一个家庭,检查是否被踢出
const family = families[0];
if (family.is_kicked) {
// 被踢出状态,显示选择弹窗让用户知道
showFamilySelector.value = true;
mockFamilies.value = totalFamilies.value;
} else {
// 未被踢出,直接加入
const joinFamily = await joinFamilyAPI({
family_id: family.id,
role: selectedRole.value
});
if (joinFamily.code) {
Taro.showToast({
title: '加入成功',
icon: 'success'
});
setTimeout(() => {
Taro.redirectTo({
url: '/pages/Dashboard/index'
});
}, 1500);
}
}
} else {
// 多个家庭,显示选择弹窗
showFamilySelector.value = true
mockFamilies.value = totalFamilies.value
}
}
} catch (error) {
console.error('加入家庭失败:', error)
Taro.showToast({
title: '加入失败,请重试',
icon: 'none'
})
}
};
// 加载更多家庭数据
const loadMoreFamilies = async () => {
if (isLoadingMore.value || !hasMoreData.value) return;
isLoadingMore.value = true;
try {
const motto = mottoChars.value.join('');
currentPage.value += 1;
const { code, data } = await searchFamilyByPassphraseAPI({
passphrase: motto,
page: currentPage.value,
limit: pageSize.value
});
if (code && data) {
// 合并新数据到现有数据
totalFamilies.value = [...totalFamilies.value, ...data];
// 检查是否还有更多数据
hasMoreData.value = data.length === pageSize.value;
// 更新mockFamilies用于显示
mockFamilies.value = totalFamilies.value;
console.log('加载更多家庭:', {
page: currentPage.value,
newCount: data.length,
totalCount: totalFamilies.value.length,
hasMore: hasMoreData.value
});
} else {
hasMoreData.value = false;
Taro.showToast({
title: '加载失败',
icon: 'none'
});
}
} catch (error) {
console.error('加载更多家庭失败:', error);
currentPage.value -= 1; // 回退页码
Taro.showToast({
title: '加载失败,请重试',
icon: 'none'
});
} finally {
isLoadingMore.value = false;
}
};
</script>
<style lang="less">
@import './index.less';
</style>