forked from Gitlink/forgeplus-react
211 lines
7.4 KiB
TypeScript
211 lines
7.4 KiB
TypeScript
import React, { FC, useEffect, useState } from 'react';
|
|
import { ConfigProvider, Modal } from 'antd';
|
|
import zhCN from 'antd/es/locale/zh_CN';
|
|
import UserPopLogin from '@/components/User/PopLoginRegister/PopLogin'
|
|
import { Spin, Alert } from 'antd';
|
|
import { setLocale } from 'umi';
|
|
import { delCookie, trackEvent, trackEventCustom } from '@/utils/util';
|
|
import moment from 'moment';
|
|
import { connect, Loading, GlobalSettingModelState, UserModelState, Dispatch, useLocation } from 'umi';
|
|
import { getCourseParam } from '@/components/Header';
|
|
import ENV from '@/utils/env';
|
|
import { Base64 } from 'js-base64';
|
|
import { setCookie } from '@/utils/util';
|
|
|
|
interface PageProps {
|
|
loading: Loading,
|
|
globalSetting: GlobalSettingModelState,
|
|
user: UserModelState,
|
|
dispatch: Dispatch;
|
|
location: any
|
|
}
|
|
const SimpleLayouts: FC<PageProps> = ({ loading, globalSetting, children, user, dispatch, location, ...props }) => {
|
|
const loca = useLocation()
|
|
const payload = getCourseParam();
|
|
const { globalLoading } = globalSetting;
|
|
|
|
const getData = async () => {
|
|
let res;
|
|
|
|
if (location?.search) {
|
|
let subject_id;
|
|
const path = location.pathname.split("/");
|
|
if (path.includes("paths")) {
|
|
subject_id = path[2]
|
|
};
|
|
res = await dispatch({
|
|
type: 'user/getUserInfo',
|
|
payload: {
|
|
...payload,
|
|
...location.query,
|
|
subject_id,
|
|
websiteName: 'educoder',
|
|
},
|
|
})
|
|
} else {
|
|
res = await dispatch({
|
|
type: 'user/getUserInfo',
|
|
payload: {
|
|
...payload,
|
|
},
|
|
})
|
|
}
|
|
if(res){
|
|
InitSetting();
|
|
}
|
|
if (res?.username && loca.pathname === '/login') {
|
|
location.href = "/"
|
|
}
|
|
};
|
|
|
|
useEffect(()=>{
|
|
delCookie("logintrustie");
|
|
if(user?.userInfo?.profile_completed){
|
|
getEducoderUserInfo();
|
|
}
|
|
},[user?.userInfo])
|
|
|
|
const getEducoderUserInfo=async()=>{
|
|
if(user?.userInfo?.profile_completed){
|
|
delCookie("logintrustie");
|
|
var res = await dispatch({
|
|
type:"user/getEducoderUserInfo",
|
|
payload:{
|
|
websiteName: "gitlink" ,
|
|
openkey: Base64.encode("05e9081ede2e7425db064df44b5fb1897234f44f922443b89597d17b60dc8f3e"),
|
|
sign:"cdf0f69b4d5b4293f7914cc1f1f31742",
|
|
login: user?.userInfo?.login,
|
|
lastname:user?.userInfo?.real_name,
|
|
nickname:user?.userInfo?.nickname,
|
|
mail:user?.userInfo?.email,
|
|
school_name:user?.userInfo?.custom_department
|
|
}
|
|
})
|
|
if(res && res.token){
|
|
setCookie("logintrustie",res.token,1);
|
|
}
|
|
// setCookie("logintrustie","7e43fe5f1f10a8a78728fabc1d03c6a827e21f7e",1);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
delCookie("logintrustie");
|
|
getData();
|
|
}, [location.pathname])
|
|
|
|
const InitSetting= async () =>{
|
|
if (location.pathname.indexOf("classrooms") > -1 && location.pathname.indexOf("detail") === -1) {
|
|
} else {
|
|
document.body.scrollIntoView()
|
|
}
|
|
// 教育部考试系统
|
|
if(location.pathname.indexOf("/classrooms/4RW9CYHY") > -1 && document.domain !== "localhost" && document.domain !== "kepukehuan.educoder.net"){
|
|
window.location.href = "https://kepukehuan.educoder.net/classrooms/4RW9CYHY/exercise"
|
|
return;
|
|
}
|
|
|
|
dispatch({
|
|
type: 'globalSetting/query',
|
|
payload: {},
|
|
})
|
|
dispatch({ type: "globalSetting/setGlobalLoading", payload: { show: false, text: "" } })
|
|
|
|
if (window.location.href === 'https://jetcoder.educoder.net' || window.location.href === 'https://jetcoder.educoder.net/') {
|
|
window.location.href = 'https://www.educoder.net/problems'
|
|
return
|
|
}
|
|
if (location.pathname === '/admins' || location.pathname === '/sidekiq') {
|
|
const domain = document.domain
|
|
if (domain.indexOf("educoder.net") > -1) {
|
|
switch (domain) {
|
|
case "www.educoder.net":
|
|
case "educoder.net":
|
|
window.location.href = `https://data.educoder.net${location.pathname}`
|
|
break;
|
|
default:
|
|
let str = document.domain.split(".")
|
|
str[0] = str[0] + "-data";
|
|
window.location.href = `https://${str.join(".")}${location.pathname}`
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
Modal.destroyAll();
|
|
setLocale('zh-CN', false);
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (globalSetting.updateData.system_update) {
|
|
// if(new Date().getTime() < new Date(globalSetting.updateData.end_time).getTime())
|
|
if (!localStorage.updateEndTime) {
|
|
if (new Date().getTime() < new Date(globalSetting.updateData.end_time).getTime())
|
|
Modal.info({
|
|
title: globalSetting.updateData.subject,
|
|
content: globalSetting.updateData.system_score,
|
|
onOk: () => {
|
|
localStorage.updateEndTime = globalSetting.updateData.end_time
|
|
}
|
|
})
|
|
}
|
|
} else {
|
|
localStorage.removeItem("updateEndTime")
|
|
}
|
|
}, [globalSetting.updateData.system_update]);
|
|
|
|
|
|
|
|
const toTrackEvent = () => {
|
|
const trackUserTime = localStorage.trackUserTime
|
|
const trackUser = localStorage.trackUser
|
|
if (trackUserTime && moment(trackUserTime).isSame(moment().startOf('day'), 'd') && trackUser == user.userInfo?.login) {
|
|
|
|
} else {
|
|
localStorage.trackUserTime = new Date();
|
|
localStorage.trackUser = user.userInfo?.login;
|
|
let text = ''
|
|
switch (user.userInfo.role) {
|
|
case 1:
|
|
text = '超管'
|
|
break;
|
|
case 2:
|
|
text = '运营'
|
|
break;
|
|
case 5:
|
|
text = '认证教师'
|
|
break;
|
|
case 12:
|
|
text = '未认证教师'
|
|
break;
|
|
case 15:
|
|
text = '学生'
|
|
break;
|
|
}
|
|
if (user.userInfo?.identity !== 'student' && user.userInfo?.identity !== 'teacher') {
|
|
text = '专业人士'
|
|
}
|
|
if (user.userInfo?.user_school) {
|
|
trackEvent(['学校活跃用户', user.userInfo?.user_school])
|
|
trackEvent(['用户访问明细', user.userInfo?.login, user.userInfo?.user_school])
|
|
}
|
|
|
|
trackEvent(['平台PC端', '总活跃用户数'])
|
|
trackEvent(['平台PC端', text])
|
|
}
|
|
}
|
|
|
|
return (
|
|
<ConfigProvider locale={zhCN}>
|
|
<UserPopLogin />
|
|
<Spin size="large" spinning={globalLoading.show} tip={globalLoading.text} className="ant-spin-nested-loading-black">
|
|
{children}
|
|
</Spin>
|
|
</ConfigProvider>
|
|
);
|
|
};
|
|
|
|
export default connect(({ loading, globalSetting, user }: { loading: Loading, globalSetting: GlobalSettingModelState, user: UserModelState }) => ({
|
|
loading: loading,
|
|
globalSetting: globalSetting,
|
|
user
|
|
}))(SimpleLayouts);
|