login.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <h3 class="title">{{title}}</h3>
  5. <el-form-item prop="username">
  6. <el-input
  7. v-model="loginForm.username"
  8. type="text"
  9. auto-complete="off"
  10. placeholder="账号"
  11. >
  12. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input
  17. v-model="loginForm.password"
  18. type="password"
  19. auto-complete="off"
  20. placeholder="密码"
  21. @keyup.enter.native="handleLogin"
  22. >
  23. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  24. </el-input>
  25. </el-form-item>
  26. <el-form-item prop="code" v-if="captchaEnabled">
  27. <el-input
  28. v-model="loginForm.code"
  29. auto-complete="off"
  30. placeholder="验证码"
  31. style="width: 63%"
  32. @keyup.enter.native="handleLogin"
  33. >
  34. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  35. </el-input>
  36. <div class="login-code">
  37. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  38. </div>
  39. </el-form-item>
  40. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  41. <el-form-item style="width:100%;">
  42. <el-button
  43. :loading="loading"
  44. size="medium"
  45. type="primary"
  46. style="width:100%;"
  47. @click.native.prevent="handleLogin"
  48. >
  49. <span v-if="!loading">登 录</span>
  50. <span v-else>登 录 中...</span>
  51. </el-button>
  52. <div style="float: right;" v-if="register">
  53. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  54. </div>
  55. </el-form-item>
  56. </el-form>
  57. <!-- 底部 -->
  58. <div class="el-login-footer">
  59. <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
  60. </div>
  61. </div>
  62. </template>
  63. <script>
  64. import { getCodeImg } from "@/api/login";
  65. import Cookies from "js-cookie";
  66. import { encrypt, decrypt } from '@/utils/jsencrypt'
  67. export default {
  68. name: "Login",
  69. data() {
  70. return {
  71. title: process.env.VUE_APP_TITLE,
  72. codeUrl: "",
  73. loginForm: {
  74. username: "admin",
  75. password: "admin123",
  76. rememberMe: false,
  77. code: "",
  78. uuid: ""
  79. },
  80. loginRules: {
  81. username: [
  82. { required: true, trigger: "blur", message: "请输入您的账号" }
  83. ],
  84. password: [
  85. { required: true, trigger: "blur", message: "请输入您的密码" }
  86. ],
  87. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  88. },
  89. loading: false,
  90. // 验证码开关
  91. captchaEnabled: true,
  92. // 注册开关
  93. register: false,
  94. redirect: undefined
  95. };
  96. },
  97. watch: {
  98. $route: {
  99. handler: function(route) {
  100. this.redirect = route.query && route.query.redirect;
  101. },
  102. immediate: true
  103. }
  104. },
  105. created() {
  106. this.getCode();
  107. this.getCookie();
  108. },
  109. methods: {
  110. getCode() {
  111. getCodeImg().then(res => {
  112. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
  113. if (this.captchaEnabled) {
  114. this.codeUrl = "data:image/gif;base64," + res.img;
  115. this.loginForm.uuid = res.uuid;
  116. }
  117. });
  118. },
  119. getCookie() {
  120. const username = Cookies.get("username");
  121. const password = Cookies.get("password");
  122. const rememberMe = Cookies.get('rememberMe')
  123. this.loginForm = {
  124. username: username === undefined ? this.loginForm.username : username,
  125. password: password === undefined ? this.loginForm.password : decrypt(password),
  126. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  127. };
  128. },
  129. handleLogin() {
  130. this.$refs.loginForm.validate(valid => {
  131. if (valid) {
  132. this.loading = true;
  133. if (this.loginForm.rememberMe) {
  134. Cookies.set("username", this.loginForm.username, { expires: 30 });
  135. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  136. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  137. } else {
  138. Cookies.remove("username");
  139. Cookies.remove("password");
  140. Cookies.remove('rememberMe');
  141. }
  142. this.$store.dispatch("Login", this.loginForm).then(() => {
  143. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  144. }).catch(() => {
  145. this.loading = false;
  146. if (this.captchaEnabled) {
  147. this.getCode();
  148. }
  149. });
  150. }
  151. });
  152. }
  153. }
  154. };
  155. </script>
  156. <style rel="stylesheet/scss" lang="scss">
  157. .login {
  158. display: flex;
  159. justify-content: center;
  160. align-items: center;
  161. height: 100%;
  162. background-image: url("../assets/images/login-background.jpg");
  163. background-size: cover;
  164. }
  165. .title {
  166. margin: 0px auto 30px auto;
  167. text-align: center;
  168. color: #707070;
  169. }
  170. .login-form {
  171. border-radius: 6px;
  172. background: #ffffff;
  173. width: 400px;
  174. padding: 25px 25px 5px 25px;
  175. z-index: 1;
  176. .el-input {
  177. height: 38px;
  178. input {
  179. height: 38px;
  180. }
  181. }
  182. .input-icon {
  183. height: 39px;
  184. width: 14px;
  185. margin-left: 2px;
  186. }
  187. }
  188. .login-tip {
  189. font-size: 13px;
  190. text-align: center;
  191. color: #bfbfbf;
  192. }
  193. .login-code {
  194. width: 33%;
  195. height: 38px;
  196. float: right;
  197. img {
  198. cursor: pointer;
  199. vertical-align: middle;
  200. }
  201. }
  202. .el-login-footer {
  203. height: 40px;
  204. line-height: 40px;
  205. position: fixed;
  206. bottom: 0;
  207. width: 100%;
  208. text-align: center;
  209. color: #fff;
  210. font-family: Arial;
  211. font-size: 12px;
  212. letter-spacing: 1px;
  213. }
  214. .login-code-img {
  215. height: 38px;
  216. }
  217. </style>