permission.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. import md5 from "./plugins/md5.min.js"
  6. let uid = "";
  7. // eslint-disable-next-line prettier/prettier
  8. // let flag = false; //查看是否登录
  9. // function testApi() {
  10. // ajax.post(store.state.api + '/testApi').then(res => {
  11. // console.log(res)
  12. // }).catch(err => {
  13. // console.log(err)
  14. // })
  15. // }
  16. function userLogin(callback) {
  17. if (store.state.isLogin) return callback(true);
  18. ajax.post(store.state.api + '/userLogin', {
  19. id: uid,
  20. }).then(res => {
  21. if (res['data'] != "ERROR") {
  22. store.commit('update', ['userInfo', res['data']]);
  23. store.commit('update', ['isLogin', true]);
  24. callback(false)
  25. } else {
  26. console.error("错误")
  27. }
  28. }).catch(err => {
  29. console.log(err)
  30. })
  31. }
  32. function getSystemState(to, callback) {
  33. ajax.get(store.state.api + '/getSystemState', {
  34. uid: uid,
  35. }).then(res => {
  36. let state = res['data'][0][0]['state'];
  37. store.commit('update', ['systemState', state]);
  38. if (state != 0 && ![0,3,4].includes(store.state.userInfo.type) && to.name != 'CloseWeb') return router.push('/CloseWeb');
  39. if (state == 0 && to.name == 'CloseWeb') return router.push('/projectApplication');
  40. callback(state);
  41. }).catch(err => {
  42. console.log(err)
  43. })
  44. }
  45. router.beforeEach(async(to, from, next) => {
  46. try {
  47. /*本地环境👇*/
  48. // uid = "1270";
  49. // setToken(btoa(md5(uid)));
  50. // userLogin((isLogin) => {
  51. // getSystemState(to, () => {
  52. // if (!isLogin && to.name != 'resultShowDetail') {
  53. // store.commit('getPlanEndProjectData');
  54. // }
  55. // next()
  56. // })
  57. // });
  58. /*本地环境👆*/
  59. /*正式环境👇*/
  60. const response = await ajax.get('https://cxcy.ssti.net.cn/sso/api');
  61. let data = response.data.replaceAll("\'", "\"")
  62. if (data && JSON.parse(data).uid) {
  63. uid = JSON.parse(data).uid;
  64. setToken(btoa(md5(JSON.parse(data).uid)));
  65. userLogin((isLogin) => {
  66. getSystemState(to, () => {
  67. if (!isLogin && to.name != 'resultShowDetail') {
  68. store.commit('getPlanEndProjectData');
  69. }
  70. next()
  71. })
  72. });
  73. } else {
  74. window.location.href = "https://cxcy.ssti.net.cn/sso/caslogin.jsp";
  75. }
  76. /*正式环境👆 */
  77. } catch {
  78. /*本地环境👇 */
  79. // uid = "1270";
  80. // setToken(btoa(md5(uid)));
  81. // userLogin((isLogin) => {
  82. // getSystemState(to, () => {
  83. // if (!isLogin && to.name != 'resultShowDetail') {
  84. // store.commit('getPlanEndProjectData');
  85. // }
  86. // next()
  87. // })
  88. // });
  89. /*本地环境👆 */
  90. /*正式环境👇 */
  91. window.location.href = "https://cxcy.ssti.net.cn/sso/caslogin.jsp";
  92. /*正式环境👆 */
  93. }
  94. })