login.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <template>
  2. <div id="skin-blur-violate">
  3. <section id="login">
  4. <header>
  5. <img src="../static/images/login/logo.png" height="109" width="109" id="logo"/>
  6. <img src="../static/images/login/title.png" height="100%" width="274px"/>
  7. </header>
  8. <div class="clearfix"></div>
  9. <!-- Login -->
  10. <form class="box tile animated active" id="box-login">
  11. <h2 class="m-t-0 m-b-15"></h2>
  12. <div class="rowHeight">
  13. <input type="text" class="login-control login_account" placeholder="输入手机号"
  14. v-model="login.phone">
  15. </div>
  16. <div class="rowHeight">
  17. <input type="number" class="login-control login_valid" placeholder="输入右侧图形码"
  18. v-model="login.uservalid">
  19. <img id="imgValidcode" :src="valImgSrc" :title="InvisibilityGiveitatry" @click="getValImgSrc">
  20. </div>
  21. <div class="rowHeight">
  22. <input type="number" class="login-control login_pwd" placeholder="输入验证码" v-model="login.userpwd"
  23. @keyup.enter="pwdLoginBtn">
  24. <mu-button id="getValidSms" :disabled="getValidSmsState" color="success" @click="getValidSmsBtn">
  25. {{ btnText }}
  26. </mu-button>
  27. </div>
  28. <div class="btn_center">
  29. <span class="btn btn-md login_btn" @click="pwdLoginBtn">登 录</span>
  30. </div>
  31. </form>
  32. </section>
  33. <mu-snackbar :position="normal.position" :open.sync="normal.open" :color="normal.color">
  34. {{ normal.message }}
  35. <mu-button flat slot="action" color="secondary" @click="normal.open = false">{{ "close" }}</mu-button>
  36. </mu-snackbar>
  37. </div>
  38. </template>
  39. <script>
  40. import {
  41. SignIn,
  42. GenVerifyPic,
  43. GetPhoneVFCode,
  44. PhoneSignIn,
  45. WXBind,
  46. testSelect
  47. } from '../api/getApiRes.js'
  48. let qs = require('qs');
  49. export default {
  50. data() {
  51. let samepass = (rule, value, callback) => {
  52. if (value !== this.form.newpwd) {
  53. callback(new Error('两次输入密码不一致!'));
  54. } else {
  55. callback();
  56. }
  57. };
  58. let pwdPass = (rule, value, callback) => {
  59. let re = /^[0-9a-zA-Z_]{1,}$/;
  60. if (value.search(re) == -1) {
  61. callback(new Error('错了哦,密码只能由字母、数字及下划线组成'));
  62. } else {
  63. callback()
  64. }
  65. };
  66. let phonetest = (rule, value, callback) => {
  67. let re = /^1[3|4|5|7|8|9][0-9]\d{8}$/;
  68. if (value.search(re) == -1) {
  69. callback(new Error('错了哦,手机号码格式不正确'));
  70. } else {
  71. callback()
  72. }
  73. };
  74. return {
  75. Password: "Password",
  76. VerificationCode: "Verification Code",
  77. InvisibilityGiveitatry: "无效?再点一下试试吧!",
  78. valImgSrc: '',//
  79. picId: '',//
  80. getValidSmsState: false,//
  81. overtime: '',
  82. btnText: '获取验证码',
  83. normal: normal,
  84. login: {
  85. phone: '',
  86. userpwd: '',
  87. uservalid: '',
  88. },
  89. rules: {
  90. username: [
  91. {required: true, message: '请输入用户名', trigger: 'blur'},
  92. {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'blur'},
  93. ],
  94. phone: [
  95. {required: true, message: '请输入手机号', trigger: 'blur'},
  96. {min: 11, max: 12, message: '手机号长度是11位', trigger: 'blur'},
  97. {validator: phonetest, trigger: 'blur'}
  98. ],
  99. re_phonevalid: [
  100. {required: true, message: '请输入验证码', trigger: 'blur'},
  101. {min: 4, max: 4, message: '长度在 4 个字符', trigger: 'blur'},
  102. ],
  103. newpwd: [
  104. {required: true, message: '请输入新密码', trigger: 'blur'},
  105. {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'blur'},
  106. {validator: pwdPass, trigger: 'blur'}
  107. ],
  108. again: [
  109. {required: true, message: '请输入确认密码', trigger: 'blur'},
  110. {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'blur'},
  111. {validator: pwdPass, trigger: 'blur'},
  112. {validator: samepass, trigger: 'blur'},
  113. ],
  114. }
  115. }
  116. },
  117. mounted() {
  118. // 入口状态有 3 种
  119. // 1.全新登入,拿weixinId 拿验证码 ,绑定 ,登陆
  120. // 2.token 未失效用户,直接进入内页,继续凭token使用
  121. // 3.token 已失效用户,删掉本地token ,重新按照1的模式登陆
  122. if (localStorage.token) {
  123. // 凭借旧token直接登陆
  124. this.$router.push({path: '/'});
  125. } else {
  126. // 获取图形验证码
  127. this.getValImgSrc();
  128. this.overtime = new Date();
  129. let from = this.$route.query.from;//获取来源
  130. let userAgent = navigator.userAgent;
  131. let host = window.location.host;
  132. console.log(host);
  133. if (from == 'app' || host == '192.168.0.162:8080') {//判断是否微信浏览器
  134. // 浏览器测试
  135. console.log('from app & brower');
  136. } else {
  137. console.log('form wx');
  138. this.checkcode();
  139. }
  140. }
  141. },
  142. methods: {
  143. // 获取CODE
  144. checkcode() {
  145. // 0213tIFa1fBjAz0OVDIa1LfXwc03tIFK
  146. if (this.getUrlKey('state') == 123) {
  147. // 取到值了
  148. localStorage.code = this.getUrlKey('code');
  149. // console.log('code:' + localStorage.code);
  150. } else {
  151. // 第一次加载
  152. this.GetOpenId();
  153. }
  154. },
  155. // 获取公众号ID
  156. GetOpenId() {
  157. let origin = location.href;
  158. // let origin = 'http://wx.xfeilong.com';
  159. let urlNow = encodeURIComponent(origin);
  160. let scope = 'snsapi_userinfo'; //snsapi_userinfo //静默授权 用户无感知
  161. let appid = 'wx36cb8b6661d71811';//正式
  162. let state = '123';
  163. let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + urlNow + '&response_type=code&scope=' + scope + '&state=' + state + '#wechat_redirect';
  164. self.location = url;
  165. },
  166. getUrlKey(name) {
  167. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
  168. },
  169. // 获取验证码
  170. getValidSmsBtn() {
  171. let that = this;
  172. let phone = that.login.phone;
  173. let uservalid = that.login.uservalid;
  174. if (!phone) {
  175. that.Toast('手机号不能为空');
  176. return false
  177. }
  178. if (phone.length != 11) {
  179. that.Toast('手机号只能是11位');
  180. return false
  181. }
  182. if (!globalCheckPhone(phone)) {
  183. that.Toast('手机号格式不正确');
  184. return false
  185. }
  186. if (!uservalid) {
  187. that.Toast('图形验证码不能为空');
  188. return false
  189. }
  190. if (uservalid.length != 4) {
  191. that.Toast('图形验证码只能是4位');
  192. return false
  193. }
  194. let param = {
  195. token: localStorage.token,
  196. phone: phone,
  197. codeType: 1,
  198. picId: this.valImgId,
  199. picCode: uservalid,
  200. };
  201. let postdata = qs.stringify(param);
  202. GetPhoneVFCode(postdata).then(res => {
  203. let json = res;
  204. if (json.Code == 0) {
  205. that.Toast('短信验证码已发送', 'success');
  206. that.calcTime();
  207. } else {
  208. that.Toast(json.Memo);
  209. }
  210. })
  211. },
  212. calcTime() {
  213. // 短信倒数计时
  214. let countdown = 60;
  215. let that = this;
  216. //设置button效果,开始计时
  217. that.getValidSmsState = true;
  218. that.btnText = countdown + "S"
  219. //启动计时器,1秒执行一次
  220. let timer = setInterval(function () {
  221. if (countdown == 0) {
  222. clearInterval(timer);//停止计时器
  223. that.getValidSmsState = false;
  224. that.btnText = "重新发送"
  225. } else {
  226. countdown--;
  227. that.btnText = countdown + "S"
  228. }
  229. }, 1000);
  230. },
  231. clearLogin() {
  232. this.login.username = '';
  233. this.login.userpwd = '';
  234. this.login.uservalid = '';
  235. this.getValImgSrc();
  236. },
  237. // 点击验证码切换
  238. getValImgSrc() {
  239. let that = this;
  240. let param = {
  241. token: localStorage.token,
  242. width: 100,
  243. height: 30,
  244. noiseCount: 1,
  245. length: 4,
  246. source: 123456789,
  247. };
  248. let postdata = qs.stringify(param);
  249. GenVerifyPic(postdata).then(res => {
  250. let json = res;
  251. that.valImgSrc = json.Pic;
  252. that.valImgId = json.Id;
  253. })
  254. },
  255. // pwd登录
  256. pwdLoginBtn() {
  257. let that = this;
  258. let phone = this.login.phone;
  259. let userpwd = this.login.userpwd;
  260. // 重置验证码超时
  261. that.overtime = new Date();
  262. if (!that.globalValid(phone, 10, 12, '手机号', that)) return;
  263. if (!that.globalValid(userpwd, 5, 7, '短信验证码', that)) return;
  264. this.loginInfo();
  265. },
  266. // 校验内容长度
  267. globalValid(data, mins, maxs, text, that) {
  268. let thisVal = data;
  269. let thisLeng = thisVal.length;
  270. let min = parseInt(mins);
  271. let max = parseInt(maxs);
  272. let dispalyMin = min + 1;
  273. if (thisVal == '') {
  274. this.Toast(text + ' ' + '不能为空');
  275. return false
  276. } else if (thisLeng <= min) {
  277. this.Toast(text + '最少' + dispalyMin + '字符');
  278. return false
  279. } else if (thisLeng > max) {
  280. this.Toast(text + '超过限制长度');
  281. return false
  282. } else {
  283. return true
  284. }
  285. },
  286. // pwd登陆
  287. loginInfo: function () {
  288. const that = this;
  289. let current = new Date();
  290. let betweenTime = current - that.overtime;
  291. let s = 120;
  292. if (betweenTime > s * 1000) {
  293. that.changeValImg();
  294. that.$toast.message('验证码已超时,请重新输入');
  295. return false
  296. }
  297. let param = {
  298. phone: that.login.phone,
  299. code: that.login.userpwd,
  300. channel: 3
  301. };
  302. let postdata = qs.stringify(param);
  303. PhoneSignIn(postdata).then(res => {
  304. if (res.Code == 0) {
  305. that.res = res.Rs;
  306. // 使用token做免校验登陆
  307. localStorage.token = res.Rs.token;
  308. // 如果是wx登陆就执行绑定操作
  309. let from = this.$route.query.from;//获取来源
  310. if (from != 'app') {
  311. that.bindWx();
  312. }
  313. // 607862e8828013be90c2ccb78468b43a
  314. // test
  315. that.$router.push({path: '/'});
  316. } else {
  317. this.getValImgSrc();
  318. if (res.Code == 10005) {
  319. that.$refs.userpwd.value = '';
  320. that.Toast('密码错误,请重新输入');
  321. } else {
  322. that.Toast(res.Memo + ',错误代码:' + res.Code);
  323. that.form.valid = '';
  324. }
  325. }
  326. })
  327. },
  328. bindWx() {
  329. let that = this;
  330. let param = {
  331. token: localStorage.token,
  332. code: localStorage.code,
  333. };
  334. let postdata = qs.stringify(param);
  335. WXBind(postdata).then(res => {
  336. let json = res;
  337. if (json.Code == 0) {
  338. // that.$router.push({path: '/'});
  339. } else {
  340. // that.Toast(res.Memo + ',错误代码:' + res.Code);
  341. }
  342. })
  343. },
  344. getUserInfo(token) {
  345. let that = this;
  346. let param = {
  347. token: token,
  348. };
  349. let postdata = qs.stringify(param);
  350. testSelect(postdata).then(res => {
  351. let json = res;
  352. if (json.Code == 0) {
  353. var userLevel = json.Rs.Rolesname;
  354. localStorage.userLevel = userLevel;
  355. localStorage.comId = json.Rs.Comid;
  356. localStorage.Insname = json.Rs.Insname;
  357. that.$router.push({path: '/'});
  358. } else {
  359. that.$toast.message(json.Memo);
  360. }
  361. })
  362. }
  363. },
  364. components: {}
  365. }
  366. </script>
  367. <style scoped>
  368. #skin-blur-violate {
  369. position: relative;
  370. top: 0;
  371. left: 0;
  372. right: 0;
  373. bottom: 0;
  374. width: 100%;
  375. height: 100%;
  376. overflow: hidden;
  377. display: block;
  378. margin: 0 auto;
  379. background: #3B3B3B;
  380. background: url("../static/images/login/bg.png") top center no-repeat;
  381. background-size: 100% 100%;
  382. }
  383. #login {
  384. width: 90%;
  385. overflow: hidden;
  386. display: block;
  387. margin: 0 auto;
  388. }
  389. #login .box {
  390. margin: 0 auto;
  391. position: inherit;
  392. margin-top: 20px;
  393. border-radius: 9px;
  394. padding: 20px 20px;
  395. }
  396. h1 {
  397. color: #FFA200;
  398. font-size: 30px;
  399. }
  400. header p {
  401. font-size: 14px;
  402. color: #7B7B7B;
  403. }
  404. #login header {
  405. text-align: center;
  406. margin-top: 15%;
  407. }
  408. #box-login h2 {
  409. width: 100%;
  410. overflow: hidden;
  411. display: block;
  412. margin: 0 auto;
  413. margin-bottom: 20px;
  414. text-align: center;
  415. font-size: 24px;
  416. color: #fff;
  417. }
  418. #tableBordered {
  419. margin: 0;
  420. padding: 0;
  421. }
  422. #tableBordered h5 {
  423. margin: 0;
  424. text-align: center;
  425. padding: 0;
  426. }
  427. #login .box {
  428. margin: 0 auto;
  429. position: inherit;
  430. margin-top: 20px;
  431. border-radius: 9px !important;
  432. }
  433. #login header {
  434. text-align: center;
  435. margin-top: 15%;
  436. }
  437. #tableBordered {
  438. margin: 0;
  439. padding: 0;
  440. }
  441. #tableBordered h5 {
  442. margin: 0;
  443. text-align: center;
  444. padding: 0;
  445. }
  446. #imgValidcode {
  447. position: relative;
  448. float: right;
  449. /*bottom: 54px;*/
  450. /*width: 100px;*/
  451. width: 30%;
  452. height: 50px;
  453. background: #eee;
  454. }
  455. .login_btn {
  456. width: 100%;
  457. height: 50px;
  458. line-height: 50px;
  459. overflow: hidden;
  460. display: block;
  461. margin: 0 auto;
  462. margin-top: 42px;
  463. background: #E75296;
  464. color: #fff;
  465. font-size: 14px;
  466. text-align: center;
  467. border: none;
  468. padding: 0;
  469. font-size: 24px;
  470. margin-bottom: 3%;
  471. }
  472. .clear_btn {
  473. width: 100%;
  474. height: 40px;
  475. line-height: 40px;
  476. overflow: hidden;
  477. display: block;
  478. margin: 0 auto;
  479. background: rgba(255, 255, 255, 0.32);
  480. color: #FFCC00;
  481. font-size: 14px;
  482. text-align: center;
  483. border: none;
  484. padding: 0;
  485. }
  486. header p {
  487. margin-top: 10px;
  488. }
  489. #login .login-control {
  490. width: 100%;
  491. height: 50px;
  492. line-height: 50px;
  493. background: rgba(255, 255, 255, 0.4);
  494. margin-bottom: 3px;
  495. text-indent: 10px;
  496. border: none;
  497. }
  498. #login .login-control::placeholder {
  499. color: #000000;
  500. font-size: 16px;
  501. }
  502. #logo {
  503. overflow: hidden;
  504. display: block;
  505. margin: 0 auto;
  506. margin-top: 10%;
  507. margin-bottom: 10px;
  508. }
  509. .rowHeight {
  510. width: 100%;
  511. overflow: hidden;
  512. display: block;
  513. margin: 0 auto;
  514. height: 50px;
  515. margin-bottom: 3px;
  516. }
  517. #getValidSms {
  518. /*width: 100px;*/
  519. width: 30%;
  520. height: 50px;
  521. background: #fff;
  522. border: 1px solid #fff;
  523. opacity: 0.6;
  524. position: relative;
  525. /*bottom: 55px;*/
  526. float: right;
  527. text-align: center;
  528. color: #E75296;
  529. line-height: 20px;
  530. font-size: 12px;
  531. min-width: 30px;
  532. }
  533. #login .login_valid {
  534. /*width: 173px;*/
  535. width:65%;
  536. float: left;
  537. }
  538. #login .login_pwd {
  539. width: 173px;
  540. float: left;
  541. }
  542. @media only screen and (max-width: 640px) {
  543. }
  544. @media only screen and (max-width: 480px) {
  545. #login .login_valid {
  546. /*width: 195px;*/
  547. width:65%;
  548. }
  549. #login .login_pwd {
  550. /*width: 195px;*/
  551. width:65%;
  552. }
  553. }
  554. @media only screen and (max-width: 414px) {
  555. #login .login_valid {
  556. width:65%;
  557. /*width: 223px;*/
  558. }
  559. #login .login_pwd {
  560. /*width: 223px;*/
  561. width:65%;
  562. }
  563. }
  564. @media only screen and (max-width: 400px) {
  565. #login .login_valid {
  566. width:65%;
  567. /*width: 218px;*/
  568. }
  569. #login .login_pwd {
  570. /*width: 218px;*/
  571. width:65%;
  572. }
  573. #getValidSms {
  574. font-size: 13px;
  575. }
  576. }
  577. @media only screen and (max-width: 375px) {
  578. #login .login_valid {
  579. /*width: 190px;*/
  580. width:65%;
  581. }
  582. #login .login_pwd {
  583. /*width: 190px;*/
  584. width:65%;
  585. }
  586. #getValidSms {
  587. font-size: 14px;
  588. }
  589. }
  590. @media only screen and (max-width: 360px) {
  591. #login .login_valid {
  592. /*width: 160px;*/
  593. width:65%;
  594. }
  595. #login .login_pwd {
  596. /*width: 160px;*/
  597. width:65%;
  598. }
  599. #getValidSms {
  600. font-size: 14px;
  601. }
  602. }
  603. @media only screen and (max-width: 320px) {
  604. #login .login_valid {
  605. /*width: 144px;*/
  606. width:65%;
  607. }
  608. #login .login_pwd {
  609. width:65%;
  610. /*width: 144px;*/
  611. }
  612. #getValidSms {
  613. font-size: 12px;
  614. }
  615. }
  616. </style>