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

220 lines
8.2 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const _sfc_main = {
data() {
return {
loading: false,
tab: "login",
loginForm: { email: "", password: "" },
regForm: { name: "", email: "", code: "", password: "", password2: "" },
resetForm: { email: "", code: "", password: "", password2: "" },
regCountdown: 0,
resetCountdown: 0,
_timers: []
};
},
beforeUnmount() {
this._timers.forEach((t) => clearInterval(t));
},
methods: {
toast(msg) {
try {
common_vendor.index.showToast({ title: String(msg || "操作失败"), icon: "none" });
} catch (_) {
}
},
validateEmail(v) {
return /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(String(v || "").trim());
},
startCountdown(key) {
if (this[key] > 0)
return;
this[key] = 60;
const timer = setInterval(() => {
this[key] = Math.max(0, this[key] - 1);
if (this[key] === 0)
clearInterval(timer);
}, 1e3);
this._timers.push(timer);
},
async onLogin() {
const { email, password } = this.loginForm;
if (!this.validateEmail(email))
return this.toast("请输入正确邮箱");
if (!password || password.length < 6)
return this.toast("请输入至少6位密码");
this.loading = true;
try {
const data = await common_http.post("/api/auth/password/login", { email, password });
this.afterLogin(data);
} catch (e) {
this.toast(e.message);
} finally {
this.loading = false;
}
},
afterLogin(data) {
try {
if (data && data.token) {
common_vendor.index.setStorageSync("TOKEN", data.token);
if (data.user && data.user.shopId)
common_vendor.index.setStorageSync("SHOP_ID", data.user.shopId);
common_vendor.index.setStorageSync("ENABLE_DEFAULT_USER", "false");
common_vendor.index.removeStorageSync("DEFAULT_USER_ID");
this.toast("登录成功");
setTimeout(() => {
common_vendor.index.reLaunch({ url: "/pages/index/index" });
}, 300);
} else {
this.toast("登录失败");
}
} catch (_) {
this.toast("登录失败");
}
},
async sendRegCode() {
if (!this.validateEmail(this.regForm.email))
return this.toast("请输入正确邮箱");
this.loading = true;
try {
const r = await common_http.post("/api/auth/email/send", { email: this.regForm.email, scene: "register" });
if (r && r.ok)
this.startCountdown("regCountdown");
this.toast(r && r.ok ? "验证码已发送" : "发送过于频繁");
} catch (e) {
this.toast(e.message);
} finally {
this.loading = false;
}
},
async onRegister() {
const f = this.regForm;
if (!f.name || f.name.trim().length < 1)
return this.toast("请输入用户名");
if (!this.validateEmail(f.email))
return this.toast("请输入正确邮箱");
if (!f.code)
return this.toast("请输入验证码");
if (!f.password || f.password.length < 6)
return this.toast("密码至少6位");
if (f.password !== f.password2)
return this.toast("两次密码不一致");
this.loading = true;
try {
const data = await common_http.post("/api/auth/email/register", { name: f.name.trim(), email: f.email.trim(), code: f.code.trim(), password: f.password });
this.afterLogin(data);
} catch (e) {
this.toast(e.message);
} finally {
this.loading = false;
}
},
async sendResetCode() {
if (!this.validateEmail(this.resetForm.email))
return this.toast("请输入正确邮箱");
this.loading = true;
try {
const r = await common_http.post("/api/auth/email/send", { email: this.resetForm.email, scene: "reset" });
if (r && r.ok)
this.startCountdown("resetCountdown");
this.toast(r && r.ok ? "验证码已发送" : "发送过于频繁");
} catch (e) {
this.toast(e.message);
} finally {
this.loading = false;
}
},
async onReset() {
const f = this.resetForm;
if (!this.validateEmail(f.email))
return this.toast("请输入正确邮箱");
if (!f.code)
return this.toast("请输入验证码");
if (!f.password || f.password.length < 6)
return this.toast("新密码至少6位");
if (f.password !== f.password2)
return this.toast("两次密码不一致");
this.loading = true;
try {
const r = await common_http.post("/api/auth/email/reset-password", { email: f.email.trim(), code: f.code.trim(), newPassword: f.password, confirmPassword: f.password2 });
if (r && r.ok) {
this.toast("已重置,请使用新密码登录");
this.tab = "login";
this.loginForm.email = f.email;
} else
this.toast("重置失败");
} catch (e) {
this.toast(e.message);
} finally {
this.loading = false;
}
}
}
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return common_vendor.e({
a: common_vendor.n($data.tab === "login" ? "active" : ""),
b: common_vendor.o(($event) => $data.tab = "login"),
c: common_vendor.n($data.tab === "register" ? "active" : ""),
d: common_vendor.o(($event) => $data.tab = "register"),
e: common_vendor.n($data.tab === "reset" ? "active" : ""),
f: common_vendor.o(($event) => $data.tab = "reset"),
g: $data.tab === "login"
}, $data.tab === "login" ? {
h: $data.loginForm.email,
i: common_vendor.o(common_vendor.m(($event) => $data.loginForm.email = $event.detail.value, {
trim: true
})),
j: $data.loginForm.password,
k: common_vendor.o(($event) => $data.loginForm.password = $event.detail.value),
l: $data.loading,
m: common_vendor.o((...args) => $options.onLogin && $options.onLogin(...args))
} : $data.tab === "register" ? {
o: $data.regForm.name,
p: common_vendor.o(common_vendor.m(($event) => $data.regForm.name = $event.detail.value, {
trim: true
})),
q: $data.regForm.email,
r: common_vendor.o(common_vendor.m(($event) => $data.regForm.email = $event.detail.value, {
trim: true
})),
s: $data.regForm.code,
t: common_vendor.o(common_vendor.m(($event) => $data.regForm.code = $event.detail.value, {
trim: true
})),
v: common_vendor.t($data.regCountdown > 0 ? $data.regCountdown + "s" : "获取验证码"),
w: $data.regCountdown > 0 || $data.loading,
x: common_vendor.o((...args) => $options.sendRegCode && $options.sendRegCode(...args)),
y: $data.regForm.password,
z: common_vendor.o(($event) => $data.regForm.password = $event.detail.value),
A: $data.regForm.password2,
B: common_vendor.o(($event) => $data.regForm.password2 = $event.detail.value),
C: $data.loading,
D: common_vendor.o((...args) => $options.onRegister && $options.onRegister(...args))
} : {
E: $data.resetForm.email,
F: common_vendor.o(common_vendor.m(($event) => $data.resetForm.email = $event.detail.value, {
trim: true
})),
G: $data.resetForm.code,
H: common_vendor.o(common_vendor.m(($event) => $data.resetForm.code = $event.detail.value, {
trim: true
})),
I: common_vendor.t($data.resetCountdown > 0 ? $data.resetCountdown + "s" : "获取验证码"),
J: $data.resetCountdown > 0 || $data.loading,
K: common_vendor.o((...args) => $options.sendResetCode && $options.sendResetCode(...args)),
L: $data.resetForm.password,
M: common_vendor.o(($event) => $data.resetForm.password = $event.detail.value),
N: $data.resetForm.password2,
O: common_vendor.o(($event) => $data.resetForm.password2 = $event.detail.value),
P: $data.loading,
Q: common_vendor.o((...args) => $options.onReset && $options.onReset(...args))
}, {
n: $data.tab === "register"
});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/auth/login.js.map