login.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. new Vue({
  2. el: "#main",
  3. data: {
  4. type: 1, //记录选择的登录方式,1-手机号登录,2-验证码登录
  5. mobile: '',
  6. password: '',
  7. captcha: '',
  8. eyeIsOpen: false,
  9. infoText: '获取验证码',
  10. countdown: false //是否在倒计时
  11. },
  12. methods: {
  13. changeType: function(val){
  14. this.type = val;
  15. },
  16. getCaptcha: function(){
  17. //先校验手机号码是否正确
  18. var isMobile = isMobilePhone(this.mobile);
  19. if(!isMobile){
  20. tip('请输入正确的手机号码!');
  21. return false;
  22. }
  23. setTimer(this);
  24. //发送请求
  25. sendToGetCaptcha(this);
  26. },
  27. login: function(){
  28. if(this.type == 1){
  29. var validation = $("#form1").valid();
  30. if(validation){
  31. getPublicKey(this);
  32. }
  33. }else{
  34. var validation = $("#form2").valid();
  35. if(validation){
  36. login({
  37. mobile: this.mobile,
  38. captcha: this.captcha,
  39. platform: 4
  40. })
  41. }
  42. }
  43. },
  44. }
  45. });
  46. //验证码定时
  47. function setTimer(vm) {
  48. var seconds = 59;
  49. vm.countdown = true;
  50. timer = setInterval(function() {
  51. if(seconds == 0) {
  52. clearInterval(timer);
  53. timer = null;
  54. seconds = 59;
  55. vm.infoText = "获取验证码";
  56. vm.countdown = false;
  57. return ;
  58. }
  59. seconds --;
  60. vm.infoText = seconds+"s后重新获取";
  61. }, 1000)
  62. }
  63. function sendToGetCaptcha(vm){
  64. var url = "common/captcha",
  65. params = {
  66. mobile: vm.mobile,
  67. type: 5,
  68. captchaToken: 5
  69. };
  70. httpRequest.post(url, {data: params}).then(function(res){
  71. if(res.status == 200){
  72. tip('发送成功','add');
  73. }else{
  74. tip('获取验证码失败');
  75. }
  76. });
  77. }
  78. function getPublicKey(vm){
  79. var url = "login/public_key";
  80. httpRequest.post(url).then(function(res){
  81. if(res.status){
  82. var mod = res.data.modulus;
  83. var exp = res.data.exponent;
  84. key = RSAUtils.getKeyPair(exp, "", mod);
  85. if (key) {
  86. encryedPwd = RSAUtils.encryStr(key, vm.password);
  87. login({
  88. mobile: vm.mobile,
  89. password: encryedPwd,
  90. platform: 4
  91. })
  92. }else {
  93. tip("获取数据失败")
  94. $submit.removeAttr('disabled')
  95. }
  96. }else{
  97. tip(res.msg);
  98. }
  99. })
  100. }
  101. function login(data) {
  102. httpRequest.post('login/doctor', {data: data}).then(function(res){
  103. if(res.status == 200){
  104. var docInfo = res.data;
  105. localStorage.setItem(httpRequest.agentName,JSON.stringify({
  106. id: docInfo.id,
  107. uid: docInfo.uid,
  108. token: docInfo.token,
  109. imei: localStorage.getItem('WLYY_IMEI'),
  110. platform: 4
  111. }));
  112. localStorage.setItem("app_storage", JSON.stringify({
  113. 'api_login_doctor': docInfo,
  114. 'IMEI': localStorage.getItem('WLYY_IMEI')
  115. }))
  116. //将用户的角色信息单独存储在localstorage中
  117. localStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
  118. window.location.href = "../home/html/index.html";
  119. }else{
  120. tip(res.msg);
  121. }
  122. });
  123. }