/** * Created by RaoMeng on 2019/1/3 * Desc: 智慧校园首页 */ import React, {Component} from 'react' import 'bootstrap/dist/css/bootstrap.css' import 'bootstrap/dist/css/bootstrap.min.css' import {switchCurrentUser, switchUser} from "../../redux/actions/userInfo"; import Swiper from 'swiper/dist/js/swiper' import 'swiper/dist/css/swiper.min.css' import {Avatar} from 'antd' import {Toast, Modal} from 'antd-mobile' import {clearListState} from 'action/listState' import {clearClassData} from "action/classData"; import {connect} from "react-redux"; import {CONFIG_TEACHER_MENU, CONFIG_PARENT_MENU, CONFIG_HOME_TOP_MENU} from "../../utils/homePage.constants"; import {getIntValue, getStrValue, isObjEmpty} from "../../utils/common"; import 'css/home-page.css' import {fetchGet, fetchPost} from "../../utils/fetchRequest"; import {_baseURL, API} from "../../configs/api.config"; import icon_home_change from "../../style/imgs/icon_home_change.png"; import {getWeixinInfo} from '../../utils/api.request' const operation = Modal.operation; class HomePage extends Component { constructor() { super() this.state = { studentIndex: 0, } } componentWillMount() { // getWeixinInfo() } componentDidMount() { //清除列表缓存数据 clearListState()() //清除班级缓存数据 clearClassData()() document.title = "智慧校园"; this.mySwiper = new Swiper('.home-swiper-container', { autoplay: { disableOnInteraction: false, }, loop: true, touchRatio: 2, effect: 'coverflow', grabCursor: true, centeredSlides: true, slidesPerView: 'auto', init: false, speed: 200, freeMode: true, freeModeSticky: true, coverflowEffect: { rotate: 50, stretch: 0, depth: 100, modifier: 1, slideShadows: false, } }) if (!isObjEmpty(this.props.userInfo.parent, this.props.userInfo.parent.student, this.props.userInfo.parent.students)) { this.props.userInfo.parent.students.forEach((item, index) => { if (this.props.userInfo.parent.student.stuId == item.stuId) { this.setState({ studentIndex: index }) } }) } this.getHomeData() } componentWillUnmount() { } render() { const {userInfo} = this.props let {studentIndex} = this.state const studentList = [] if (!isObjEmpty(userInfo.parent) && !isObjEmpty(userInfo.parent.students) && userInfo.userRole === 1) { userInfo.parent.students.forEach((item, index) => { studentList.push() }) } const albumItems = [] let albums = '' if (userInfo.userRole === 1) { if (!isObjEmpty(userInfo.parent, userInfo.parent.student)) { albums = userInfo.parent.student.picAlbum } } else if (userInfo.userRole === 2) { if (userInfo.teacher) { albums = userInfo.teacher.albums } } if (!isObjEmpty(albums)) { albums.forEach((item, index) => { albumItems.push(
{/**/}
) }) } return (
{this.userInfoLayout()}
{studentList}
{this.getTopMenus()}
{/*功能菜单*/} {userInfo.userRole == 1 ? this.getParentMenu() : this.getTeacherMenu()} {/*班级相册*/} {
{albumItems}
} {/*{this.getAlbumLayout()}*/} {/*精彩瞬间*/} {/*{this.getVideoLayout()}*/} {/*底线标识*/} {this.getBottomLayout()}
) } getHomeData = () => { const {userInfo} = this.props let {studentIndex} = this.state let openId, schoolId if (userInfo.userRole === 1) { if (userInfo.parent) { openId = userInfo.parent.openid schoolId = userInfo.parent.schoolId } } else if (userInfo.userRole === 2) { if (userInfo.teacher) { openId = userInfo.teacher.openid schoolId = userInfo.teacher.schoolId } } //获取首页接口 fetchPost(API.homeIndex, { openid: openId, schoolId: schoolId }).then((response) => { Toast.hide(); if (response && response.data) { if (!isObjEmpty(response.data.teacher)) { const teacherDOS = response.data.teacher switchUser({ teacher: { teacherId: getIntValue(teacherDOS, 'teacherId'), userId: getIntValue(teacherDOS, 'userId'), openid: getStrValue(teacherDOS, 'openid'), userPhone: getStrValue(teacherDOS, 'userPhone'), schoolId: getStrValue(teacherDOS, 'schoolId'), schoolName: getStrValue(teacherDOS, 'schoolName'), teacherNumber: getStrValue(teacherDOS, 'teacherNumber'), teacherName: getStrValue(teacherDOS, 'teacherName'), teacherPhoto: getStrValue(teacherDOS, 'teacherPhoto'), teacherAddress: getStrValue(teacherDOS, 'teacherAddress'), teacherEntry: getStrValue(teacherDOS, 'teacherEntry'), teacherBirthday: getStrValue(teacherDOS, 'teacherBirthday'), teacherSex: getStrValue(teacherDOS, 'teacherSex'),//1:男,2:女 albums: getStrValue(teacherDOS, 'picAlbum'), videos: getStrValue(teacherDOS, 'videoAlbum') } })() } else { switchUser({ teacher: '' })() } if (!isObjEmpty(response.data.parents)) { const parentsDOS = response.data.parents switchUser({ parent: { parentId: getIntValue(parentsDOS, 'parentId'), userId: getIntValue(parentsDOS, 'userId'), userPhone: getStrValue(parentsDOS, 'userPhone'), openid: getStrValue(parentsDOS, 'openid'), schoolId: getIntValue(parentsDOS, 'schoolId'), schoolName: getStrValue(parentsDOS, 'schoolName'), parentsName: getStrValue(parentsDOS, 'parentsName'), parentsBirthday: getStrValue(parentsDOS, 'parentsBirthday'), parentsSex: getIntValue(parentsDOS, 'parentsSex'), parentsPhoto: getStrValue(parentsDOS, 'parentsPhoto'), parentsAddress: getStrValue(parentsDOS, 'parentsAddress'), students: getStrValue(parentsDOS, 'studentDOs'), student: isObjEmpty(userInfo.parent.student) ? getStrValue(parentsDOS, 'studentDOs')[0] : userInfo.parent.student, } })() } else { switchUser({ parent: '' })() } let userRole = userInfo.userRole if (!isObjEmpty(response.data.teacher)) { if (isObjEmpty(response.data.parents)) { userRole = 2 } } else if (!isObjEmpty(response.data.parents)) { userRole = 1 } //切换当前user信息 switchCurrentUser(userRole)() if (!isObjEmpty(userInfo.parent, userInfo.parent.students)) { userInfo.parent.students.forEach((item, index) => { if (userInfo.parent.student.stuId == item.stuId) { studentIndex = index } }) this.setState({studentIndex}) } this.mySwiper.init() } }).catch((error) => { Toast.hide(); if (typeof error === 'string') { Toast.fail(error, 2) } else { Toast.fail('数据加载失败', 2) } }) } onIdentitySwitch = () => { operation([ { text: '家长', onPress: () => { if (this.props.userInfo.userRole === 2) { Toast.loading('身份切换中...', 0) setTimeout(() => { Toast.success('切换成功!', 1) switchCurrentUser(1)() }, 500) } } }, { text: '教师', onPress: () => { if (this.props.userInfo.userRole === 1) { Toast.loading('身份切换中...', 0) setTimeout(() => { Toast.success('切换成功!', 1) switchCurrentUser(2)() }, 500) } } }, ]) } userInfoLayout = () => { const {userInfo} = this.props let schoolName = '', userAvatar = '', userName = '', albums = '' if (userInfo.userRole === 1) { if (userInfo.parent) { schoolName = userInfo.parent.schoolName userAvatar = userInfo.parent.parentsPhoto userName = userInfo.parent.parentsName } } else if (userInfo.userRole === 2) { if (userInfo.teacher) { schoolName = userInfo.teacher.schoolName userAvatar = userInfo.teacher.teacherPhoto userName = userInfo.teacher.teacherName } } return (
{schoolName}
{isObjEmpty(userAvatar) ? : } 尊敬的{userName + (userInfo.userRole === 1 ? '家长' : '老师')} {(userInfo.teacher && userInfo.parent) ? : ''}
) } getBottomLayout = () => { return (
已经到底了
) } getAlbumLayout = () => { const {userInfo} = this.props let albums = '' if (userInfo.userRole === 1) { if (!isObjEmpty(userInfo.parent, userInfo.parent.student)) { albums = userInfo.parent.student.picAlbum } } else if (userInfo.userRole === 2) { if (userInfo.teacher) { albums = userInfo.teacher.albums } } if (isObjEmpty(albums)) { return
} else { const albumItems = [] albums.forEach((item, index) => { albumItems.push(
{/**/}
) }) return
{albumItems}
} } getVideoLayout = () => { const {videos} = this.state if (isObjEmpty(videos)) { return
} else { return
} } getTopMenus = () => { const topMenus = [] CONFIG_HOME_TOP_MENU.forEach((topMenu, index) => { let menuPage = topMenu.page if (menuPage === '/userInfoPage') { if (this.props.userInfo.userRole == 1) { menuPage = '/userInfoPage/2' } else { menuPage = '/userInfoPage/1' } } topMenus.push() }) return topMenus } onStuSwitch = (stuIndex) => { if (this.state.studentIndex !== stuIndex) { Toast.loading('学生切换中...', 0) setTimeout(() => { Toast.success('切换成功!', 1) this.setState({ studentIndex: stuIndex }) switchUser({ stuName: this.props.userInfo.students[stuIndex].stuName, stuId: this.props.userInfo.students[stuIndex].stuId, student: this.props.userInfo.students[stuIndex] })() }, 500) } } getTeacherMenu = () => { let teacherMenu = [] if (!isObjEmpty(CONFIG_TEACHER_MENU)) { CONFIG_TEACHER_MENU.forEach((groupItem, groupIndex) => { if (groupIndex !== 0) { teacherMenu.push(
) } teacherMenu.push() const funcList = groupItem.funcList if (!isObjEmpty(funcList)) { funcList.forEach((funcItem, funcIndex) => { teacherMenu.push() }) } }) } return teacherMenu } getParentMenu = () => { let parentMenu = [] if (!isObjEmpty(CONFIG_PARENT_MENU)) { CONFIG_PARENT_MENU.forEach((groupItem, groupIndex) => { if (groupIndex !== 0) { parentMenu.push(
) } parentMenu.push() const funcList = groupItem.funcList if (!isObjEmpty(funcList)) { funcList.forEach((funcItem, funcIndex) => { parentMenu.push() }) } }) } return parentMenu } onTopFunc = (page) => { this.props.history.push(page) } onFuncClick = (page) => { if (!isObjEmpty(page)) { this.props.history.push(page) } } } class MenuGroup extends Component { render() { return (
{this.props.groupText}
) } } class FuncItem extends Component { render() { const {funcObj} = this.props return ( isObjEmpty(funcObj) ?
:
{funcObj.funcText}
) } onFuncClick = () => { if (this.props.onFuncClick) { this.props.onFuncClick(this.props.funcObj.funcPage) } } } class StuItem extends Component { render() { return (
{this.props.stuObj.stuPhoto ? : } {this.props.stuObj.stuName}
); } onStuSwitch = () => { if (this.props.onStuSwitch) { this.props.onStuSwitch(this.props.stuIndex) } } } class TopMenuItem extends Component { render() { return (
{this.props.text}
); } onMenuClick = () => { if (this.props.onMenuClick) { this.props.onMenuClick(this.props.page) } } } let mapStateToProps = (state) => ({ userInfo: {...state.redUserInfo} }) let mapDispatchToProps = (dispatch) => ({}) export default connect(mapStateToProps, mapDispatchToProps)(HomePage)