Files
PartsInquiry/frontend/unpackage/dist/dev/mp-weixin/pages/auth/login.js
2025-09-29 21:38:32 +08:00

227 lines
8.3 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 {
loading: false,
tab: "login",
authLoginTopImage: common_config.AUTH_LOGIN_TOP_IMAGE,
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: {
gotoRegister() {
this.tab = "register";
},
gotoReset() {
this.tab = "reset";
},
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: $data.authLoginTopImage,
b: $data.tab === "login"
}, $data.tab === "login" ? {
c: $data.loginForm.email,
d: common_vendor.o(common_vendor.m(($event) => $data.loginForm.email = $event.detail.value, {
trim: true
})),
e: $data.loginForm.password,
f: common_vendor.o(($event) => $data.loginForm.password = $event.detail.value),
g: $data.loading,
h: common_vendor.o((...args) => $options.onLogin && $options.onLogin(...args)),
i: common_vendor.o((...args) => $options.gotoRegister && $options.gotoRegister(...args)),
j: common_vendor.o((...args) => $options.gotoReset && $options.gotoReset(...args))
} : {}, {
k: $data.tab === "register"
}, $data.tab === "register" ? {
l: $data.regForm.name,
m: common_vendor.o(common_vendor.m(($event) => $data.regForm.name = $event.detail.value, {
trim: true
})),
n: $data.regForm.email,
o: common_vendor.o(common_vendor.m(($event) => $data.regForm.email = $event.detail.value, {
trim: true
})),
p: $data.regForm.code,
q: common_vendor.o(common_vendor.m(($event) => $data.regForm.code = $event.detail.value, {
trim: true
})),
r: common_vendor.t($data.regCountdown > 0 ? $data.regCountdown + "s" : "获取验证码"),
s: $data.regCountdown > 0 || $data.loading,
t: common_vendor.o((...args) => $options.sendRegCode && $options.sendRegCode(...args)),
v: $data.regForm.password,
w: common_vendor.o(($event) => $data.regForm.password = $event.detail.value),
x: $data.regForm.password2,
y: common_vendor.o(($event) => $data.regForm.password2 = $event.detail.value),
z: $data.loading,
A: common_vendor.o((...args) => $options.onRegister && $options.onRegister(...args))
} : {}, {
B: $data.tab === "reset"
}, $data.tab === "reset" ? {
C: $data.resetForm.email,
D: common_vendor.o(common_vendor.m(($event) => $data.resetForm.email = $event.detail.value, {
trim: true
})),
E: $data.resetForm.code,
F: common_vendor.o(common_vendor.m(($event) => $data.resetForm.code = $event.detail.value, {
trim: true
})),
G: common_vendor.t($data.resetCountdown > 0 ? $data.resetCountdown + "s" : "获取验证码"),
H: $data.resetCountdown > 0 || $data.loading,
I: common_vendor.o((...args) => $options.sendResetCode && $options.sendResetCode(...args)),
J: $data.resetForm.password,
K: common_vendor.o(($event) => $data.resetForm.password = $event.detail.value),
L: $data.resetForm.password2,
M: common_vendor.o(($event) => $data.resetForm.password2 = $event.detail.value),
N: $data.loading,
O: common_vendor.o((...args) => $options.onReset && $options.onReset(...args))
} : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/auth/login.js.map