-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
178 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const CracoLessPlugin = require('craco-less'); | ||
|
||
module.exports = { | ||
plugins: [ | ||
{ | ||
plugin: CracoLessPlugin, | ||
options: { | ||
lessLoaderOptions: { | ||
lessOptions: { | ||
modifyVars: {'@primary-color': 'rgb(0,82,204)', '@font-size-base': '16px'}, | ||
javascriptEnabled: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import qs from "qs" | ||
import * as auth from 'auth-provider' | ||
import {useAuth} from "../context/auth-context"; | ||
|
||
const apiUrl = process.env.REACT_APP_API_URL | ||
|
||
interface Config extends RequestInit { | ||
token?: string; | ||
data?: object; | ||
} | ||
|
||
export const http = async (endpoint: string, {data, token, headers, ...customConfig}: Config = {}) => { | ||
const config = { | ||
method: 'GET', //默认值为GET 如果customConfig有method传进来就会覆盖掉这里的值 | ||
headers: { | ||
Authorization: token ? `Bearer ${token}` : '', | ||
'Content-Type': data ? 'application/json' : '' | ||
}, | ||
...customConfig | ||
} | ||
|
||
if (config.method.toUpperCase() === 'GET') { | ||
endpoint += `?${qs.stringify(data)}` | ||
} else { | ||
config.body = JSON.stringify(data || {}) | ||
} | ||
|
||
return window.fetch(`${apiUrl}/${endpoint}`, config).then(async res => { | ||
//token过期的时候 | ||
if (res.status === 401) { | ||
await auth.logout() //退出重新登录 | ||
window.location.reload() //页面刷新 | ||
return Promise.reject({message: '请重新登录'}) | ||
} | ||
const data = await res.json() | ||
if (res.ok) { | ||
return data | ||
} else { | ||
return Promise.reject(data) | ||
} | ||
}) | ||
} | ||
|
||
//只有Hook里面可以使用其他的Hook | ||
export const useHttp = () => { | ||
const {user} = useAuth() | ||
//utility type的用法:用泛型给它传入一个其它类型,然后utility type对这个类型进行某种操作 | ||
return (...[endpoint, config]: Parameters<typeof http>) => http(endpoint, {...config, token: user?.token}) | ||
} |
Oops, something went wrong.