login.vue 6.2 KB

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