"use strict"; const common_vendor = require("../../common/vendor.js"); const utils_api = require("../../utils/api.js"); const defaultAvatar = "https://picsum.photos/seed/avatar/200/200"; const _sfc_main = { data() { return { userInfo: { id: null, username: "", nickname: "", avatar: defaultAvatar, gender: "", birthday: "", bio: "", phone: "", email: "" }, isLogin: false, isSaving: false }; }, onLoad() { this.loadUserInfo(); }, onShow() { const tempBio = common_vendor.index.getStorageSync("temp_text_bio"); if (typeof tempBio === "string") { this.userInfo.bio = tempBio; common_vendor.index.removeStorageSync("temp_text_bio"); const storedUser = common_vendor.index.getStorageSync("userInfo"); if (storedUser) { storedUser.bio = tempBio; common_vendor.index.setStorageSync("userInfo", storedUser); } return; } this.loadUserInfo(true); }, methods: { async loadUserInfo(forceRemote = false) { try { const storedUser = common_vendor.index.getStorageSync("userInfo") || null; const isLogin = common_vendor.index.getStorageSync("isLogin"); if (!storedUser || !storedUser.id || !isLogin) { this.isLogin = false; this.userInfo = { ...this.userInfo, id: null, username: "", nickname: "", avatar: defaultAvatar, gender: "", birthday: "", bio: "", phone: "", email: "" }; return; } this.isLogin = true; this.userInfo = { ...this.userInfo, ...storedUser, avatar: storedUser.avatar || defaultAvatar }; if (!forceRemote && this.userInfo.nickname) { return; } const res = await utils_api.getUserProfile(storedUser.id); if (res && res.code === 200 && res.data) { this.userInfo = { ...this.userInfo, ...res.data, avatar: res.data.avatar || defaultAvatar }; common_vendor.index.setStorageSync("userInfo", { ...storedUser, ...res.data }); } } catch (e) { common_vendor.index.__f__("error", "at pages/edit-profile/edit-profile.vue:154", "加载用户资料失败:", e); common_vendor.index.showToast({ title: "加载失败,请重试", icon: "none" }); } }, goBack() { common_vendor.index.navigateBack(); }, async handleSave() { try { if (!this.isLogin || !this.userInfo.id) { common_vendor.index.showToast({ title: "请先登录", icon: "none" }); return; } if (this.isSaving) return; this.isSaving = true; common_vendor.index.showLoading({ title: "保存中..." }); const payload = { userId: this.userInfo.id, nickname: this.userInfo.nickname || "", avatar: this.userInfo.avatar === defaultAvatar ? "" : this.userInfo.avatar || "", gender: this.userInfo.gender || "", birthday: this.userInfo.birthday || "", bio: this.userInfo.bio || "", phone: this.userInfo.phone || "", email: this.userInfo.email || "" }; const res = await utils_api.updateUserProfile(payload); common_vendor.index.hideLoading(); this.isSaving = false; if (res && res.code === 200) { const merged = { ...this.userInfo, ...res.data, avatar: res.data.avatar || this.userInfo.avatar || defaultAvatar }; this.userInfo = merged; common_vendor.index.setStorageSync("userInfo", merged); common_vendor.index.showToast({ title: "保存成功", icon: "success" }); setTimeout(() => common_vendor.index.navigateBack(), 800); } else { common_vendor.index.showToast({ title: res && res.message || "保存失败", icon: "none" }); } } catch (e) { common_vendor.index.hideLoading(); this.isSaving = false; common_vendor.index.__f__("error", "at pages/edit-profile/edit-profile.vue:202", "保存用户资料失败:", e); common_vendor.index.showToast({ title: "网络错误,请稍后再试", icon: "none" }); } }, handleAvatarClick() { common_vendor.index.chooseImage({ count: 1, sizeType: ["compressed"], sourceType: ["album", "camera"], success: (res) => { const tempFilePath = res.tempFilePaths[0]; this.userInfo.avatar = tempFilePath; common_vendor.index.showToast({ title: "头像已更新", icon: "success" }); } }); }, handleNicknameClick() { common_vendor.index.showModal({ title: "修改昵称", editable: true, placeholderText: "请输入昵称", content: this.userInfo.nickname, success: (res) => { if (res.confirm && res.content) { this.userInfo.nickname = res.content.trim(); } } }); }, handleGenderClick() { if (!this.isLogin) { this.goToLogin(); return; } common_vendor.index.showActionSheet({ itemList: ["男", "女", "保密"], success: (res) => { const genders = ["男", "女", "保密"]; this.userInfo.gender = genders[res.tapIndex]; } }); }, handleBirthdayClick() { if (!this.isLogin) { this.goToLogin(); return; } common_vendor.index.showModal({ title: "设置生日", editable: true, placeholderText: "格式:YYYY-MM-DD", content: this.userInfo.birthday || "", success: (modalRes) => { if (modalRes.confirm) { const value = (modalRes.content || "").trim(); const ok = /^\d{4}-\d{2}-\d{2}$/.test(value); if (!value || ok) { this.userInfo.birthday = value; } else { common_vendor.index.showToast({ title: "日期格式应为YYYY-MM-DD", icon: "none" }); } } } }); }, handleBioClick() { if (!this.isLogin) { this.goToLogin(); return; } const bioValue = this.userInfo.bio || ""; common_vendor.index.navigateTo({ url: `/pages/text-edit/text-edit?key=bio&value=${encodeURIComponent(bioValue)}&placeholder=${encodeURIComponent("请输入个人简介")}&title=${encodeURIComponent("个人简介")}` }); }, goToLogin() { common_vendor.index.navigateTo({ url: "/pages/login/login" }); } } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)), b: common_vendor.o((...args) => $options.handleSave && $options.handleSave(...args)), c: $data.userInfo.avatar, d: common_vendor.o((...args) => $options.handleAvatarClick && $options.handleAvatarClick(...args)), e: common_vendor.t($data.userInfo.nickname), f: common_vendor.o((...args) => $options.handleNicknameClick && $options.handleNicknameClick(...args)), g: common_vendor.t($data.userInfo.gender || "未设置"), h: common_vendor.o((...args) => $options.handleGenderClick && $options.handleGenderClick(...args)), i: common_vendor.t($data.userInfo.birthday || "未设置"), j: common_vendor.o((...args) => $options.handleBirthdayClick && $options.handleBirthdayClick(...args)), k: common_vendor.t($data.userInfo.bio || "未设置"), l: common_vendor.o((...args) => $options.handleBioClick && $options.handleBioClick(...args)), m: !$data.isLogin }, !$data.isLogin ? { n: common_vendor.o((...args) => $options.goToLogin && $options.goToLogin(...args)) } : {}); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-c0f45e44"]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/edit-profile/edit-profile.js.map