66 lines
1.9 KiB
JavaScript
66 lines
1.9 KiB
JavaScript
const api = require("../../utils/api");
|
|
const app = getApp();
|
|
|
|
function readCode(options) {
|
|
if (options.code) return String(options.code).toUpperCase();
|
|
// 小程序码带的 scene 是 "login:XXXXXXXX" 或纯 8 位码
|
|
if (options.scene) {
|
|
const raw = decodeURIComponent(options.scene);
|
|
const m = raw.match(/[A-Z0-9]{8}/i);
|
|
return m ? m[0].toUpperCase() : "";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
Page({
|
|
data: { code: "", input: "", busy: false },
|
|
|
|
onLoad(options) {
|
|
const code = readCode(options || {});
|
|
this.setData({ code, input: code });
|
|
if (!app.globalData.token && !wx.getStorageSync("ql_token")) {
|
|
wx.showToast({ title: "请先登录小程序", icon: "none" });
|
|
}
|
|
},
|
|
|
|
onInput(e) {
|
|
this.setData({ input: String(e.detail.value || "").toUpperCase().slice(0, 8) });
|
|
},
|
|
|
|
onScan() {
|
|
wx.scanCode({
|
|
onlyFromCamera: false,
|
|
scanType: ["qrCode"],
|
|
success: (res) => {
|
|
const m = String(res.result || "").match(/[A-Z0-9]{8}/i);
|
|
if (m) this.setData({ input: m[0].toUpperCase() });
|
|
else wx.showToast({ title: "这不是登录码", icon: "none" });
|
|
},
|
|
fail: () => {},
|
|
});
|
|
},
|
|
|
|
async onConfirm() {
|
|
if (!api.requireLogin()) return;
|
|
const code = (this.data.input || this.data.code || "").trim().toUpperCase();
|
|
if (code.length !== 8) {
|
|
wx.showToast({ title: "登录码是 8 位", icon: "none" });
|
|
return;
|
|
}
|
|
this.setData({ busy: true });
|
|
try {
|
|
const r = await api.request("POST", "/api/auth/scan-confirm", { code });
|
|
wx.showToast({ title: r.hint || "已确认,看电脑", icon: "none" });
|
|
setTimeout(() => wx.navigateBack({ fail: () => wx.switchTab({ url: "/pages/home/home" }) }), 900);
|
|
} catch (e) {
|
|
wx.showToast({ title: e.message || "确认失败", icon: "none" });
|
|
} finally {
|
|
this.setData({ busy: false });
|
|
}
|
|
},
|
|
|
|
onLater() {
|
|
wx.navigateBack({ fail: () => wx.switchTab({ url: "/pages/home/home" }) });
|
|
},
|
|
});
|