229 lines
9.2 KiB
JavaScript
229 lines
9.2 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const utils_globalState = require("../../utils/globalState.js");
|
|
const api_index = require("../../api/index.js");
|
|
const utils_session = require("../../utils/session.js");
|
|
if (!Math) {
|
|
AppIcon();
|
|
}
|
|
const AppIcon = () => "../../components/AppIcon.js";
|
|
const _sfc_main = {
|
|
__name: "Profile",
|
|
emits: ["change-page"],
|
|
setup(__props, { emit: __emit }) {
|
|
const navSafeStyle = (() => {
|
|
var _a, _b, _c, _d;
|
|
const statusBarHeight = ((_b = (_a = common_vendor.index).getSystemInfoSync) == null ? void 0 : _b.call(_a).statusBarHeight) || 20;
|
|
let navHeight = statusBarHeight + 44;
|
|
const menuRect = (_d = (_c = common_vendor.index).getMenuButtonBoundingClientRect) == null ? void 0 : _d.call(_c);
|
|
if (menuRect && menuRect.top && menuRect.height) {
|
|
const verticalGap = Math.max(menuRect.top - statusBarHeight, 4);
|
|
navHeight = statusBarHeight + verticalGap * 2 + menuRect.height;
|
|
}
|
|
return `padding-top:${statusBarHeight}px;height:${navHeight}px;`;
|
|
})();
|
|
const userInfo = common_vendor.ref(utils_session.getUserSession());
|
|
const showEditName = common_vendor.ref(false);
|
|
const showEditPhone = common_vendor.ref(false);
|
|
const saving = common_vendor.ref(false);
|
|
const savingPhone = common_vendor.ref(false);
|
|
const smsCountdown = common_vendor.ref(0);
|
|
let smsTimer = null;
|
|
const editForm = common_vendor.ref({ name: "", phone: "", code: "" });
|
|
const initials = common_vendor.computed(() => {
|
|
if (!userInfo.value.name) return "?";
|
|
return userInfo.value.name.slice(0, 1).toUpperCase();
|
|
});
|
|
const COLORS = ["#ff7c43", "#07c160", "#8b6914", "#e06040", "#5090d0"];
|
|
const avatarBg = common_vendor.computed(() => {
|
|
var _a;
|
|
const idx = (((_a = userInfo.value.name) == null ? void 0 : _a.charCodeAt(0)) || 0) % COLORS.length;
|
|
return { background: COLORS[idx] };
|
|
});
|
|
const sendSms = async () => {
|
|
const phone = editForm.value.phone;
|
|
if (!phone || phone.length !== 11) {
|
|
common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
|
|
return;
|
|
}
|
|
const res = await api_index.sendSms(phone);
|
|
if (res.code === 200) {
|
|
common_vendor.index.showToast({ title: "验证码已发送", icon: "none" });
|
|
smsCountdown.value = 60;
|
|
smsTimer = setInterval(() => {
|
|
smsCountdown.value--;
|
|
if (smsCountdown.value <= 0) clearInterval(smsTimer);
|
|
}, 1e3);
|
|
} else {
|
|
common_vendor.index.showToast({ title: res.message || "发送失败", icon: "none" });
|
|
}
|
|
};
|
|
const confirmEditName = async () => {
|
|
if (!editForm.value.name || !editForm.value.name.trim()) {
|
|
common_vendor.index.showToast({ title: "请输入姓名", icon: "none" });
|
|
return;
|
|
}
|
|
saving.value = true;
|
|
const res = await api_index.updateUser({ id: userInfo.value.id, name: editForm.value.name.trim() });
|
|
saving.value = false;
|
|
if (res.code === 200) {
|
|
userInfo.value.name = editForm.value.name.trim();
|
|
utils_session.setUserSession(userInfo.value);
|
|
common_vendor.index.showToast({ title: "修改成功", icon: "success" });
|
|
showEditName.value = false;
|
|
} else {
|
|
common_vendor.index.showToast({ title: res.message || "修改失败", icon: "none" });
|
|
}
|
|
};
|
|
const confirmEditPhone = async () => {
|
|
const { phone, code } = editForm.value;
|
|
if (!phone || phone.length !== 11) {
|
|
common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
|
|
return;
|
|
}
|
|
if (!code || code.length !== 6) {
|
|
common_vendor.index.showToast({ title: "请输入6位验证码", icon: "none" });
|
|
return;
|
|
}
|
|
savingPhone.value = true;
|
|
const res = await api_index.updateUser({ id: userInfo.value.id, phone, code });
|
|
savingPhone.value = false;
|
|
if (res.code === 200) {
|
|
userInfo.value.phone = phone;
|
|
utils_session.setUserSession(userInfo.value);
|
|
common_vendor.index.showToast({ title: "修改成功", icon: "success" });
|
|
showEditPhone.value = false;
|
|
} else {
|
|
common_vendor.index.showToast({ title: res.message || "修改失败", icon: "none" });
|
|
}
|
|
};
|
|
const changeAvatar = () => {
|
|
common_vendor.index.chooseImage({
|
|
count: 1,
|
|
success: async (res) => {
|
|
const tempPath = res.tempFilePaths[0];
|
|
common_vendor.index.showLoading({ title: "上传中..." });
|
|
common_vendor.index.uploadFile({
|
|
url: `${api_index.BASE_URL}/upload/image`,
|
|
filePath: tempPath,
|
|
name: "file",
|
|
success: async (uploadRes) => {
|
|
common_vendor.index.hideLoading();
|
|
const data = JSON.parse(uploadRes.data);
|
|
if (data.code === 200) {
|
|
const avatarUrl = api_index.imgUrl(data.data.url);
|
|
const updateRes = await api_index.updateUser({ id: userInfo.value.id, avatar: data.data.url });
|
|
if (updateRes.code === 200) {
|
|
userInfo.value.avatar = avatarUrl;
|
|
utils_session.setUserSession(userInfo.value);
|
|
common_vendor.index.showToast({ title: "头像已更新", icon: "success" });
|
|
} else {
|
|
common_vendor.index.showToast({ title: updateRes.message || "更新失败", icon: "none" });
|
|
}
|
|
} else {
|
|
common_vendor.index.showToast({ title: data.message || "上传失败", icon: "none" });
|
|
}
|
|
},
|
|
fail: () => {
|
|
common_vendor.index.hideLoading();
|
|
common_vendor.index.showToast({ title: "上传失败", icon: "none" });
|
|
}
|
|
});
|
|
}
|
|
});
|
|
};
|
|
common_vendor.onMounted(() => {
|
|
editForm.value.name = userInfo.value.name || "";
|
|
editForm.value.phone = userInfo.value.phone || "";
|
|
});
|
|
common_vendor.onUnmounted(() => {
|
|
if (smsTimer) clearInterval(smsTimer);
|
|
});
|
|
return (_ctx, _cache) => {
|
|
return common_vendor.e({
|
|
a: common_vendor.p({
|
|
name: "back",
|
|
size: 18,
|
|
color: "#ffffff"
|
|
}),
|
|
b: common_vendor.o(($event) => common_vendor.unref(utils_globalState.navigateTo)("mine")),
|
|
c: common_vendor.s(common_vendor.unref(navSafeStyle)),
|
|
d: userInfo.value.avatar
|
|
}, userInfo.value.avatar ? {
|
|
e: common_vendor.unref(api_index.imgUrl)(userInfo.value.avatar)
|
|
} : {
|
|
f: common_vendor.t(initials.value),
|
|
g: common_vendor.s(avatarBg.value)
|
|
}, {
|
|
h: common_vendor.p({
|
|
name: "camera",
|
|
size: 12,
|
|
color: "#64748b"
|
|
}),
|
|
i: common_vendor.o(changeAvatar),
|
|
j: common_vendor.t(userInfo.value.name || "未设置姓名"),
|
|
k: common_vendor.t(userInfo.value.phone || "未绑定手机号"),
|
|
l: userInfo.value.role === "boss"
|
|
}, userInfo.value.role === "boss" ? {} : {}, {
|
|
m: common_vendor.p({
|
|
name: "profile",
|
|
size: 15
|
|
}),
|
|
n: common_vendor.t(userInfo.value.name || "未设置"),
|
|
o: common_vendor.o(($event) => showEditName.value = true),
|
|
p: common_vendor.p({
|
|
name: "orders",
|
|
size: 15
|
|
}),
|
|
q: common_vendor.t(userInfo.value.phone || "未设置"),
|
|
r: common_vendor.o(($event) => showEditPhone.value = true),
|
|
s: common_vendor.p({
|
|
name: "mine",
|
|
size: 15
|
|
}),
|
|
t: userInfo.value.role === "boss"
|
|
}, userInfo.value.role === "boss" ? {} : {}, {
|
|
v: common_vendor.t(userInfo.value.role === "boss" ? "商家版" : "员工版"),
|
|
w: showEditName.value
|
|
}, showEditName.value ? {
|
|
x: common_vendor.p({
|
|
name: "close",
|
|
size: 18,
|
|
color: "#94a3b8"
|
|
}),
|
|
y: common_vendor.o(($event) => showEditName.value = false),
|
|
z: editForm.value.name,
|
|
A: common_vendor.o(($event) => editForm.value.name = $event.detail.value),
|
|
B: saving.value,
|
|
C: common_vendor.o(confirmEditName),
|
|
D: common_vendor.o(() => {
|
|
}),
|
|
E: common_vendor.o(($event) => showEditName.value = false)
|
|
} : {}, {
|
|
F: showEditPhone.value
|
|
}, showEditPhone.value ? {
|
|
G: common_vendor.p({
|
|
name: "close",
|
|
size: 18,
|
|
color: "#94a3b8"
|
|
}),
|
|
H: common_vendor.o(($event) => showEditPhone.value = false),
|
|
I: editForm.value.phone,
|
|
J: common_vendor.o(($event) => editForm.value.phone = $event.detail.value),
|
|
K: editForm.value.code,
|
|
L: common_vendor.o(($event) => editForm.value.code = $event.detail.value),
|
|
M: common_vendor.t(smsCountdown.value > 0 ? smsCountdown.value + "s" : "获取验证码"),
|
|
N: smsCountdown.value > 0,
|
|
O: common_vendor.o(sendSms),
|
|
P: savingPhone.value,
|
|
Q: common_vendor.o(confirmEditPhone),
|
|
R: common_vendor.o(() => {
|
|
}),
|
|
S: common_vendor.o(($event) => showEditPhone.value = false)
|
|
} : {});
|
|
};
|
|
}
|
|
};
|
|
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-1111041b"]]);
|
|
wx.createPage(MiniProgramPage);
|