account-tenant.vue 10 KB

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