request.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import axios, { AxiosRequestConfig, AxiosResponse, AxiosPromise } from 'axios'
  21. import { DEFAULT_JWT_TOKEN_EXPIRED } from 'app/globalConstants'
  22. let tokenExpired = DEFAULT_JWT_TOKEN_EXPIRED
  23. axios.defaults.validateStatus = function(status) {
  24. return status < 400
  25. }
  26. function parseJSON(response: AxiosResponse) {
  27. return response.data
  28. }
  29. function refreshToken(response: AxiosResponse) {
  30. const token = response.data.header && response.data.header.token
  31. if (response.data === 'token认证错误') {
  32. removeToken()
  33. }
  34. if (token) {
  35. setToken(token)
  36. }
  37. return response
  38. }
  39. export function request(url: string, options?: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>>
  40. export function request(config: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>>
  41. export default function request(url: string | AxiosRequestConfig, options?: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>> {
  42. const axiosPromise =
  43. typeof url === 'string' ? axios(url, options) : axios(url)
  44. return axiosPromise
  45. .then(refreshToken)
  46. .then(parseJSON)
  47. }
  48. export function setToken(token: string) {
  49. localStorage.setItem('TOKEN', token)
  50. // localStorage.setItem('TOKEN_EXPIRE', `${new Date().getTime() + tokenExpired}`)
  51. axios.defaults.headers.common['Authorization'] = `${token}`
  52. }
  53. function syncToken(e: StorageEvent) {
  54. const { key, newValue } = e
  55. if (key !== 'TOKEN') {
  56. return
  57. }
  58. if (!newValue) {
  59. delete axios.defaults.headers.common['Authorization']
  60. } else {
  61. axios.defaults.headers.common['Authorization'] = `${newValue}`
  62. }
  63. }
  64. export function removeToken() {
  65. localStorage.removeItem('TOKEN')
  66. // localStorage.removeItem('TOKEN_EXPIRE')
  67. delete axios.defaults.headers.common['Authorization']
  68. }
  69. export function getToken() {
  70. return axios.defaults.headers.common['Authorization']
  71. }
  72. export function setTokenExpired(expired) {
  73. tokenExpired = Number(expired)
  74. }
  75. window.addEventListener('storage', syncToken)
  76. interface IDavinciResponseHeader {
  77. code: number
  78. msg: string
  79. token: string
  80. }
  81. export interface IDavinciResponse<T> {
  82. header: IDavinciResponseHeader,
  83. payload: T
  84. }