Files
PartsInquiry/frontend/unpackage/dist/dev/mp-weixin/pages/my/security.js
2025-09-27 22:57:59 +08:00

197 lines
7.6 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const common_config = require("../../common/config.js");
const _sfc_main = {
data() {
return {
form: { name: "", avatarUrl: "" },
pwd: { oldPassword: "", newPassword: "" },
phone: { phone: "" },
savingProfile: false,
savingPwd: false,
savingPhone: false,
sendingCode: false,
originalAvatarUrl: ""
};
},
onShow() {
this.loadProfile();
},
computed: {
avatarPreview() {
return this.normalizeAvatar(this.form.avatarUrl);
},
canSendPhone() {
const p = String(this.phone.phone || "").trim();
return /^1\d{10}$/.test(p);
}
},
methods: {
async loadProfile() {
try {
const data = await common_http.get("/api/user/me");
const rawAvatar = (data == null ? void 0 : data.avatarUrl) || (common_vendor.index.getStorageSync("USER_AVATAR_RAW") || "");
this.originalAvatarUrl = rawAvatar;
this.form.name = (data == null ? void 0 : data.name) || (common_vendor.index.getStorageSync("USER_NAME") || "");
this.form.avatarUrl = rawAvatar;
} catch (e) {
}
},
normalizeAvatar(url) {
if (!url)
return "/static/icons/icons8-mitt-24.png";
const s = String(url);
if (/^https?:\/\//i.test(s))
return s;
const base = common_config.API_BASE_URL || "";
if (!base)
return s;
if (s.startsWith("/"))
return `${base}${s}`;
return `${base}/${s}`;
},
openAvatarDialog() {
common_vendor.index.showActionSheet({
itemList: ["粘贴图片URL", "从相册选择并上传"],
success: (res) => {
if (res.tapIndex === 0) {
common_vendor.index.showModal({
title: "头像URL",
editable: true,
placeholderText: "https://...",
success: async (m) => {
if (m.confirm && m.content) {
this.form.avatarUrl = m.content.trim();
await this.saveProfile({ auto: true });
}
}
});
} else if (res.tapIndex === 1) {
common_vendor.index.chooseImage({ count: 1, sizeType: ["compressed"], success: (ci) => {
const filePath = ci.tempFilePaths && ci.tempFilePaths[0] || "";
if (!filePath)
return;
common_vendor.index.showLoading({ title: "上传中..." });
common_http.upload("/api/attachments", filePath, { ownerType: "user_avatar", ownerId: 0 }).then(async (data) => {
const url = data && (data.url || data.path);
if (url) {
this.form.avatarUrl = url;
await this.saveProfile({ auto: true });
}
common_vendor.index.showToast({ title: "已上传", icon: "success" });
}).catch((e) => {
common_vendor.index.showToast({ title: e && e.message || "上传失败", icon: "none" });
}).finally(() => {
common_vendor.index.hideLoading();
});
} });
}
}
});
},
async saveProfile(opts = {}) {
const auto = opts && opts.auto;
const payload = {};
if (this.form.name && this.form.name !== common_vendor.index.getStorageSync("USER_NAME"))
payload.name = this.form.name;
if (this.form.avatarUrl && this.form.avatarUrl !== this.originalAvatarUrl)
payload.avatarUrl = this.form.avatarUrl;
if (Object.keys(payload).length === 0) {
if (!auto)
common_vendor.index.showToast({ title: "无需修改", icon: "none" });
return;
}
if (this.savingProfile)
return;
this.savingProfile = true;
try {
await common_http.put("/api/user/me", payload);
try {
if (payload.name)
common_vendor.index.setStorageSync("USER_NAME", payload.name);
if (payload.avatarUrl) {
const rawUrl = payload.avatarUrl;
const displayUrl = `${rawUrl}${rawUrl.includes("?") ? "&" : "?"}t=${Date.now()}`;
common_vendor.index.setStorageSync("USER_AVATAR_RAW", rawUrl);
common_vendor.index.setStorageSync("USER_AVATAR", rawUrl);
this.originalAvatarUrl = rawUrl;
this.form.avatarUrl = rawUrl;
}
} catch (_) {
}
if (!payload.avatarUrl && this.form.avatarUrl) {
common_vendor.index.setStorageSync("USER_AVATAR_RAW", this.form.avatarUrl);
common_vendor.index.setStorageSync("USER_AVATAR", this.form.avatarUrl);
}
common_vendor.index.showToast({ title: auto ? "头像已更新" : "已保存", icon: "success" });
} catch (e) {
const msg = e && e.message || "保存失败";
common_vendor.index.showToast({ title: msg, icon: "none" });
} finally {
this.savingProfile = false;
}
},
async changePassword() {
if (!this.pwd.newPassword || this.pwd.newPassword.length < 6)
return common_vendor.index.showToast({ title: "新密码至少6位", icon: "none" });
this.savingPwd = true;
try {
await common_http.put("/api/user/me/password", { oldPassword: this.pwd.oldPassword || void 0, newPassword: this.pwd.newPassword });
this.pwd.oldPassword = "";
this.pwd.newPassword = "";
common_vendor.index.showToast({ title: "密码已修改", icon: "success" });
} catch (e) {
common_vendor.index.showToast({ title: e && e.message || "修改失败", icon: "none" });
} finally {
this.savingPwd = false;
}
},
async changePhoneDirect() {
if (!this.canSendPhone)
return common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
this.savingPhone = true;
try {
await common_http.put("/api/user/me/phone", { phone: this.phone.phone });
common_vendor.index.setStorageSync("USER_MOBILE", this.phone.phone);
common_vendor.index.showToast({ title: "手机号已保存", icon: "success" });
} catch (e) {
common_vendor.index.showToast({ title: e && e.message || "保存失败", icon: "none" });
} finally {
this.savingPhone = false;
}
}
}
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {
a: $options.avatarPreview,
b: common_vendor.o((...args) => $options.openAvatarDialog && $options.openAvatarDialog(...args)),
c: $data.form.name,
d: common_vendor.o(common_vendor.m(($event) => $data.form.name = $event.detail.value, {
trim: true
})),
e: $data.savingProfile,
f: common_vendor.o((...args) => $options.saveProfile && $options.saveProfile(...args)),
g: $data.pwd.oldPassword,
h: common_vendor.o(common_vendor.m(($event) => $data.pwd.oldPassword = $event.detail.value, {
trim: true
})),
i: $data.pwd.newPassword,
j: common_vendor.o(common_vendor.m(($event) => $data.pwd.newPassword = $event.detail.value, {
trim: true
})),
k: $data.savingPwd,
l: common_vendor.o((...args) => $options.changePassword && $options.changePassword(...args)),
m: $data.phone.phone,
n: common_vendor.o(common_vendor.m(($event) => $data.phone.phone = $event.detail.value, {
trim: true
})),
o: $data.savingPhone,
p: common_vendor.o((...args) => $options.changePhoneDirect && $options.changePhoneDirect(...args))
};
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/my/security.js.map