account.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. <el-image 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" @click="onSignIn" :loading="loading.signIn">
  44. <span>{{ $t('message.account.accountBtnText') }}</span>
  45. </el-button>
  46. </el-form-item>
  47. <changePwd ref="changePwdRef"></changePwd>
  48. </el-form>
  49. </template>
  50. <script lang="ts">
  51. import { ref, toRefs, reactive, defineComponent, computed, onMounted, getCurrentInstance } from 'vue';
  52. import { useRoute, useRouter } from 'vue-router';
  53. import changePwd from './changePwd.vue';
  54. import { ElMessage } from 'element-plus';
  55. import { useI18n } from 'vue-i18n';
  56. import { initFrontEndControlRoutes } from '/@/router/frontEnd';
  57. import { initBackEndControlRoutes } from '/@/router/backEnd';
  58. import { useStore } from '/@/store/index';
  59. import { Session, Local } from '/@/utils/storage';
  60. import { formatAxis } from '/@/utils/formatTime';
  61. import { encrypt } from '/@/utils/rsa'
  62. import api from '/@/api/system';
  63. // 是否是开源版本
  64. const ISOPEN = import.meta.env.VITE_ISOPEN
  65. export default defineComponent({
  66. name: 'loginAccount',
  67. components: {
  68. changePwd
  69. },
  70. setup() {
  71. const changePwdRef = ref();
  72. const { t } = useI18n();
  73. const store = useStore();
  74. const route = useRoute();
  75. const router = useRouter();
  76. const { proxy } = getCurrentInstance() as any;
  77. const state = reactive({
  78. isShowPassword: false,
  79. ruleForm: {
  80. userName: ISOPEN ? 'demo' : '',
  81. password: ISOPEN ? 'demo123456' : '',
  82. captcha: '',
  83. VerifyKey: '',
  84. },
  85. formRules: {
  86. userName: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
  87. password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
  88. captcha: [{ required: true, trigger: 'blur', message: '验证码不能为空' }],
  89. },
  90. loading: {
  91. signIn: false,
  92. },
  93. captchaSrc: '',
  94. });
  95. onMounted(() => {
  96. getCaptcha();
  97. });
  98. // 时间获取
  99. const currentTime = computed(() => {
  100. return formatAxis(new Date());
  101. });
  102. const getCaptcha = () => {
  103. api.login.captcha().then((res: any) => {
  104. state.captchaSrc = res.img;
  105. state.ruleForm.VerifyKey = res.key;
  106. });
  107. };
  108. // 登录
  109. const onSignIn = () => {
  110. // 验证表单
  111. proxy.$refs.loginForm
  112. .validate(async (valid: boolean) => {
  113. if (valid) {
  114. state.loading.signIn = true;
  115. let password: string
  116. if (sessionStorage.isRsaEnabled) {
  117. password = await encrypt(state.ruleForm.password)
  118. } else {
  119. password = state.ruleForm.password
  120. }
  121. api.login
  122. .login({
  123. ...state.ruleForm,
  124. password
  125. })
  126. .then(async (res: any) => {
  127. // 检查是否需要更换密码
  128. if (res.isChangePwd) {
  129. ElMessage.error(`密码已超过${sessionStorage.sysPasswordChangePeriod}天未修改,请先修改密码再登录`)
  130. state.loading.signIn = false;
  131. getCaptcha();
  132. return changePwdRef.value.toShow({
  133. userName: state.ruleForm.userName,
  134. oldUserPassword: state.ruleForm.password,
  135. })
  136. }
  137. localStorage.setItem('token', res.token);
  138. const userInfos = res.userInfo;
  139. userInfos.avatar = proxy.getUpFileUrl(userInfos.avatar);
  140. // 存储 token 到浏览器缓存
  141. Local.set('userInfo', userInfos);
  142. // 存储用户信息到浏览器缓存
  143. Session.set('userInfo', userInfos);
  144. // 获取权限配置,上传文件类型等
  145. const [columnRes, buttonRes, uploadFileRes] = await Promise.all([api.getInfoByKey('sys.column.switch'), api.getInfoByKey('sys.button.switch'), api.getInfoByKey('sys.uploadFile.way')])
  146. const isSecurityControlEnabled = sessionStorage.isSecurityControlEnabled || null
  147. localStorage.setItem('btnNoAuth', (isSecurityControlEnabled && Number(buttonRes?.data?.configValue)) ? '' : '1');
  148. localStorage.setItem('colNoAuth', (isSecurityControlEnabled && Number(columnRes?.data?.configValue)) ? '' : '1');
  149. localStorage.setItem('uploadFileWay', uploadFileRes?.data?.configValue || '0');
  150. await store.dispatch('userInfos/setUserInfos', userInfos);
  151. currentUser();
  152. })
  153. .catch(() => {
  154. state.loading.signIn = false;
  155. getCaptcha();
  156. });
  157. }
  158. })
  159. .catch(() => { });
  160. };
  161. // 获取登录用户信息
  162. const currentUser = async () => {
  163. api.login.currentUser().then(async (res: any) => {
  164. localStorage.setItem('userId', res.Info.id);
  165. // 设置用户菜单
  166. Session.set('userMenu', res.Data || []);
  167. store.dispatch('requestOldRoutes/setBackEndControlRoutes', res || []);
  168. if (!store.state.themeConfig.themeConfig.isRequestRoutes) {
  169. // 前端控制路由,2、请注意执行顺序
  170. await initFrontEndControlRoutes();
  171. signInSuccess();
  172. } else {
  173. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  174. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  175. await initBackEndControlRoutes();
  176. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  177. signInSuccess();
  178. }
  179. });
  180. // // 设置按钮权限
  181. // Session.set('permissions', res.data.permissions);
  182. // // 1、请注意执行顺序(存储用户信息到vuex)
  183. // await store.dispatch('userInfos/setPermissions', res.data.permissions);
  184. };
  185. // 登录成功后的跳转
  186. const signInSuccess = () => {
  187. // 修改首页重定向的地址,从后台配置中获取首页的地址并在登录之后和刷新页面时进行修改
  188. const sysinfo = JSON.parse(localStorage.sysinfo || '{}');
  189. const homePage = router.getRoutes().find((item) => item.path === '/');
  190. homePage && (homePage.redirect = sysinfo.systemHomePageRoute || '/home');
  191. // 初始化登录成功时间问候语
  192. let currentTimeInfo = currentTime.value;
  193. // 登录成功,跳到转首页
  194. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  195. // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
  196. if (route.query?.redirect) {
  197. router.push({
  198. path: route.query?.redirect as string,
  199. query: route.query.params ? (Object.keys(route.query?.params as string).length > 0 ? JSON.parse(route.query?.params as string) : '') : '',
  200. });
  201. } else {
  202. router.push('/');
  203. }
  204. // 登录成功提示
  205. // 关闭 loading
  206. state.loading.signIn = false;
  207. const signInText = t('message.signInText');
  208. ElMessage.success(`${currentTimeInfo},${signInText}`);
  209. };
  210. return {
  211. changePwdRef,
  212. onSignIn,
  213. getCaptcha,
  214. ...toRefs(state),
  215. };
  216. },
  217. });
  218. </script>
  219. <style scoped lang="scss">
  220. .login-content-form {
  221. width: 400px;
  222. margin-top: 20px;
  223. @for $i from 1 through 4 {
  224. .login-animation#{$i} {
  225. opacity: 0;
  226. animation-name: error-num;
  227. animation-duration: 0.5s;
  228. animation-fill-mode: forwards;
  229. animation-delay: calc($i/10) + s;
  230. }
  231. }
  232. .login-content-password {
  233. display: inline-block;
  234. width: 20px;
  235. cursor: pointer;
  236. &:hover {
  237. color: #909399;
  238. }
  239. }
  240. .login-content-code {
  241. display: flex;
  242. align-items: center;
  243. justify-content: space-around;
  244. .login-content-code-img {
  245. width: 100%;
  246. height: 40px;
  247. line-height: 40px;
  248. background-color: #ffffff;
  249. border: 1px solid rgb(220, 223, 230);
  250. cursor: pointer;
  251. transition: all ease 0.2s;
  252. border-radius: 4px;
  253. user-select: none;
  254. &:hover {
  255. border-color: #c0c4cc;
  256. transition: all ease 0.2s;
  257. }
  258. }
  259. }
  260. .login-content-submit {
  261. width: 100%;
  262. letter-spacing: 2px;
  263. font-weight: 300;
  264. margin-top: 15px;
  265. }
  266. }
  267. </style>