wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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>" : "");
}