123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /*
- * <<
- * Davinci
- * ==
- * Copyright (C) 2016 - 2017 EDP
- * ==
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * >>
- */
- import axios, { AxiosRequestConfig, AxiosResponse, AxiosPromise } from 'axios'
- import { DEFAULT_JWT_TOKEN_EXPIRED } from 'app/globalConstants'
- let tokenExpired = DEFAULT_JWT_TOKEN_EXPIRED
- axios.defaults.validateStatus = function(status) {
- return status < 400
- }
- function parseJSON(response: AxiosResponse) {
- return response.data
- }
- function refreshToken(response: AxiosResponse) {
- const token = response.data.header && response.data.header.token
- if (response.data === 'token认证错误') {
- removeToken()
- }
- if (token) {
- setToken(token)
- }
- return response
- }
- export function request(url: string, options?: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>>
- export function request(config: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>>
- export default function request(url: string | AxiosRequestConfig, options?: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>> {
- const axiosPromise =
- typeof url === 'string' ? axios(url, options) : axios(url)
- return axiosPromise
- .then(refreshToken)
- .then(parseJSON)
- }
- export function setToken(token: string) {
- localStorage.setItem('TOKEN', token)
- // localStorage.setItem('TOKEN_EXPIRE', `${new Date().getTime() + tokenExpired}`)
- axios.defaults.headers.common['Authorization'] = `${token}`
- }
- function syncToken(e: StorageEvent) {
- const { key, newValue } = e
- if (key !== 'TOKEN') {
- return
- }
- if (!newValue) {
- delete axios.defaults.headers.common['Authorization']
- } else {
- axios.defaults.headers.common['Authorization'] = `${newValue}`
- }
- }
- export function removeToken() {
- localStorage.removeItem('TOKEN')
- // localStorage.removeItem('TOKEN_EXPIRE')
- delete axios.defaults.headers.common['Authorization']
- }
- export function getToken() {
- return axios.defaults.headers.common['Authorization']
- }
- export function setTokenExpired(expired) {
- tokenExpired = Number(expired)
- }
- window.addEventListener('storage', syncToken)
- interface IDavinciResponseHeader {
- code: number
- msg: string
- token: string
- }
- export interface IDavinciResponse<T> {
- header: IDavinciResponseHeader,
- payload: T
- }
|