account.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <el-form ref="formRef" :model="state.ruleForm" :rules="rules" class="login-content-form" size="large"
  3. @keyup.enter="loginClick">
  4. <el-form-item class="login-animation1" prop="username">
  5. <el-input v-model="ruleForm.username" :placeholder="$t('message.account.accountPlaceholder1')" autocomplete="off"
  6. clearable type="text">
  7. <template #prefix>
  8. <el-icon class="el-input__icon">
  9. <ele-User />
  10. </el-icon>
  11. </template>
  12. </el-input>
  13. </el-form-item>
  14. <el-form-item class="login-animation2" prop="password">
  15. <el-input v-model="ruleForm.password" :placeholder="$t('message.account.accountPlaceholder2')"
  16. :type="isShowPassword ? 'text' : 'password'">
  17. <template #prefix>
  18. <el-icon class="el-input__icon">
  19. <ele-Unlock />
  20. </el-icon>
  21. </template>
  22. <template #suffix>
  23. <i :class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
  24. class="iconfont el-input__icon login-content-password"
  25. @click="isShowPassword = !isShowPassword">
  26. </i>
  27. </template>
  28. </el-input>
  29. </el-form-item>
  30. <el-form-item v-if="isShowCaptcha" class="login-animation3" prop="captcha">
  31. <el-col :span="15">
  32. <el-input v-model="ruleForm.captcha" :placeholder="$t('message.account.accountPlaceholder3')" autocomplete="off"
  33. clearable maxlength="4" type="text">
  34. <template #prefix>
  35. <el-icon class="el-input__icon">
  36. <ele-Position />
  37. </el-icon>
  38. </template>
  39. </el-input>
  40. </el-col>
  41. <el-col :span="1"></el-col>
  42. <el-col :span="8">
  43. <el-button class="login-content-captcha">
  44. <el-image :src="ruleForm.captchaImgBase" @click="refreshCaptcha" />
  45. </el-button>
  46. </el-col>
  47. </el-form-item>
  48. <el-form-item class="login-animation4">
  49. <el-button :loading="loginLoading" class="login-content-submit" round type="primary"
  50. @click="loginClick">
  51. <span>{{ $t('message.account.accountBtnText') }}</span>
  52. </el-button>
  53. </el-form-item>
  54. </el-form>
  55. </template>
  56. <script lang="ts">
  57. import { computed, defineComponent, onMounted, reactive, ref, toRefs } from 'vue';
  58. import { useRoute, useRouter } from 'vue-router';
  59. import { ElMessage, FormRules } from 'element-plus';
  60. import { useI18n } from 'vue-i18n';
  61. import Cookies from 'js-cookie';
  62. import { storeToRefs } from 'pinia';
  63. import { useThemeConfig } from '/@/stores/themeConfig';
  64. import { initFrontEndControlRoutes } from '/@/router/frontEnd';
  65. import { initBackEndControlRoutes } from '/@/router/backEnd';
  66. import { Session } from '/@/utils/storage';
  67. import { formatAxis } from '/@/utils/formatTime';
  68. import { NextLoading } from '/@/utils/loading';
  69. import * as loginApi from '/@/views/system/login/api';
  70. import { useUserInfo } from '/@/stores/userInfo';
  71. import { DictionaryStore } from '/@/stores/dictionary';
  72. import { SystemConfigStore } from '/@/stores/systemConfig';
  73. import { Md5 } from 'ts-md5';
  74. import { errorMessage } from '/@/utils/message';
  75. import emitter from '/@/utils/emitter'
  76. export default defineComponent({
  77. name: 'loginAccount',
  78. setup() {
  79. const loginLoading = ref(false);
  80. const { t } = useI18n();
  81. const storesThemeConfig = useThemeConfig();
  82. const { themeConfig } = storeToRefs(storesThemeConfig);
  83. const { userInfos } = storeToRefs(useUserInfo());
  84. const route = useRoute();
  85. const router = useRouter();
  86. const state = reactive({
  87. isShowPassword: false,
  88. ruleForm: {
  89. username: '',
  90. password: '',
  91. captcha: '',
  92. captchaKey: '',
  93. captchaImgBase: ''
  94. },
  95. loading: {
  96. signIn: false
  97. }
  98. });
  99. const rules = reactive<FormRules>({
  100. username: [
  101. { required: true, message: '请填写账号', trigger: 'blur' }
  102. ],
  103. password: [
  104. {
  105. required: true,
  106. message: '请填写密码',
  107. trigger: 'blur'
  108. }
  109. ],
  110. captcha: [
  111. {
  112. required: true,
  113. message: '请填写验证码',
  114. trigger: 'blur'
  115. }
  116. ]
  117. });
  118. const formRef = ref();
  119. // 时间获取
  120. const currentTime = computed(() => {
  121. return formatAxis(new Date());
  122. });
  123. // 是否关闭验证码
  124. const isShowCaptcha = computed(() => {
  125. return SystemConfigStore().systemConfig['base.captcha_state'];
  126. });
  127. const getCaptcha = async () => {
  128. loginApi.getCaptcha().then((ret: any) => {
  129. state.ruleForm.captchaImgBase = ret.data.image_base;
  130. state.ruleForm.captchaKey = ret.data.key;
  131. });
  132. };
  133. const refreshCaptcha = async () => {
  134. state.ruleForm.captcha = '';
  135. loginApi.getCaptcha().then((ret: any) => {
  136. state.ruleForm.captchaImgBase = ret.data.image_base;
  137. state.ruleForm.captchaKey = ret.data.key;
  138. });
  139. };
  140. const loginClick = async () => {
  141. if (!formRef.value) return;
  142. await formRef.value.validate((valid: any) => {
  143. if (valid) {
  144. loginLoading.value = true;
  145. loginApi.login({ ...state.ruleForm, password: Md5.hashStr(state.ruleForm.password) }).then((res: any) => {
  146. if (res.code === 2000) {
  147. Session.set('token', res.data.access);
  148. Cookies.set('username', res.data.name);
  149. if (!themeConfig.value.isRequestRoutes) {
  150. // 前端控制路由,2、请注意执行顺序
  151. initFrontEndControlRoutes();
  152. loginSuccess();
  153. } else {
  154. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  155. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  156. initBackEndControlRoutes();
  157. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  158. loginSuccess();
  159. }
  160. }
  161. }).catch((err: any) => {
  162. // 登录错误之后,刷新验证码
  163. refreshCaptcha();
  164. }).finally(() => {
  165. loginLoading.value = false;
  166. });
  167. } else {
  168. errorMessage('请填写登录信息');
  169. }
  170. });
  171. };
  172. const getUserInfo = () => {
  173. useUserInfo().setUserInfos();
  174. };
  175. emitter.on('scan-wecomLogin', (userInfo: any) => {
  176. if (userInfo.loginInfo.code == 2000) {
  177. Session.set('token', userInfo.loginInfo.data.access)
  178. Cookies.set('username', userInfo.loginInfo.data.name)
  179. if (!themeConfig.value.isRequestRoutes) {
  180. // 前端控制路由,2、请注意执行顺序
  181. // console.log('成功获取token,开始初始化前端路由')
  182. initFrontEndControlRoutes()
  183. loginSuccess()
  184. } else {
  185. initBackEndControlRoutes()
  186. loginSuccess()
  187. }
  188. emitter.off('scan-wecomLogin')
  189. }
  190. })
  191. // 登录成功后的跳转
  192. const loginSuccess = () => {
  193. //登录成功获取用户信息,获取系统字典数据
  194. getUserInfo();
  195. //获取所有字典
  196. DictionaryStore().getSystemDictionarys();
  197. // 初始化登录成功时间问候语
  198. let currentTimeInfo = currentTime.value;
  199. // 登录成功,跳到转首页
  200. // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
  201. if (route.query?.redirect) {
  202. router.push({
  203. path: <string>route.query?.redirect,
  204. query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : ''
  205. });
  206. } else {
  207. router.push('/');
  208. }
  209. // 登录成功提示
  210. // 关闭 loading
  211. state.loading.signIn = true;
  212. const signInText = t('message.signInText');
  213. ElMessage.success(`${ currentTimeInfo },${ signInText }`);
  214. // 添加 loading,防止第一次进入界面时出现短暂空白
  215. NextLoading.start();
  216. };
  217. onMounted(() => {
  218. getCaptcha();
  219. //获取系统配置
  220. SystemConfigStore().getSystemConfigs();
  221. });
  222. return {
  223. loginLoading,
  224. refreshCaptcha,
  225. loginClick,
  226. loginSuccess,
  227. isShowCaptcha,
  228. state,
  229. formRef,
  230. rules,
  231. ...toRefs(state)
  232. };
  233. }
  234. });
  235. </script>
  236. <style lang="scss" scoped>
  237. .login-content-form {
  238. margin-top: 20px;
  239. @for $i from 1 through 4 {
  240. .login-animation#{$i} {
  241. opacity: 0;
  242. animation-name: error-num;
  243. animation-duration: 0.5s;
  244. animation-fill-mode: forwards;
  245. animation-delay: calc($i/10) + s;
  246. }
  247. }
  248. .login-content-password {
  249. display: inline-block;
  250. width: 20px;
  251. cursor: pointer;
  252. &:hover {
  253. color: #909399;
  254. }
  255. }
  256. .login-content-captcha {
  257. width: 100%;
  258. padding: 0;
  259. font-weight: bold;
  260. letter-spacing: 5px;
  261. }
  262. .login-content-submit {
  263. width: 100%;
  264. letter-spacing: 2px;
  265. font-weight: 300;
  266. margin-top: 15px;
  267. }
  268. }
  269. </style>