forgeplus-react/src/forge/Head/Header.js

402 lines
13 KiB
JavaScript
Raw Normal View History

2021-03-23 17:40:23 +08:00
import React, { Component } from 'react';
import AccountProfile from "../../modules/user/AccountProfile";
import { getImageUrl } from 'educoder'
import axios from 'axios';
2021-11-08 13:29:12 +08:00
import cookie from 'react-cookies';
2021-11-10 15:07:39 +08:00
import { notification , Dropdown ,Popover, Menu,Badge } from 'antd';
2021-07-22 17:44:12 +08:00
import { Link } from 'react-router-dom';
2021-03-23 17:40:23 +08:00
import LoginDialog from '../../modules/login/LoginDialog';
2021-06-03 10:00:24 +08:00
import HeadSearch from '../Component/HeadSearch';
2021-05-06 17:12:42 +08:00
import AddProjectModal from './AddProjectModal';
2021-03-23 17:40:23 +08:00
import '../../modules/tpm/TPMIndex.css';
2021-09-01 17:46:58 +08:00
import CheckProfile from '../Component/ProfileModal/Profile';
2021-05-06 17:12:42 +08:00
2021-03-23 17:40:23 +08:00
import './header.scss';
2021-09-09 09:34:51 +08:00
import NoticeContent from './NoticeContent';
2021-11-10 15:07:39 +08:00
import MainLogo from './img/logo.png';
2021-03-23 17:40:23 +08:00
// TODO 这部分脚本从公共脚本中直接调用
window._header_componentHandler = null;
// 非trustie链接则新开页跳转
const str = ['www.trustie.net','forgeplus.trustie.net','forum.trustie.net','testforgeplus.trustie.net']
class NewHeader extends Component {
constructor(props) {
super(props)
this.state = {
Addcoursestypes: false,
tojoinitemtype: false,
tojoinclasstitle: undefined,
rolearr: ["", ""],
Checkboxteacherchecked: false,
Checkboxstudentchecked: false,
Checkboxteachingchecked: false,
Checkboxteachertype: false,
Checkboxteachingtype: false,
code_notice: false,
RadioGroupvalue: undefined,
submitapplications: false,
isRender: false,
showTrial: false,
setevaluatinghides: false,
occupation: 0,
mydisplay: false,
2022-01-05 11:48:31 +08:00
// headtypesonClickbool: false,
// headtypess: "/",
2021-03-23 17:40:23 +08:00
settings: null,
visiblemyss: false,
openSearch:false,
visible:false, //浮动消息框展示控制
2021-03-23 17:40:23 +08:00
}
}
componentDidMount() {
this.geturlsdata();
window._header_componentHandler = this;
try {
window.sessionStorage.setItem("yslgeturls", JSON.stringify(window.location.href))
} catch (e) {}
}
openNotification = (messge) => {
notification.open({
message: "提示",
description:
messge,
});
};
componentWillReceiveProps(newProps, oldProps) {
this.setState({
user: newProps.user
})
}
educoderlogin = () => {
//登录账号
2021-11-08 13:29:12 +08:00
if(window.location.pathname === "/"){
window.location.href="/login";
}else{
this.setState({
isRender: true
})
}
2021-03-23 17:40:23 +08:00
}
educoderloginysl = () => {
2021-11-08 13:29:12 +08:00
//退出账号时清除登录页面的下次自动登录(用户再次打开登录页面时下次自动登录框不勾选)
cookie.remove("autologin");
2021-03-23 17:40:23 +08:00
//退出账号
var url = `/accounts/logout.json`;
axios.get((url)).then((result) => {
if (result !== undefined) {
window.location.href = "/";
}
}).catch((error) => {
console.log(error);
})
}
hideAddcoursestypes = () => {
this.setState({
Addcoursestypes: false
})
};
HideAddcoursestypess = (i) => {
this.setState({
Addcoursestypes: false,
mydisplay: true,
occupation: i,
})
};
ModalCancelsy = () => {
this.setState({
mydisplay: false,
})
};
// 关闭
cancelModulationModels = () => {
this.setState({ isRenders: false })
}
setevaluatinghides = () => {
this.setState({
setevaluatinghides: true
})
}
//修改登录方法
Modifyloginvalue = () => {
this.setState({
isRender: false,
})
}
hideAccountProfile = () => {
this.setState({
AccountProfiletype: false
})
};
2022-01-05 11:48:31 +08:00
// headtypesonClick = (url, bool) => {
// this.setState({
// headtypess: url,
// headtypesonClickbool: bool,
// })
// }
2021-03-23 17:40:23 +08:00
//获取数据为空的时候
gettablogourlnull = () => {
this.setState({
settings: undefined
});
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = "/react/build/./favicon.ico";
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
};
//获取数据的时候
gettablogourldata = (response) => {
document.title = response.data.setting.name;
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = '/' + response.data.setting.tab_logo_url;
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
}
handleVisibleChanges = (boll) => {
this.setState({
visiblemyss: boll,
})
}
geturlsdata = () => {
let url = "/setting.json";
axios.get(url).then((response) => {
if (response && response.data) {
this.setState({ settings: response.data.setting });
localStorage.setItem('chromesetting', JSON.stringify(response.data.setting));
localStorage.setItem('chromesettingresponse', JSON.stringify(response));
2021-03-23 17:40:23 +08:00
} else {
this.gettablogourlnull();
}
}).catch((error) => {
this.gettablogourlnull();
});
}
matchpaths = (url) => {
const { match } = this.props;
2021-12-01 18:04:49 +08:00
const isDev = window.location.port == 3007;
const isdev2= isDev ?'https://testforgeplus.trustie.net':`https://${window.location.hostname}`;
let str = "";
if(url.indexOf("http")>-1){
2022-04-08 16:37:35 +08:00
str = isdev2+match.url;
2021-12-01 18:04:49 +08:00
}else{
str = match.path;
}
2022-05-23 14:36:51 +08:00
if (url && (str === url || (str === url+'/'))) {
2021-12-01 18:04:49 +08:00
return true
}else {
return false
2021-03-23 17:40:23 +08:00
}
}
2021-09-01 11:24:37 +08:00
checkProfile=(url)=>{
const { showCompeleteDialog , completeProfile } = this.props;
2021-09-01 17:46:58 +08:00
if(!completeProfile){
2021-09-01 11:24:37 +08:00
showCompeleteDialog && showCompeleteDialog();
}else{
2021-09-01 17:46:58 +08:00
window.location.href(url);
2021-09-01 11:24:37 +08:00
}
}
2021-03-23 17:40:23 +08:00
addMenu=(list)=>{
return(
list && list.length >0 &&
<div className="dropdownFlex">
<Menu>
{
list.map((item,key)=>{
return(
2021-09-01 10:25:12 +08:00
(item.name !=="加入课堂" && item.name !=="加入开发项目") &&
2021-09-01 17:46:58 +08:00
<Menu.Item key={item.name+key}>
<CheckProfile {...this.props} sureFunc={()=>{window.location.href=item.url}}>{item.name}</CheckProfile>
</Menu.Item>
2021-03-23 17:40:23 +08:00
)
})
}
2021-09-01 17:46:58 +08:00
<Menu.Item>
<AddProjectModal {...this.props} showNotification={this.props.showNotification}/>
</Menu.Item>
2021-03-23 17:40:23 +08:00
</Menu>
</div>
)
}
2021-09-07 09:33:46 +08:00
2021-06-09 11:52:36 +08:00
renderMenu=(personal)=>{
const { current_user } = this.props;
return(
<Menu className="currentMenu">
<Menu.Item>
2021-09-03 14:57:39 +08:00
<span className="currentName" title={current_user && current_user.username}>{current_user && current_user.username}</span>
2021-06-09 11:52:36 +08:00
</Menu.Item>
{
personal && personal.length > 0 && personal.map((item,key)=>{
return(
<li key={key}><a href={item.url} target="_blank">{item.name}</a></li>
)
})
}
2021-10-09 13:37:56 +08:00
{/* <li><Link to={`/settings/profile`}>设置</Link></li> */}
2021-06-09 11:52:36 +08:00
<Menu.Item><a onClick={() => this.educoderloginysl()}>退出</a></Menu.Item>
</Menu>
)
}
2021-03-23 17:40:23 +08:00
handleVisibleChange = visible => {
this.setState({ visible });
};
2021-03-23 17:40:23 +08:00
render() {
2021-11-08 15:12:12 +08:00
const { resetUserInfo ,showNotification,publicNav} = this.props;
2021-03-23 17:40:23 +08:00
let current_user = this.props.user;
2021-07-22 17:44:12 +08:00
let {
2021-03-23 17:40:23 +08:00
AccountProfiletype,
user,
isRender,
settings,
visible,
2021-03-23 17:40:23 +08:00
} = this.state;
2021-09-03 17:14:45 +08:00
let search_url = settings && settings.common && settings.common.search;
2021-03-23 17:40:23 +08:00
return (
2021-10-29 14:18:30 +08:00
<div className={publicNav ? `newHeaders publicNav`:`newHeaders`} id="nHeader">
2021-03-23 17:40:23 +08:00
<div className="headerContent">
{isRender === true ?
<LoginDialog
{...this.props}
{...this.state}
Modifyloginvalue={() => this.Modifyloginvalue()}
/> : ""}
{AccountProfiletype === true ?
<AccountProfile
hideAccountProfile={() => this.hideAccountProfile()}
{...this.props}
{...this.state}
/> : ""}
2022-01-05 11:48:31 +08:00
<div style={{width:"78px"}}>
{
publicNav &&
<a href={'https://www.ccf.org.cn/'} className={"fl"}>
2022-04-08 16:37:35 +08:00
<img src={MainLogo} alt="ccf" width="63px"/>
2022-01-05 11:48:31 +08:00
</a>
}
</div>
2021-03-23 17:40:23 +08:00
{
settings && settings.nav_logo_url ?
2021-11-11 16:35:37 +08:00
<a href={settings && settings.new_course.default_url} className={"fl mr50"}>
<img alt="可控开源社区" className="logoimg" src={getImageUrl(`/${settings.nav_logo_url}`)}></img>
</a>
2021-03-23 17:40:23 +08:00
:
""
2021-03-23 17:40:23 +08:00
}
<div className="head-nav pr" id={"head-navpre1"}>
{
settings && settings.navbar && settings.navbar.length > 0 ?
<ul id="header-nav">
{
settings.navbar && settings.navbar.map((item, key) => {
var new_link = item.link;
2022-01-05 11:48:31 +08:00
// var user_login = current_user && current_user.login;
var is_hidden = item.hidden;
// if (new_link && (new_link.indexOf("courses") > -1 || new_link.indexOf("contests") > -1)) {
// if (user_login) {
// if (new_link.indexOf("courses") > -1) {
// new_link = new_link.replace(/courses/g, user_login + "/courses")
// } else if (new_link.indexOf("contests") > -1) {
// new_link = new_link.replace(/contests/g, user_login + "/contests")
// }
// } else {
// is_hidden = true
// }
// }
// if (user_login && (new_link && new_link.indexOf("homes") > -1)) {
// new_link = new_link.replace(/homes/g, user_login + "/user_activities")
// }
2021-03-23 17:40:23 +08:00
var waiLian = (new_link && str.filter(item=>new_link.indexOf(item)>-1) );
var wl = waiLian && waiLian.length>0;
return (
2022-01-05 11:48:31 +08:00
<li key={key} className={`${this.matchpaths(new_link) === true ? 'pr active' : 'pr'}`} style={{display:!is_hidden ? 'flex' : 'none'} }>
2021-03-23 17:40:23 +08:00
<a href={new_link} target={wl ? "_self":"_blank"}>{item.name}</a>
</li>
)
})
}
</ul>
: ""
}
</div>
<div className="head-right">
2021-09-03 17:14:45 +08:00
{ search_url && <HeadSearch {...this.props}/>}
2021-03-23 17:40:23 +08:00
{
current_user && (current_user.main_site || current_user.login) && (settings && settings.add && settings.add.length>0)?
<Dropdown overlay={this.addMenu(settings && settings.add)} placement="bottomRight">
2021-10-29 17:01:55 +08:00
<img src={require(`./img/add.png`)} alt="" width="16px" className="mr15 ml30"/>
2021-03-23 17:40:23 +08:00
</Dropdown>:""
}
2021-10-13 15:14:47 +08:00
{ (settings && settings.common && settings.common.notice) && (current_user && current_user.login)?
<Popover
overlayClassName="notice-popover"
placement={`bottomRight`}
2021-09-14 16:33:08 +08:00
content={<NoticeContent visible={visible} current_user={current_user} showNotification={showNotification} resetUserInfo={resetUserInfo}/>}
visible={visible}
onVisibleChange={this.handleVisibleChange}
2021-09-22 10:14:22 +08:00
destroyTooltipOnHide
>
2021-09-27 11:32:05 +08:00
<Link to={"/settings/notice"} className="message-icon">
2021-10-13 13:37:51 +08:00
{current_user && <Badge count={current_user.message_unread_total}>
2021-10-29 17:01:55 +08:00
<img src={require(`./img/ring.png`)} alt="" width="16px" className="ml15 mr15"/>
2021-10-13 13:37:51 +08:00
</Badge>}
2021-09-16 17:16:23 +08:00
</Link>
2021-09-09 09:34:51 +08:00
</Popover>
: ""
2021-03-23 17:40:23 +08:00
}
</div>
{!user || (user && !user.login) ?
<span className="font-15 ml30">
2021-10-26 09:40:35 +08:00
<a onClick={() => this.educoderlogin()} className="mr5 color-white">登录</a>
2021-03-23 17:40:23 +08:00
{
2021-11-11 13:41:55 +08:00
(settings && settings.common && settings.common.register) ?
publicNav ?
2021-11-12 12:33:46 +08:00
<a href='/register' className="regBtn">立即注册</a>
2021-11-11 13:41:55 +08:00
:
2021-11-12 12:33:46 +08:00
<span><em className="vertical-line"></em><a className="ml5 color-white" href='/register'></a></span>
2021-11-11 13:41:55 +08:00
:""
2021-03-23 17:40:23 +08:00
}
</span>
:
<Dropdown placement={`bottomRight`} overlay={this.renderMenu(settings && settings.personal)}>
2021-08-30 10:31:51 +08:00
<a href={`/${this.props.current_user && this.props.current_user.login}`}>
2021-06-09 11:52:36 +08:00
<img alt="头像" src={getImageUrl(`/${user.image_url}`)} className="currentImg"></img>
2021-03-23 17:40:23 +08:00
</a>
2021-06-09 11:52:36 +08:00
</Dropdown>
2021-03-23 17:40:23 +08:00
}
</div>
</div>
);
}
}
export default NewHeader;