login.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  6. <title>外发明细登录</title>
  7. <style>
  8. * { box-sizing: border-box; }
  9. html, body { margin: 0; height: 100%; width: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: #f0f2f5; }
  10. body { display: flex; flex-direction: column; min-height: 100vh; }
  11. .login-shell {
  12. flex: 1 1 auto;
  13. width: 100%;
  14. min-height: 0;
  15. display: flex;
  16. flex-direction: column;
  17. align-items: center;
  18. justify-content: center;
  19. padding: 24px 16px 52px;
  20. box-sizing: border-box;
  21. }
  22. .page { width: 100%; max-width: 420px; margin: 0; padding: 0; flex: 0 0 auto; }
  23. .logo-area { text-align: center; margin-bottom: 18px; }
  24. .logo-area h1 { font-size: 1.2rem; color: #333; margin: 0; font-weight: 600; }
  25. .login-tabs {
  26. display: flex;
  27. margin-bottom: 14px;
  28. border-radius: 10px;
  29. overflow: hidden;
  30. border: 1px solid #ddd;
  31. background: #fff;
  32. }
  33. .login-tabs button {
  34. flex: 1;
  35. padding: 12px 8px;
  36. border: none;
  37. background: #f5f5f5;
  38. color: #666;
  39. font-size: 15px;
  40. cursor: pointer;
  41. }
  42. .login-tabs button.active {
  43. background: #3c8dbc;
  44. color: #fff;
  45. font-weight: 600;
  46. }
  47. .panel { display: none; }
  48. .panel.active { display: block; }
  49. .card { background: #fff; border-radius: 12px; padding: 22px 18px; box-shadow: 0 2px 12px rgba(0,0,0,.06); }
  50. .field { margin-bottom: 16px; }
  51. .field label { display: block; font-size: 0.85rem; color: #555; margin-bottom: 6px; }
  52. .field input { width: 100%; padding: 12px 14px; border: 1px solid #ddd; border-radius: 8px; font-size: 16px; }
  53. .field input:focus { outline: none; border-color: #3c8dbc; }
  54. .row-code { display: flex; gap: 10px; align-items: center; }
  55. .row-code input { flex: 1; min-width: 0; }
  56. .row-captcha img {
  57. flex-shrink: 0;
  58. width: 110px;
  59. height: 40px;
  60. border-radius: 8px;
  61. border: 1px solid #ddd;
  62. cursor: pointer;
  63. display: block;
  64. background: #f5f5f5;
  65. }
  66. .btn-code { flex-shrink: 0; padding: 0 14px; border: 1px solid #3c8dbc; background: #fff; color: #3c8dbc; border-radius: 8px; font-size: 14px; white-space: nowrap; min-width: 108px; height: 44px; }
  67. .btn-code:disabled { opacity: .55; border-color: #ccc; color: #999; }
  68. .btn-submit { width: 100%; padding: 14px; border: none; border-radius: 8px; background: #3c8dbc; color: #fff; font-size: 16px; font-weight: 600; margin-top: 8px; }
  69. .btn-submit:active { opacity: .9; }
  70. .btn-submit.busy { opacity: .75; cursor: wait; }
  71. .tip { font-size: 12px; color: #999; margin-top: 12px; line-height: 1.5; }
  72. .footer { position: fixed; bottom: 0; left: 0; right: 0; text-align: center; padding: 10px; font-size: 12px; color: #999; background: #f0f2f5; }
  73. /* 页内提示(替代 alert) */
  74. .login-toast {
  75. position: fixed;
  76. left: 50%;
  77. /* 抬高:避开页脚版权区,大致落在登录按钮下方附近 */
  78. bottom: calc(max(120px, min(32vh, 240px)) + env(safe-area-inset-bottom, 0px));
  79. transform: translate3d(-50%, 16px, 0);
  80. max-width: min(92vw, 360px);
  81. padding: 12px 18px;
  82. border-radius: 10px;
  83. font-size: 14px;
  84. line-height: 1.5;
  85. text-align: center;
  86. box-shadow: 0 4px 20px rgba(0, 0, 0, .2);
  87. z-index: 10000;
  88. opacity: 0;
  89. visibility: hidden;
  90. pointer-events: none;
  91. transition: opacity .25s ease, transform .25s ease, visibility .25s;
  92. }
  93. .login-toast.show {
  94. opacity: 1;
  95. visibility: visible;
  96. transform: translate3d(-50%, 0, 0);
  97. }
  98. .login-toast.err {
  99. background: #c0392b;
  100. color: #fff;
  101. }
  102. .login-toast.info {
  103. background: rgba(60, 141, 188, .96);
  104. color: #fff;
  105. }
  106. .login-toast.ok {
  107. background: #27ae60;
  108. color: #fff;
  109. }
  110. </style>
  111. </head>
  112. <body>
  113. <div class="login-shell">
  114. <div class="page">
  115. <div class="logo-area">
  116. <h1>登录</h1>
  117. </div>
  118. <div class="login-tabs" role="tablist">
  119. <button type="button" class="active" data-tab="account">账号密码</button>
  120. <button type="button" data-tab="phone">手机号登录</button>
  121. </div>
  122. <div class="card">
  123. <div id="panel-account" class="panel active">
  124. <div class="field">
  125. <label for="username">账号</label>
  126. <input type="text" id="username" name="username" maxlength="11" placeholder="登录账号(手机号)" autocomplete="username">
  127. </div>
  128. <div class="field">
  129. <label for="password">密码</label>
  130. <input type="password" id="password" name="password" placeholder="密码" autocomplete="current-password">
  131. </div>
  132. <button type="button" class="btn-submit" id="btn-login-pwd">登 录</button>
  133. </div>
  134. <div id="panel-phone" class="panel">
  135. <div class="field">
  136. <label for="phone">手机号</label>
  137. <input type="tel" id="phone" name="phone" maxlength="11" placeholder="请输入11位手机号" autocomplete="tel">
  138. </div>
  139. <div class="field">
  140. <label for="captcha">图形验证码</label>
  141. <div class="row-code row-captcha">
  142. <input type="text" id="captcha" name="captcha" maxlength="6" placeholder="请输入图形验证码" autocomplete="off">
  143. <img id="img-captcha" src="{$mprocCaptchaUrl|default=''|htmlentities}" width="110" height="40" alt="图形验证码" title="点击刷新">
  144. </div>
  145. </div>
  146. <div class="field">
  147. <label for="code">短信验证码</label>
  148. <div class="row-code">
  149. <input type="text" id="code" name="code" maxlength="6" inputmode="numeric" placeholder="6位验证码" autocomplete="one-time-code">
  150. <button type="button" class="btn-code" id="btn-send">获取验证码</button>
  151. </div>
  152. </div>
  153. <button type="button" class="btn-submit" id="btn-login">登 录</button>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <div id="login-toast" class="login-toast err" role="status" aria-live="polite" aria-atomic="true" hidden></div>
  159. <script>
  160. (function () {
  161. var sendUrl = "{:url('index/index/sendSms')}";
  162. var loginUrl = "{:url('index/index/doLogin')}";
  163. var loginPwdUrl = "{:url('index/index/doLoginPwd')}";
  164. var indexUrl = "{:url('index/index/index')}";
  165. var restoreUrl = "{:url('index/index/mprocRestore')}";
  166. var captchaBaseUrl = {:json_encode(isset($mprocCaptchaUrl) ? $mprocCaptchaUrl : url('index/index/captcha'), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)};
  167. var loginRedirect = {:json_encode(isset($mprocLoginRedirect) ? $mprocLoginRedirect : '', JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)};
  168. var $tabs = document.querySelectorAll('.login-tabs button');
  169. var $panelPhone = document.getElementById('panel-phone');
  170. var $panelAccount = document.getElementById('panel-account');
  171. var $phone = document.getElementById('phone');
  172. var $captcha = document.getElementById('captcha');
  173. var $imgCaptcha = document.getElementById('img-captcha');
  174. var $code = document.getElementById('code');
  175. var $username = document.getElementById('username');
  176. var $password = document.getElementById('password');
  177. var $btnSend = document.getElementById('btn-send');
  178. var $btnLogin = document.getElementById('btn-login');
  179. var $btnLoginPwd = document.getElementById('btn-login-pwd');
  180. var cd = 0;
  181. var timer = null;
  182. var $toast = document.getElementById('login-toast');
  183. var toastHideTimer = null;
  184. /** 页内轻提示,替代 alert */
  185. function showToast(msg, kind) {
  186. kind = (kind === 'ok' || kind === 'info') ? kind : 'err';
  187. if (!$toast) {
  188. return;
  189. }
  190. if (toastHideTimer) {
  191. clearTimeout(toastHideTimer);
  192. toastHideTimer = null;
  193. }
  194. $toast.textContent = msg || '';
  195. $toast.className = 'login-toast ' + kind;
  196. $toast.removeAttribute('hidden');
  197. requestAnimationFrame(function () {
  198. $toast.classList.add('show');
  199. });
  200. toastHideTimer = setTimeout(function () {
  201. $toast.classList.remove('show');
  202. toastHideTimer = setTimeout(function () {
  203. $toast.setAttribute('hidden', 'hidden');
  204. $toast.textContent = '';
  205. }, 280);
  206. }, 3200);
  207. }
  208. function postForm(url, data, timeoutMs) {
  209. timeoutMs = timeoutMs || 0;
  210. var body = [];
  211. for (var k in data) {
  212. if (Object.prototype.hasOwnProperty.call(data, k)) {
  213. body.push(encodeURIComponent(k) + '=' + encodeURIComponent(data[k]));
  214. }
  215. }
  216. var opts = {
  217. method: 'POST',
  218. headers: {
  219. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  220. 'X-Requested-With': 'XMLHttpRequest'
  221. },
  222. body: body.join('&'),
  223. credentials: 'same-origin'
  224. };
  225. var tid = null;
  226. if (timeoutMs > 0 && typeof AbortController !== 'undefined') {
  227. var ctrl = new AbortController();
  228. opts.signal = ctrl.signal;
  229. tid = setTimeout(function () {
  230. try { ctrl.abort(); } catch (e1) {}
  231. }, timeoutMs);
  232. }
  233. return fetch(url, opts).then(function (r) {
  234. if (tid) {
  235. clearTimeout(tid);
  236. tid = null;
  237. }
  238. return r.json();
  239. }).catch(function (e) {
  240. if (tid) {
  241. clearTimeout(tid);
  242. tid = null;
  243. }
  244. throw e;
  245. });
  246. }
  247. /** 提交中:立刻改文案,避免用户以为没点到 */
  248. function setSubmitBusy(btn, busy, busyText) {
  249. if (!btn) return;
  250. if (!Object.prototype.hasOwnProperty.call(btn.dataset, 'idleText')) {
  251. btn.dataset.idleText = (btn.textContent || '').trim();
  252. }
  253. btn.disabled = !!busy;
  254. btn.classList.toggle('busy', !!busy);
  255. btn.textContent = busy ? (busyText || '请稍候…') : btn.dataset.idleText;
  256. }
  257. function mprocExtractFocusEidFromRedirect(redir) {
  258. if (!redir) {
  259. return 0;
  260. }
  261. var fe = 0;
  262. try {
  263. var s = String(redir);
  264. var qIdx = s.indexOf('?');
  265. if (qIdx >= 0) {
  266. fe = parseInt(new URLSearchParams(s.substring(qIdx + 1)).get('focus_eid'), 10) || 0;
  267. }
  268. if (fe <= 0) {
  269. var m = s.match(/(?:[?&;]|%26)(?:amp;)*focus_eid=(\d+)/i);
  270. if (m) {
  271. fe = parseInt(m[1], 10) || 0;
  272. }
  273. }
  274. } catch (ignoreParse) {
  275. fe = 0;
  276. }
  277. if (fe <= 0) {
  278. try {
  279. fe = parseInt(sessionStorage.getItem('mproc_focus_eid'), 10) || 0;
  280. } catch (ignoreSs) {
  281. fe = 0;
  282. }
  283. }
  284. return fe;
  285. }
  286. function mprocEnsureUrlHasFocusEid(url, redir) {
  287. if (!url) {
  288. return url;
  289. }
  290. if (/(?:\?|#)(?:mproc_fe=|focus_eid=)\d+/.test(url)) {
  291. return url;
  292. }
  293. var fe = mprocExtractFocusEidFromRedirect(redir);
  294. if (fe <= 0) {
  295. return url;
  296. }
  297. var base = url.split('#')[0].split('?')[0];
  298. return base + '?focus_eid=' + encodeURIComponent(String(fe)) + '#mproc_fe=' + fe;
  299. }
  300. function goHome(ret) {
  301. if (ret && (ret.code === 1 || ret.code === '1')) {
  302. var d = ret.data || {};
  303. if (d.mproc_token) {
  304. try {
  305. var days = parseInt(d.keep_days, 10) || 7;
  306. localStorage.setItem('mproc_token', d.mproc_token);
  307. localStorage.setItem('mproc_token_exp', String(Date.now() + days * 86400000));
  308. } catch (eStore) {}
  309. }
  310. var jump = ret.url || indexUrl;
  311. jump = mprocEnsureUrlHasFocusEid(jump, loginRedirect);
  312. var feJump = mprocExtractFocusEidFromRedirect(jump) || mprocExtractFocusEidFromRedirect(loginRedirect);
  313. if (feJump > 0) {
  314. try {
  315. sessionStorage.setItem('mproc_focus_eid', String(feJump));
  316. } catch (ignoreSs) {}
  317. }
  318. location.href = jump;
  319. } else {
  320. showToast(ret && ret.msg ? ret.msg : '登录失败', 'err');
  321. }
  322. }
  323. function refreshCaptchaImage() {
  324. if (!$imgCaptcha || !captchaBaseUrl) return;
  325. $imgCaptcha.src = captchaBaseUrl + (captchaBaseUrl.indexOf('?') >= 0 ? '&' : '?') + 'r=' + Date.now();
  326. }
  327. /** 手动换图 / 验证码已被服务端校验消耗后:刷新图片并清空输入 */
  328. function resetCaptchaField() {
  329. refreshCaptchaImage();
  330. if ($captcha) $captcha.value = '';
  331. }
  332. if ($imgCaptcha) {
  333. $imgCaptcha.addEventListener('click', resetCaptchaField);
  334. }
  335. $tabs.forEach(function (btn) {
  336. btn.addEventListener('click', function () {
  337. var tab = btn.getAttribute('data-tab');
  338. $tabs.forEach(function (b) { b.classList.toggle('active', b === btn); });
  339. $panelPhone.classList.toggle('active', tab === 'phone');
  340. $panelAccount.classList.toggle('active', tab === 'account');
  341. if (tab === 'phone') resetCaptchaField();
  342. });
  343. });
  344. function tick() {
  345. if (cd <= 0) {
  346. clearInterval(timer);
  347. timer = null;
  348. $btnSend.disabled = false;
  349. $btnSend.textContent = '获取验证码';
  350. return;
  351. }
  352. $btnSend.textContent = cd + '秒后重发';
  353. cd--;
  354. }
  355. function sendSmsCode() {
  356. var phone = ($phone.value || '').trim();
  357. var captcha = ($captcha && $captcha.value) ? $captcha.value.trim() : '';
  358. if (!/^1\d{10}$/.test(phone)) {
  359. showToast('请输入正确的手机号', 'err');
  360. return;
  361. }
  362. if (!captcha) {
  363. showToast('请输入图形验证码', 'err');
  364. return;
  365. }
  366. if (cd > 0) return;
  367. $btnSend.disabled = true;
  368. $btnSend.textContent = '发送中…';
  369. postForm(sendUrl, { phone: phone, captcha: captcha }, 25000).then(function (ret) {
  370. if (ret && (ret.code === 1 || ret.code === '1')) {
  371. cd = 60;
  372. tick();
  373. timer = setInterval(tick, 1000);
  374. showToast('已发送', 'ok');
  375. } else {
  376. $btnSend.disabled = false;
  377. $btnSend.textContent = '获取验证码';
  378. var errMsg = ret && ret.msg ? ret.msg : '发送失败';
  379. showToast(errMsg, 'err');
  380. // 图形码已通过校验(如短信发送失败)时才换图;填错/手机号/频控等保留原输入
  381. if (/短信发送失败/.test(errMsg)) {
  382. resetCaptchaField();
  383. }
  384. }
  385. }).catch(function (e) {
  386. $btnSend.disabled = false;
  387. $btnSend.textContent = '获取验证码';
  388. if (e && e.name === 'AbortError') {
  389. showToast('请求超时,请稍后再试', 'err');
  390. } else {
  391. showToast('网络错误', 'err');
  392. }
  393. });
  394. }
  395. $btnSend.addEventListener('click', function () {
  396. var active = document.activeElement;
  397. if (active && active !== $btnSend && typeof active.blur === 'function') {
  398. active.blur();
  399. }
  400. // 移动端:先 blur 再读取输入框,避免点击按钮时图形码尚未写入 value
  401. setTimeout(sendSmsCode, 0);
  402. });
  403. $btnLogin.addEventListener('click', function () {
  404. var phone = ($phone.value || '').trim();
  405. var code = ($code.value || '').trim();
  406. if (!/^1\d{10}$/.test(phone)) {
  407. showToast('请输入正确的手机号', 'err');
  408. return;
  409. }
  410. if (!/^\d{6}$/.test(code)) {
  411. showToast('请输入6位验证码', 'err');
  412. return;
  413. }
  414. setSubmitBusy($btnLogin, true, '登录中…');
  415. postForm(loginUrl, { phone: phone, code: code, redirect: loginRedirect }, 25000).then(function (ret) {
  416. setSubmitBusy($btnLogin, false);
  417. goHome(ret);
  418. }).catch(function (e) {
  419. setSubmitBusy($btnLogin, false);
  420. if (e && e.name === 'AbortError') {
  421. showToast('请求超时,请稍后再试', 'err');
  422. } else {
  423. showToast('网络错误', 'err');
  424. }
  425. });
  426. });
  427. $btnLoginPwd.addEventListener('click', function () {
  428. var u = ($username.value || '').trim();
  429. var p = $password.value || '';
  430. if (!u) {
  431. showToast('请输入账号', 'err');
  432. return;
  433. }
  434. if (!p) {
  435. showToast('请输入密码', 'err');
  436. return;
  437. }
  438. setSubmitBusy($btnLoginPwd, true, '登录中…');
  439. postForm(loginPwdUrl, { username: u, password: p, redirect: loginRedirect }, 25000).then(function (ret) {
  440. setSubmitBusy($btnLoginPwd, false);
  441. goHome(ret);
  442. }).catch(function (e) {
  443. setSubmitBusy($btnLoginPwd, false);
  444. if (e && e.name === 'AbortError') {
  445. showToast('请求超时,请稍后再试', 'err');
  446. } else {
  447. showToast('网络错误', 'err');
  448. }
  449. });
  450. });
  451. /** 微信等移动端 Session/Cookie 易失效:用 localStorage 中的 token 自动恢复 7 天登录并回跳原链接(含 focus_eid) */
  452. function mprocTryAutoRestoreLogin() {
  453. var tok = '';
  454. var exp = 0;
  455. try {
  456. tok = localStorage.getItem('mproc_token') || '';
  457. exp = parseInt(localStorage.getItem('mproc_token_exp'), 10) || 0;
  458. } catch (eRead) {
  459. return;
  460. }
  461. if (!tok || !exp || Date.now() > exp) {
  462. if (tok) {
  463. try {
  464. localStorage.removeItem('mproc_token');
  465. localStorage.removeItem('mproc_token_exp');
  466. } catch (eClr) {}
  467. }
  468. return;
  469. }
  470. setSubmitBusy($btnLogin, true, '正在恢复登录…');
  471. if ($btnLoginPwd) {
  472. $btnLoginPwd.disabled = true;
  473. }
  474. var body = 'mproc_token=' + encodeURIComponent(tok);
  475. if (loginRedirect) {
  476. body += '&redirect=' + encodeURIComponent(loginRedirect);
  477. }
  478. fetch(restoreUrl, {
  479. method: 'POST',
  480. credentials: 'same-origin',
  481. headers: {
  482. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  483. 'X-Requested-With': 'XMLHttpRequest',
  484. 'X-Mproc-Token': tok
  485. },
  486. body: body
  487. }).then(function (r) {
  488. return r.json();
  489. }).then(function (ret) {
  490. if (ret && (ret.code === 1 || ret.code === '1')) {
  491. goHome(ret);
  492. return;
  493. }
  494. setSubmitBusy($btnLogin, false);
  495. if ($btnLoginPwd) {
  496. $btnLoginPwd.disabled = false;
  497. }
  498. var msg = ret && ret.msg ? String(ret.msg) : '';
  499. if (/过期|重新登录|未登录|请先登录/i.test(msg)) {
  500. try {
  501. localStorage.removeItem('mproc_token');
  502. localStorage.removeItem('mproc_token_exp');
  503. } catch (eDrop) {}
  504. }
  505. }).catch(function () {
  506. setSubmitBusy($btnLogin, false);
  507. if ($btnLoginPwd) {
  508. $btnLoginPwd.disabled = false;
  509. }
  510. });
  511. }
  512. mprocTryAutoRestoreLogin();
  513. })();
  514. </script>
  515. </body>
  516. </html>