2
This commit is contained in:
@@ -4,92 +4,215 @@ const common_http = require("../../common/http.js");
|
||||
const _sfc_main = {
|
||||
data() {
|
||||
return {
|
||||
form: { phone: "", password: "" },
|
||||
phoneFocused: false,
|
||||
passwordFocused: false
|
||||
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: {
|
||||
validate() {
|
||||
const p = String(this.form.phone || "").trim();
|
||||
const okPhone = /^1[3-9]\d{9}$/.test(p);
|
||||
if (!okPhone) {
|
||||
common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
|
||||
return false;
|
||||
toast(msg) {
|
||||
try {
|
||||
common_vendor.index.showToast({ title: String(msg || "操作失败"), icon: "none" });
|
||||
} catch (_) {
|
||||
}
|
||||
return true;
|
||||
},
|
||||
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() {
|
||||
if (!this.validate())
|
||||
return;
|
||||
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 phone = String(this.form.phone || "").trim();
|
||||
const password = String(this.form.password || "");
|
||||
const res = await common_http.post("/api/auth/password/login", { phone, password });
|
||||
if (res && res.token) {
|
||||
common_vendor.index.setStorageSync("TOKEN", res.token);
|
||||
if (res.user && res.user.phone)
|
||||
common_vendor.index.setStorageSync("USER_MOBILE", res.user.phone);
|
||||
common_vendor.index.showToast({ title: "登录成功", icon: "none" });
|
||||
setTimeout(() => {
|
||||
common_vendor.index.reLaunch({ url: "/pages/index/index" });
|
||||
}, 200);
|
||||
}
|
||||
const data = await common_http.post("/api/auth/password/login", { email, password });
|
||||
this.afterLogin(data);
|
||||
} catch (e) {
|
||||
common_vendor.index.showToast({ title: e && e.message || "登录失败", icon: "none" });
|
||||
this.toast(e.message);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
onGoRegister() {
|
||||
common_vendor.index.navigateTo({
|
||||
url: "/pages/auth/register"
|
||||
});
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if (!Array) {
|
||||
const _component_path = common_vendor.resolveComponent("path");
|
||||
const _component_svg = common_vendor.resolveComponent("svg");
|
||||
(_component_path + _component_svg)();
|
||||
}
|
||||
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
||||
return {
|
||||
a: common_vendor.p({
|
||||
d: "M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2ZM21 9V7L15 4V6C15 7.66 13.66 9 12 9S9 7.66 9 6V4L3 7V9C3 10.1 3.9 11 5 11V17C5 18.1 5.9 19 7 19H9C9 20.1 9.9 21 11 21H13C14.1 21 15 20.1 15 19H17C18.1 19 19 18.1 19 17V11C20.1 11 21 10.1 21 9Z"
|
||||
}),
|
||||
b: common_vendor.p({
|
||||
viewBox: "0 0 24 24"
|
||||
}),
|
||||
c: common_vendor.p({
|
||||
d: "M6.62,10.79C8.06,13.62 10.38,15.94 13.21,17.38L15.41,15.18C15.69,14.9 16.08,14.82 16.43,14.93C17.55,15.3 18.75,15.5 20,15.5A1,1 0 0,1 21,16.5V20A1,1 0 0,1 20,21A17,17 0 0,1 3,4A1,1 0 0,1 4,3H7.5A1,1 0 0,1 8.5,4C8.5,5.25 8.7,6.45 9.07,7.57C9.18,7.92 9.1,8.31 8.82,8.59L6.62,10.79Z"
|
||||
}),
|
||||
d: common_vendor.p({
|
||||
viewBox: "0 0 24 24"
|
||||
}),
|
||||
e: common_vendor.o(($event) => $data.phoneFocused = true),
|
||||
f: common_vendor.o(($event) => $data.phoneFocused = false),
|
||||
g: $data.form.phone,
|
||||
h: common_vendor.o(common_vendor.m(($event) => $data.form.phone = $event.detail.value, {
|
||||
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
|
||||
})),
|
||||
i: $data.phoneFocused ? 1 : "",
|
||||
j: $data.form.phone ? 1 : "",
|
||||
k: common_vendor.p({
|
||||
d: "M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"
|
||||
}),
|
||||
l: common_vendor.p({
|
||||
viewBox: "0 0 24 24"
|
||||
}),
|
||||
m: common_vendor.o(($event) => $data.passwordFocused = true),
|
||||
n: common_vendor.o(($event) => $data.passwordFocused = false),
|
||||
o: $data.form.password,
|
||||
p: common_vendor.o(common_vendor.m(($event) => $data.form.password = $event.detail.value, {
|
||||
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.passwordFocused ? 1 : "",
|
||||
r: $data.form.password ? 1 : "",
|
||||
s: common_vendor.o((...args) => $options.onLogin && $options.onLogin(...args)),
|
||||
t: common_vendor.o((...args) => $options.onGoRegister && $options.onGoRegister(...args))
|
||||
};
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user