/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow */ import React, { Component } from 'react'; import { Platform, StyleSheet, Text, View, Image, FlatList, TouchableOpacity, ImageBackground, TextInput, Animated, StatusBar, DeviceEventEmitter } from 'react-native'; import BasePage from '../BasePage'; import Header from './Header'; import MySwiper from './Swiper'; import Dimensions from '../utils/dimensions'; import TopicTitle from './TopicTitle'; import ScrollRow from './ScrollRow'; import LibraryUrl from '../services/library'; import { ScrollView } from 'react-native-gesture-handler'; import commonutil from '../utils/commonutil'; import http_user from '../services/user'; import Toast from '../components/Toast'; export default class MainPage extends BasePage { constructor(props) { super(props); state: { file_user_data: new Map(); } } updateRender(index) { // LibraryUrl.getIndex(index).success(res => { // }).fail(() => { // alert(1111) // }) // 触发更新 LibraryUrl.getIndex(index).then((res) => { console.log(res); const monthTopicTitle = res.data.monthTopic.zone.title; const monthTopicScroll = res.data.monthTopic.zoneCourseVOList; const schedule = res.data.schedule; const hotCourse = res.data.hotCourse; const recommend = res.data.recommend; //全部课程 const banner = res.data.banner; //轮播图 const pageCode = res.data.pageCode; // const listData = []; // for(let item in res.data){ // console.log(item) // listData.push(1) // } // console.log(typeof listData) // console.log(listData) // console.log(listData.length) let scheduleWeek = schedule[0].featureMap ? schedule[0].featureMap.weekSeq : '1'; this.setState({ // listData: listData.length? listData : [], hotCourse, monthTopicTitle, monthTopicScroll, schedule, recommend, banner, scheduleWeek, pageCode }); console.log(this.state.banner); }); } componentWillUnmount() { this.refreshSubScription.remove(); } state = { grade: '大学', ifGradeMenuShow: false, fadeAnim: new Animated.Value(0), schedule: [], recommend: [], listData: [], banner: [], currentSwitch: { title: '小学', param: 'PRIMARY_SCHOOL' }, switchList: [ // { title: '幼儿园', param: 'KINDERGARTEN' }, { title: '学前', param: 'PRESCHOOL' }, { title: '小学', param: 'PRIMARY_SCHOOL' }, { title: '中学', param: 'MIDDLE_SCHOOL' } ], scheduleWeek: '1', username: '游客', avatar: '', isVip: false, showLoading: true }; renderBtn = (item, index) => { return ( // this.state.currentSwitch.param === item.param // ? // null // : // this.switchGrade(index)} key={index} style={index === 1 ? styles.gradeItemMid : styles.gradeItem}> // {item.title} // this.switchGrade(index)} key={index} style={index === 1 ? styles.gradeItemMid : styles.gradeItem} > {item.title} ); }; componentWillMount() { this.refreshSubScription = DeviceEventEmitter.addListener('indexInfo', () => { this.getUserInfo(); }); DeviceEventEmitter.emit('indexInfo'); } componentDidMount() { // this.updateRender('PRIMARY_SCHOOL'); } switchGrade = (index) => { const obj = this.state.switchList[index]; this.setState({ currentSwitch: obj }); this.hideGradeMenu(); // if (obj.param === this.state.currentSwitch.param) return if (!this.state.file_user_data.get('isVisitor')) { //这里更新个人信息学龄选项 var bool = this.updateUserInfo(obj.param); } this.updateRender(obj.param); }; render() { // const abc =Object.keys(this.state.data).map((k)=>this.state.data[k]) return ( {/* { return this.loadFlatItem(item); }} keyExtractor={(item, index) => item.key.toString()} ListHeaderComponent={() => this.headerorfooterComponent()} renderSectionHeader={this.sectionTitle} onScroll={this.hideGradeMenu} /> */} {this.headerorfooterComponent()} {this.getSwiperElement()} {this.getScheduleElement()} {this.getMonthTopic()} {this.getHotCourse()} {this.getAllCourses()} {this.state.ifGradeMenuShow ? ( {this.state.switchList.map((item, index) => this.renderBtn(item, index))} ) : null} ); } // 头部组件 headerorfooterComponent() { return (
this.toNextPage('userCenter')} /> {/* 搜索区域 */} this.search(this)} style={{ position: 'absolute', left: '45%', top: 10 }} > {this.state.currentSwitch.title} ); } // 渲染轮播图 getSwiperElement() { return ( ); } // 渲染课程表 getScheduleElement() { let renderScheduleItem = (item, index) => { const id = item.operationContent; return ( this.toNextPage('SchedulePage', { pagecode: id })} key={index} activeOpacity={1} > {item.operationContent === 'KINDERGARTE' ? null : ( {item.title} {item.summary} )} ); }; return ( {/* {this.state.schedule.map((item, index) => renderScheduleItem(item, index))} */} renderScheduleItem(item, index)} keyExtractor={(item, index) => index.toString()} showsHorizontalScrollIndicator={false} /> ); } // 渲染月主题 getMonthTopic() { return ( ); } // 渲染热门课程 getHotCourse() { return ( ); } // 渲染全部课程 getAllCourses() { return ( {this.state.recommend.map((item, index) => { return ( {index === 0 ? null : ( )} ); })} ); } // 渲染每一个块儿 sectionList组件的data,没有用到,以后可能会用 loadFlatItem(data) { switch (data.title) { // 轮播图 case 'banner': return this.getSwiperElement(); break; // 课程表 case 'schedule': return this.getScheduleElement(); break; // 大图主题 case 'monthTopic': return this.getMonthTopic(); break; // 热门课程 case 'hotCourse': return this.getHotCourse(); break; case 'recommend': return this.getAllCourses(); break; default: break; } } // 渲染每一个section的title sectionList组件的data,没有用到,以后可能会用 sectionTitle = (info) => { switch (info.section.title) { case 'operation': return ( ); break; case 'courses': return ; break; default: break; } }; // 点击呼出学龄菜单 tabCallOutHandler = () => { this.setState( { ifGradeMenuShow: !this.state.ifGradeMenuShow }, () => { Animated.timing(this.state.fadeAnim, { toValue: this.state.ifGradeMenuShow ? 1 : 0, duration: 300 }).start(); } ); }; // 如果切换学龄在显示状态,那么在它失去焦点时将它隐藏 hideGradeMenu = () => { if (this.state.ifGradeMenuShow) { this.setState({ ifGradeMenuShow: false }); } }; // 获取用户信息 async getUserInfo() { await global.storage .load({ key: 'userInfo' }) .then((result) => { this.state.file_user_data = commonutil.jsonToMap(result); var ageGroup = this.state.file_user_data.get('ageGroup'); const username = this.state.file_user_data.get('nickName'); const avatar = this.state.file_user_data.get('avatar'); this.setState({ username, avatar }); var ageindex = 0; if (ageGroup === 'PRESCHOOL') { ageindex = 0; } else if (ageGroup === 'PRIMARY_SCHOOL') { ageindex = 1; } else if (ageGroup === 'MIDDLE_SCHOOL') { ageindex = 2; } this.switchGrade(ageindex); }) .catch((err) => { console.log('ERROR' + err.message); }); } async updateUserInfo(group) { let opts = { method: 'PUT', //请求方法 body: { ageGroup: group } //请求体 }; await http_user.update_UserInfo(opts).then((res) => { this.state.file_user_data.set('ageGroup', res.data.ageGroup); this.state.file_user_data.set('avatar', res.data.avatar); this.state.file_user_data.set('birthday', res.data.birthday); this.state.file_user_data.set('channel', res.data.channel); this.state.file_user_data.set('city', res.data.city); this.state.file_user_data.set('country', res.data.country); this.state.file_user_data.set('eid', res.data.eid); this.state.file_user_data.set('gmtCreated', res.data.gmtCreated); this.state.file_user_data.set('gmtModified', res.data.gmtModified); this.state.file_user_data.set('mobile', res.data.mobile); this.state.file_user_data.set('nickName', res.data.nickName); this.state.file_user_data.set('province', res.data.province); this.state.file_user_data.set('school', res.data.school); this.state.file_user_data.set('sex', res.data.sex); this.state.file_user_data.set('status', res.data.status); this.state.file_user_data.set('uid', res.data.uid); this.saveUserInfo(commonutil.mapToJson(this.state.file_user_data)); return true; }); } // 搜索 search() { this.toNextPage('SearchResult'); } } const styles = StyleSheet.create({ title_text: { justifyContent: 'center', alignItems: 'center', color: 'red', fontSize: 30, textAlign: 'center', marginTop: 30, marginBottom: 50 }, /* 搜索区域 */ searchSection: { flex: 1, width: Dimensions.width, height: Dimensions.getHeight(60), flexDirection: 'row', alignItems: 'center', backgroundColor: '#f0f1f5', justifyContent: 'space-around', position: 'relative' }, searchBox: { position: 'relative', width: Dimensions.getWidth(232), height: Dimensions.getHeight(40), backgroundColor: '#fff', borderRadius: 20 }, search: { width: 30, height: 30 }, rightBtn: { width: Dimensions.getWidth(113), height: Dimensions.getHeight(40), backgroundColor: '#fff', borderRadius: 20, alignItems: 'center', justifyContent: 'center', justifyContent: 'space-around', flexDirection: 'row', paddingLeft: 20, paddingRight: 10 }, rightBtnIcon: { width: Dimensions.getWidth(23), height: Dimensions.getHeight(23) }, rightBtnText: { color: '#151515', fontSize: 18 }, callUpGrade: { width: 113, height: 120, borderRadius: 20, position: 'absolute', right: 9, top: 125, zIndex: 999, elevation: 999, backgroundColor: '#fff', flexDirection: 'column' }, gradeItem: { zIndex: 999, flex: 1, alignItems: 'center', justifyContent: 'center' }, gradeItemMid: { zIndex: 999, flex: 1, alignItems: 'center', justifyContent: 'center', borderTopWidth: 1, borderBottomWidth: 1, borderColor: '#f0f1f5' }, gradeText: { color: '#151515', fontSize: 18 } }); const sectionData = [ { title: 'swiper', data: [ { //轮播 key: 111, typea: 1, text: '第一种类型' }, { //只有一个标题下面两个图片 key: 222, typea: 2, item: { item_type: 1, title: '课程表:第一周', icon_item: [ { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第一张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第二张图片文字' } ] } } ] }, { title: 'operation', data: [ { //一个标题,大长图 key: 444, typea: 3, title: '五月主题:五一启程,去看看不同世界', text: '每天30分钟,玩学两不误', icon_item: [ { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第一张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第二张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第三张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第四张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第五张图片文字' } ] }, { key: 555, typea: 4, title: '热门课程', text: 'text', mask: false, icon_item: [ { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第一张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第二张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第三张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第四张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第五张图片文字' } ] } ] }, { title: 'courses', data: [ { //一个标题,大长图 key: 444, typea: 5, title: '五月主题:五一启程,去看看不同世界', text: '每天30分钟,玩学两不误', icon_item: [ { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第一张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第二张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第三张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第四张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第五张图片文字' } ] }, { key: 555, typea: 5, title: '热门课程', text: 'text', mask: false, icon_item: [ { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第一张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第二张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第三张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第四张图片文字' }, { icon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg', name: '第五张图片文字' } ] } ] } ];