login.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template></template>
  2. <script>
  3. import user from '@/http/api/user'
  4. import { setToken,getToken } from '@/utils/auth'
  5. // import { getToken} from '@/utils/auth';
  6. import { encrypt } from '@/utils/jsencrypt'
  7. import base from '@/http/common/base'
  8. export default {
  9. data() {
  10. return {}
  11. },
  12. mounted() {
  13. // if(getToken()){
  14. // const directUrl = "/studentAnalysisReport/list";
  15. // this.$router.push({ path: directUrl });
  16. // return false
  17. // }else{
  18. // }
  19. // this.SubmitLogin();
  20. this.GetUserInfo()
  21. },
  22. methods: {
  23. //登录提交
  24. SubmitLogin() {
  25. const code = this.$route.query.code || ''
  26. const pw = this.$route.query.pw || ''
  27. const type = this.$route.query.type || '2' //1:单校 2:联校
  28. if (!code && !pw) {
  29. window.location.href = base.STUDENT_LOGIN //跳转到教师端登录页面
  30. }
  31. sessionStorage.setItem('schoolType', type) //1:单校 2:联校
  32. const params = {
  33. password: decodeURI(pw), //解密 encrypt(decodeURI(pw))
  34. username: code
  35. }
  36. user.loginEmailPass(params).then(res => {
  37. if (res.code === 200) {
  38. this.$store.dispatch('user/CLEAR_LOCAL_STORAGE') //清空本地存储
  39. setToken(res.data.tokenValue)
  40. this.$store.dispatch('user/SET_TOKEN', res.data.tokenValue)
  41. this.$store.dispatch('user/SET_SCHOOL_LOGO', res.data.schoolLogoUrl) //设置学校logo
  42. this.$store.dispatch('user/SET_SCHOOL_WEB_SITE_ID', res.data.cloudMonitorSiteId || '') //设置学校网站id
  43. this.$store.dispatch('user/getUserInfoByToken') //获取用户登录信息
  44. const directUrl = '/jointStudentAnalysisReport/list';//默认跳转到联校的地址
  45. this.$router.push({ path: directUrl })
  46. } else {
  47. this.$message({
  48. message: res.msg,
  49. type: 'warning',
  50. offset: 100
  51. })
  52. }
  53. })
  54. },
  55. //获取用户信息
  56. GetUserInfo() {
  57. const messageHandler = event => {
  58. console.log('打印父窗口传来的数据', event.data)
  59. console.log('打印父窗口的源', event.origin)
  60. console.log('打印当前窗口的源', window.location.origin)
  61. const schoolType = sessionStorage.getItem('schoolType');
  62. //安全检查 验证消息来源
  63. if (event.origin !== window.location.origin) {
  64. return
  65. }
  66. if (event.data.type === 'STU_TOKEN') {
  67. const userInfo = event.data.userInfo;
  68. const tokenValue = userInfo.tokenValue;
  69. const oldToken = getToken();
  70. if(oldToken && oldToken === tokenValue && schoolType){
  71. const currentPath = sessionStorage.getItem('student_currentPath');//获取当前进入的路由
  72. const directUrl = currentPath?currentPath:'/jointStudentAnalysisReport/list';
  73. this.$router.push({ path: directUrl })
  74. return
  75. }
  76. this.$store.dispatch('user/CLEAR_LOCAL_STORAGE') //清空本地存储
  77. setToken(tokenValue)
  78. sessionStorage.setItem('schoolType', schoolType || 2) //1:单校 2:联校// 暂时写死单校 线上还是单校的
  79. this.$store.dispatch('user/SET_TOKEN', userInfo.tokenValue)
  80. this.$store.dispatch('user/SET_SCHOOL_LOGO', userInfo.schoolLogoUrl) //设置学校logo
  81. this.$store.dispatch('user/SET_SCHOOL_WEB_SITE_ID', userInfo.cloudMonitorSiteId || '') //设置学校网站id
  82. this.$store.dispatch('user/getUserInfoByToken').then(user => {
  83. // console.log('获取用户信息成功', this.$store.state.user.userInfo)
  84. let userInfos = this.$store.state.user.userInfo
  85. // 移除事件监听器,避免重复处理
  86. window.removeEventListener('message', messageHandler)
  87. if (userInfos.phone) {
  88. const directUrl = '/jointStudentAnalysisReport/list'
  89. this.$router.push({ path: directUrl })
  90. } else {
  91. this.$router.push({ path: '/userInfo/personInfo' })
  92. // // 使用 this.$confirm 替代
  93. // this.$confirm('为了方便找回密码,我们建议您绑定手机号!', '', {
  94. // confirmButtonText: '确定',
  95. // cancelButtonText: '取消',
  96. // customClass:"page_dialog",
  97. // type: 'warning'
  98. // }).then(() => {
  99. // // 用户点击立即绑定,跳转到个人资料页面
  100. // }).catch(() => {
  101. // // 用户点击稍后绑定,直接进入首页
  102. // const directUrl = "/studentAnalysisReport/list";
  103. // this.$router.push({ path: directUrl });
  104. // });
  105. }
  106. }) //获取用户登录信息
  107. }
  108. }
  109. // 添加消息监听器
  110. window.addEventListener('message', messageHandler)
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss">
  116. @use '@/styles/login.scss';
  117. </style>