Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
408 lines (357 loc) · 22.9 KB
/
Copy pathindex.html
File metadata and controls
408 lines (357 loc) · 22.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>智能部门排班系统 (云端动态模板版)</title>
<!-- 引入 Vue3 -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<!-- 引入 Tailwind CSS 美化界面 -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- 引入 SheetJS 用于生成 Excel -->
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
</head>
<body class="bg-gray-100 text-gray-800 font-sans antialiased">
<div id="app" class="min-h-screen p-4 max-w-md mx-auto sm:max-w-2xl sm:p-8">
<!-- 首页角色选择 -->
<div v-if="currentView === 'home'" class="bg-white rounded-xl shadow-lg p-6 text-center mt-10">
<h1 class="text-2xl font-bold text-blue-600 mb-6">📅 智能排班协同中台</h1>
<p class="text-gray-500 mb-8 text-sm">纯前端零成本协同 · 支持自定义排班模板</p>
<div class="space-y-4">
<button @click="currentView = 'adminConfigTemplate'" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded-lg transition duration-200">
👑 我是排班管理员 (配置模板并创建任务)
</button>
<button @click="currentView = 'userLogin'" class="w-full bg-green-500 hover:bg-green-600 text-white font-bold py-3 px-4 rounded-lg transition duration-200">
🧑💻 我是部门成员 (加入已有任务)
</button>
</div>
</div>
<!-- 管理员:配置模板页 -->
<div v-if="currentView === 'adminConfigTemplate'" class="bg-white rounded-xl shadow-lg p-6 mt-6">
<button @click="currentView = 'home'" class="text-blue-500 text-sm mb-4">← 返回首页</button>
<h2 class="text-xl font-bold mb-4">⚙️ 第一步:配置您的排班表模板</h2>
<p class="text-sm text-gray-500 mb-6">默认已填好标准模板,您可以自由添加或删除。</p>
<div class="mb-6">
<h3 class="font-bold mb-3 text-blue-700">横向表头 (日期/星期)</h3>
<div class="flex flex-wrap gap-2 mb-3">
<div v-for="(col, idx) in headersCol.slice(1)" :key="'c'+idx" class="bg-blue-100 text-blue-700 px-3 py-1.5 rounded flex items-center shadow-sm">
{{ col }}
<button @click="headersCol.splice(idx+1, 1)" class="ml-2 text-red-500 font-bold hover:text-red-700">×</button>
</div>
</div>
<div class="flex">
<input v-model="newCol" placeholder="例如: 星期六" class="border-2 border-gray-200 p-2 rounded-l w-full text-sm">
<button @click="addCol" class="bg-blue-500 text-white px-4 rounded-r font-bold">添加</button>
</div>
</div>
<div class="mb-8">
<h3 class="font-bold mb-3 text-green-700">纵向表头 (班次/时间段)</h3>
<div class="flex flex-col gap-2 mb-3">
<div v-for="(row, idx) in headersRow.slice(1)" :key="'r'+idx" class="bg-green-100 text-green-700 px-3 py-1.5 rounded flex items-center justify-between shadow-sm">
<span>{{ row }}</span>
<button @click="headersRow.splice(idx+1, 1)" class="text-red-500 font-bold hover:text-red-700">×</button>
</div>
</div>
<div class="flex">
<input v-model="newRow" placeholder="例如: 晚班 (18:00-22:00)" class="border-2 border-gray-200 p-2 rounded-l w-full text-sm">
<button @click="addRow" class="bg-green-500 text-white px-4 rounded-r font-bold">添加</button>
</div>
</div>
<button @click="createRoomWithTemplate" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-3 px-4 rounded-lg shadow-md">
确认模板并生成收集任务 🚀
</button>
</div>
<!-- 员工填报登录页 -->
<div v-if="currentView === 'userLogin'" class="bg-white rounded-xl shadow-lg p-6 mt-10">
<button @click="currentView = 'home'" class="text-blue-500 text-sm mb-4">← 返回首页</button>
<h2 class="text-xl font-bold mb-4">加入排班填报</h2>
<input v-model="inputRoomKey" placeholder="请输入管理员发放的 6 位房间密钥" class="w-full border-2 border-gray-200 p-3 rounded-lg mb-4 uppercase font-mono text-center tracking-widest text-lg" maxlength="6">
<input v-model="userName" placeholder="请输入您的真实姓名" class="w-full border-2 border-gray-200 p-3 rounded-lg mb-6 text-center text-lg">
<button @click="enterRoom" :disabled="isLoadingTemplate" class="w-full bg-green-500 hover:bg-green-600 text-white font-bold py-3 px-4 rounded-lg disabled:opacity-50">
{{ isLoadingTemplate ? '正在拉取动态模板...' : '获取模板并进入' }}
</button>
</div>
<!-- 员工填报网格页 -->
<div v-if="currentView === 'userGrid'" class="bg-white rounded-xl shadow-lg p-4 sm:p-6 mt-4">
<h2 class="text-lg font-bold mb-2">欢迎,{{ userName }}</h2>
<p class="text-sm text-gray-500 mb-4">房间: <span class="font-mono text-blue-600">{{ inputRoomKey }}</span> | 请点击下方格子状态</p>
<div class="overflow-x-auto pb-4">
<table class="w-full border-collapse text-center text-sm min-w-max">
<thead>
<tr>
<th v-for="(col, cIdx) in headersCol" :key="'th'+cIdx" class="border p-2 bg-gray-50 whitespace-nowrap">{{ col }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(rowName, rIdx) in headersRow.slice(1)" :key="'tr'+rIdx">
<td class="border p-2 bg-gray-50 font-medium whitespace-nowrap">{{ rowName }}</td>
<td v-for="cIdx in (headersCol.length - 1)" :key="'td'+cIdx"
@click="toggleCell(rIdx + 1, cIdx)"
:class="['border p-3 cursor-pointer transition-colors duration-200 select-none shadow-inner', userGrid[rIdx + 1][cIdx] === 0 ? 'bg-green-400 text-white' : 'bg-gray-200 text-gray-400']">
{{ userGrid[rIdx + 1][cIdx] === 0 ? '🟩 有空' : '⬛ 有课' }}
</td>
</tr>
</tbody>
</table>
</div>
<button @click="submitSchedule" :disabled="isSubmitting" class="w-full bg-blue-600 text-white font-bold py-3 px-4 rounded-lg mt-2 disabled:opacity-50 shadow-md">
{{ isSubmitting ? '正在提交...' : '一键提交至管理员大屏' }}
</button>
</div>
<!-- 员工提交通知 -->
<div v-if="currentView === 'userDone'" class="bg-white rounded-xl shadow-lg p-6 mt-10 text-center">
<div class="text-5xl mb-4">🎉</div>
<h2 class="text-xl font-bold mb-2 text-green-600">提交成功!</h2>
<p class="text-gray-500">数据已送达管理员,您可以直接关闭此网页了。</p>
</div>
<!-- 管理员控制台 -->
<div v-if="currentView === 'adminPanel'" class="bg-white rounded-xl shadow-lg p-6 mt-4">
<!-- 密钥与一键复制链接 -->
<div class="bg-indigo-50 border-l-4 border-indigo-500 p-4 mb-6 rounded">
<h3 class="font-bold text-indigo-700 text-lg mb-2 flex items-center justify-between">
<span>🔑 房间密钥:<span class="text-2xl ml-2 tracking-widest font-mono bg-white px-2 py-1 rounded shadow-sm">{{ roomKey }}</span></span>
</h3>
<div class="mt-3 flex gap-2">
<input type="text" readonly :value="shareUrl" class="text-xs p-2 border border-indigo-200 w-full rounded bg-white text-gray-500">
<button @click="copyLink" class="bg-indigo-500 text-white text-xs px-3 rounded hover:bg-indigo-600 whitespace-nowrap">复制快捷链接</button>
</div>
<p class="text-xs text-indigo-500 mt-2">💡 发送快捷链接给员工,他们点开将自动输入密钥并加载模板。切记:此大屏网页请勿刷新或关闭!</p>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
<!-- 规则设置 -->
<div class="border rounded-lg p-4 bg-white shadow-sm">
<h4 class="font-bold mb-4">⚙️ 引擎运算规则</h4>
<div class="space-y-3 text-sm">
<div class="flex justify-between items-center">
<span>每班次需求人数:</span>
<input type="number" v-model.number="config.numPerShift" class="border-2 w-16 p-1 rounded text-center" min="1">
</div>
<div class="flex justify-between items-center">
<span>允许单人重复排班:</span>
<input type="checkbox" v-model="config.allowRepeat" class="w-4 h-4 text-blue-600">
</div>
<div class="flex justify-between items-center">
<span>允许出现空缺:</span>
<input type="checkbox" v-model="config.allowVacancy" class="w-4 h-4 text-blue-600">
</div>
</div>
</div>
<!-- 实时收集大屏 -->
<div class="border rounded-lg p-4 bg-gray-50 shadow-inner">
<h4 class="font-bold mb-2 flex justify-between">
<span>📡 实时雷达收集大屏</span>
<span class="text-green-600 bg-green-100 px-2 rounded">{{ Object.keys(collectedData).length }} 人已交</span>
</h4>
<div class="h-40 overflow-y-auto text-sm space-y-2 p-1">
<div v-if="Object.keys(collectedData).length === 0" class="text-gray-400 text-center mt-10">静静等待成员提交数据...</div>
<!-- 带有防捣乱剔除功能的列表项 -->
<div v-for="(grid, name) in collectedData" :key="name" class="bg-white p-2 border-l-4 border-green-500 rounded shadow-sm flex justify-between items-center transition-all hover:border-red-400 group">
<span class="font-bold truncate max-w-[50%]">{{ name }}</span>
<div class="flex items-center gap-2">
<span class="text-green-500 text-xs flex items-center group-hover:hidden">✔ 内存已接收</span>
<button @click="removeRecord(name)" class="text-red-500 hover:text-white border border-red-200 hover:bg-red-500 hover:border-red-500 text-xs px-2 py-1 rounded transition-colors shadow-sm bg-red-50 sm:hidden group-hover:block" title="剔除此人">
🗑️ 删除
</button>
</div>
</div>
</div>
</div>
</div>
<button @click="generateAndExport" :disabled="Object.keys(collectedData).length === 0" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-4 px-4 rounded-xl disabled:opacity-50 shadow-lg text-lg">
🚀 所有人员就绪,执行核心算法并生成 Excel
</button>
</div>
</div>
<script>
const { createApp, ref, reactive, onMounted } = Vue;
createApp({
setup() {
const currentView = ref('home');
const NTFY_TOPIC_PREFIX = "sched_prod_v5_";
// === 模板与管理员状态 ===
const headersCol = ref(["", "星期一", "星期二", "星期三", "星期四", "星期五"]);
const headersRow = ref(["", "一二节课", "三四节课", "五六节课", "七八节课"]);
const newCol = ref('');
const newRow = ref('');
const roomKey = ref('');
const shareUrl = ref('');
const collectedData = reactive({});
const config = reactive({ numPerShift: 1, allowRepeat: false, allowVacancy: true });
let eventSource = null;
// === 员工状态 ===
const inputRoomKey = ref('');
const userName = ref('');
const userGrid = ref([]);
const isSubmitting = ref(false);
const isLoadingTemplate = ref(false);
// --- 自动识别 URL 参数 ---
onMounted(() => {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('room')) {
inputRoomKey.value = urlParams.get('room').toUpperCase();
currentView.value = 'userLogin';
}
});
// ================= 模板编辑逻辑 =================
const addCol = () => { if(newCol.value.trim()){ headersCol.value.push(newCol.value.trim()); newCol.value=''; } };
const addRow = () => { if(newRow.value.trim()){ headersRow.value.push(newRow.value.trim()); newRow.value=''; } };
// ================= 管理员逻辑 =================
const createRoomWithTemplate = async () => {
if (headersCol.value.length < 2 || headersRow.value.length < 2) {
return alert("请至少保留一个日期和一个班次!");
}
roomKey.value = Math.random().toString(36).substring(2, 8).toUpperCase();
shareUrl.value = window.location.href.split('?')[0] + '?room=' + roomKey.value;
currentView.value = 'adminPanel';
const tplPayload = { cols: headersCol.value, rows: headersRow.value };
await fetch(`https://ntfy.sh/${NTFY_TOPIC_PREFIX}tpl_${roomKey.value}`, {
method: 'POST',
body: JSON.stringify(tplPayload)
});
const topicUrl = `https://ntfy.sh/${NTFY_TOPIC_PREFIX}data_${roomKey.value}/sse`;
eventSource = new EventSource(topicUrl);
eventSource.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
if(data.event === "message" && data.message) {
const payload = JSON.parse(data.message);
if(payload.name && payload.grid) {
collectedData[payload.name] = payload.grid;
}
}
} catch (e) { }
};
};
const copyLink = () => {
navigator.clipboard.writeText(`请点击填写排班课表,房间密钥 ${roomKey.value}:\n${shareUrl.value}`);
alert("快捷链接与邀请文案已复制!去微信群粘贴吧。");
};
// 管理员:剔除错误或恶意的提交记录
const removeRecord = (targetName) => {
if (confirm(`🚨 确定要删除【${targetName}】的排班数据吗?\n删除后,该成员可通过原链接重新提交。`)) {
delete collectedData[targetName];
}
};
// ================= 员工逻辑 =================
const enterRoom = async () => {
if(!inputRoomKey.value || !userName.value) return alert("房间号和姓名不能为空!");
const key = inputRoomKey.value.toUpperCase().trim();
isLoadingTemplate.value = true;
try {
const response = await fetch(`https://ntfy.sh/${NTFY_TOPIC_PREFIX}tpl_${key}/json?poll=1`);
const text = await response.text();
if (!text) throw new Error("未找到缓存消息");
const lines = text.trim().split('\n');
let foundTemplate = null;
for (let i = lines.length - 1; i >= 0; i--) {
if (!lines[i]) continue;
const msg = JSON.parse(lines[i]);
if (msg.event === "message" && msg.message) {
foundTemplate = JSON.parse(msg.message);
break;
}
}
if (!foundTemplate) throw new Error("解析不到有效模板");
headersCol.value = foundTemplate.cols;
headersRow.value = foundTemplate.rows;
initUserGrid();
currentView.value = 'userGrid';
} catch (e) {
console.error(e);
alert("❌ 无法获取模板数据!原因可能是:\n1. 密钥输入错误\n2. 任务开启已超过数小时过期\n请联系管理员确认。");
}
isLoadingTemplate.value = false;
};
const initUserGrid = () => {
let grid = [];
for(let i = 0; i < headersRow.value.length; i++){
let row = [];
for(let j = 0; j < headersCol.value.length; j++){
if(i === 0 || j === 0) row.push("");
else row.push(0);
}
grid.push(row);
}
userGrid.value = grid;
};
const toggleCell = (r, c) => { userGrid.value[r][c] = userGrid.value[r][c] === 0 ? 1 : 0; };
const submitSchedule = async () => {
isSubmitting.value = true;
try {
await fetch(`https://ntfy.sh/${NTFY_TOPIC_PREFIX}data_${inputRoomKey.value.toUpperCase()}`, {
method: 'POST',
body: JSON.stringify({ name: userName.value.trim(), grid: userGrid.value })
});
currentView.value = 'userDone';
} catch (e) { alert("网络异常,请重试!"); }
isSubmitting.value = false;
};
// ================= 核心排班算法引擎 =================
const assignShifts = (schedules) => {
let roster = [headersCol.value.slice()];
for (let r = 1; r < headersRow.value.length; r++) {
roster.push([headersRow.value[r], ...Array(headersCol.value.length - 1).fill("空缺")]);
}
let shiftCounts = {};
Object.keys(schedules).forEach(name => shiftCounts[name] = 0);
let slots = [];
for (let r = 1; r < headersRow.value.length; r++) {
for (let c = 1; c < headersCol.value.length; c++) {
slots.push({ r, c });
}
}
slots.forEach(slot => {
slot.freePeople = Object.keys(schedules).filter(name => schedules[name][slot.r][slot.c] === 0);
});
slots.sort(() => Math.random() - 0.5);
slots.sort((a, b) => a.freePeople.length - b.freePeople.length);
slots.forEach(slot => {
let freePeople = slot.freePeople;
if (freePeople.length === 0) return;
let neverWorked = freePeople.filter(p => shiftCounts[p] === 0);
let chosen = [];
if (neverWorked.length >= config.numPerShift) {
neverWorked.sort(() => Math.random() - 0.5);
chosen = neverWorked.slice(0, config.numPerShift);
} else {
if (config.allowRepeat) {
let available = [...freePeople];
available.sort(() => Math.random() - 0.5);
available.sort((a, b) => shiftCounts[a] - shiftCounts[b]);
chosen = available.slice(0, config.numPerShift);
} else {
if (config.allowVacancy) chosen = [...neverWorked];
else {
let available = [...freePeople];
available.sort(() => Math.random() - 0.5);
available.sort((a, b) => shiftCounts[a] - shiftCounts[b]);
chosen = available.slice(0, config.numPerShift);
}
}
}
if (chosen.length > 0) {
roster[slot.r][slot.c] = chosen.join("\r\n");
chosen.forEach(p => shiftCounts[p]++);
}
});
return roster;
};
const generateAndExport = () => {
const finalRoster = assignShifts(collectedData);
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(finalRoster);
XLSX.utils.book_append_sheet(wb, ws, "最终排班表");
XLSX.writeFile(wb, `排班结果_${roomKey.value}.xlsx`);
};
return {
currentView, initUserGrid, enterRoom, headersCol, headersRow,
newCol, newRow, addCol, addRow, createRoomWithTemplate, copyLink, shareUrl,
roomKey, collectedData, config, generateAndExport,
inputRoomKey, userName, userGrid, toggleCell, submitSchedule,
isSubmitting, isLoadingTemplate, removeRecord // 暴露删除方法
};
}
}).mount('#app');
</script>
</body>
</html>