login.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. imgCode: "",
  12. imgCaptcha: ""
  13. },
  14. mounted: function() {
  15. this.getImgCode()
  16. },
  17. methods: {
  18. changeType: function(val){
  19. this.type = val;
  20. },
  21. getCaptcha: function(){
  22. //先校验手机号码是否正确
  23. var isMobile = isMobilePhone(this.mobile);
  24. if(!isMobile){
  25. toastr.error('请输入正确的手机号码!');
  26. return false;
  27. }
  28. this.sendSmsCode()
  29. },
  30. login: function(){
  31. var vm = this
  32. if(this.type == 1){
  33. var validation = $("#form1").valid();
  34. if(validation){
  35. getPublicKey(this);
  36. }
  37. }else{
  38. var validation = $("#form2").valid();
  39. if(validation){
  40. login({
  41. mobile: this.mobile,
  42. captcha: this.captcha,
  43. platform: 4,
  44. imgCaptcha: this.imgCaptcha
  45. }).catch(function() {
  46. vm.getImgCode()
  47. })
  48. }
  49. }
  50. },
  51. getImgCode: getImgCode,
  52. sendSmsCode: sendSmsCode
  53. }
  54. });
  55. function guid() {
  56. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  57. var r = Math.random() * 16 | 0,
  58. v = c == 'x' ? r : (r & 0x3 | 0x8);
  59. return v.toString(16);
  60. });
  61. }
  62. function getImgCode() {
  63. var vm = this
  64. httpRequest.get('/weixin/imgCaptcha', {key: guid()}).then(function(res) {
  65. if(res.status == 200){
  66. vm.imgCode = res.data.image
  67. } else {
  68. toastr.error('获取图形验证码失败');
  69. }
  70. })
  71. }
  72. function getSmsImgCode() {
  73. var vm = this
  74. httpRequest.get('/weixin/imgCaptcha', {key: guid()}).then(function(res) {
  75. if(res.status == 200){
  76. $("#imgSmsCode img").attr('src', res.data.image)
  77. } else {
  78. toastr.error('获取图形验证码失败');
  79. }
  80. })
  81. }
  82. //验证码定时
  83. function setTimer(vm) {
  84. var seconds = 59;
  85. vm.countdown = true;
  86. timer = setInterval(function() {
  87. if(seconds == 0) {
  88. clearInterval(timer);
  89. timer = null;
  90. seconds = 59;
  91. vm.infoText = "获取验证码";
  92. vm.countdown = false;
  93. return ;
  94. }
  95. seconds --;
  96. vm.infoText = seconds+"s后重新获取";
  97. }, 1000)
  98. }
  99. function sendToGetCaptcha(vm){
  100. var url = "common/captcha",
  101. params = {
  102. mobile: vm.mobile,
  103. type: 5,
  104. captchaToken: 5,
  105. imgCaptcha: vm.imgCaptcha
  106. };
  107. setTimer(this);
  108. httpRequest.post(url, {data: params}).then(function(res){
  109. if(res.status == 200){
  110. toastr.success('发送成功');
  111. }else{
  112. toastr.error('获取验证码失败');
  113. }
  114. });
  115. }
  116. function getPublicKey(vm){
  117. var url = "login/public_key";
  118. httpRequest.post(url).then(function(res){
  119. if(res.status){
  120. var mod = res.data.modulus;
  121. var exp = res.data.exponent;
  122. key = RSAUtils.getKeyPair(exp, "", mod);
  123. if (key) {
  124. encryedPwd = RSAUtils.encryStr(key, vm.password);
  125. login({
  126. mobile: vm.mobile,
  127. password: encryedPwd,
  128. platform: 4,
  129. imgCaptcha: vm.imgCaptcha
  130. })
  131. }else {
  132. toastr.error("获取数据失败")
  133. $submit.removeAttr('disabled')
  134. }
  135. }else{
  136. tip(res.msg);
  137. }
  138. })
  139. }
  140. function login(data) {
  141. return httpRequest.post('login/doctor', {data: data}).then(function(res){
  142. if(res.status == 200){
  143. var docInfo = res.data;
  144. localStorage.setItem(httpRequest.agentName,JSON.stringify({
  145. id: docInfo.id,
  146. uid: docInfo.uid,
  147. token: docInfo.token,
  148. imei: localStorage.getItem('WLYY_IMEI'),
  149. platform: 4
  150. }));
  151. localStorage.setItem("app_storage", JSON.stringify({
  152. 'api_login_doctor': docInfo,
  153. 'IMEI': localStorage.getItem('WLYY_IMEI')
  154. }))
  155. //将用户的角色信息单独存储在localstorage中
  156. localStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
  157. sessionStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
  158. window.location.href = "../home/html/index.html";
  159. }else{
  160. toastr.error(res.msg);
  161. return Promise.reject()
  162. }
  163. });
  164. }
  165. function sendSmsCode() {
  166. var vm = this
  167. dialog({
  168. title:'验证码',
  169. content: '<div id="imgSmsCode" class="form-group" style="display: -webkit-flex;display: flex;">\
  170. <input id="imgSmsCaptchaCode" class="form-control" type="text" placeholder="请输入图形验证码" style="-webkit-flex: 1;flex: 1;">\
  171. <img style="width: 120px;height: 34px;" onclick="getSmsImgCode()"/>\
  172. </div>',
  173. okValue:'发送',
  174. cancelValue: "取消",
  175. ok: function (){
  176. var imgCaptcha = $('#imgSmsCaptchaCode').val()
  177. if(!imgCaptcha) {
  178. toastr.error('请输入图形验证码')
  179. return false
  180. }
  181. vm.imgCaptcha = imgCaptcha
  182. sendToGetCaptcha(vm)
  183. },
  184. cancel: function() {
  185. }
  186. }).showModal();
  187. setTimeout(function() {
  188. getSmsImgCode()
  189. }, 100)
  190. }