request.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { geteSessionStorage } from '@/utils/auth'
  5. import qs from 'qs'
  6. import router from '@/router'
  7. const JSONbigString = require('json-bigint')({
  8. storeAsString: true
  9. // objectBaseClass: Object
  10. })
  11. // console.log(JSONbigString)
  12. const transformResponse = [
  13. (res) => {
  14. if (typeof res == 'string') {
  15. try {
  16. return JSON.parse(JSON.stringify(JSONbigString.parse(res)))
  17. } catch (error) {
  18. console.log(error)
  19. return JSON.parse(res)
  20. }
  21. } else {
  22. return res
  23. }
  24. }
  25. ]
  26. declare module 'axios' {
  27. export interface AxiosRequestConfig {
  28. /**
  29. * 是否上传静态文件
  30. */
  31. file?: boolean
  32. }
  33. export interface AxiosResponse<T = any> {
  34. result: T
  35. code: number
  36. message: string
  37. }
  38. export interface AxiosInstance {
  39. <T = any>(config: AxiosRequestConfig): Promise<T>
  40. request<T = any>(config: AxiosRequestConfig): Promise<T>
  41. get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>
  42. delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>
  43. head<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>
  44. post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>
  45. put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>
  46. patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>
  47. }
  48. }
  49. // 创建axios实例
  50. // export const IP = 'https://yeepms.yeec.com.cn/yclw-api' //
  51. // export const IP = 'https://tgdcepc.ctg.com.cn:8443/yclw-api/' //
  52. // export const IP = 'http://192.168.2.238:3081' //
  53. export const IP = 'http://221.182.8.141:3081' //
  54. // 暴露IP地址
  55. export const baseAddress = IP
  56. const service = axios.create({
  57. baseURL: IP,
  58. withCredentials: true, // 跨域请求时发送cookiesokies
  59. timeout: 30000, // 请求超时
  60. transformResponse
  61. })
  62. let requestTimes = 0
  63. // 请求拦截器
  64. service.interceptors.request.use(
  65. (config) => {
  66. if (config.method === 'post' && config.url === '/auth/oauth/tokenWithShort') {
  67. // 请求为post 使用qs转换数据
  68. // if (config.method === 'post') { // 请求为post 使用qs转换数据
  69. config.data = qs.stringify(config.data)
  70. config.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  71. } else {
  72. config.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
  73. }
  74. if (config.file) {
  75. config.headers.post['Content-Type'] = 'multipart/form-data'
  76. }
  77. // 在发出请求前做点什么
  78. if (store.getters.token) {
  79. config.headers['Authorization'] = 'bearer ' + geteSessionStorage('token')
  80. }
  81. return config
  82. },
  83. (error) => {
  84. // 处理请求错误
  85. console.log(error)
  86. return Promise.reject(error)
  87. }
  88. )
  89. // 响应拦截器
  90. service.interceptors.response.use(
  91. /**
  92. * 如果您想要获取诸如头或状态之类的http信息
  93. * return response => response
  94. */
  95. /**
  96. * 通过自定义代码确定请求状态
  97. */
  98. (response) => {
  99. const res = response.data
  100. // 如果直接返回字符串(请求的图片 直接返回base64
  101. if (typeof res === 'string') return res
  102. // 下载文件 后端返回二进制流 直接返回data
  103. if (res instanceof ArrayBuffer) return res
  104. // 下载文件 后端返回二进制流 直接返回data
  105. if (res instanceof Blob) return res
  106. // 如果自定义代码是-1,则判断为错误。
  107. if (res.code === -1) {
  108. if (!res.message || res.message.length < 100) {
  109. console.log(res.message || 'Error')
  110. // Message({
  111. // message: res.message || 'Error',
  112. // type: 'error',
  113. // duration: 2 * 1000
  114. // })
  115. } else {
  116. console.log('系统错误')
  117. // Message({
  118. // message: '系统错误',
  119. // type: 'error',
  120. // duration: 2 * 1000
  121. // })
  122. }
  123. } else if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  124. // to re-login
  125. MessageBox.confirm('您已注销,您可以取消以停留在此页,或重新登录', '确认注销', {
  126. confirmButtonText: '确定',
  127. cancelButtonText: '取消',
  128. type: 'warning'
  129. }).then(() => {
  130. store.dispatch('user/resetToken').then(() => {
  131. location.reload()
  132. })
  133. })
  134. } else {
  135. requestTimes = 0
  136. return res
  137. }
  138. return Promise.reject(new Error(res.message || 'Error')).catch((err) => {
  139. console.log(err)
  140. })
  141. },
  142. (error) => {
  143. function clearSession() {
  144. return new Promise((resolve) => {
  145. sessionStorage.clear()
  146. resolve(1)
  147. })
  148. }
  149. if (error.response.data.code === 401) {
  150. if (requestTimes === 0) {
  151. requestTimes = 1
  152. MessageBox.confirm('登录失效,请重新登录!', '提示', {
  153. confirmButtonText: '确定',
  154. showCancelButton: false,
  155. showClose: false,
  156. closeOnPressEscape: false,
  157. closeOnClickModal: false
  158. }).then(async () => {
  159. const result = await clearSession()
  160. if (result === 1) {
  161. requestTimes = 0
  162. router.replace('/login')
  163. }
  164. })
  165. }
  166. } else {
  167. console.log(error.message)
  168. // Message({
  169. // message: error.message,
  170. // type: 'error',
  171. // duration: 5 * 1000
  172. // })
  173. }
  174. }
  175. )
  176. export default service