loginAgent.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Coding登录</title>
  6. </head>
  7. <body>
  8. <script>
  9. var GWC = {
  10. version: '1.0.0',
  11. urlParams: {},
  12. appendParams: function(url, params) {
  13. if (params) {
  14. var baseWithSearch = url.split('#')[0];
  15. var hash = url.split('#')[1];
  16. for (var key in params) {
  17. var attrValue = params[key];
  18. if (attrValue !== undefined) {
  19. var newParam = key + "=" + attrValue;
  20. if (baseWithSearch.indexOf('?') > 0) {
  21. var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
  22. if (oldParamReg.test(baseWithSearch)) {
  23. baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
  24. } else {
  25. baseWithSearch += "&" + newParam;
  26. }
  27. } else {
  28. baseWithSearch += "?" + newParam;
  29. }
  30. }
  31. }
  32. if (hash) {
  33. url = baseWithSearch + '#' + hash;
  34. } else {
  35. url = baseWithSearch;
  36. }
  37. }
  38. return url;
  39. },
  40. getUrlParams: function() {
  41. var pairs = location.search.substring(1).split('&');
  42. for (var i = 0; i < pairs.length; i++) {
  43. var pos = pairs[i].indexOf('=');
  44. if (pos === -1) {
  45. continue;
  46. }
  47. GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
  48. }
  49. },
  50. doRedirect: function() {
  51. var code = GWC.urlParams['code'];
  52. var appId = GWC.urlParams['client_id'];
  53. var scope = GWC.urlParams['scope'];
  54. var response_type = GWC.urlParams['response_type'];
  55. var redirectUri;
  56. if (!code) {
  57. //第一步,没有拿到code,跳转至授权页面获取code
  58. redirectUri = GWC.appendParams('https://coding.net/oauth_authorize.html', {
  59. 'client_id': appId,
  60. 'redirect_uri': encodeURIComponent(location.href),
  61. 'response_type': response_type,
  62. 'scope': scope,
  63. });
  64. } else {
  65. //第二步,从授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
  66. redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {
  67. 'code': code,
  68. });
  69. }
  70. location.href = redirectUri;
  71. }
  72. };
  73. GWC.getUrlParams();
  74. GWC.doRedirect();
  75. </script>
  76. </body>
  77. </html>