login.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <div class="login">
  3. <div class="color-model"></div>
  4. <div class="background-border"></div>
  5. <img class="login-text" src="../assets/images/login-text.svg" alt="">
  6. <img class="login-logo" src="../assets/images/login-logo.svg" alt="">
  7. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-width="80px">
  8. <h3 class="mini-title">欢迎登录</h3>
  9. <h3 class="title">移动安全保卫管理平台项目</h3>
  10. <el-form-item prop="username" label="用户名">
  11. <el-input
  12. v-model="loginForm.username"
  13. type="text"
  14. auto-complete="off"
  15. placeholder="账号">
  16. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  17. </el-input>
  18. </el-form-item>
  19. <el-form-item prop="password" label="密码">
  20. <el-input
  21. v-model="loginForm.password"
  22. type="password"
  23. auto-complete="off"
  24. placeholder="密码"
  25. @keyup.enter.native="handleLogin">
  26. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  27. </el-input>
  28. </el-form-item>
  29. <el-form-item prop="code" v-if="captchaEnabled">
  30. <el-input
  31. v-model="loginForm.code"
  32. auto-complete="off"
  33. placeholder="验证码"
  34. style="width: 63%"
  35. @keyup.enter.native="handleLogin"
  36. >
  37. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  38. </el-input>
  39. <div class="login-code">
  40. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  41. </div>
  42. </el-form-item>
  43. <el-form-item>
  44. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  45. </el-form-item>
  46. <div style="width:80%;margin: 0 auto;">
  47. <el-button
  48. :loading="loading"
  49. size="medium"
  50. style="width:100%;background-color: #00afff;color:#fff;"
  51. @click.native.prevent="handleLogin">
  52. <span v-if="!loading">登 录</span>
  53. <span v-else>登 录 中...</span>
  54. </el-button>
  55. <div style="float: right;" v-if="register">
  56. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  57. </div>
  58. </div>
  59. </el-form>
  60. <!-- 底部 -->
  61. <div class="el-login-footer">
  62. <!-- <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>-->
  63. </div>
  64. </div>
  65. </template>
  66. <script>
  67. import { getCodeImg } from "@/api/login";
  68. import { encrypt, decrypt } from '@/utils/jsencrypt'
  69. import { setLocal, getLocal, removeLocal } from '../utils/auth'
  70. export default {
  71. name: "Login",
  72. data() {
  73. return {
  74. codeUrl: "",
  75. loginForm: {
  76. username: "admin",
  77. password: "11111",
  78. rememberMe: false,
  79. code: "",
  80. uuid: ""
  81. },
  82. loginRules: {
  83. username: [
  84. { required: true, trigger: "blur", message: "请输入您的账号" }
  85. ],
  86. password: [
  87. { required: true, trigger: "blur", message: "请输入您的密码" }
  88. ],
  89. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  90. },
  91. loading: false,
  92. // 验证码开关
  93. captchaEnabled: false,
  94. // 注册开关
  95. register: false,
  96. redirect: undefined
  97. };
  98. },
  99. watch: {
  100. $route: {
  101. handler: function(route) {
  102. this.redirect = route.query && route.query.redirect;
  103. console.log(this.redirect,'redirect')
  104. },
  105. immediate: true
  106. }
  107. },
  108. created() {
  109. //this.getCode();
  110. this.getUserInfo();
  111. },
  112. methods: {
  113. getCode() {
  114. getCodeImg().then(res => {
  115. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
  116. if (this.captchaEnabled) {
  117. this.codeUrl = "data:image/gif;base64," + res.img;
  118. this.loginForm.uuid = res.uuid;
  119. }
  120. });
  121. },
  122. getUserInfo() {
  123. const username = getLocal("username");
  124. const password = getLocal("password");
  125. const rememberMe = getLocal('rememberMe')
  126. this.loginForm = {
  127. username: !username? this.loginForm.username : username,
  128. password: !password? this.loginForm.password : decrypt(password),
  129. rememberMe: !rememberMe ? false : Boolean(rememberMe)
  130. };
  131. },
  132. handleLogin(key, value) {
  133. this.$refs.loginForm.validate(valid => {
  134. if (valid) {
  135. this.loading = true;
  136. //判断是否保存登录信息,没有则登陆时删除
  137. if (this.loginForm.rememberMe) {
  138. setLocal("username", this.loginForm.username);
  139. setLocal("password", encrypt(this.loginForm.password));
  140. setLocal('rememberMe', this.loginForm.rememberMe);
  141. } else {
  142. removeLocal("username");
  143. removeLocal("password");
  144. removeLocal('rememberMe');
  145. }
  146. //登录请求
  147. this.$store.dispatch("Login", this.loginForm).then(() => {
  148. this.loading = false;
  149. console.log(this.redirect,'this.redirect')
  150. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  151. // this.$store.dispatch('GenerateRoutes').then(accessRoutes => {
  152. // // 根据roles权限生成可访问的路由表
  153. // this.$router.addRoutes(accessRoutes) // 动态添加可访问路由表
  154. // //next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  155. // })
  156. // console.log(this.redirect,'11111')
  157. // this.$router.push({ path: 'index' });
  158. }).catch(() => {
  159. this.loading = false;
  160. if (this.captchaEnabled) {
  161. this.getCode();
  162. }
  163. });
  164. }
  165. });
  166. }
  167. }
  168. };
  169. </script>
  170. <style rel="stylesheet/scss" lang="scss">
  171. .login {
  172. display: flex;
  173. justify-content: right;
  174. align-items: center;
  175. height: 100%;
  176. background: rgba(0, 140, 214, 1) url("../assets/images/login-background.png") no-repeat;
  177. background-size: cover;
  178. position: relative;
  179. }
  180. .color-model{
  181. width: 100%;
  182. height: 100%;
  183. position: fixed;
  184. background-color: rgba(0, 140, 214, .5);
  185. }
  186. .background-border{
  187. position: absolute;
  188. width: 100%;
  189. height: 342px;
  190. background-color: #fff;
  191. opacity: .2;
  192. z-index:0;
  193. display: flex;
  194. }
  195. .login-logo{
  196. position: absolute;
  197. left: 118px;
  198. top: 77px;
  199. width: 245px;
  200. height: 69px;
  201. }
  202. .login-text{
  203. position: absolute;
  204. left:23%;
  205. align-items: center;
  206. width: 510px;
  207. height: 190px;
  208. }
  209. .mini-title{
  210. font-size: 16px;
  211. text-align: center;
  212. color: #999;
  213. }
  214. .title {
  215. font-size: 32px;
  216. margin: 0 auto 50px auto;
  217. text-align: center;
  218. color: #333;
  219. }
  220. .login-form {
  221. margin-right: 15%;
  222. border-radius: 6px;
  223. background: #ffffff;
  224. width: 544px;
  225. height: 488px;
  226. padding: 25px 60px 50px 60px;
  227. z-index:2;
  228. .el-input {
  229. height: 38px;
  230. input {
  231. height: 38px;
  232. }
  233. }
  234. .input-icon {
  235. height: 39px;
  236. width: 14px;
  237. margin-left: 2px;
  238. }
  239. }
  240. .login-tip {
  241. font-size: 13px;
  242. text-align: center;
  243. color: #bfbfbf;
  244. }
  245. .login-code {
  246. width: 33%;
  247. height: 38px;
  248. float: right;
  249. img {
  250. cursor: pointer;
  251. vertical-align: middle;
  252. }
  253. }
  254. .el-login-footer {
  255. height: 40px;
  256. line-height: 40px;
  257. position: fixed;
  258. bottom: 0;
  259. width: 100%;
  260. text-align: center;
  261. color: #fff;
  262. font-family: Arial;
  263. font-size: 12px;
  264. letter-spacing: 1px;
  265. }
  266. .login-code-img {
  267. height: 38px;
  268. }
  269. </style>