login.vue 5.5 KB

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