diff --git a/README.md b/README.md index 872e00a..19cd3f6 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,5 @@ - [ ] 适配 iOS 登录失败 toast - [ ] 在启动时检测是否已登录 - [ ] Splash 检测网络出错后在 Splash UI 中显示错误并不跳转 +- [ ] Forum 初始化失败后 UI 显示错误信息 diff --git a/src/component/YooForum.js b/src/component/YooForum.js index d8afbdd..40f292d 100644 --- a/src/component/YooForum.js +++ b/src/component/YooForum.js @@ -1,7 +1,101 @@ import React, {Component} from 'react'; +import {ToastAndroid} from 'react-native'; import YooForumUI from '../ui/YooForumUI'; +import {gbkFetch} from '../api/HTTP'; +import CookieManager from '@react-native-community/cookies'; + +const ShowErrorToast = () => + ToastAndroid.show('获取数据时出错', ToastAndroid.SHORT); export default class YooForum extends Component { + constructor(props) { + super(props); + this.initForum = this.initForum.bind(this); + } + + componentDidMount() { + this.initForum(); + } + + initForum() { + // get dwr session id + gbkFetch( + 'POST', + 'http://eol.ctbu.edu.cn/meol/dwr/call/plaincall/__System.generateId.dwr', + { + headers: { + Origin: 'http://eol.ctbu.edu.cn', + 'Content-Type': 'text/plain', + 'User-Agent': 'YooMooc', + Referer: + 'http://eol.ctbu.edu.cn/meol/jpk/course/layout/newpage/index.jsp?courseId=46445', + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7', + }, + body: + 'callCount=1\n' + + 'c0-scriptName=__System\n' + + 'c0-methodName=generateId\n' + + 'c0-id=0\n' + + 'batchId=0\n' + + 'instanceId=0\n' + + 'page=%2Fmeol%2Fjpk%2Fcourse%2Flayout%2Fnewpage%2Findex.jsp%3FcourseId%3D46445\n' + + 'scriptSessionId=\n' + + 'windowName=\n', + }, + ) + .then((response) => { + if (/[^"]*"\);/.test(response)) { + this.setState({ + textContent: response.match(/[^"]*"\);/)[0].split('"')[0], + }); + const exDate = new Date(); + exDate.setDate(exDate.getDate() + 1); + CookieManager.set( + 'http://eol.ctbu.edu.cn/meol/jpk/course/layout/newpage/index.jsp?courseId=46445', + { + name: 'DWRSESSIONID', + value: response.match(/[^"]*"\);/)[0].split('"')[0], + domain: 'eol.ctbu.edu.cn', + path: '/meol', + version: '1', + expires: exDate.toUTCString(), + }, + ).then(() => { + // it is needed to request some pages before getting the topic list + // maybe the server is judging which course the user is + gbkFetch( + 'GET', + 'http://eol.ctbu.edu.cn/meol/jpk/course/layout/newpage/index.jsp?courseId=46445', + { + headers: { + 'Upgrade-Insecure-Requests': '1', + 'User-Agent': 'YooMooc', + }, + }, + ) + .then(() => { + gbkFetch( + 'GET', + 'http://eol.ctbu.edu.cn/meol/jpk/course/layout/newpage/default_demonstrate.jsp', + { + 'Upgrade-Insecure-Requests': '1', + 'User-Agent': 'YooMooc', + Referer: + 'http://eol.ctbu.edu.cn/meol/jpk/course/layout/newpage/index.jsp?courseId=46445', + }, + ) + .then() + .catch(ShowErrorToast); + }) + .catch(ShowErrorToast); + }); + } else { + ShowErrorToast(); + } + }) + .catch(ShowErrorToast); + } + render() { return ; }