login.vue 6.1 KB

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