miniprogramme/pages/edit/edit.js
2025-09-12 16:08:17 +08:00

622 lines
No EOL
17 KiB
JavaScript

const app = getApp();
const config = require('../../config/config.js');
const apiClient = require('../../utils/api-client.js');
const imageCacheManager = require('../../utils/image-cache-manager.js');
Page({
data: {
userInfo: {
avatar: '',
nickname: '未设置昵称',
customId: '123456789',
signature: '',
career: '',
education: '',
gender: '',
birthday: '',
hometown: '',
constellation: '',
height: '',
personalityType: '',
sleepHabit: '',
socialActivity: ''
},
isEditingNickname: false,
isEditingSignature: false,
tempNickname: '',
tempSignature: '',
showConstellationPicker: false,
showPersonalityPicker: false,
showCareerPicker: false,
showEducationPicker: false,
showHometownPicker: false,
showBirthdayPicker: false,
showHeightPicker: false,
showGenderPicker: false,
showSleepHabitPicker: false,
showSocialActivityPicker: false,
constellations: ['水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座'],
personalityTypes: ['INTJ', 'INTP', 'ENTJ', 'INFP', 'ENTP', 'INFJ', 'ENFP', 'ENFJ', 'ISTJ', 'ISFJ', 'ISTP', 'ISFP', 'ESTJ', 'ESFJ', 'ESTP', 'ESFP'],
careers: ['初中生', '高中生', '大学生', '研究生', '留学生', '科研', '警察', '医生', '护士', '程序员', '老师', '化妆师', '摄影师', '音乐', '美术', '金融', '厨师', '工程师', '公务员', '互联网', '产品经理', '模特', '演员', '导演', '律师', '创业者', '其他'],
educations: ['北京大学', '清华大学', '复旦大学', '上海交通大学', '浙江大学', '南京大学', '武汉大学', '中山大学', '四川大学', '哈尔滨工业大学', '大专', '中专', '高职', '高中'],
genders: ['男', '女'],
sleepHabits: ['早起鸟儿', '夜猫子', '规律型', '深度睡眠追求者', '碎片化睡眠者', '失眠困扰者', '咖啡因敏感型', '数字戒断者', '运动调节型', '挑战打卡型', '鼾声监测者', '生物钟调节者', '社区分享型'],
socialActivities: ['内容创作者', '观察者', '吃瓜者', '潜水者', '机器人', '社群型用户', 'KOL', 'KOC', '普通用户', '算法依赖型用户', '事件驱动型用户', '季节性活跃用户', '社交维系型用户', '兴趣社群型用户', '职业网络型用户', '娱乐消遣型用户', '购物种草型用户', '互动型用户'],
selectedConstellation: '',
selectedPersonality: '',
selectedCareer: '',
selectedEducation: '',
selectedGender: '',
selectedHeight: 170,
selectedSleepHabit: '',
selectedSocialActivity: '',
searchCareerText: '',
searchEducationText: '',
filteredCareers: [],
filteredEducations: [],
provinces: [],
cities: [],
selectedProvince: '',
selectedCity: '',
selectedYear: '',
selectedMonth: '',
selectedDay: '',
years: [],
months: [],
days: []
},
onLoad: function() {
this.loadUserData();
this.initDatePicker();
this.initLocationData();
},
loadUserData: function() {
const userInfo = app.globalData.userInfo || {};
this.setData({
userInfo: {
avatar: userInfo.avatar || '',
nickname: userInfo.nickname || '未设置昵称',
customId: userInfo.customId || '123456789',
signature: userInfo.signature || '',
career: userInfo.career || '',
education: userInfo.education || '',
gender: userInfo.gender || '',
birthday: userInfo.birthday || '',
hometown: userInfo.hometown || '',
constellation: userInfo.constellation || '',
height: userInfo.height || '',
personalityType: userInfo.personalityType || '',
sleepHabit: userInfo.sleepHabit || '',
socialActivity: userInfo.socialActivity || ''
},
tempNickname: userInfo.nickname || '未设置昵称',
tempSignature: userInfo.signature || ''
});
},
// 头像相关功能
changeAvatar: function() {
wx.showActionSheet({
itemList: ['拍照', '从相册选择'],
success: (res) => {
const sourceType = res.tapIndex === 0 ? ['camera'] : ['album'];
this.chooseImage(sourceType);
}
});
},
chooseImage: function(sourceType) {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: sourceType,
success: (res) => {
if (sourceType[0] === 'camera') {
this.setData({
tempAvatarPath: res.tempFilePaths[0],
showCameraPreview: true
});
} else {
this.uploadAvatar(res.tempFilePaths[0]);
}
}
});
},
retakePhoto: function() {
this.setData({ showCameraPreview: false });
this.chooseImage(['camera']);
},
usePhoto: function() {
this.uploadAvatar(this.data.tempAvatarPath);
this.setData({ showCameraPreview: false });
},
uploadAvatar: async function(tempFilePath) {
try {
wx.showLoading({ title: '上传中...' });
// 模拟上传头像
await new Promise(resolve => setTimeout(resolve, 1000));
const newAvatarUrl = tempFilePath;
const userInfo = this.data.userInfo;
userInfo.avatar = newAvatarUrl;
this.setData({ userInfo });
// 更新全局用户信息
if (app.globalData.userInfo) {
app.globalData.userInfo.avatar = newAvatarUrl;
}
wx.hideLoading();
wx.showToast({ title: '头像更新成功', icon: 'success' });
} catch (error) {
wx.hideLoading();
wx.showToast({ title: '上传失败', icon: 'none' });
}
},
// 昵称编辑
startEditNickname: function() {
this.setData({
isEditingNickname: true,
tempNickname: this.data.userInfo.nickname
});
},
confirmEditNickname: function() {
if (this.data.tempNickname.length > 30) {
wx.showToast({ title: '昵称不能超过30字节', icon: 'none' });
return;
}
const userInfo = this.data.userInfo;
userInfo.nickname = this.data.tempNickname;
this.setData({
userInfo: userInfo,
isEditingNickname: false
});
// 更新全局用户信息
if (app.globalData.userInfo) {
app.globalData.userInfo.nickname = this.data.tempNickname;
}
},
cancelEditNickname: function() {
this.setData({ isEditingNickname: false });
},
// 签名编辑
startEditSignature: function() {
this.setData({
isEditingSignature: true,
tempSignature: this.data.userInfo.signature
});
},
confirmEditSignature: function() {
if (this.data.tempSignature.length > 200) {
wx.showToast({ title: '简介不能超过200字节', icon: 'none' });
return;
}
const userInfo = this.data.userInfo;
userInfo.signature = this.data.tempSignature;
this.setData({
userInfo: userInfo,
isEditingSignature: false
});
// 更新全局用户信息
if (app.globalData.userInfo) {
app.globalData.userInfo.signature = this.data.tempSignature;
}
},
cancelEditSignature: function() {
this.setData({ isEditingSignature: false });
},
// 星座选择
openConstellationPicker: function() {
this.setData({
showConstellationPicker: true,
selectedConstellation: this.data.userInfo.constellation
});
},
selectConstellation: function(e) {
const constellation = e.currentTarget.dataset.value;
this.setData({
selectedConstellation: constellation
});
},
confirmConstellation: function() {
const userInfo = this.data.userInfo;
userInfo.constellation = this.data.selectedConstellation;
this.setData({
userInfo: userInfo,
showConstellationPicker: false
});
},
// 人格类型选择
openPersonalityPicker: function() {
this.setData({
showPersonalityPicker: true,
selectedPersonality: this.data.userInfo.personalityType
});
},
selectPersonality: function(e) {
const personality = e.currentTarget.dataset.value;
this.setData({
selectedPersonality: personality
});
},
confirmPersonality: function() {
const userInfo = this.data.userInfo;
userInfo.personalityType = this.data.selectedPersonality;
this.setData({
userInfo: userInfo,
showPersonalityPicker: false
});
},
// 职业选择
openCareerPicker: function() {
this.setData({
showCareerPicker: true,
searchCareerText: '',
filteredCareers: this.data.careers
});
},
searchCareer: function(e) {
const text = e.detail.value;
const filtered = this.data.careers.filter(career =>
career.includes(text)
);
this.setData({
searchCareerText: text,
filteredCareers: filtered
});
},
selectCareer: function(e) {
const career = e.currentTarget.dataset.value;
const userInfo = this.data.userInfo;
userInfo.career = career;
this.setData({
userInfo: userInfo,
showCareerPicker: false
});
},
// 教育背景选择
openEducationPicker: function() {
this.setData({
showEducationPicker: true,
searchEducationText: '',
filteredEducations: this.data.educations
});
},
searchEducation: function(e) {
const text = e.detail.value;
const filtered = this.data.educations.filter(edu =>
edu.includes(text)
);
this.setData({
searchEducationText: text,
filteredEducations: filtered
});
},
selectEducation: function(e) {
const education = e.currentTarget.dataset.value;
const userInfo = this.data.userInfo;
userInfo.education = education;
this.setData({
userInfo: userInfo,
showEducationPicker: false
});
},
// 家乡选择
openHometownPicker: function() {
this.setData({
showHometownPicker: true,
selectedProvince: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[0] : '',
selectedCity: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[1] : ''
});
},
confirmHometown: function() {
const userInfo = this.data.userInfo;
userInfo.hometown = `${this.data.selectedProvince} ${this.data.selectedCity}`;
this.setData({
userInfo: userInfo,
showHometownPicker: false
});
},
// 生日选择
openBirthdayPicker: function() {
this.setData({
showBirthdayPicker: true
});
},
confirmBirthday: function() {
const userInfo = this.data.userInfo;
userInfo.birthday = `${this.data.selectedYear}-${this.data.selectedMonth.toString().padStart(2, '0')}-${this.data.selectedDay.toString().padStart(2, '0')}`;
this.setData({
userInfo: userInfo,
showBirthdayPicker: false
});
},
// 身高选择
openHeightPicker: function() {
this.setData({
showHeightPicker: true,
selectedHeight: this.data.userInfo.height ? parseInt(this.data.userInfo.height) : 170
});
},
// 性别选择
openGenderPicker: function() {
this.setData({
showGenderPicker: true,
selectedGender: this.data.userInfo.gender
});
},
// 睡眠习惯选择
openSleepHabitPicker: function() {
this.setData({
showSleepHabitPicker: true,
selectedSleepHabit: this.data.userInfo.sleepHabit
});
},
// 社交活跃度选择
openSocialActivityPicker: function() {
this.setData({
showSocialActivityPicker: true,
selectedSocialActivity: this.data.userInfo.socialActivity
});
},
// 初始化位置数据
initLocationData: function() {
// 模拟省市数据
this.setData({
provinces: ['北京市', '上海市', '广东省', '江苏省', '浙江省'],
cities: {
'北京市': ['北京市'],
'上海市': ['上海市'],
'广东省': ['广州市', '深圳市', '珠海市'],
'江苏省': ['南京市', '苏州市', '无锡市'],
'浙江省': ['杭州市', '宁波市', '温州市']
},
selectedProvince: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[0] : '',
selectedCity: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[1] : '',
hometownValue: [0, 0] // 默认选中第一项
});
},
// 家乡选择变化处理
onHometownChange: function(e) {
const value = e.detail.value;
const province = this.data.provinces[value[0]];
const city = this.data.cities[province][value[1]];
this.setData({
selectedProvince: province,
selectedCity: city,
hometownValue: value
});
},
initDatePicker: function() {
const years = [];
const currentYear = new Date().getFullYear();
for (let i = currentYear; i >= 1950; i--) {
years.push(i);
}
const months = [];
for (let i = 1; i <= 12; i++) {
months.push(i);
}
const days = [];
for (let i = 1; i <= 31; i++) {
days.push(i);
}
// 设置默认日期
let defaultYear = currentYear - 20;
let defaultMonth = 1;
let defaultDay = 1;
if (this.data.userInfo.birthday) {
const parts = this.data.userInfo.birthday.split('-');
if (parts.length === 3) {
defaultYear = parseInt(parts[0]);
defaultMonth = parseInt(parts[1]);
defaultDay = parseInt(parts[2]);
}
}
// 计算默认值的索引
const yearIndex = years.indexOf(defaultYear);
const monthIndex = months.indexOf(defaultMonth);
const dayIndex = days.indexOf(defaultDay);
this.setData({
years: years,
months: months,
days: days,
selectedYear: defaultYear,
selectedMonth: defaultMonth,
selectedDay: defaultDay,
birthdayValue: [yearIndex, monthIndex, dayIndex]
});
},
// 生日选择变化处理
onBirthdayChange: function(e) {
const value = e.detail.value;
const year = this.data.years[value[0]];
const month = this.data.months[value[1]];
const day = this.data.days[value[2]];
this.setData({
selectedYear: year,
selectedMonth: month,
selectedDay: day,
birthdayValue: value
});
},
bindProvinceChange: function(e) {
const province = this.data.provinces[e.detail.value];
this.setData({
selectedProvince: province,
selectedCity: this.data.cities[province][0]
});
},
bindCityChange: function(e) {
this.setData({
selectedCity: this.data.cities[this.data.selectedProvince][e.detail.value]
});
},
bindYearChange: function(e) {
this.setData({
selectedYear: this.data.years[e.detail.value]
});
},
bindMonthChange: function(e) {
this.setData({
selectedMonth: this.data.months[e.detail.value]
});
},
bindDayChange: function(e) {
this.setData({
selectedDay: this.data.days[e.detail.value]
});
},
adjustHeight: function(e) {
this.setData({
selectedHeight: e.detail.value
});
},
selectGender: function(e) {
const gender = e.currentTarget.dataset.value;
const userInfo = this.data.userInfo;
userInfo.gender = gender;
this.setData({
userInfo: userInfo,
showHeightGenderPicker: false
});
},
confirmHeight: function() {
const userInfo = this.data.userInfo;
userInfo.height = this.data.selectedHeight;
this.setData({
userInfo: userInfo,
showHeightPicker: false
});
},
// 睡眠习惯选择处理
// 社交活跃度选择处理
selectSleepHabit: function(e) {
const sleepHabit = e.currentTarget.dataset.value;
const userInfo = this.data.userInfo;
userInfo.sleepHabit = sleepHabit;
this.setData({
userInfo: userInfo,
showSleepHabitPicker: false
});
},
selectSocialActivity: function(e) {
const socialActivity = e.currentTarget.dataset.value;
const userInfo = this.data.userInfo;
userInfo.socialActivity = socialActivity;
this.setData({
userInfo: userInfo,
showSocialActivityPicker: false
});
},
// 身高选择确认
confirmHeight: function() {
const userInfo = this.data.userInfo;
userInfo.height = this.data.selectedHeight;
this.setData({
userInfo: userInfo,
showHeightPicker: false
});
},
// 性别选择确认
selectGender: function(e) {
const gender = e.currentTarget.dataset.value;
const userInfo = this.data.userInfo;
userInfo.gender = gender;
this.setData({
userInfo: userInfo,
showGenderPicker: false
});
},
// 关闭所有弹出层
closeAllPickers: function() {
this.setData({
showConstellationPicker: false,
showPersonalityPicker: false,
showCareerPicker: false,
showEducationPicker: false,
showHometownPicker: false,
showBirthdayPicker: false,
showHeightPicker: false,
showGenderPicker: false,
showSleepHabitPicker: false,
showSocialActivityPicker: false,
showCameraPreview: false
});
},
// 保存所有修改
saveChanges: function() {
// 这里可以添加保存到服务器的逻辑
wx.showLoading({ title: '保存中...' });
setTimeout(() => {
wx.hideLoading();
wx.showToast({ title: '资料保存成功', icon: 'success' });
// 返回个人资料页面
wx.navigateBack();
}, 1000);
}
});