window.__cbs = {};
|
window.__wompCb = function (id, ok, body) {
|
var fn = window.__cbs[id];
|
delete window.__cbs[id];
|
if (!fn) return;
|
var data = body;
|
if (typeof body === "string") {
|
try { data = JSON.parse(body); } catch (e) { data = body; }
|
}
|
fn(ok, data);
|
};
|
function callNative(name, a, b, c) {
|
return new Promise(function (resolve, reject) {
|
var id = "cb_" + Date.now() + "_" + Math.random().toString(16).slice(2);
|
window.__cbs[id] = function (ok, data) {
|
if (ok === false) reject(data);
|
else resolve(data);
|
};
|
if (name === "request") Womp.request(a, b || "{}", id);
|
else if (name === "scan") Womp.scan(id);
|
else if (name === "getLocation") Womp.getLocation(id);
|
});
|
}
|
function api(path, body) {
|
var payload = body || {};
|
try {
|
var sorter = JSON.parse(Womp.getSorter() || "{}");
|
if (sorter.sorterId && payload.sorterId == null) payload.sorterId = sorter.sorterId;
|
} catch (e) {}
|
return callNative("request", path, JSON.stringify(payload)).then(function (res) {
|
if (res && typeof res === "object" && res.code && res.code !== 1000) {
|
var err = new Error(res.message || "请求失败");
|
err.code = res.code;
|
throw err;
|
}
|
return res && res.data !== undefined ? res.data : res;
|
});
|
}
|
function errMsg(e, fallback) {
|
if (!e) return fallback || "操作失败";
|
if (typeof e === "string") return e;
|
if (e.message) return e.message;
|
return fallback || "操作失败";
|
}
|
function loginErrMsg(e) {
|
var msg = errMsg(e, "登录失败,请稍后重试");
|
if (msg.length > 80 || /<html/i.test(msg)) return "登录失败,请稍后重试";
|
if (/账号或密码错误|用户不存在|unauthorized|password/i.test(msg)) {
|
return "手机号或密码错误,请重新输入";
|
}
|
return msg;
|
}
|
function scan() { return callNative("scan"); }
|
function getLocation() { return callNative("getLocation"); }
|
function toast(msg) { Womp.toast(String(msg || "")); }
|
function go(page) { location.href = page; }
|
function qs(k) {
|
var m = location.search.match(new RegExp("[?&]" + k + "=([^&]*)"));
|
return m ? decodeURIComponent(m[1]) : "";
|
}
|
function $(id) { return document.getElementById(id); }
|
function html(el, s) { el.innerHTML = s; }
|
function money(n) { return Number(n || 0).toFixed(2); }
|
function userHtml(u, extra) {
|
if (!u) return "<div class='muted'>暂无用户信息</div>";
|
return "<div><b>" + (u.nickName || "-") + "</b> · " + (u.role || "") + "</div>" +
|
"<div class='muted'>手机 " + (u.phone || "-") + " · 积分 " + (u.carbonBalance == null ? "-" : u.carbonBalance) + "</div>" +
|
(u.collegeName || u.className ? "<div class='muted'>" + (u.collegeName || "") + " " + (u.className || "") + "</div>" : "") +
|
(extra ? "<div class='muted'>" + extra + "</div>" : "");
|
}
|