account.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <el-form ref="loginForm" size="large" class="login-content-form" :model="ruleForm" :rules="formRules">
  3. <el-form-item class="login-animation1" prop="userName">
  4. <el-input type="text" :placeholder="$t('message.account.accountPlaceholder1')" v-model="ruleForm.userName" clearable autocomplete="off">
  5. <template #prefix>
  6. <el-icon class="el-input__icon">
  7. <ele-User />
  8. </el-icon>
  9. </template>
  10. </el-input>
  11. </el-form-item>
  12. <el-form-item class="login-animation2" prop="password">
  13. <el-input :type="isShowPassword ? 'text' : 'password'" :placeholder="$t('message.account.accountPlaceholder2')" v-model="ruleForm.password" autocomplete="off" @keyup.enter="onSignIn">
  14. <template #prefix>
  15. <el-icon class="el-input__icon">
  16. <ele-Unlock />
  17. </el-icon>
  18. </template>
  19. <template #suffix>
  20. <i class="iconfont el-input__icon login-content-password" :class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'" @click="isShowPassword = !isShowPassword">
  21. </i>
  22. </template>
  23. </el-input>
  24. </el-form-item>
  25. <el-form-item class="login-animation3" prop="captcha">
  26. <el-col :span="15">
  27. <el-input type="text" maxlength="4" :placeholder="$t('message.account.accountPlaceholder3')" v-model="ruleForm.captcha" clearable autocomplete="off" @keyup.enter="onSignIn">
  28. <template #prefix>
  29. <el-icon class="el-input__icon">
  30. <ele-Position />
  31. </el-icon>
  32. </template>
  33. </el-input>
  34. </el-col>
  35. <el-col :span="1"></el-col>
  36. <el-col :span="8">
  37. <div class="login-content-code">
  38. <img class="login-content-code-img" @click="getCaptcha" width="130" height="38" :src="captchaSrc" style="cursor: pointer" />
  39. </div>
  40. </el-col>
  41. </el-form-item>
  42. <el-form-item class="login-animation4">
  43. <el-button type="primary" class="login-content-submit" round @click="onSignIn" :loading="loading.signIn">
  44. <span>{{ $t('message.account.accountBtnText') }}</span>
  45. </el-button>
  46. </el-form-item>
  47. </el-form>
  48. </template>
  49. <script lang="ts">
  50. import { toRefs, reactive, defineComponent, computed, onMounted, getCurrentInstance } from 'vue';
  51. import { useRoute, useRouter } from 'vue-router';
  52. import { ElMessage } from 'element-plus';
  53. import { useI18n } from 'vue-i18n';
  54. import { initFrontEndControlRoutes } from '/@/router/frontEnd';
  55. import { initBackEndControlRoutes } from '/@/router/backEnd';
  56. import { useStore } from '/@/store/index';
  57. import { Session } from '/@/utils/storage';
  58. import { formatAxis } from '/@/utils/formatTime';
  59. import api from '/@/api/system';
  60. export default defineComponent({
  61. name: 'loginAccount',
  62. setup() {
  63. const { t } = useI18n();
  64. const store = useStore();
  65. const route = useRoute();
  66. const router = useRouter();
  67. const { proxy } = getCurrentInstance() as any;
  68. const state = reactive({
  69. isShowPassword: false,
  70. ruleForm: {
  71. userName: 'admin',
  72. password: 'admin123456',
  73. captcha: '',
  74. VerifyKey: '',
  75. },
  76. formRules: {
  77. userName: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
  78. password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
  79. captcha: [{ required: true, trigger: 'blur', message: '验证码不能为空' }],
  80. },
  81. loading: {
  82. signIn: false,
  83. },
  84. captchaSrc: '',
  85. });
  86. onMounted(() => {
  87. getCaptcha();
  88. });
  89. // 时间获取
  90. const currentTime = computed(() => {
  91. return formatAxis(new Date());
  92. });
  93. const getCaptcha = () => {
  94. api.login.captcha().then((res: any) => {
  95. state.captchaSrc = res.img;
  96. state.ruleForm.VerifyKey = res.key;
  97. });
  98. };
  99. // 登录
  100. const onSignIn = () => {
  101. // 验证表单
  102. proxy.$refs.loginForm
  103. .validate((valid: boolean) => {
  104. if (valid) {
  105. state.loading.signIn = true;
  106. api.login
  107. .login(state.ruleForm)
  108. .then(async (res: any) => {
  109. const userInfos = res.userInfo;
  110. userInfos.avatar = proxy.getUpFileUrl(userInfos.avatar);
  111. // 存储 token 到浏览器缓存
  112. Session.set('token', res.token);
  113. // 存储用户信息到浏览器缓存
  114. Session.set('userInfo', userInfos);
  115. await store.dispatch('userInfos/setUserInfos', userInfos);
  116. currentUser();
  117. })
  118. .catch(() => {
  119. state.loading.signIn = false;
  120. getCaptcha();
  121. });
  122. }
  123. })
  124. .catch(() => {});
  125. };
  126. // 获取登录用户信息
  127. const currentUser = async () => {
  128. api.login.currentUser().then(async (res: any) => {
  129. // 设置用户菜单
  130. Session.set('userMenu', res);
  131. store.dispatch('requestOldRoutes/setBackEndControlRoutes', res);
  132. if (!store.state.themeConfig.themeConfig.isRequestRoutes) {
  133. // 前端控制路由,2、请注意执行顺序
  134. await initFrontEndControlRoutes();
  135. signInSuccess();
  136. } else {
  137. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  138. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  139. await initBackEndControlRoutes();
  140. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  141. signInSuccess();
  142. }
  143. });
  144. // // 设置按钮权限
  145. // Session.set('permissions', res.data.permissions);
  146. // // 1、请注意执行顺序(存储用户信息到vuex)
  147. // await store.dispatch('userInfos/setPermissions', res.data.permissions);
  148. };
  149. // 登录成功后的跳转
  150. const signInSuccess = () => {
  151. // 初始化登录成功时间问候语
  152. let currentTimeInfo = currentTime.value;
  153. // 登录成功,跳到转首页
  154. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  155. // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
  156. if (route.query?.redirect) {
  157. router.push({
  158. path: route.query?.redirect as string,
  159. query: Object.keys(route.query?.params as string).length > 0 ? JSON.parse(route.query?.params as string) : '',
  160. });
  161. } else {
  162. router.push('/');
  163. }
  164. // 登录成功提示
  165. // 关闭 loading
  166. state.loading.signIn = false;
  167. const signInText = t('message.signInText');
  168. ElMessage.success(`${currentTimeInfo},${signInText}`);
  169. };
  170. return {
  171. onSignIn,
  172. getCaptcha,
  173. ...toRefs(state),
  174. };
  175. },
  176. });
  177. </script>
  178. <style scoped lang="scss">
  179. .login-content-form {
  180. margin-top: 20px;
  181. @for $i from 1 through 4 {
  182. .login-animation#{$i} {
  183. opacity: 0;
  184. animation-name: error-num;
  185. animation-duration: 0.5s;
  186. animation-fill-mode: forwards;
  187. animation-delay: calc($i/10) + s;
  188. }
  189. }
  190. .login-content-password {
  191. display: inline-block;
  192. width: 20px;
  193. cursor: pointer;
  194. &:hover {
  195. color: #909399;
  196. }
  197. }
  198. .login-content-code {
  199. display: flex;
  200. align-items: center;
  201. justify-content: space-around;
  202. .login-content-code-img {
  203. width: 100%;
  204. height: 40px;
  205. line-height: 40px;
  206. background-color: #ffffff;
  207. border: 1px solid rgb(220, 223, 230);
  208. cursor: pointer;
  209. transition: all ease 0.2s;
  210. border-radius: 4px;
  211. user-select: none;
  212. &:hover {
  213. border-color: #c0c4cc;
  214. transition: all ease 0.2s;
  215. }
  216. }
  217. }
  218. .login-content-submit {
  219. width: 100%;
  220. letter-spacing: 2px;
  221. font-weight: 300;
  222. margin-top: 15px;
  223. }
  224. }
  225. </style>