diff --git a/src/pages/index/DutyInfo.tsx b/src/pages/index/DutyInfo.tsx index 4e3daac..1b41a54 100644 --- a/src/pages/index/DutyInfo.tsx +++ b/src/pages/index/DutyInfo.tsx @@ -15,10 +15,10 @@ export class DutyData { } isInDuty: boolean; - inDutyCnt: number; - currentDuty: 'off' | '1' | '2' | '3'; - offDutyReason: string; // from backend - dutyRecoverTime: string; // from backend + inDutyCnt?: number; + currentDuty?: 'off' | '1' | '2' | '3'; + offDutyReason?: string; + dutyRecoverTime?: string; } class Card extends Component { @@ -73,8 +73,8 @@ export class DutyInfo extends Component { @@ -89,8 +89,8 @@ export class DutyInfo extends Component { diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index 04f38fb..9d8951f 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -5,7 +5,7 @@ import { AtCard, AtAccordion } from 'taro-ui'; import type CustomTabBar from '@/custom-tab-bar'; import PageFooter from '@/components/PageFooter/PageFooter'; import pt from '@/plain-text'; -import { getUrl } from '@/service'; +import { getDutyInfo } from '@/service/MainPage'; import './index.scss'; import TitleCard from './TitleCard'; import { DutyInfo, DutyData } from './DutyInfo'; @@ -56,7 +56,15 @@ class ExpandItem extends Component { } } -export default class Index extends Component { +interface MainPageState { + dutyData: DutyData; + dutyInfoCard: CardContent; + stepInfoCard: CardContent; + tipsInfoCard: CardContent; + //rs: RequestState; +} + +export default class MainPage extends Component<{}, MainPageState> { state = { dutyData: new DutyData(), dutyInfoCard: { @@ -77,46 +85,11 @@ export default class Index extends Component { extra: '额外信息', content: () => , }, + // rs: new RequestState(), }; componentDidMount(): void { - Taro.request({ - url: getUrl('/dutyinfo'), - method: 'GET', - data: { - token: 'token_test', - }, - }) - .then(res => { - const data = res.data.data; - if (data.isInDuty) { - this.setState({ - dutyData: { - isInDuty: data.isInDuty, - inDutyCnt: data.inDutyCnt, - currentDuty: data.currentDuty, - }, - }); - } else { - this.setState({ - dutyData: { - isInDuty: data.isInDuty, - offDutyReason: data.offDutyReason, - dutyRecoverTime: data.dutyRecoverTime, - }, - }); - } - }) - .catch(err => { - console.log(err); - this.setState({ - dutyData: { - isInDuty: false, - offDutyReason: '获取失败!Network Error!', - dutyRecoverTime: '获取失败!Network Error!', - }, - }); - }); + getDutyInfo(this); } // 以下是TabBar相关 diff --git a/src/service/MainPage.ts b/src/service/MainPage.ts new file mode 100644 index 0000000..5cf7125 --- /dev/null +++ b/src/service/MainPage.ts @@ -0,0 +1,43 @@ +import MainPage from '@/pages/index'; +import Taro from '@tarojs/taro'; +import { getUrl } from '.'; + +export function getDutyInfo(that: MainPage) { + Taro.request({ + url: getUrl('/dutyinfo'), + method: 'GET', + data: { + token: 'token_test', + }, + }) + .then(res => { + const data = res.data.data; + if (data.isInDuty) { + that.setState({ + dutyData: { + isInDuty: data.isInDuty, + inDutyCnt: data.inDutyCnt, + currentDuty: data.currentDuty, + }, + }); + } else { + that.setState({ + dutyData: { + isInDuty: data.isInDuty, + offDutyReason: data.offDutyReason, + dutyRecoverTime: data.dutyRecoverTime, + }, + }); + } + }) + .catch(err => { + console.log(err); + that.setState({ + dutyData: { + isInDuty: false, + offDutyReason: '获取失败!Network Error!', + dutyRecoverTime: '获取失败!Network Error!', + }, + }); + }); +}