loginGate.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function LoginGate() {
  2. let loginPopup = null, style = null;
  3. function closePopup() {
  4. document.body.removeChild(loginPopup);
  5. document.head.removeChild(style);
  6. }
  7. function onSubmit() {
  8. const idNo = $('#idNo').val();
  9. const telExt = $('#telExt').val();
  10. let output = false;
  11. if (!/\d{4}/.test(idNo)) {
  12. $('.invalid-feedback--idNo').css('display', 'block');
  13. } else {
  14. $('.invalid-feedback--idNo').css('display', 'none');
  15. output = true;
  16. }
  17. if (!/\d{3}/.test(telExt)) {
  18. $('.invalid-feedback--telExt').css('display', 'block');
  19. } else {
  20. $('.invalid-feedback--telExt').css('display', 'none');
  21. output = true;
  22. }
  23. if (!output) return;
  24. $.ajax({
  25. url: 'https://ogilvytw.azurewebsites.net/api/verifyMember.ashx',
  26. data: {
  27. idNo: idNo,
  28. telExt: telExt,
  29. },
  30. success: (response) => {
  31. if (!response.success) {
  32. alert(response.msg);
  33. return;
  34. }
  35. alert('登入成功');
  36. $.cookie('innovationlab_cognitive_user', idNo + ':' + telExt, { path: '/' }); // expires: 30,
  37. closePopup();
  38. afterLogin();
  39. },
  40. error: err => {
  41. console.log(err);
  42. alert(err)
  43. }
  44. });
  45. }
  46. function init() {
  47. const customcss = '.login__popup{position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,.8);z-index:1000}.login__popup table,.login__popup tr,.login__popup td{border:none;vertical-align:top}.login__popup .container{position:relative;background-color:#fff;padding-top:10px;padding-bottom:50px}.login__popup .button__close{font-size:36px;background:unset;border:none;color:#000;position:absolute;right:10px;top:0;cursor:pointer}.login__popup h2{padding-top:50px}.login__popup label{position:relative;width:160px;text-align:left}'
  48. style = document.createElement('style');
  49. style.innerText = customcss;
  50. document.head.appendChild(style);
  51. loginPopup = document.createElement('div');
  52. loginPopup.className = 'login__popup';
  53. loginPopup.innerHTML = '<div class="login__popup"><div class="container mt-5"><button type="button" class="button__close" aria-label="Close"><span aria-hidden="true">&times;</span></button><h2 class="text-center">Welcome To Innovation Lab</h2><div><table class="mx-auto"><tbody><tr class="form-group"><td><label for="idNo">請輸入身分證後四碼:</label></td><td><input type="text" id="idNo" maxlength="4"><div class="invalid-feedback invalid-feedback--idNo">身分證後四碼不正確</div></td></tr><tr class="form-group"><td><label for="idNo">請輸入分機號碼:</label></td><td><input type="text" id="telExt" maxlength="3"><div class="invalid-feedback invalid-feedback--telExt">分機號碼不正確</div></td></tr></tbody></table><div class="mt-3" align="center"><button class="button__login" class="mx-auto">登入使用服務</button></div></div><hr></div></div>';
  54. document.body.appendChild(loginPopup);
  55. loginPopup.querySelector('.button__login').addEventListener('click', onSubmit);
  56. loginPopup.querySelector('.button__close').addEventListener('click', closePopup);
  57. }
  58. function beforeLogin() {
  59. $('.app').css('display', 'none');
  60. }
  61. function afterLogin() {
  62. $('.app').css('display', 'block');
  63. }
  64. function execute() {
  65. beforeLogin();
  66. if ($.cookie('innovationlab_cognitive_user')) {
  67. afterLogin();
  68. return;
  69. }
  70. init();
  71. }
  72. {
  73. $( document ).ready(function() {
  74. execute();
  75. })
  76. }
  77. }
  78. window.loginGate = new LoginGate();