permission.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import router from '@/router'
  2. import store from '@/config/config'
  3. import ajax from '@/api/userApi'
  4. import { setToken, getToken } from '@/utils/auth' // get token from cookie
  5. // eslint-disable-next-line prettier/prettier
  6. // let flag = false; //查看是否登录
  7. function userLogin(callback) {
  8. if (store.state.isLogin) return callback(true);
  9. ajax.post(store.state.api + '/userLogin', {
  10. id: getToken(),
  11. }).then(res => {
  12. if (res['data'] != "ERROR") {
  13. store.commit('update', ['userInfo', res['data']]);
  14. store.commit('update', ['isLogin', true]);
  15. callback(false)
  16. } else {
  17. console.error("错误")
  18. }
  19. }).catch(err => {
  20. console.log(err)
  21. })
  22. }
  23. function getSystemState(to, callback) {
  24. ajax.get(store.state.api + '/getSystemState', {
  25. uid: store.state.userInfo.userid
  26. }).then(res => {
  27. let state = res['data'][0][0]['state'];
  28. store.commit('update', ['systemState', state]);
  29. if (state != 0 && store.state.userInfo.type != 0 && to.name != 'CloseWeb') return router.push('/CloseWeb');
  30. if (state == 0 && to.name == 'CloseWeb') return router.push('/home');
  31. callback(state);
  32. }).catch(err => {
  33. console.log(err)
  34. })
  35. }
  36. router.beforeEach(async(to, from, next) => {
  37. // set page title
  38. try {
  39. // throw Error('测试')
  40. const response = await ajax.get('https://cxcy.ssti.net.cn/sso/api');
  41. console.log(response);
  42. let data = response.data.replaceAll("\'", "\"")
  43. if (data && JSON.parse(data).uid) {
  44. setToken(JSON.parse(data).uid);
  45. userLogin((isLogin) => {
  46. getSystemState(to, () => {
  47. if (!isLogin && to.name != 'resultShowDetail') {
  48. store.commit('getPlanEndProjectData');
  49. }
  50. next()
  51. })
  52. });
  53. } else {
  54. // setToken('2021400203')//yym
  55. // setToken("201901766");//qgt
  56. // userLogin((isLogin)=>{
  57. // getSystemState(to,()=>{
  58. // if(!isLogin&&to.name!='resultShowDetail'){
  59. // store.commit('getPlanEndProjectData');
  60. // }
  61. // next()
  62. // })
  63. // });
  64. window.location.href = "https://cxcy.ssti.net.cn/sso/caslogin.jsp";
  65. }
  66. } catch {
  67. // setToken('2021400203')//yym
  68. // setToken("1270"); //wyt
  69. // userLogin((isLogin) => {
  70. // getSystemState(to, () => {
  71. // if (!isLogin && to.name != 'resultShowDetail') {
  72. // store.commit('getPlanEndProjectData');
  73. // }
  74. // next()
  75. // })
  76. // });
  77. window.location.href = "https://cxcy.ssti.net.cn/sso/caslogin.jsp";
  78. }
  79. // determine whether the user has logged in
  80. })