| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_auth = require("../../utils/auth.js");
- const utils_api = require("../../utils/api.js");
- const _sfc_main = {
- __name: "edit",
- setup(__props) {
- const avatarUrl = common_vendor.ref(utils_api.getImageUrl("/static/images/head.png"));
- const nickname = common_vendor.ref("");
- const phone = common_vendor.ref("");
- const originalAvatar = common_vendor.ref("");
- const originalNickname = common_vendor.ref("");
- const saving = common_vendor.ref(false);
- const handleBack = () => {
- const pages = getCurrentPages();
- pages.length > 1 ? common_vendor.index.navigateBack() : common_vendor.index.switchTab({ url: "/pages/mine/mine" });
- };
- common_vendor.onMounted(() => {
- loadUserInfo();
- });
- common_vendor.onShow(() => {
- loadUserInfo();
- });
- const loadUserInfo = async () => {
- const userInfo = utils_auth.getUserInfo();
- if (userInfo) {
- avatarUrl.value = userInfo.avatar ? utils_api.getImageUrl(userInfo.avatar) : utils_api.getImageUrl("/static/images/head.png");
- nickname.value = userInfo.nickname || "";
- phone.value = userInfo.phone || "";
- originalAvatar.value = userInfo.avatar || "";
- originalNickname.value = nickname.value;
- }
- const latestInfo = await utils_auth.refreshUserInfo();
- if (latestInfo) {
- avatarUrl.value = latestInfo.avatar ? utils_api.getImageUrl(latestInfo.avatar) : utils_api.getImageUrl("/static/images/head.png");
- nickname.value = latestInfo.nickname || "";
- phone.value = latestInfo.phone || "";
- originalAvatar.value = latestInfo.avatar || "";
- originalNickname.value = nickname.value;
- }
- };
- const formatPhone = (p) => {
- if (!p || p.length !== 11)
- return p;
- return p.substring(0, 3) + "****" + p.substring(7);
- };
- const onChooseAvatar = (e) => {
- avatarUrl.value = e.detail.avatarUrl;
- };
- const uploadAvatar = async (tempPath) => {
- return new Promise((resolve, reject) => {
- const token = common_vendor.index.getStorageSync("user_token");
- common_vendor.index.uploadFile({
- url: `${utils_api.BASE_URL}/v1/file/upload`,
- filePath: tempPath,
- name: "file",
- header: {
- "Authorization": `Bearer ${token}`
- },
- success: (res) => {
- var _a;
- if (res.statusCode === 200) {
- const data = JSON.parse(res.data);
- if (data.code === 200 && ((_a = data.data) == null ? void 0 : _a.url)) {
- resolve(data.data.url);
- } else {
- reject(new Error(data.message || "上传失败"));
- }
- } else {
- reject(new Error("上传失败"));
- }
- },
- fail: (err) => {
- reject(new Error("网络错误"));
- }
- });
- });
- };
- const handleSave = async () => {
- if (!nickname.value || nickname.value.trim() === "") {
- common_vendor.index.showToast({ title: "请输入昵称", icon: "none" });
- return;
- }
- let currentAvatarPath = avatarUrl.value;
- if (currentAvatarPath.startsWith(utils_api.BASE_URL)) {
- currentAvatarPath = currentAvatarPath.replace(utils_api.BASE_URL, "");
- }
- if (currentAvatarPath === originalAvatar.value && nickname.value === originalNickname.value) {
- common_vendor.index.showToast({ title: "没有修改", icon: "none" });
- return;
- }
- saving.value = true;
- common_vendor.index.showLoading({ title: "保存中..." });
- try {
- let uploadedAvatarUrl = currentAvatarPath;
- const isTempPath = avatarUrl.value.startsWith("http://tmp/") || avatarUrl.value.startsWith("wxfile://") || avatarUrl.value.includes("/tmp/wx");
- const needUpload = currentAvatarPath !== originalAvatar.value && !avatarUrl.value.startsWith("/static/") && (!avatarUrl.value.startsWith("http") || isTempPath);
- if (needUpload) {
- try {
- console.log("[编辑资料] 开始上传头像:", avatarUrl.value);
- uploadedAvatarUrl = await uploadAvatar(avatarUrl.value);
- console.log("[编辑资料] 头像上传成功:", uploadedAvatarUrl);
- } catch (e) {
- console.error("[编辑资料] 头像上传失败:", e.message);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({ title: "头像上传失败", icon: "none" });
- saving.value = false;
- return;
- }
- }
- await utils_api.updateUserProfile({
- nickname: nickname.value.trim(),
- avatar: uploadedAvatarUrl
- });
- const userInfo = utils_auth.getUserInfo();
- userInfo.nickname = nickname.value.trim();
- userInfo.avatar = uploadedAvatarUrl;
- utils_auth.setUserInfo(userInfo);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({ title: "保存成功", icon: "success" });
- setTimeout(() => {
- const pages = getCurrentPages();
- pages.length > 1 ? common_vendor.index.navigateBack() : common_vendor.index.switchTab({ url: "/pages/mine/mine" });
- }, 1500);
- } catch (error) {
- console.error("保存失败:", error);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({ title: error.message || "保存失败", icon: "none" });
- } finally {
- saving.value = false;
- }
- };
- return (_ctx, _cache) => {
- return common_vendor.e({
- a: common_vendor.o(handleBack),
- b: avatarUrl.value,
- c: common_vendor.o(onChooseAvatar),
- d: nickname.value,
- e: common_vendor.o(($event) => nickname.value = $event.detail.value),
- f: phone.value
- }, phone.value ? {
- g: common_vendor.t(formatPhone(phone.value))
- } : {}, {
- h: common_vendor.t(saving.value ? "保存中..." : "保存"),
- i: common_vendor.o(handleSave),
- j: saving.value
- });
- };
- }
- };
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-7e5a80f3"], ["__file", "D:/program/gupiao/gupiao-wx/src/pages/profile/edit.vue"]]);
- wx.createPage(MiniProgramPage);
|