login.vue 6.5 KB

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