login.vue 5.9 KB

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