HomePage.jsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /**
  2. * Created by RaoMeng on 2019/1/3
  3. * Desc: 智慧校园首页
  4. */
  5. import React, {Component} from 'react'
  6. import 'bootstrap/dist/css/bootstrap.css'
  7. import 'bootstrap/dist/css/bootstrap.min.css'
  8. import {switchCurrentUser, switchUser} from "../../redux/actions/userInfo";
  9. import Swiper from 'swiper/dist/js/swiper'
  10. import 'swiper/dist/css/swiper.min.css'
  11. import {Avatar} from 'antd'
  12. import {Toast, Modal} from 'antd-mobile'
  13. import {clearListState} from 'action/listState'
  14. import {clearClassData} from "action/classData";
  15. import {connect} from "react-redux";
  16. import {CONFIG_TEACHER_MENU, CONFIG_PARENT_MENU, CONFIG_HOME_TOP_MENU} from "../../utils/homePage.constants";
  17. import {getIntValue, getStrValue, isObjEmpty} from "../../utils/common";
  18. import 'css/home-page.css'
  19. import {fetchGet, fetchPost} from "../../utils/fetchRequest";
  20. import {_baseURL, API} from "../../configs/api.config";
  21. import icon_home_change from "../../style/imgs/icon_home_change.png";
  22. import {getWeixinInfo} from '../../utils/api.request'
  23. const operation = Modal.operation;
  24. class HomePage extends Component {
  25. constructor() {
  26. super()
  27. this.state = {
  28. studentIndex: 0,
  29. }
  30. }
  31. componentWillMount() {
  32. // getWeixinInfo()
  33. }
  34. componentDidMount() {
  35. //清除列表缓存数据
  36. clearListState()()
  37. //清除班级缓存数据
  38. clearClassData()()
  39. document.title = "智慧校园";
  40. this.mySwiper = new Swiper('.home-swiper-container', {
  41. autoplay: {
  42. disableOnInteraction: false,
  43. },
  44. loop: true,
  45. touchRatio: 2,
  46. effect: 'coverflow',
  47. grabCursor: true,
  48. centeredSlides: true,
  49. slidesPerView: 'auto',
  50. init: false,
  51. speed: 200,
  52. freeMode: true,
  53. freeModeSticky: true,
  54. coverflowEffect: {
  55. rotate: 50,
  56. stretch: 0,
  57. depth: 100,
  58. modifier: 1,
  59. slideShadows: false,
  60. }
  61. })
  62. if (!isObjEmpty(this.props.userInfo.parent, this.props.userInfo.parent.student, this.props.userInfo.parent.students)) {
  63. this.props.userInfo.parent.students.forEach((item, index) => {
  64. if (this.props.userInfo.parent.student.stuId == item.stuId) {
  65. this.setState({
  66. studentIndex: index
  67. })
  68. }
  69. })
  70. }
  71. this.getHomeData()
  72. }
  73. componentWillUnmount() {
  74. }
  75. render() {
  76. const {userInfo} = this.props
  77. let {studentIndex} = this.state
  78. const studentList = []
  79. if (!isObjEmpty(userInfo.parent) && !isObjEmpty(userInfo.parent.students) && userInfo.userRole === 1) {
  80. userInfo.parent.students.forEach((item, index) => {
  81. studentList.push(<StuItem isSelect={studentIndex === index} stuObj={item}
  82. stuIndex={index} onStuSwitch={this.onStuSwitch}/>)
  83. })
  84. }
  85. const albumItems = []
  86. let albums = ''
  87. if (userInfo.userRole === 1) {
  88. if (!isObjEmpty(userInfo.parent, userInfo.parent.student)) {
  89. albums = userInfo.parent.student.picAlbum
  90. }
  91. } else if (userInfo.userRole === 2) {
  92. if (userInfo.teacher) {
  93. albums = userInfo.teacher.albums
  94. }
  95. }
  96. if (!isObjEmpty(albums)) {
  97. albums.forEach((item, index) => {
  98. albumItems.push(<div className="swiper-slide"
  99. style={{backgroundImage: 'url(' + _baseURL + item.albumUrl + ')'}}>
  100. {/*<img className='home-albums-img' src={_baseURL + item.picUrl}/>*/}
  101. </div>)
  102. })
  103. }
  104. return (
  105. <div className='home-page-root'>
  106. <div className='home-top-layout-root'>
  107. {this.userInfoLayout()}
  108. <div className='home-student-layout-root'>
  109. {studentList}
  110. </div>
  111. <div className='home-top-menu-root'>
  112. {this.getTopMenus()}
  113. </div>
  114. </div>
  115. {/*功能菜单*/}
  116. {userInfo.userRole == 1 ? this.getParentMenu() : this.getTeacherMenu()}
  117. {/*班级相册*/}
  118. {<div className={isObjEmpty(albumItems) ? 'displayNone' : ''}>
  119. <div className='gray-line'></div>
  120. <MenuGroup groupIcon={require('imgs/ic_group_album.png')} groupText='班级相册'/>
  121. <div className="home-swiper-container">
  122. <div className="swiper-wrapper">
  123. {albumItems}
  124. </div>
  125. </div>
  126. </div>}
  127. {/*{this.getAlbumLayout()}*/}
  128. {/*精彩瞬间*/}
  129. {/*{this.getVideoLayout()}*/}
  130. {/*底线标识*/}
  131. {this.getBottomLayout()}
  132. </div>
  133. )
  134. }
  135. getHomeData = () => {
  136. const {userInfo} = this.props
  137. let {studentIndex} = this.state
  138. let openId, schoolId
  139. if (userInfo.userRole === 1) {
  140. if (userInfo.parent) {
  141. openId = userInfo.parent.openid
  142. schoolId = userInfo.parent.schoolId
  143. }
  144. } else if (userInfo.userRole === 2) {
  145. if (userInfo.teacher) {
  146. openId = userInfo.teacher.openid
  147. schoolId = userInfo.teacher.schoolId
  148. }
  149. }
  150. //获取首页接口
  151. fetchPost(API.homeIndex, {
  152. openid: openId,
  153. schoolId: schoolId
  154. }).then((response) => {
  155. Toast.hide();
  156. if (response && response.data) {
  157. if (!isObjEmpty(response.data.teacher)) {
  158. const teacherDOS = response.data.teacher
  159. switchUser({
  160. teacher: {
  161. teacherId: getIntValue(teacherDOS, 'teacherId'),
  162. userId: getIntValue(teacherDOS, 'userId'),
  163. openid: getStrValue(teacherDOS, 'openid'),
  164. userPhone: getStrValue(teacherDOS, 'userPhone'),
  165. schoolId: getStrValue(teacherDOS, 'schoolId'),
  166. schoolName: getStrValue(teacherDOS, 'schoolName'),
  167. teacherNumber: getStrValue(teacherDOS, 'teacherNumber'),
  168. teacherName: getStrValue(teacherDOS, 'teacherName'),
  169. teacherPhoto: getStrValue(teacherDOS, 'teacherPhoto'),
  170. teacherAddress: getStrValue(teacherDOS, 'teacherAddress'),
  171. teacherEntry: getStrValue(teacherDOS, 'teacherEntry'),
  172. teacherBirthday: getStrValue(teacherDOS, 'teacherBirthday'),
  173. teacherSex: getStrValue(teacherDOS, 'teacherSex'),//1:男,2:女
  174. albums: getStrValue(teacherDOS, 'picAlbum'),
  175. videos: getStrValue(teacherDOS, 'videoAlbum')
  176. }
  177. })()
  178. } else {
  179. switchUser({
  180. teacher: ''
  181. })()
  182. }
  183. if (!isObjEmpty(response.data.parents)) {
  184. const parentsDOS = response.data.parents
  185. switchUser({
  186. parent: {
  187. parentId: getIntValue(parentsDOS, 'parentId'),
  188. userId: getIntValue(parentsDOS, 'userId'),
  189. userPhone: getStrValue(parentsDOS, 'userPhone'),
  190. openid: getStrValue(parentsDOS, 'openid'),
  191. schoolId: getIntValue(parentsDOS, 'schoolId'),
  192. schoolName: getStrValue(parentsDOS, 'schoolName'),
  193. parentsName: getStrValue(parentsDOS, 'parentsName'),
  194. parentsBirthday: getStrValue(parentsDOS, 'parentsBirthday'),
  195. parentsSex: getIntValue(parentsDOS, 'parentsSex'),
  196. parentsPhoto: getStrValue(parentsDOS, 'parentsPhoto'),
  197. parentsAddress: getStrValue(parentsDOS, 'parentsAddress'),
  198. students: getStrValue(parentsDOS, 'studentDOs'),
  199. student: isObjEmpty(userInfo.parent.student) ? getStrValue(parentsDOS, 'studentDOs')[0] : userInfo.parent.student,
  200. }
  201. })()
  202. } else {
  203. switchUser({
  204. parent: ''
  205. })()
  206. }
  207. let userRole = userInfo.userRole
  208. if (!isObjEmpty(response.data.teacher)) {
  209. if (isObjEmpty(response.data.parents)) {
  210. userRole = 2
  211. }
  212. } else if (!isObjEmpty(response.data.parents)) {
  213. userRole = 1
  214. }
  215. //切换当前user信息
  216. switchCurrentUser(userRole)()
  217. if (!isObjEmpty(userInfo.parent, userInfo.parent.students)) {
  218. userInfo.parent.students.forEach((item, index) => {
  219. if (userInfo.parent.student.stuId == item.stuId) {
  220. studentIndex = index
  221. }
  222. })
  223. this.setState({studentIndex})
  224. }
  225. this.mySwiper.init()
  226. }
  227. }).catch((error) => {
  228. Toast.hide();
  229. if (typeof error === 'string') {
  230. Toast.fail(error, 2)
  231. } else {
  232. Toast.fail('数据加载失败', 2)
  233. }
  234. })
  235. }
  236. onIdentitySwitch = () => {
  237. operation([
  238. {
  239. text: '家长', onPress: () => {
  240. if (this.props.userInfo.userRole === 2) {
  241. Toast.loading('身份切换中...', 0)
  242. setTimeout(() => {
  243. Toast.success('切换成功!', 1)
  244. switchCurrentUser(1)()
  245. }, 500)
  246. }
  247. }
  248. },
  249. {
  250. text: '教师', onPress: () => {
  251. if (this.props.userInfo.userRole === 1) {
  252. Toast.loading('身份切换中...', 0)
  253. setTimeout(() => {
  254. Toast.success('切换成功!', 1)
  255. switchCurrentUser(2)()
  256. }, 500)
  257. }
  258. }
  259. },
  260. ])
  261. }
  262. userInfoLayout = () => {
  263. const {userInfo} = this.props
  264. let schoolName = '', userAvatar = '', userName = '', albums = ''
  265. if (userInfo.userRole === 1) {
  266. if (userInfo.parent) {
  267. schoolName = userInfo.parent.schoolName
  268. userAvatar = userInfo.parent.parentsPhoto
  269. userName = userInfo.parent.parentsName
  270. }
  271. } else if (userInfo.userRole === 2) {
  272. if (userInfo.teacher) {
  273. schoolName = userInfo.teacher.schoolName
  274. userAvatar = userInfo.teacher.teacherPhoto
  275. userName = userInfo.teacher.teacherName
  276. }
  277. }
  278. return (
  279. <div>
  280. <div className='home-top-school-text'>{schoolName}</div>
  281. <div className='home-top-msg-root'>
  282. {isObjEmpty(userAvatar) ?
  283. <Avatar size={50} icon='user'/> :
  284. <img
  285. src={userAvatar}
  286. width={50} height={50} className="img-circle"
  287. style={{border: '3px solid #ffffff'}}/>
  288. }
  289. <span
  290. className='home-top-msg-text'>尊敬的{userName + (userInfo.userRole === 1 ? '家长' : '老师')}</span>
  291. {(userInfo.teacher && userInfo.parent) ?
  292. <img style={{marginLeft: "5px"}} src={icon_home_change} width={16}
  293. height={16} onClick={this.onIdentitySwitch}/> : ''}
  294. </div>
  295. </div>
  296. )
  297. }
  298. getBottomLayout = () => {
  299. return (
  300. <div className='common-flex-row-x-y-center' style={{padding: 28}}>
  301. <div style={{background: '#cccccc', width: '60px', height: '1px'}}></div>
  302. <span style={{color: '#999999', fontSize: '12px', padding: '6px'}}>已经到底了</span>
  303. <div style={{background: '#cccccc', width: '60px', height: '1px'}}></div>
  304. </div>
  305. )
  306. }
  307. getAlbumLayout = () => {
  308. const {userInfo} = this.props
  309. let albums = ''
  310. if (userInfo.userRole === 1) {
  311. if (!isObjEmpty(userInfo.parent, userInfo.parent.student)) {
  312. albums = userInfo.parent.student.picAlbum
  313. }
  314. } else if (userInfo.userRole === 2) {
  315. if (userInfo.teacher) {
  316. albums = userInfo.teacher.albums
  317. }
  318. }
  319. if (isObjEmpty(albums)) {
  320. return <div></div>
  321. } else {
  322. const albumItems = []
  323. albums.forEach((item, index) => {
  324. albumItems.push(<div className="swiper-slide"
  325. style={{backgroundImage: 'url(' + _baseURL + item.picUrl + ')'}}>
  326. {/*<img className='home-albums-img' src={_baseURL + item.picUrl}/>*/}
  327. </div>)
  328. })
  329. return <div>
  330. <div className='gray-line'></div>
  331. <MenuGroup groupIcon={require('imgs/ic_group_album.png')} groupText='班级相册'/>
  332. <div className="home-swiper-container">
  333. <div className="swiper-wrapper">
  334. {albumItems}
  335. </div>
  336. </div>
  337. </div>
  338. }
  339. }
  340. getVideoLayout = () => {
  341. const {videos} = this.state
  342. if (isObjEmpty(videos)) {
  343. return <div></div>
  344. } else {
  345. return <div>
  346. <div className='gray-line'></div>
  347. <MenuGroup groupIcon={require('imgs/ic_group_moment.png')} groupText='精彩瞬间'/>
  348. </div>
  349. }
  350. }
  351. getTopMenus = () => {
  352. const topMenus = []
  353. CONFIG_HOME_TOP_MENU.forEach((topMenu, index) => {
  354. let menuPage = topMenu.page
  355. if (menuPage === '/userInfoPage') {
  356. if (this.props.userInfo.userRole == 1) {
  357. menuPage = '/userInfoPage/2'
  358. } else {
  359. menuPage = '/userInfoPage/1'
  360. }
  361. }
  362. topMenus.push(<TopMenuItem icon={topMenu.icon} text={topMenu.text} page={menuPage}
  363. onMenuClick={this.onTopFunc}/>)
  364. })
  365. return topMenus
  366. }
  367. onStuSwitch = (stuIndex) => {
  368. if (this.state.studentIndex !== stuIndex) {
  369. Toast.loading('学生切换中...', 0)
  370. setTimeout(() => {
  371. Toast.success('切换成功!', 1)
  372. this.setState({
  373. studentIndex: stuIndex
  374. })
  375. switchUser({
  376. stuName: this.props.userInfo.students[stuIndex].stuName,
  377. stuId: this.props.userInfo.students[stuIndex].stuId,
  378. student: this.props.userInfo.students[stuIndex]
  379. })()
  380. }, 500)
  381. }
  382. }
  383. getTeacherMenu = () => {
  384. let teacherMenu = []
  385. if (!isObjEmpty(CONFIG_TEACHER_MENU)) {
  386. CONFIG_TEACHER_MENU.forEach((groupItem, groupIndex) => {
  387. if (groupIndex !== 0) {
  388. teacherMenu.push(<div className='gray-line'></div>)
  389. }
  390. teacherMenu.push(<MenuGroup groupIcon={groupItem.groupIcon} groupText={groupItem.groupText}/>)
  391. const funcList = groupItem.funcList
  392. if (!isObjEmpty(funcList)) {
  393. funcList.forEach((funcItem, funcIndex) => {
  394. teacherMenu.push(<FuncItem funcObj={funcItem} onFuncClick={this.onFuncClick}/>)
  395. })
  396. }
  397. })
  398. }
  399. return teacherMenu
  400. }
  401. getParentMenu = () => {
  402. let parentMenu = []
  403. if (!isObjEmpty(CONFIG_PARENT_MENU)) {
  404. CONFIG_PARENT_MENU.forEach((groupItem, groupIndex) => {
  405. if (groupIndex !== 0) {
  406. parentMenu.push(<div className='gray-line'></div>)
  407. }
  408. parentMenu.push(<MenuGroup groupIcon={groupItem.groupIcon} groupText={groupItem.groupText}/>)
  409. const funcList = groupItem.funcList
  410. if (!isObjEmpty(funcList)) {
  411. funcList.forEach((funcItem, funcIndex) => {
  412. parentMenu.push(<FuncItem funcObj={funcItem} onFuncClick={this.onFuncClick}/>)
  413. })
  414. }
  415. })
  416. }
  417. return parentMenu
  418. }
  419. onTopFunc = (page) => {
  420. this.props.history.push(page)
  421. }
  422. onFuncClick = (page) => {
  423. if (!isObjEmpty(page)) {
  424. this.props.history.push(page)
  425. }
  426. }
  427. }
  428. class MenuGroup extends Component {
  429. render() {
  430. return (
  431. <div className='home-group-item-layout'>
  432. <img className='home-group-item-icon' src={this.props.groupIcon}/>
  433. <span className='home-group-item-text'>{this.props.groupText}</span>
  434. </div>
  435. )
  436. }
  437. }
  438. class FuncItem extends Component {
  439. render() {
  440. const {funcObj} = this.props
  441. return (
  442. isObjEmpty(funcObj) ?
  443. <div></div> :
  444. <div className='home-func-item-root' onClick={this.onFuncClick}>
  445. <div className='home-func-item-layout'>
  446. <img className='home-func-item-icon' src={funcObj.funcIcon}/>
  447. <span className='home-func-item-text'>{funcObj.funcText}</span>
  448. </div>
  449. </div>
  450. )
  451. }
  452. onFuncClick = () => {
  453. if (this.props.onFuncClick) {
  454. this.props.onFuncClick(this.props.funcObj.funcPage)
  455. }
  456. }
  457. }
  458. class StuItem extends Component {
  459. render() {
  460. return (
  461. <div onClick={this.onStuSwitch} className='home-top-stu-layout'>
  462. {this.props.stuObj.stuPhoto ?
  463. <img className={this.props.isSelect ? 'border-radius-50-blue' : 'border-radius-50'}
  464. src={_baseURL + this.props.stuObj.stuPhoto}
  465. width={25} height={25}/> :
  466. <Avatar className={this.props.isSelect ? 'border-radius-50-blue' : 'border-radius-50'}
  467. size={23} icon='user'/>}
  468. <span
  469. className={this.props.isSelect ? "margin_left_5 color_blue text_bold"
  470. : 'margin_left_5'}>{this.props.stuObj.stuName}</span>
  471. </div>
  472. );
  473. }
  474. onStuSwitch = () => {
  475. if (this.props.onStuSwitch) {
  476. this.props.onStuSwitch(this.props.stuIndex)
  477. }
  478. }
  479. }
  480. class TopMenuItem extends Component {
  481. render() {
  482. return (
  483. <div className='home-top-menu-item' onClick={this.onMenuClick}>
  484. <img className='home-top-menu-icon' src={this.props.icon}/>
  485. <span className='home-top-menu-text'>{this.props.text}</span>
  486. </div>
  487. );
  488. }
  489. onMenuClick = () => {
  490. if (this.props.onMenuClick) {
  491. this.props.onMenuClick(this.props.page)
  492. }
  493. }
  494. }
  495. let mapStateToProps = (state) => ({
  496. userInfo: {...state.redUserInfo}
  497. })
  498. let mapDispatchToProps = (dispatch) => ({})
  499. export default connect(mapStateToProps, mapDispatchToProps)(HomePage)