login.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. var userAgent = window.localStorage.getItem('wlyyAgentForDoc')
  2. var $eyeIcon = $('.psw-eye'),
  3. $mobile1 = $('#mobile1'),
  4. $password = $('#password'),
  5. $mobile2 = $('#mobile2'),
  6. $captcha = $('#captcha'),
  7. $mobile = $('input[mobile]'),
  8. $tabs = $('.nav-tabs'),
  9. $getCaptcha = $('#getCaptcha'),
  10. $submit = $('button[type="submit"]');
  11. var timer = null;
  12. $(function() {
  13. bindEvent()
  14. function bindEvent(){
  15. //密码隐藏
  16. $eyeIcon.click(function() {
  17. $el = $(this);
  18. if($el.hasClass('fa-eye')) {
  19. $el.removeClass('fa-eye').addClass('fa-eye-slash');
  20. $password.attr('type','password')
  21. } else {
  22. $el.removeClass('fa-eye-slash').addClass('fa-eye');
  23. $password.attr('type','text')
  24. }
  25. })
  26. //验证码定时
  27. function setTimer() {
  28. $getCaptcha.addClass('disabled');
  29. var seconds = 59;
  30. timer = setInterval(function() {
  31. if(seconds == 0) {
  32. clearInterval(timer);
  33. timer = null;
  34. seconds = 59;
  35. $getCaptcha.text("获取验证码");
  36. $getCaptcha.removeClass('disabled');
  37. return ;
  38. }
  39. $getCaptcha.text((seconds--)+"s后重新获取");
  40. }, 1000)
  41. }
  42. //表单验证
  43. var validator1 = $("#form1").validate({
  44. onsubmit: false,
  45. onfocusout: function(ele) {
  46. validator1.element(ele)
  47. },
  48. rules: {
  49. mobile1: {
  50. required: true,
  51. mobile: true,
  52. },
  53.    password: "required",
  54. }
  55. });
  56. var validator2 = $("#form2").validate({
  57. onsubmit: false,
  58. onfocusout: function(ele) {
  59. validator2.element(ele)
  60. },
  61. rules: {
  62. mobile2: {
  63. required: true,
  64. mobile: true,
  65. },
  66.    captcha: "required",
  67. }
  68. });
  69. jQuery.validator.addMethod("mobile", function(value, element, param) {
  70. var reg=/^[1][3,4,5,7,8][0-9]{9}$/;
  71. return reg.test(value);
  72. }, "手机号码有误");
  73. //切换tab
  74. $tabs.on('click','li',function() {
  75. var idx = $(this).index(),
  76. mob = '';
  77. if(idx == 1) { // 点击验证码登录选项卡
  78. mob = $mobile1.val();
  79. if(mob) {
  80. $mobile.val(mob);
  81. validator2.element($mobile2)
  82. }
  83. } else { // 点击手机登陆选项卡
  84. mob = $mobile2.val();
  85. if(mob) {
  86. $mobile.val(mob);
  87. validator1.element($mobile1)
  88. }
  89. }
  90. })
  91. //发送短信
  92. $getCaptcha.click(function() {
  93. if($getCaptcha.hasClass('disabled')) {
  94. return ;
  95. }
  96. if(validator2.element($mobile2)) {
  97. setTimer();
  98. sendPost('common/captcha',{mobile: $mobile2.val(),type: 5,captchaToken: 5},function(){
  99. ask('请求失败')
  100. },function(res){
  101. if(res.status == 200){
  102. tip('发送成功','add')
  103. }else{
  104. tip('获取验证码失败')
  105. }
  106. })
  107. }
  108. })
  109. //提交
  110. $submit.click(function() {
  111. var index = $('.nav-tabs>li.active').index();
  112. var mobile = '',
  113. password = '',
  114. captcha = '';
  115. if(index == 0) { // 点击手机登陆选项卡
  116. if($("#form1").valid()){
  117. $(this).attr('disabled','disabled')
  118. mobile = $mobile1.val();
  119. password = $password.val();
  120. sendPost('login/public_key',{},function(){
  121. tip('请求失败')
  122. $submit.removeAttr('disabled')
  123. },function(res){
  124. if (res.status == 200) {
  125. var mod = res.data.modulus;
  126. var exp = res.data.exponent;
  127. key = RSAUtils.getKeyPair(exp, "", mod);
  128. if (key) {
  129. encryedPwd = RSAUtils.encryStr(key, password);
  130. login({
  131. mobile: mobile,
  132. password: encryedPwd,
  133. platform: 4
  134. })
  135. }else {
  136. tip("获取数据失败")
  137. $submit.removeAttr('disabled')
  138. }
  139. }
  140. })
  141. }
  142. }else{ // 点击验证码登录选项卡
  143. if($("#form2").valid()) {
  144. $(this).attr('disabled','disabled')
  145. mobile = $mobile2.val();
  146. captcha = $captcha.val();
  147. login({
  148. mobile: mobile,
  149. captcha: captcha,
  150. platform: 4
  151. })
  152. }
  153. }
  154. })
  155. }
  156. })
  157. //登录
  158. function login(data) {
  159. sendPost('login/doctor',data,function(){
  160. $submit.removeAttr('disabled')
  161. tip('请求失败')
  162. },function(res){
  163. $submit.removeAttr('disabled')
  164. if(res.status == 200) {
  165. var data = res.data;
  166. //添加医生到userRole
  167. localStorage.setItem('wlyyAgentForDoc',JSON.stringify(data))
  168. alert("登录成功")
  169. location.href = "app/record/html/record.html"
  170. }else{
  171. tip(res.msg)
  172. }
  173. })
  174. }