154 lines
5.8 KiB
JavaScript
154 lines
5.8 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const common_http = require("../../common/http.js");
|
|
const _sfc_main = {
|
|
data() {
|
|
return { phone: "", code: "", countdown: 0, timer: null, sending: false, logging: false, showDebug: true };
|
|
},
|
|
computed: {
|
|
btnText() {
|
|
return this.countdown > 0 ? `${this.countdown}s` : this.sending ? "发送中..." : "获取验证码";
|
|
},
|
|
trimmedPhone() {
|
|
return String(this.phone || "").trim();
|
|
},
|
|
sendBodyJson() {
|
|
return JSON.stringify({ phone: this.trimmedPhone, scene: "login" }, null, 2);
|
|
},
|
|
loginBodyJson() {
|
|
return JSON.stringify({ phone: this.trimmedPhone, code: String(this.code || "").trim() }, null, 2);
|
|
}
|
|
},
|
|
onUnload() {
|
|
if (this.timer)
|
|
clearInterval(this.timer);
|
|
},
|
|
methods: {
|
|
validatePhone(p) {
|
|
return /^1\d{10}$/.test(String(p || "").trim());
|
|
},
|
|
startCountdown(sec) {
|
|
this.countdown = sec;
|
|
if (this.timer)
|
|
clearInterval(this.timer);
|
|
this.timer = setInterval(() => {
|
|
if (this.countdown <= 1) {
|
|
clearInterval(this.timer);
|
|
this.timer = null;
|
|
this.countdown = 0;
|
|
return;
|
|
}
|
|
this.countdown--;
|
|
}, 1e3);
|
|
},
|
|
async sendCode() {
|
|
if (this.sending || this.countdown > 0)
|
|
return;
|
|
const p = String(this.phone || "").trim();
|
|
if (!this.validatePhone(p))
|
|
return common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
|
|
this.sending = true;
|
|
try {
|
|
const res = await common_http.post("/api/auth/sms/send", { phone: p, scene: "login" });
|
|
const cd = Number(res && res.cooldownSec || 60);
|
|
this.startCountdown(cd);
|
|
common_vendor.index.showToast({ title: "验证码已发送", icon: "none" });
|
|
} catch (e) {
|
|
const msg = e && e.message || "发送失败";
|
|
common_vendor.index.showToast({ title: msg, icon: "none" });
|
|
} finally {
|
|
this.sending = false;
|
|
}
|
|
},
|
|
async doLogin() {
|
|
if (this.logging)
|
|
return;
|
|
const p = String(this.phone || "").trim();
|
|
const c = String(this.code || "").trim();
|
|
if (!this.validatePhone(p))
|
|
return common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
|
|
if (!/^\d{6}$/.test(c))
|
|
return common_vendor.index.showToast({ title: "验证码格式不正确", icon: "none" });
|
|
this.logging = true;
|
|
try {
|
|
const data = await common_http.post("/api/auth/sms/login", { phone: p, code: c });
|
|
if (data && data.token) {
|
|
common_vendor.index.setStorageSync("TOKEN", data.token);
|
|
if (data.user && data.user.phone)
|
|
common_vendor.index.setStorageSync("USER_MOBILE", data.user.phone);
|
|
common_vendor.index.showToast({ title: "登录成功", icon: "none" });
|
|
setTimeout(() => {
|
|
common_vendor.index.reLaunch({ url: "/pages/index/index" });
|
|
}, 300);
|
|
}
|
|
} catch (e) {
|
|
const msg = e && e.message || "登录失败";
|
|
common_vendor.index.showToast({ title: msg, icon: "none" });
|
|
} finally {
|
|
this.logging = false;
|
|
}
|
|
},
|
|
async quickRegister() {
|
|
if (this.logging)
|
|
return;
|
|
const p = String(this.phone || "").trim();
|
|
if (!this.validatePhone(p))
|
|
return common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
|
|
this.logging = true;
|
|
try {
|
|
const data = await common_http.post("/api/auth/register", { phone: p });
|
|
if (data && data.token) {
|
|
common_vendor.index.setStorageSync("TOKEN", data.token);
|
|
if (data.user && data.user.phone)
|
|
common_vendor.index.setStorageSync("USER_MOBILE", data.user.phone);
|
|
common_vendor.index.showToast({ title: "注册成功", icon: "none" });
|
|
setTimeout(() => {
|
|
common_vendor.index.reLaunch({ url: "/pages/index/index" });
|
|
}, 300);
|
|
}
|
|
} catch (e) {
|
|
const msg = e && e.message || "注册失败";
|
|
common_vendor.index.showToast({ title: msg, icon: "none" });
|
|
} finally {
|
|
this.logging = false;
|
|
}
|
|
},
|
|
copy(text) {
|
|
try {
|
|
common_vendor.index.setClipboardData({ data: String(text || "") });
|
|
common_vendor.index.showToast({ title: "已复制", icon: "none" });
|
|
} catch (e) {
|
|
}
|
|
},
|
|
toggleDebug() {
|
|
this.showDebug = !this.showDebug;
|
|
}
|
|
}
|
|
};
|
|
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
return common_vendor.e({
|
|
a: $data.phone,
|
|
b: common_vendor.o(($event) => $data.phone = $event.detail.value),
|
|
c: $data.code,
|
|
d: common_vendor.o(($event) => $data.code = $event.detail.value),
|
|
e: common_vendor.t($options.btnText),
|
|
f: $data.countdown > 0 || $data.sending,
|
|
g: common_vendor.o((...args) => $options.sendCode && $options.sendCode(...args)),
|
|
h: $data.logging,
|
|
i: common_vendor.o((...args) => $options.doLogin && $options.doLogin(...args)),
|
|
j: $data.logging,
|
|
k: common_vendor.o((...args) => $options.quickRegister && $options.quickRegister(...args)),
|
|
l: common_vendor.t($data.showDebug ? "收起" : "展开"),
|
|
m: common_vendor.o((...args) => $options.toggleDebug && $options.toggleDebug(...args)),
|
|
n: $data.showDebug
|
|
}, $data.showDebug ? {
|
|
o: common_vendor.t($options.sendBodyJson),
|
|
p: common_vendor.o(($event) => $options.copy($options.sendBodyJson)),
|
|
q: common_vendor.t($options.loginBodyJson),
|
|
r: common_vendor.o(($event) => $options.copy($options.loginBodyJson))
|
|
} : {});
|
|
}
|
|
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
|
|
wx.createPage(MiniProgramPage);
|
|
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/my/sms-login.js.map
|