hku-class/miniprogram/pages/profile-edit/index.js
2026-05-13 00:36:39 +08:00

72 lines
1.7 KiB
JavaScript

const { put } = require("../../utils/api");
const { refreshMe, saveSession } = require("../../utils/auth");
const { showError } = require("../../utils/page-helpers");
Page({
data: {
industryOptions: [
"金融",
"科技",
"咨询",
"医疗健康",
"教育",
"房地产",
"制造业",
"消费零售",
"能源",
"传媒",
"政府/公共事业",
"其他"
],
form: {
name: "",
industry: "",
company: "",
position: "",
wechat_id: "",
bio: ""
},
loading: false
},
async onLoad() {
wx.setNavigationBarTitle({ title: "编辑资料" });
const user = await refreshMe();
this.setData({
form: {
name: user.name || "",
industry: user.industry || "",
company: user.company || "",
position: user.position || "",
wechat_id: user.wechat_id || "",
bio: user.bio || ""
}
});
},
onInput(event) {
const field = event.currentTarget.dataset.field;
this.setData({ [`form.${field}`]: event.detail.value });
},
onIndustryChange(event) {
const index = Number(event.detail.value);
this.setData({ "form.industry": this.data.industryOptions[index] });
},
async save() {
this.setData({ loading: true });
try {
const user = await put("/api/users/me", this.data.form);
const token = wx.getStorageSync("auth_token");
saveSession(token, user);
wx.showToast({ title: "已保存", icon: "success" });
setTimeout(() => wx.navigateBack(), 500);
} catch (error) {
showError(error, "保存失败");
} finally {
this.setData({ loading: false });
}
}
});