import router from '@/router' import store from '@/config/config' import ajax from '@/api/userApi' import { setToken, getToken } from '@/utils/auth' // get token from cookie import md5 from "./plugins/md5.min.js" let uid = ""; // eslint-disable-next-line prettier/prettier // let flag = false; //查看是否登录 // function testApi() { // ajax.post(store.state.api + '/testApi').then(res => { // console.log(res) // }).catch(err => { // console.log(err) // }) // } function userLogin(callback) { if (store.state.isLogin) return callback(true); ajax.post(store.state.api + '/userLogin', { id: uid, }).then(res => { if (res['data'] != "ERROR") { store.commit('update', ['userInfo', res['data']]); store.commit('update', ['isLogin', true]); callback(false) } else { console.error("错误") } }).catch(err => { console.log(err) }) } function getSystemState(to, callback) { ajax.get(store.state.api + '/getSystemState', { uid: uid, }).then(res => { let state = res['data'][0][0]['state']; store.commit('update', ['systemState', state]); if (state != 0 && ![0,3,4].includes(store.state.userInfo.type) && to.name != 'CloseWeb') return router.push('/CloseWeb'); if (state == 0 && to.name == 'CloseWeb') return router.push('/projectApplication'); callback(state); }).catch(err => { console.log(err) }) } router.beforeEach(async(to, from, next) => { try { /*本地环境👇*/ // uid = "1270"; // setToken(btoa(md5(uid))); // userLogin((isLogin) => { // getSystemState(to, () => { // if (!isLogin && to.name != 'resultShowDetail') { // store.commit('getPlanEndProjectData'); // } // next() // }) // }); /*本地环境👆*/ /*正式环境👇*/ const response = await ajax.get('https://cxcy.ssti.net.cn/sso/api'); let data = response.data.replaceAll("\'", "\"") if (data && JSON.parse(data).uid) { uid = JSON.parse(data).uid; setToken(btoa(md5(JSON.parse(data).uid))); userLogin((isLogin) => { getSystemState(to, () => { if (!isLogin && to.name != 'resultShowDetail') { store.commit('getPlanEndProjectData'); } next() }) }); } else { window.location.href = "https://cxcy.ssti.net.cn/sso/caslogin.jsp"; } /*正式环境👆 */ } catch { /*本地环境👇 */ // uid = "1270"; // setToken(btoa(md5(uid))); // userLogin((isLogin) => { // getSystemState(to, () => { // if (!isLogin && to.name != 'resultShowDetail') { // store.commit('getPlanEndProjectData'); // } // next() // }) // }); /*本地环境👆 */ /*正式环境👇 */ window.location.href = "https://cxcy.ssti.net.cn/sso/caslogin.jsp"; /*正式环境👆 */ } })