43 lines
3.7 KiB
JavaScript
43 lines
3.7 KiB
JavaScript
'use strict';
|
|
const fs=require('node:fs');
|
|
const path=require('node:path');
|
|
const crypto=require('node:crypto');
|
|
const {escapeLoginText:esc}=require('./public/login-ui');
|
|
const script=fs.readFileSync(path.join(__dirname,'public/login-ui.js'),'utf8');
|
|
const scriptHash=crypto.createHash('sha256').update(script).digest('base64');
|
|
|
|
function loginForm({mode,setup,loginOptions}){
|
|
if(mode==='demo')return `<form method="post" action="/api/demo-login">${loginOptions.map(row=>`<button type="submit" value="${esc(row.member)}" disabled>以${esc(row.label)}身份进入演示</button>`).join('')}</form>`;
|
|
if(setup)return `<form method="post" action="/api/setup">
|
|
<label>店铺名称<input name="storeName" maxlength="80" required></label>
|
|
<label>店主显示名<input name="ownerLabel" value="店主" maxlength="30" required></label>
|
|
<label>店主登录名<input name="ownerLoginName" value="owner" maxlength="60" autocomplete="username" required></label>
|
|
<label>店主密码(至少12个字符)<input name="ownerPassword" type="password" minlength="12" maxlength="500" autocomplete="new-password" required></label>
|
|
<button type="submit" disabled>建立试用账号</button></form>`;
|
|
return `<form method="post" action="/api/login">
|
|
<label>登录账号<select name="member" required autocomplete="username"><option value="">请选择账号</option>${loginOptions.map(row=>`<option value="${esc(row.member)}">${esc(row.label)}</option>`).join('')}</select></label>
|
|
<label>密码<input name="password" type="password" required autocomplete="current-password"></label>
|
|
<button type="submit" disabled>登录</button></form>`;
|
|
}
|
|
|
|
function loginPage(info){
|
|
const store=info.brand.storeName||'珠宝台账';
|
|
return `<!doctype html>
|
|
<html lang="zh-CN"><head><meta charset="utf-8"><link rel="icon" href="data:,"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${esc(store)} · 新账系统</title>
|
|
<style>
|
|
*{box-sizing:border-box}body{margin:0;background:#f6f5f0;color:#233d35;font:16px/1.5 system-ui,sans-serif}
|
|
main{max-width:460px;margin:clamp(20px,10vh,90px) auto;padding:clamp(20px,5vw,36px);background:#fffefa;border:1px solid #dedfd6;border-radius:14px}
|
|
.mark{display:grid;place-items:center;width:46px;height:46px;background:#254e40;color:#fffefa;border-radius:10px;font:600 24px serif}
|
|
h1{margin:18px 0 6px;font-size:24px}p{margin:6px 0 16px;color:#53645d}
|
|
form,label{display:grid;gap:6px}form{gap:14px}label{font-size:14px}
|
|
input,select,button{width:100%;min-height:44px;border:1px solid #cbd3cd;border-radius:6px;padding:8px 11px;font:inherit;font-size:16px;background:white;color:inherit}
|
|
button{cursor:pointer;color:white;background:#254e40;border-color:#254e40;font-weight:600}button:disabled{opacity:.6}
|
|
.error{margin:14px 0;padding:14px;background:#fae8e3;border-radius:6px;color:#883f32}.error p{color:inherit}.error button{margin-top:10px;width:auto}
|
|
[role=status]:empty{display:none}[role=status]{margin:12px 0 0}@media(max-width:500px){main{margin:20px 12px}}
|
|
</style></head><body data-standalone-login><main><div aria-hidden="true" class="mark">${esc(Array.from(store)[0])}</div>
|
|
<h1>${esc(store)} · 新账系统</h1><p>${info.accessWindow?.state==='closed'?esc(info.accessWindow.message)+' 超级管理员可登录。':info.accessWindow?.state==='unset'?'员工营业时间待设置,超级管理员可登录。':info.mode==='demo'?'选择账号,进入演示账本。':info.setup?'建立店主账号后,可在员工管理中添加员工。':'录入、收款与复核,从同一张订单开始。'}</p>
|
|
<div id="login-error"></div>${loginForm(info)}<p id="login-status" role="status"></p><noscript>请启用 JavaScript 后登录。</noscript></main>
|
|
<script>${script}</script></body></html>`;
|
|
}
|
|
module.exports={loginPage,scriptHash};
|