Skip to content

Commit

Permalink
fix(issues): 修复之前 issuse 的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Yangfan2016 committed May 10, 2019
1 parent 0394623 commit 09a3919
Show file tree
Hide file tree
Showing 8 changed files with 17,638 additions and 34 deletions.
4 changes: 2 additions & 2 deletions notes/issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@

## Todo

- 需要在组件卸载时,取消请求,和其他异步任务,订阅事件
- 监听路由变化,将滚动条重置到页面顶部
- 需要在组件卸载时,~~取消请求~~,和其他异步任务,订阅事件
- ~~监听路由变化,将滚动条重置到页面顶部~~
- 404 错误页的编写
17,524 changes: 17,524 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ import RouterView from './router/RouterView';

moment.locale('zh-cn');

function routerBeforeEnterHook(path: string) {
// 取消所有请求
window.cancalXHRList.forEach((source: any) => {
source.cancel("cancel xhr");
});
// 滚动条复位,回到原点
window.scrollTo({
top: 0,
behavior: "smooth"
});
}

class App extends Component {
render() {
return (
<LocaleProvider locale={zhCN}>
<div className="App">
<RouterView></RouterView>
<RouterView beforeEnter={routerBeforeEnterHook} />
</div>
</LocaleProvider>
);
Expand Down
50 changes: 35 additions & 15 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,73 @@
import axios from "axios";


declare global {
interface Window {
cancalXHRList: any
}
}

const BASE_URL = "/api/movie";
const API_KEY = "0b2bdeda43b5688921839c8ecb20399b";

let instance = axios.create({
baseURL: BASE_URL,
timeout: 3e4,
params: {
"apikey": API_KEY,
}
});

window.cancalXHRList = [];

function http() {
const CancelToken = axios.CancelToken;
const source = CancelToken.source();

window.cancalXHRList.push(source);

let instance: any = axios.create({
baseURL: BASE_URL,
timeout: 3e4,
params: {
"apikey": API_KEY,
},
cancelToken: source.token,
});

return instance;
}

// 热映
export function getHotShowing(params?: any) {
return instance.get("/in_theaters", {
params
return http().get("/in_theaters", {
params,
});
}

// top250
export function getTop250(params?: any) {
return instance.get("/top250", {
return http().get("/top250", {
params,
});
}

// 新片
export function getNew() {
return instance.get("/new_movies");
return http().get("/new_movies");
}

// 电影详情
export function getDetail(id: string) {
return instance.get(`/subject/${id}`);
return http().get(`/subject/${id}`);
}

// 北美票房榜
export function getGoodbox() {
return instance.get("/us_box");
return http().get("/us_box");
}

// 搜索条目
export function getContentBySearch(str: string, params?: any) {
return instance.get(`/search?q=${str}`, {
return http().get(`/search?q=${str}`, {
params
});
}

// 口碑榜
export function getWeeklyMovie() {
return instance.get("/weekly");
return http().get("/weekly");
}
1 change: 1 addition & 0 deletions src/pages/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { reSerialize } from '../utils';
import { List, Pagination, Affix, Tag } from 'antd';

import '../css/Search.css';
import { SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG } from 'constants';

class Search extends React.Component {
constructor(props: any) {
Expand Down
4 changes: 2 additions & 2 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ declare module '*.png' {
}

declare module '*.webp' {
const src: string;
export default src;
const src: string;
export default src;
}

declare module '*.svg' {
Expand Down
45 changes: 31 additions & 14 deletions src/router/RouterView.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import React from 'react';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';

import Home from '../pages/Home';
import Detail from '../pages/Detail';
import Box from '../pages/Box';
import Search from '../pages/Search';
import routerMap from "./config";

import NotFound from '../errors/NotFound';

export default function () {

function CustomRoute(props: any) {
let path = props.location.pathname;

props.beforeEnter && props.beforeEnter(path);

// '/'-> '/home
if (path === '/') return <Redirect to='/home' />

// if can match
let matchRoute: any = routerMap.find(item => {
let url = item.path;
// /detail/:id -> \\/detail\\/[^/+]
url = url.replace(/(\:.+)/g, "[^/]+").replace(/\//g, "\\/");

return new RegExp(`${url}(\\/|\\/)?$`, 'gi').test(path);
});

if (matchRoute) {
return <Route exact={!matchRoute.hasChild} path={matchRoute.path} component={matchRoute.component} />
}
return <Redirect to='/404' />
}

export default function (props: any) {
return (
<BrowserRouter>
<Switch>
<Redirect exact from="/" to="/home" />
<Route exact path="/home" component={Home} />
<Route exact path="/detail/:id" component={Detail} />
<Route exact path="/box" component={Box} />
<Route exact path="/search" component={Search} />
<Route component={NotFound} />
</Switch>
<>
<Switch>
<CustomRoute {...props} />
</Switch>
</>
</BrowserRouter>
);
}
Expand Down
30 changes: 30 additions & 0 deletions src/router/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import Home from '../pages/Home';
import Detail from '../pages/Detail';
import Box from '../pages/Box';
import Search from '../pages/Search';

import NotFound from '../errors/NotFound';

export default [
{
path: '/home',
component: Home,
},
{
path: '/detail/:id',
component: Detail,
},
{
path: '/box',
component: Box,
},
{
path: '/search',
component: Search,
},
{
path: '/404',
component: NotFound,
}
];

0 comments on commit 09a3919

Please sign in to comment.