signin.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class Utils {
  2. /**
  3. * 发送Get请求
  4. *
  5. * @param {!string} url 请求地址
  6. * @param {?function} next 回调函数
  7. */
  8. static ajaxGet(url, next) {
  9. let xhr = new XMLHttpRequest();
  10. if (url.includes('?')) {
  11. // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
  12. xhr.open('GET', `${url}&date=${new Date().getTime()}`, true);
  13. } else {
  14. // 添加一个date参数加入当前时间以避免缓存
  15. xhr.open('GET', `${url}&date=${new Date().getTime()}`, true);
  16. }
  17. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  18. // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
  19. xhr.withCredentials = true;
  20. xhr.responseType = 'json';
  21. xhr.onload = function (res) {
  22. // 获取请求接口返回值
  23. let response = res.target.response;
  24. next && next(response);
  25. };
  26. xhr.send();
  27. }
  28. /**
  29. * 发送Post请求
  30. *
  31. * @param {!string} url 请求地址
  32. * @param {?string} data post请求参数
  33. * @param {?function} next 回调函数
  34. */
  35. static ajaxPost(url, data, next) {
  36. let xhr = new XMLHttpRequest();
  37. if (url.includes('?')) {
  38. // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
  39. xhr.open("POST", `${url}&${new Date().getTime()}`, true);
  40. } else {
  41. // 添加一个date参数加入当前时间以避免缓存
  42. xhr.open("POST", `${url}?${new Date().getTime()}`, true);
  43. }
  44. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  45. // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
  46. xhr.withCredentials = true;
  47. xhr.responseType = 'json';
  48. xhr.onload = function (res) {
  49. // 获取请求接口返回值
  50. let response = res.target.response;
  51. next && next(response);
  52. };
  53. xhr.send(data);
  54. }
  55. }
  56. window.onload = function () {
  57. let signinButton = document.querySelector('#signin-button')
  58. signinButton.addEventListener('click', () => {
  59. let userId = document.querySelector('body > div.content > div.content1 > ul > li:nth-child(1) > input').value
  60. let password = document.querySelector('body > div.content > div.content1 > ul > li:nth-child(2) > input').value
  61. let cCode = document.querySelector('body > div.content > div.content1 > ul > li:nth-child(3) > input').value
  62. let signInUrl = 'http://admin.cloudsql.1473.cn/v1/signin'
  63. let postData = `userId=${userId}&password=${password}&captcha=${cCode}`
  64. Utils.ajaxPost(signInUrl, postData, res => {
  65. if (res.status == 'signedin') {
  66. //跳转到首页
  67. window.location.href = 'index.html'
  68. } else {
  69. alert('登陆失败')
  70. }
  71. })
  72. })
  73. }