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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"/>
  <title>回收清单</title>
  <link rel="stylesheet" href="css.css"/>
</head>
<body>
<div class="wrap">
  <div class="page-head">
    <h1 id="title">现场回收</h1>
    <a class="back-link" href="home.html">返回工作台</a>
  </div>
  <div class="card">
    <div id="userBox" class="muted">请先扫描</div>
    <button class="btn" id="scanBtn" onclick="doScan()">扫描回收码</button>
  </div>
  <div class="card">
    <div class="sec-title">选择品类</div>
    <div id="cats" class="muted">正在加载品类…</div>
    <label>计量(kg/件)</label>
    <input id="weight" type="number" step="0.01" oninput="preview()"/>
    <div id="preview" class="preview">请选择品类并填写计量</div>
    <button class="btn ghost" onclick="addItem()">添加一条</button>
    <div id="list"></div>
    <div id="totals" class="totals" style="display:none"></div>
    <button class="btn" onclick="save()">保存订单</button>
  </div>
</div>
<script src="app.js"></script>
<script>
var mode = qs("mode");
var user = null, appointmentId = null, items = [], catalog = [], selectedId = "";
if (mode === "apt") {
  $("title").textContent = "预约回收";
  $("userBox").textContent = "请扫描预约单二维码";
  $("scanBtn").textContent = "扫描预约单";
}
api("/app/shop/sorter/itemList", {}).then(function (list) {
  catalog = list || [];
  renderCats();
}).catch(function (e) {
  $("cats").innerHTML = "<div class='err'>" + (e.message || "品类加载失败") + "</div>";
});
 
function renderCats() {
  if (!catalog.length) { $("cats").innerHTML = "<div class='err'>暂无回收品类,请在后台维护</div>"; return; }
  var groups = {};
  catalog.forEach(function (i) {
    var g = i.category || "其他";
    if (!groups[g]) groups[g] = [];
    groups[g].push(i);
  });
  $("cats").innerHTML = Object.keys(groups).map(function (g) {
    return "<div class='muted' style='margin:8px 0 4px'>" + g + "</div><div class='chip-wrap'>" +
      groups[g].map(function (i) {
        return "<div class='chip" + (selectedId == i.itemId ? " on" : "") + "' onclick='pick(\"" + i.itemId + "\")'>" +
          i.itemName + " ¥" + money(i.price) + "/" + (i.unit || "kg") + "</div>";
      }).join("") + "</div>";
  }).join("");
}
function pick(id) { selectedId = String(id); renderCats(); preview(); }
function currentCat() {
  return catalog.filter(function (i) { return String(i.itemId) === String(selectedId); })[0];
}
function preview() {
  var cat = currentCat();
  var w = Number($("weight").value);
  if (!cat) { $("preview").textContent = "请选择品类并填写计量"; return; }
  if (!w) {
    $("preview").innerHTML = "已选 <b>" + cat.itemName + "</b>,单价 ¥" + money(cat.price) + "/" + (cat.unit || "kg") +
      ",碳积分 " + money(cat.carbonPoint) + "/" + (cat.unit || "kg");
    return;
  }
  $("preview").innerHTML = cat.itemName + " × " + w + (cat.unit || "") +
    " 单价 ¥" + money(cat.price) + " 小计 ¥" + money(w * cat.price) +
    " 碳积分 " + money(w * cat.carbonPoint);
}
function doScan() {
  if (mode === "apt") scanApt();
  else scanUser();
}
function scanUser() {
  scan().then(function (code) {
    if (!code) return;
    return api("/app/shop/sorter/checkUser", { unionid: code });
  }).then(function (u) {
    if (!u) return;
    user = u;
    $("userBox").innerHTML = userHtml(u, "回收码已识别");
  }).catch(function (e) { toast(e.message || "不是平台用户"); });
}
function scanApt() {
  scan().then(function (code) {
    if (!code) return;
    return api("/app/shop/sorter/scanAppointment", { appointmentNo: code });
  }).then(function (apt) {
    if (!apt) return;
    appointmentId = apt.id;
    if (!apt.campusUser) { toast("预约单缺少用户信息"); return; }
    user = apt.campusUser;
    $("userBox").innerHTML = userHtml(user, "预约号 " + (apt.appointmentNo || "") + " · " + (apt.contactName || "") + " " + (apt.contactPhone || ""));
  }).catch(function (e) { toast(e.message || "预约单无效"); });
}
function addItem() {
  var cat = currentCat();
  var w = Number($("weight").value);
  if (!cat || !w) { toast("请选择品类并填写计量"); return; }
  items.push({
    itemId: cat.itemId,
    itemName: cat.itemName,
    unit: cat.unit || "kg",
    weight: w,
    unitPrice: Number(cat.price || 0),
    amount: Number((w * Number(cat.price || 0)).toFixed(2)),
    carbonPoint: Number((w * Number(cat.carbonPoint || 0)).toFixed(2))
  });
  render();
  $("weight").value = "";
  preview();
}
function render() {
  $("list").innerHTML = items.map(function (i, idx) {
    return "<div class='item'><b>" + i.itemName + "</b> " + i.weight + (i.unit || "") +
      " ¥" + money(i.unitPrice) + " 小计 ¥" + money(i.amount) +
      " 碳积分 " + money(i.carbonPoint) +
      " <span class='muted' onclick='del(" + idx + ")'>删除</span></div>";
  }).join("");
  var tw = 0, ta = 0, tc = 0;
  items.forEach(function (i) { tw += Number(i.weight); ta += Number(i.amount); tc += Number(i.carbonPoint); });
  if (!items.length) { $("totals").style.display = "none"; return; }
  $("totals").style.display = "block";
  $("totals").innerHTML = "合计计量 " + money(tw) + " 总金额 ¥" + money(ta) + " 总碳积分 " + money(tc);
}
function del(i) { items.splice(i, 1); render(); }
function save() {
  if (!user) { toast(mode === "apt" ? "请先扫描预约单" : "请先扫描用户"); return; }
  if (mode === "apt" && !appointmentId) { toast("请先扫描预约单"); return; }
  if (!items.length) { toast("请添加清单"); return; }
  api("/app/shop/sorter/saveOrder", {
    unionid: user.unionid,
    appointmentId: appointmentId,
    items: items
  }).then(function (r) {
    toast("已保存 " + r.orderNo);
    items = []; render();
  }).catch(function (e) { toast(e.message || "保存失败"); });
}
</script>
</body>
</html>