login.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div class="login">
  3. <el-form
  4. ref="loginRef"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. >
  9. <h3 class="title">国土空间规划“一张图”实施监督信息系统</h3>
  10. <el-form-item prop="username">
  11. <el-input
  12. v-model="loginForm.username"
  13. type="text"
  14. size="large"
  15. auto-complete="off"
  16. placeholder="账号"
  17. >
  18. <template #prefix
  19. ><svg-icon icon-class="user" class="el-input__icon input-icon"
  20. /></template>
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item prop="password">
  24. <el-input
  25. v-model="loginForm.password"
  26. type="password"
  27. size="large"
  28. auto-complete="off"
  29. placeholder="密码"
  30. @keyup.enter="handleLogin"
  31. >
  32. <template #prefix
  33. ><svg-icon icon-class="password" class="el-input__icon input-icon"
  34. /></template>
  35. </el-input>
  36. </el-form-item>
  37. <el-form-item prop="code" v-if="captchaOnOff">
  38. <el-input
  39. v-model="loginForm.code"
  40. size="large"
  41. auto-complete="off"
  42. placeholder="验证码"
  43. style="width: 63%"
  44. @keyup.enter="handleLogin"
  45. >
  46. <template #prefix
  47. ><svg-icon icon-class="validCode" class="el-input__icon input-icon"
  48. /></template>
  49. </el-input>
  50. <div class="login-code">
  51. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  52. </div>
  53. </el-form-item>
  54. <el-checkbox
  55. v-model="loginForm.rememberMe"
  56. style="margin: 0px 0px 25px 0px"
  57. >记住密码</el-checkbox
  58. >
  59. <el-form-item style="width: 100%">
  60. <el-button
  61. :loading="loading"
  62. size="large"
  63. type="primary"
  64. style="width: 100%"
  65. @click.prevent="handleLogin"
  66. >
  67. <span v-if="!loading">登 录</span>
  68. <span v-else>登 录 中...</span>
  69. </el-button>
  70. <div style="float: right" v-if="register">
  71. <router-link class="link-type" :to="'/'">立即注册</router-link>
  72. </div>
  73. </el-form-item>
  74. </el-form>
  75. <!-- 底部 -->
  76. <div class="el-login-footer">
  77. <span></span>
  78. </div>
  79. </div>
  80. </template>
  81. <script setup>
  82. import { getCodeImg } from "@/api/login";
  83. import Cookies from "js-cookie";
  84. import { encrypt, decrypt } from "@/utils/jsencrypt";
  85. const store = useStore();
  86. const router = useRouter();
  87. const { proxy } = getCurrentInstance();
  88. const loginForm = ref({
  89. username: "admin",
  90. password: "123456",
  91. rememberMe: false,
  92. code: "",
  93. uuid: "",
  94. });
  95. const loginRules = {
  96. username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  97. password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  98. code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  99. };
  100. const codeUrl = ref("");
  101. const loading = ref(false);
  102. // 验证码开关
  103. const captchaOnOff = ref(true);
  104. // 注册开关
  105. const register = ref(true);
  106. const redirect = ref(undefined);
  107. function handleLogin() {
  108. proxy.$refs.loginRef.validate((valid) => {
  109. if (valid) {
  110. loading.value = true;
  111. // 勾选了需要记住密码设置在cookie中设置记住用户明和名命
  112. if (loginForm.value.rememberMe) {
  113. Cookies.set("username", loginForm.value.username, { expires: 30 });
  114. Cookies.set("password", encrypt(loginForm.value.password), {
  115. expires: 30,
  116. });
  117. Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
  118. } else {
  119. // 否则移除
  120. Cookies.remove("username");
  121. Cookies.remove("password");
  122. Cookies.remove("rememberMe");
  123. }
  124. // 调用action的登录方法
  125. store
  126. .dispatch("Login", loginForm.value)
  127. .then(() => {
  128. router.push({ path: redirect.value || "/" });
  129. })
  130. .catch(() => {
  131. loading.value = false;
  132. // 重新获取验证码
  133. if (captchaOnOff.value) {
  134. getCode();
  135. }
  136. });
  137. }
  138. });
  139. }
  140. function getCode() {
  141. getCodeImg().then((res) => {
  142. captchaOnOff.value =
  143. res.captchaOnOff === undefined ? true : res.captchaOnOff;
  144. if (captchaOnOff.value) {
  145. codeUrl.value = "data:image/gif;base64," + res.img;
  146. loginForm.value.uuid = res.uuid;
  147. }
  148. });
  149. }
  150. function getCookie() {
  151. const username = Cookies.get("username");
  152. const password = Cookies.get("password");
  153. const rememberMe = Cookies.get("rememberMe");
  154. loginForm.value = {
  155. username: username === undefined ? loginForm.value.username : username,
  156. password:
  157. password === undefined ? loginForm.value.password : decrypt(password),
  158. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  159. };
  160. }
  161. getCode();
  162. getCookie();
  163. </script>
  164. <style lang="scss" scoped>
  165. .login {
  166. display: flex;
  167. justify-content: center;
  168. align-items: center;
  169. height: 100%;
  170. background-image: url("../assets/backimg/login_back.jpeg");
  171. background-size: cover;
  172. }
  173. .title {
  174. margin: 0px auto 30px auto;
  175. text-align: center;
  176. color: #707070;
  177. }
  178. .login-form {
  179. border-radius: 6px;
  180. background: #ffffff;
  181. width: 400px;
  182. padding: 25px 25px 5px 25px;
  183. .el-input {
  184. height: 40px;
  185. input {
  186. height: 40px;
  187. }
  188. }
  189. .input-icon {
  190. height: 39px;
  191. width: 14px;
  192. margin-left: 0px;
  193. }
  194. }
  195. .login-tip {
  196. font-size: 13px;
  197. text-align: center;
  198. color: #bfbfbf;
  199. }
  200. .login-code {
  201. width: 33%;
  202. height: 40px;
  203. float: right;
  204. img {
  205. cursor: pointer;
  206. vertical-align: middle;
  207. }
  208. }
  209. .el-login-footer {
  210. height: 40px;
  211. line-height: 40px;
  212. position: fixed;
  213. bottom: 0;
  214. width: 100%;
  215. text-align: center;
  216. color: #fff;
  217. font-family: Arial;
  218. font-size: 12px;
  219. letter-spacing: 1px;
  220. }
  221. .login-code-img {
  222. height: 40px;
  223. padding-left: 12px;
  224. }
  225. </style>