qrcode.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * 生成唯一uuid
  3. * @returns uuid
  4. */
  5. function generateUUID() {
  6. var d = new Date().getTime();
  7. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
  8. function(c) {
  9. var r = (d + Math.random() * 16) % 16 | 0;
  10. d = Math.floor(d / 16);
  11. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  12. });
  13. return uuid;
  14. };
  15. /**
  16. * 超时取消轮询
  17. * @param polling
  18. * @param clientId
  19. */
  20. function cancelPollingByTimeout(polling,clientId){
  21. clearInterval(polling);
  22. //轮询结束,使二维码图片失效,通知后台去掉对应的map
  23. $.ajax({
  24. url: basePath + "common/cancelQrcodeLogin.action",
  25. type:"get",
  26. data:{
  27. "clientId":clientId
  28. },
  29. dataType:"json",
  30. success:function(result){
  31. if (result.success) {
  32. $(".qrcode-img").attr("src",basePath+"resource/sources/image/errorQrcode.png");
  33. $("#qrcode_refresh").show();
  34. }
  35. }
  36. });
  37. }
  38. /**
  39. * 手动点击按钮取消轮询
  40. * @param polling
  41. * @param clientId
  42. */
  43. function cancelPollingByClick(polling,clientId){
  44. clearInterval(polling);
  45. //轮询结束,使二维码图片失效,通知后台去掉对应的map
  46. $.ajax({
  47. url: basePath + "common/cancelQrcodeLogin.action",
  48. type:"get",
  49. data:{
  50. "clientId":clientId
  51. },
  52. dataType:"json",
  53. success:function(result){
  54. if (result.success) {
  55. }
  56. }
  57. });
  58. }
  59. /**
  60. * 检查登录
  61. * @param clientId
  62. */
  63. function checkQrcodeLogin(clientId){
  64. polling = setInterval(function(){
  65. $.ajax({
  66. url: basePath + "common/checkQrcodeLogin.action",
  67. type:"get",
  68. data:{
  69. "clientId":clientId
  70. },
  71. dataType:"json",
  72. success:function(result){
  73. //返回登录结果成功,停止论询
  74. if (result.success) {
  75. clearInterval(polling);
  76. //重新载入页面
  77. document.location.href = basePath;
  78. }else{
  79. //如果有错误原因
  80. if (result.reason) {
  81. //取消轮询
  82. clearInterval(polling);
  83. //切换二维码刷新按钮
  84. $(".qrcode-img").attr("src",basePath+"resource/sources/image/errorQrcode.png");
  85. $("#qrcode_refresh").show();
  86. //展示错误原因
  87. $.showtip(result.reason, 6000);
  88. }
  89. }
  90. }
  91. })
  92. },2000);
  93. //50s后取消轮询
  94. setTimeout(function(){
  95. if(clientId != "" && polling != ""){
  96. cancelPollingByTimeout(polling,clientId)
  97. }
  98. },50000);
  99. return polling;
  100. };
  101. /**
  102. * 页面加载完成后,执行的函数
  103. */
  104. $(function(){
  105. var qrcodeFlag = true; //状态,用于二维码与普通登陆之间的切换
  106. //全局唯一的clientId
  107. var clientId="";
  108. //全局唯一的轮询对象
  109. var polling="";
  110. //二维码切换按钮点击事件
  111. $("#qrcode-btn").click(function(){
  112. if(qrcodeFlag){
  113. //切换右上角图标
  114. $("#qrcode-btn").css("background","url(resource/sources/image/closeQrcode.png) no-repeat");
  115. //切换界面
  116. $(".prmiary-login").hide();
  117. $(".qrcode-login").show();
  118. //如果有轮询,清除
  119. if (!(""==clientId&&""==polling)) {
  120. clearInterval(polling);
  121. clientId="";
  122. polling="";
  123. }
  124. clientId = generateUUID();
  125. //请求生成带uuid的二维码
  126. $(".qrcode-img").attr("src",basePath + "common/qrcode.action"+"?clientId="+clientId)
  127. polling = checkQrcodeLogin(clientId);
  128. qrcodeFlag = !qrcodeFlag;
  129. }else{
  130. //切换右上角图标
  131. $("#qrcode-btn").css("background","url(resource/sources/image/changeQrcode.png) no-repeat");
  132. //切换界面
  133. $(".qrcode-login").hide();
  134. $(".prmiary-login").show();
  135. //隐藏刷新按钮
  136. $("#qrcode_refresh").hide();
  137. //如果有轮询,清除
  138. if (!(""==clientId&&""==polling)) {
  139. cancelPollingByClick(polling,clientId);
  140. clientId="";
  141. polling="";
  142. }
  143. qrcodeFlag = !qrcodeFlag;
  144. }
  145. }); //切换按钮点击事件 函数结尾
  146. //二维码刷新按钮点击事件
  147. $("#qrcode_refresh").click(function() {
  148. //隐藏刷新按钮
  149. $("#qrcode_refresh").hide();
  150. // 如果有轮询,清除
  151. // if (!("" == clientId && "" == polling)) {
  152. // clientId = "";
  153. // polling = "";
  154. // }
  155. clientId = generateUUID();
  156. // 请求生成带uuid的二维码
  157. $(".qrcode-img").attr("src",basePath + "common/qrcode.action" + "?clientId="+ clientId);
  158. polling = checkQrcodeLogin(clientId);
  159. });
  160. })