login.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. (function() {
  2. Vue.component('login-form', {
  3. template: `<div><div class="container-fluid">
  4. <div class="container" id="head">
  5. <div class="ptb30 top-banner">
  6. <img src="../../../images/LOGO.png"/>
  7. <span class="qr-container">
  8. <img src="../../../images/qr_code.png" class="qr_code" />
  9. <p class="c-f12 text-center c-909090" style="padding-top:5px">下载APP</p>
  10. </span>
  11. </div>
  12. </div>
  13. </div>
  14. <div class="container-fluid chahua-bg">
  15. <div class="chahua container">
  16. <div class="loginForm fr" :class="{'loginForm-active':!registerFlag}">
  17. <div class="tabs-container no-borders mtb20 plr30">
  18. <ul class="nav nav-tabs c-border-b">
  19. <li :class="{'active': type==1}" @click="changeType(1)">
  20. <a style="padding:10px 0px;margin-left:35px;margin-right: 35px;font-size: 20px;">密码登录</a>
  21. </li>
  22. <li :class="{'active': type==2}" @click="changeType(2)">
  23. <a style="padding:10px 0px;margin-left:25px;margin-right: 25px;font-size: 20px;">验证码登录</a>
  24. </li>
  25. </ul>
  26. <div class="tab-content">
  27. <div class="tab-pane" :class="{'active': type==1}">
  28. <div class="panel-body no-borders" style="padding: 0px;">
  29. <form id="form1" class="m-t" role="form" onsubmit="return false;">
  30. <div class="form-group">
  31. <input class="form-control" type="text" v-model="account" placeholder="请输入身份证号或者手机号码">
  32. </div>
  33. <div class="form-group">
  34. <input class="form-control" type="password" v-model="password" placeholder="请输入密码">
  35. </div>
  36. <div class="text-right c-323232" style="margin-top:-10px;">忘记密码</div>
  37. <button type="submit" class="btn btn-theme block full-width m-b" @click="login">登 录</button>
  38. <a type="button" class="btn btn-default block full-width m-b" @click="toggleRegister">注 册</a>
  39. </form>
  40. </div>
  41. </div>
  42. <div class="tab-pane" :class="{'active': type==2}">
  43. <div class="panel-body no-borders" style="padding: 0px;">
  44. <form id="form2" class="m-t" role="form" onsubmit="return false;">
  45. <div class="form-group">
  46. <input class="form-control" type="text" maxlength="11" v-model="mobile" placeholder="请输入手机号码">
  47. </div>
  48. <div class="form-group">
  49. <input class="form-control" type="text" v-model="imgcaptcha" placeholder="请输入图形验证码">
  50. <a class="getCaptcha" :class="{'disabled': countdown}" @click="getCaptcha">{{infoText}}</a>
  51. </div>
  52. <div class="form-group">
  53. <input class="form-control" type="text" v-model="captcha" placeholder="请输入短信验证码">
  54. <a class="getCaptcha" :class="{'disabled': countdown}" @click="getCaptcha">{{infoText}}</a>
  55. </div>
  56. <button type="submit" class="btn btn-theme block full-width m-b" @click="login">登 录</button>
  57. <a type="button" class="btn btn-default block full-width m-b" @click="toggleRegister">注 册</a>
  58. </form>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </div></div>`,
  66. props: [],
  67. data: function() {
  68. return {
  69. type: 1, //记录选择的登录方式,1-手机号登录,2-验证码登录
  70. account: '17500000001',
  71. mobile: '',
  72. password: '123456',
  73. imgcaptcha: '',
  74. captcha: '',
  75. infoText: '获取验证码',
  76. countdown: false, //是否在倒计时
  77. codeType: 1,
  78. idCardNo: "350526199501142015",
  79. rPassword: 'a123123',
  80. confirmPassword: "a123123",
  81. toggle: true,
  82. registerFlag: false,
  83. maxLength: 18,
  84. }
  85. },
  86. methods: {
  87. changeType: function(val) {
  88. this.type = val;
  89. },
  90. getCaptcha: function() {
  91. //先校验手机号码是否正确
  92. var isMobile = isMobilePhone(this.mobile);
  93. if(!isMobile) {
  94. alert('请输入正确的手机号码!');
  95. return false;
  96. }
  97. setTimer(this);
  98. //发送请求
  99. // sendToGetCaptcha(this);
  100. },
  101. login: function() {
  102. var vm = this
  103. if(this.type == 1) {
  104. if(!vm.account) {
  105. console.log("账号不能为空!");
  106. return false;
  107. }
  108. if(!vm.password) {
  109. console.log("登录密码不能为空!");
  110. return false;
  111. }
  112. console.log(httpRequest)
  113. var data = {
  114. grant_type: httpRequest.grant_type,
  115. client_id: httpRequest.client_id,
  116. username: vm.account,
  117. password: vm.password
  118. }
  119. console.log(data)
  120. loginAPI.accessToken(data).then(function(oauthInfo) {
  121. storage.setItem("oauthInfo", JSON.stringify(oauthInfo));
  122. var data1 = {
  123. userName: oauthInfo.user,
  124. accessToken: oauthInfo.accessToken
  125. }
  126. loginAPI.getUserInfo(data1).then(function(res) {
  127. console.log("登录成功");
  128. loginedList = [res.telephone]
  129. storage.setItem("loginedList", JSON.stringify(loginedList));
  130. storage.setItem("userAgent", JSON.stringify(res));
  131. vm.getJueSeData(res);
  132. }),
  133. function error(xhr, type, errorThrown) {
  134. if(type == "timeout" || type == "abort" || type == "error") {
  135. console.log("账号或密码错误");
  136. }
  137. }
  138. }).catch(function(e) {
  139. console.log("账号或密码错误")
  140. })
  141. } else {
  142. if(!vm.mobile) {
  143. console.log("手机号不能为空!");
  144. return false;
  145. }
  146. if(!vm.imgcaptcha) {
  147. console.log("图形验证码不能为空!");
  148. return false;
  149. }
  150. if(!vm.captcha) {
  151. console.log("手机验证码不能为空!");
  152. return false;
  153. }
  154. }
  155. },
  156. getJueSeData: function(userAgent) {
  157. var vm = this;
  158. //获取居民权限接口
  159. loginAPI.findByUserId({
  160. userId: userAgent.id
  161. }).then(function(data) {
  162. storage.setItem("roleList", JSON.stringify(data.detailModelList)); //缓存角色列表
  163. // app.openWebviewExtras("../../home/html/home.html");//跳转到主页面
  164. })
  165. },
  166. toggleRegister: function() {
  167. location.href = "../../register/html/register.html"
  168. }
  169. },
  170. watch: {
  171. Password: function(newVal, oldVal) {
  172. var val = newVal.toString();
  173. var reg = /\s+/g; //去除空格
  174. if(reg.test(val)) {
  175. this.Password = oldVal;
  176. }
  177. },
  178. confirmPassword: function(newVal, oldVal) {
  179. var val = newVal.toString();
  180. var reg = /\s+/g; //去除空格
  181. if(reg.test(val)) {
  182. this.confirmPassword = oldVal;
  183. }
  184. },
  185. codeType: function(newVal) {
  186. var vm = this;
  187. var val = newVal.toString();
  188. if(val == 1) {
  189. $("#IDCard").attr("placeholder", "请输入身份证号")
  190. vm.maxLength = 18
  191. } else if(val == 2) {
  192. $("#IDCard").attr("placeholder", "请输入手机号码")
  193. vm.maxLength = 11
  194. }
  195. }
  196. }
  197. })
  198. //验证码定时
  199. function setTimer(vm) {
  200. var seconds = 59;
  201. vm.countdown = true;
  202. timer = setInterval(function() {
  203. if(seconds == 0) {
  204. clearInterval(timer);
  205. timer = null;
  206. seconds = 59;
  207. vm.infoText = "获取验证码";
  208. vm.countdown = false;
  209. return;
  210. }
  211. seconds--;
  212. vm.infoText = seconds + "s后重新获取";
  213. }, 1000)
  214. }
  215. function isCardNo(card, codeType) {
  216. var isidcard = false
  217. if(codeType == 1) { // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
  218. var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  219. if(!reg.test(card)) {
  220. alert("身份证号输入不合法");
  221. } else {
  222. isidcard = true
  223. }
  224. } else {
  225. var reg = /0?(13|14|15|18|17)[0-9]{9}/;
  226. if(!reg.test(card)) {
  227. alert("手机号码输入不合法");
  228. } else {
  229. isidcard = true
  230. }
  231. }
  232. return isidcard
  233. }
  234. function sendToGetCaptcha(vm) {
  235. var url = "common/captcha",
  236. params = {
  237. mobile: vm.mobile,
  238. type: 5,
  239. captchaToken: 5
  240. };
  241. httpRequest.post(url, {
  242. data: params
  243. }).then(function(res) {
  244. if(res.status == 200) {
  245. tip('发送成功', 'add');
  246. } else {
  247. tip('获取验证码失败');
  248. }
  249. });
  250. }
  251. function getPublicKey(vm) {
  252. var url = "login/public_key";
  253. httpRequest.post(url).then(function(res) {
  254. if(res.status) {
  255. var mod = res.data.modulus;
  256. var exp = res.data.exponent;
  257. key = RSAUtils.getKeyPair(exp, "", mod);
  258. if(key) {
  259. encryedPwd = RSAUtils.encryStr(key, vm.password);
  260. login({
  261. mobile: vm.mobile,
  262. password: encryedPwd,
  263. platform: 4
  264. })
  265. } else {
  266. tip("获取数据失败")
  267. $submit.removeAttr('disabled')
  268. }
  269. } else {
  270. tip(res.msg);
  271. }
  272. })
  273. }
  274. function login(data) {
  275. httpRequest.post('login/doctor', {
  276. data: data
  277. }).then(function(res) {
  278. if(res.status == 200) {
  279. var docInfo = res.data;
  280. localoginlStorage.setItem(httpRequest.agentName, JSON.stringify({
  281. id: docInfo.id,
  282. uid: docInfo.uid,
  283. token: docInfo.token,
  284. imei: localStorage.getItem('WLYY_IMEI'),
  285. platform: 4
  286. }));
  287. //将用户的角色信息单独存储在localstorage中
  288. localStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
  289. window.location.href = "../home/html/index.html";
  290. } else {
  291. tip(res.msg);
  292. }
  293. });
  294. }
  295. })()