login.vue 5.9 KB

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