189 lines
5.9 KiB
JavaScript
189 lines
5.9 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const modules_api_AppServer = require("../../modules/api/AppServer.js");
|
|
const utils_auth = require("../../utils/auth.js");
|
|
const modules_Config = require("../../modules/Config.js");
|
|
const _sfc_main = {
|
|
data() {
|
|
return {
|
|
statusBarHeight: 0,
|
|
// 状态栏高度
|
|
userInfo: {},
|
|
// 用户信息
|
|
form: {
|
|
avatar: "",
|
|
// 头像URL
|
|
nickname: ""
|
|
// 昵称
|
|
},
|
|
saving: false
|
|
// 保存中状态
|
|
};
|
|
},
|
|
/**
|
|
* 页面加载时执行
|
|
*/
|
|
onLoad() {
|
|
const systemInfo = common_vendor.index.getSystemInfoSync();
|
|
this.statusBarHeight = systemInfo.statusBarHeight || 0;
|
|
this.loadUserInfo();
|
|
},
|
|
methods: {
|
|
/**
|
|
* 加载用户信息
|
|
* 先从本地缓存读取,再从服务器获取最新数据
|
|
*/
|
|
loadUserInfo() {
|
|
const localUserInfo = utils_auth.getUserInfo();
|
|
if (localUserInfo) {
|
|
this.userInfo = localUserInfo;
|
|
this.form = {
|
|
avatar: modules_Config.Config.getImageUrl(localUserInfo.avatar) || "",
|
|
nickname: localUserInfo.nickname || ""
|
|
};
|
|
}
|
|
this.fetchUserProfile();
|
|
},
|
|
/**
|
|
* 从服务器获取用户信息
|
|
*/
|
|
async fetchUserProfile() {
|
|
try {
|
|
const appserver = new modules_api_AppServer.AppServer();
|
|
const response = await appserver.GetUserProfile();
|
|
if (response.code === 0 && response.data) {
|
|
this.userInfo = response.data;
|
|
this.form = {
|
|
avatar: modules_Config.Config.getImageUrl(response.data.avatar) || "",
|
|
nickname: response.data.nickname || ""
|
|
};
|
|
utils_auth.saveUserInfo(response.data);
|
|
}
|
|
} catch (error) {
|
|
common_vendor.index.__f__("error", "at pages/me/profile-edit-page.vue:134", "获取用户信息失败:", error);
|
|
}
|
|
},
|
|
/**
|
|
* 选择头像
|
|
* 调用系统相册或相机选择图片
|
|
*/
|
|
chooseAvatar() {
|
|
common_vendor.index.chooseImage({
|
|
count: 1,
|
|
// 只选择一张
|
|
sizeType: ["compressed"],
|
|
// 压缩图片
|
|
sourceType: ["album", "camera"],
|
|
// 相册和相机都可以
|
|
success: (res) => {
|
|
const tempFilePath = res.tempFilePaths[0];
|
|
this.uploadAvatar(tempFilePath);
|
|
}
|
|
});
|
|
},
|
|
/**
|
|
* 上传头像到服务器
|
|
* @param {String} filePath - 本地图片路径
|
|
*/
|
|
async uploadAvatar(filePath) {
|
|
common_vendor.index.showLoading({
|
|
title: "上传中...",
|
|
mask: true
|
|
});
|
|
try {
|
|
const appserver = new modules_api_AppServer.AppServer();
|
|
const response = await appserver.UploadImage(filePath);
|
|
if (response.code === 0 && response.data) {
|
|
this.form.avatar = modules_Config.Config.getImageUrl(response.data.url);
|
|
common_vendor.index.showToast({
|
|
title: "头像上传成功",
|
|
icon: "success"
|
|
});
|
|
} else {
|
|
throw new Error(response.message || "上传失败");
|
|
}
|
|
} catch (error) {
|
|
common_vendor.index.__f__("error", "at pages/me/profile-edit-page.vue:179", "上传头像失败:", error);
|
|
common_vendor.index.showToast({
|
|
title: "上传失败",
|
|
icon: "none"
|
|
});
|
|
} finally {
|
|
common_vendor.index.hideLoading();
|
|
}
|
|
},
|
|
/**
|
|
* 保存用户信息
|
|
*/
|
|
async handleSave() {
|
|
if (!this.form.nickname || !this.form.nickname.trim()) {
|
|
common_vendor.index.showToast({
|
|
title: "请输入昵称",
|
|
icon: "none"
|
|
});
|
|
return;
|
|
}
|
|
this.saving = true;
|
|
try {
|
|
const appserver = new modules_api_AppServer.AppServer();
|
|
const updateData = {
|
|
nickname: this.form.nickname.trim(),
|
|
avatar: this.form.avatar
|
|
};
|
|
const response = await appserver.UpdateUserProfile(updateData);
|
|
if (response.code === 0) {
|
|
const updatedUserInfo = {
|
|
...this.userInfo,
|
|
...updateData
|
|
};
|
|
utils_auth.saveUserInfo(updatedUserInfo);
|
|
common_vendor.index.showToast({
|
|
title: "保存成功",
|
|
icon: "success"
|
|
});
|
|
setTimeout(() => {
|
|
this.goBack();
|
|
}, 1e3);
|
|
} else {
|
|
throw new Error(response.message || "保存失败");
|
|
}
|
|
} catch (error) {
|
|
common_vendor.index.__f__("error", "at pages/me/profile-edit-page.vue:234", "保存失败:", error);
|
|
common_vendor.index.showToast({
|
|
title: error.message || "保存失败",
|
|
icon: "none"
|
|
});
|
|
} finally {
|
|
this.saving = false;
|
|
}
|
|
},
|
|
/**
|
|
* 返回上一页
|
|
*/
|
|
goBack() {
|
|
common_vendor.index.navigateBack();
|
|
}
|
|
}
|
|
};
|
|
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
return {
|
|
a: $data.statusBarHeight + "px",
|
|
b: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
|
|
c: common_vendor.t(_ctx.$t("profile.title") || "个人资料"),
|
|
d: $data.form.avatar || "/static/default-avatar.png",
|
|
e: common_vendor.o((...args) => $options.chooseAvatar && $options.chooseAvatar(...args)),
|
|
f: common_vendor.t(_ctx.$t("profile.nickname") || "昵称"),
|
|
g: _ctx.$t("profile.nicknamePlaceholder") || "请输入昵称",
|
|
h: $data.form.nickname,
|
|
i: common_vendor.o(($event) => $data.form.nickname = $event.detail.value),
|
|
j: common_vendor.t($data.userInfo.uid || "-"),
|
|
k: common_vendor.t(_ctx.$t("common.save") || "保存"),
|
|
l: common_vendor.o((...args) => $options.handleSave && $options.handleSave(...args)),
|
|
m: $data.saving,
|
|
n: $data.saving
|
|
};
|
|
}
|
|
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-ffabd4f6"]]);
|
|
wx.createPage(MiniProgramPage);
|
|
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/me/profile-edit-page.js.map
|