login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <div class="login">
  3. <el-row type="flex">
  4. <el-col :span="12">
  5. <div class="login-form-left">
  6. <!-- <span class="copyright">Copyright © 2021 quanshu123.com All Rights Reserved.</span> -->
  7. </div>
  8. </el-col>
  9. <el-col :span="12">
  10. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" :inline='false'>
  11. <!-- <h3 class="title">{{title}}</h3> -->
  12. <div class="title-container">
  13. <div class="login_header" >
  14. <a @click="changeTab('actpwd')" class="a50" :class="{active:type=='actpwd'}" >密码登录</a>
  15. <a @click="changeTab('phonesms')" class="a50" :class="{active:type=='phonesms' }" >短信登录</a>
  16. </div>
  17. </div>
  18. <div v-if="type=='actpwd'" class="cbody_item">
  19. <el-form-item prop="username" label="手机号" id="selectForm" class=".login-form">
  20. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item prop="password" label="密码" class=".login-form">
  24. <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin" >
  25. </el-input>
  26. </el-form-item>
  27. <el-row style="height:120px">
  28. <el-col :span="15" >
  29. <el-form-item prop="code" v-if="captchaOnOff" label="验证码" class=".login-form">
  30. <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" @keyup.enter.native="handleLogin">
  31. </el-input>
  32. </el-form-item>
  33. </el-col>
  34. <el-col class="identifying-code">
  35. <div class="login-code">
  36. <img :src="codeUrl" @click="getCode" style="height:48px;width:128px;"/>
  37. </div>
  38. </el-col>
  39. </el-row>
  40. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 8px 0px;">记住密码</el-checkbox>
  41. </div>
  42. <div v-if="type=='phonesms'" class="cbody_item" style="margin-bottom:88px">
  43. <el-form-item prop="mobile" label="手机号" class="mobile-item" >
  44. <el-input ref="mobile" v-model="loginForm.mobile" placeholder="请输入手机号" name="mobile" type="text"
  45. tabindex="1" auto-complete="on">
  46. </el-input>
  47. </el-form-item>
  48. <el-form-item prop="status">
  49. <JcRange ref="jcr" status="status" :successFun="onMpanelSuccess" :errorFun="onMpanelError" :is-lock="status"></JcRange>
  50. </el-form-item>
  51. <el-row class="yzcode">
  52. <el-col :span="15">
  53. <el-form-item prop="verifyCode" label="验证码" class="yz-code">
  54. <el-input ref="verifyCode" v-model="loginForm.verifyCode" type="text" placeholder="请输入验证码" name="verifyCode" abindex="2"
  55. maxlength="6" auto-complete="off" @keyup.enter.native="handleLogin">
  56. </el-input>
  57. </el-form-item>
  58. </el-col>
  59. <el-col :span="9" class="yzcode-btn">
  60. <span style="float: right;">
  61. <el-button :loading="sending" :disabled="sendDisabled" size="medium" @click="onSendSms" class="yzcode-send" type="primary">
  62. {{ sendButtonText }}
  63. </el-button>
  64. </span>
  65. </el-col>
  66. </el-row>
  67. </div>
  68. <el-form-item class="login-bottom">
  69. <el-button :loading="loading" size="medium" type="primary" class="login-btn"
  70. @click.native.prevent="handleLogin">
  71. <span v-if="!loading">登 录</span>
  72. <span v-else>登 录 中...</span>
  73. </el-button>
  74. <div style="float: right;" v-if="register">
  75. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  76. </div>
  77. </el-form-item>
  78. </el-form>
  79. </el-col>
  80. </el-row>
  81. </div>
  82. </template>
  83. <script>
  84. import {
  85. getCodeImg,sendSms
  86. } from "@/api/login";
  87. import Cookies from "js-cookie";
  88. import {
  89. encrypt,
  90. decrypt
  91. } from '@/utils/jsencrypt'
  92. //import logoImg from '@/assets/logo/quick-logo.png'
  93. import {
  94. isBoolean
  95. } from "@/utils/validate"
  96. import JcRange from "@/components/JcRange/index.vue";
  97. import AuthImg from "@/components/AuthImg/index.vue"
  98. export default {
  99. name: "Login",
  100. components: {
  101. JcRange,AuthImg
  102. },
  103. data() {
  104. return {
  105. title: "系统登录",
  106. type: "actpwd", // 账号密码 actpwd,手机短信验证码登录 phonesms,微信登录 wechatcode
  107. sending: false,
  108. sendDisabled: false,
  109. loading: false,
  110. timer: 0,
  111. logo:"",
  112. codeUrl: "",
  113. cookiePassword: "",
  114. loginForm: {
  115. username: "admin",
  116. password: "admin123",
  117. rememberMe: false,
  118. code: "",
  119. uuid: "",
  120. mobile: "",
  121. verifyCode: "",
  122. status: false
  123. },
  124. status: false,
  125. loginRules: {
  126. username: [{
  127. required:true,
  128. trigger: "blur",
  129. message: "请输入您的账号"
  130. }],
  131. password: [{
  132. required:true,
  133. trigger: "blur",
  134. message: "请输入您的密码"
  135. }],
  136. code: [{
  137. required:true,
  138. trigger: "change",
  139. message: "请输入验证码"
  140. }]
  141. },
  142. loading: false,
  143. // 验证码开关
  144. captchaOnOff: true,
  145. // 注册开关
  146. register: false,
  147. redirect: undefined
  148. };
  149. },
  150. computed: {
  151. sendButtonText() {
  152. if (this.timer === 0) {
  153. this.sendDisabled = false
  154. return "发送验证码";
  155. } else {
  156. return `${this.timer}秒后重发`;
  157. }
  158. }
  159. },
  160. watch: {
  161. $route: {
  162. handler: function(route) {
  163. this.redirect = route.query && route.query.redirect;
  164. },
  165. immediate: true
  166. },
  167. timer(num) {
  168. if (num > 0) {
  169. setTimeout(() => {
  170. this.timer = --num;
  171. }, 1000);
  172. }
  173. }
  174. },
  175. created() {
  176. this.getCode();
  177. this.getCookie();
  178. },
  179. methods: {
  180. changeTab(tab){
  181. // console.log("changeTab====="+ tab)
  182. this.$refs["loginForm"].clearValidate();
  183. this.loginRules = {}
  184. this.type = tab
  185. if(this.type=='actpwd'){
  186. this.loginRules ={
  187. username: [{
  188. required:true,
  189. trigger: "blur",
  190. message: "请输入您的账号"
  191. }],
  192. password: [{
  193. required:true,
  194. trigger: "blur",
  195. message: "请输入您的密码"
  196. }],
  197. code: [{
  198. required:true,
  199. trigger: "change",
  200. message: "请输入验证码"
  201. }]
  202. }
  203. }else if(this.type=='phonesms'){
  204. this.loginRules ={
  205. mobile: [
  206. {required:true, trigger: "blur",message: "请输入您的手机号码"},
  207. {pattern: /^((\+?86)|(\(\+86\)))?1\d{10}$/, message: "请输入正确的手机号码", trigger: "blur" }
  208. ],
  209. verifyCode: [{
  210. required:true,
  211. trigger: "change",
  212. message: "请输入您的短信验证码"
  213. }],
  214. // status: [{
  215. // validator:isBoolean,
  216. // trigger: "change",
  217. // message: "请拖住滑块,拖动到最右边"
  218. // }]
  219. }
  220. if(this.timer == 0){
  221. this.$nextTick(() => {
  222. this.$refs.jcr.init()
  223. })
  224. this.status = false;
  225. }
  226. }
  227. },
  228. getCode() {
  229. getCodeImg().then(res => {
  230. this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
  231. if (this.captchaOnOff) {
  232. this.codeUrl = "data:image/gif;base64," + res.img;
  233. this.loginForm.uuid = res.uuid;
  234. }
  235. });
  236. },
  237. // 发送短信
  238. onSendSms() {
  239. this.$refs.loginForm.validateField("mobile", err => {
  240. if (!err) {
  241. if(!this.status){
  242. this.$message.warning("请拖住滑块,拖动到最右边");
  243. return false;
  244. }
  245. this.sending = true;
  246. this.sendDisabled = true
  247. const mobile = this.loginForm.mobile;
  248. sendSms(mobile)
  249. .then(res => {
  250. this.timer = 60;
  251. this.loginForm.uuid = res.data;
  252. this.$message.success("短信发送成功,请注意查收");
  253. Cookies.set("last-send-time", new Date());
  254. })
  255. .catch(e => {
  256. this.$message.error("网络异常");
  257. this.timer = 0;
  258. this.sendDisabled = false;
  259. // console.log(e);
  260. })
  261. .finally(() => (this.sending = false));
  262. }
  263. });
  264. },
  265. onMpanelSuccess(){
  266. // console.log("onMpanelSuccess 验证成功 =====")
  267. this.status = true
  268. },
  269. onMpanelError(){
  270. // console.log("onMpanelError 验证失败 =====")
  271. this.status = false
  272. },
  273. getCookie() {
  274. const username = Cookies.get("username");
  275. const password = Cookies.get("password");
  276. const rememberMe = Cookies.get('rememberMe')
  277. this.loginForm = {
  278. username: username === undefined ? this.loginForm.username : username,
  279. password: password === undefined ? this.loginForm.password : decrypt(password),
  280. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  281. };
  282. },
  283. handleLogin() {
  284. // console.log("this.handleLogin == "+JSON.stringify(this.loginForm))
  285. this.$refs.loginForm.validate(valid => {
  286. if (valid) {
  287. this.loading = true;
  288. if(this.type == 'actpwd'){
  289. if (this.loginForm.rememberMe) {
  290. Cookies.set("username", this.loginForm.username, {
  291. expires: 30
  292. });
  293. Cookies.set("password", encrypt(this.loginForm.password), {
  294. expires: 30
  295. });
  296. Cookies.set('rememberMe', this.loginForm.rememberMe, {
  297. expires: 30
  298. });
  299. } else {
  300. Cookies.remove("username");
  301. Cookies.remove("password");
  302. Cookies.remove('rememberMe');
  303. }
  304. }
  305. var patch = "PwdLogin";
  306. var loginParams = {
  307. uuid: this.loginForm.uuid
  308. }
  309. // console.log("this.loginForm == "+JSON.stringify(this.loginForm))
  310. if(this.type == 'phonesms'){
  311. patch = "SmsLogin";
  312. loginParams.mobile = this.loginForm.mobile
  313. loginParams.verifyCode = this.loginForm.verifyCode
  314. }else{
  315. loginParams.username = this.loginForm.username
  316. loginParams.password = this.loginForm.password
  317. loginParams.code = this.loginForm.code
  318. }
  319. // console.log("loginParams == "+JSON.stringify(loginParams))
  320. this.$store.dispatch(patch, loginParams).then(() => {
  321. // console.log("登录成功=================== this.redirect = " + this.redirect)
  322. this.$router.push({
  323. path: this.redirect || "/"
  324. }).catch(() => {
  325. // console.log("404=================== ")
  326. });
  327. }).catch(() => {
  328. this.loading = false;
  329. if (this.captchaOnOff) {
  330. this.getCode();
  331. }
  332. });
  333. }
  334. });
  335. }
  336. }
  337. };
  338. </script>
  339. <style rel="stylesheet/scss" lang="scss">
  340. .login {
  341. display: flex;
  342. justify-content: center;
  343. align-items: center;
  344. height: 100%;
  345. background-image: url("../assets/images/login-background.png");
  346. background-size: cover;
  347. }
  348. .login{
  349. // 去除el-form-item默认效验前面的红*号
  350. .el-form-item.is-required:not(.is-no-asterisk) .el-form-item__label-wrap>.el-form-item__label:before, .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before {
  351. content: '';
  352. }
  353. }
  354. .title {
  355. margin: 0px auto 30px auto;
  356. text-align: center;
  357. color: #707070;
  358. }
  359. .title-container{
  360. border-bottom: 1px solid #ebebeb;
  361. height:36px;
  362. margin-bottom:24px
  363. }
  364. .login-form-left {
  365. border-radius: 6px 0 0 6px;
  366. background-image: url("../assets/images/login-left.png");
  367. width: 439px;
  368. text-align: center;
  369. padding: 15px;
  370. height: 100%;
  371. }
  372. .login-form {
  373. border-radius: 0 16px 16px 0;
  374. background: #ffffff;
  375. width: 440px;
  376. padding: 24px 40px 6px 40px;
  377. .el-input {
  378. height: 48px;
  379. input {
  380. height: 48px;
  381. border-radius: 8px;
  382. background-color: rgba(242, 242, 242, 1);
  383. }
  384. }
  385. .input-icon {
  386. height: 39px;
  387. width: 14px;
  388. margin-left: 2px;
  389. }
  390. }
  391. .login-code {
  392. float: right;
  393. position: relative;
  394. top: -64px;
  395. img {
  396. cursor: pointer;
  397. vertical-align: middle;
  398. }
  399. }
  400. .el-form-item__content{
  401. margin-bottom: -6px;
  402. }
  403. .mobile-item{
  404. margin-bottom:29px
  405. }
  406. .a50 {
  407. font-family: SourceHanSansCN-Medium;
  408. font-size: 20px;
  409. color: #000;
  410. margin: 50px;
  411. }
  412. .login_header {
  413. margin-bottom: 20px;
  414. text-align: center;
  415. }
  416. .login_header span {
  417. margin-right: 20px;
  418. cursor: pointer;
  419. }
  420. .cbody_item {
  421. border: 0px solid #999;
  422. overflow: hidden;
  423. }
  424. .active {
  425. color: #00aaff;
  426. padding-bottom: 6px;
  427. border-bottom: 4px solid #00aaff;
  428. }
  429. .yzcode{
  430. display: flex;
  431. height: 120px;
  432. }
  433. .yz-code{
  434. height: 76px;
  435. }
  436. .yzcode-btn{
  437. position:relative;
  438. top:40px;
  439. }
  440. .yzcode-send{
  441. height:48px;
  442. width:128px;
  443. border-radius: 8px;
  444. color: rgba(255, 255, 255, 100);
  445. font-size: 16px;
  446. font-family: SourceHanSansCN-Medium;
  447. }
  448. .login-form {
  449. .el-form-item__label{
  450. font-size: 20px;
  451. color: #000;
  452. font-family: SourceHanSansCN-Medium;
  453. margin-bottom: 4px;
  454. }
  455. }
  456. // .el-input--medium {
  457. // font-size: 16px;
  458. // font-family: SourceHanSansCN-Medium
  459. // }
  460. .el-col-24{
  461. height: 36.0%;
  462. }
  463. .login-bottom{
  464. width:100%;
  465. margin-bottom: 30px;
  466. }
  467. .login-btn{
  468. font-size: 16px;
  469. color: rgba(255, 255, 255, 100);
  470. font-family: SourceHanSansCN-Bold;
  471. width:100%;
  472. height:48px;
  473. border-radius: 8px
  474. }
  475. .copyright{
  476. color: #fff;
  477. opacity: 40%;
  478. font-size: 12px;
  479. position: relative;
  480. top: 490px;
  481. }
  482. </style>
  483. <style rel="stylesheet/scss" lang="scss" scoped>
  484. .el-input--medium {
  485. font-size: 16px;
  486. font-family: SourceHanSansCN-Medium
  487. }
  488. </style>