net.service.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import useStore from '@/pages/store'
  2. import { injectable, Service } from './service'
  3. /** api接口返回值类型 */
  4. declare type NetResult = {
  5. success: boolean,
  6. data: Any
  7. }
  8. /**
  9. * API网络请求服务
  10. */
  11. @injectable
  12. export default class NetService extends Service {
  13. protected url = import.meta.env.VITE_SERVER_URL
  14. private _store = useStore()
  15. /**
  16. * post方法请求接口
  17. * @param url 接口地址
  18. * @param params 接口参数
  19. */
  20. post(url: string, params: Any = {}, timeout = 6000): Promise<any> {
  21. return this.fetch(url, { method: 'POST', body: JSON.stringify(params) }, timeout)
  22. }
  23. /**
  24. * get请求api接口
  25. * @param url 接口地址
  26. */
  27. get(url: string, timeout = 6000): Promise<NetResult> {
  28. return this.fetch(url, { method: 'GET' }, timeout)
  29. }
  30. /**
  31. * del请求api接口
  32. * @param url 接口地址
  33. */
  34. del(url: string, timeout = 6000): Promise<NetResult> {
  35. return this.fetch(url, { method: 'DELETE' }, timeout)
  36. }
  37. /**
  38. * post方法请求接口
  39. * @param url 接口地址
  40. * @param params 接口参数
  41. */
  42. put(url: string, params: Any = {}, timeout = 6000): Promise<NetResult> {
  43. return this.fetch(url, { method: 'PUT', body: JSON.stringify(params) }, timeout)
  44. }
  45. /**
  46. * 下载文件
  47. * @param url url|blob
  48. * @param fileName
  49. */
  50. downloadFile(url: string | Blob, fileName: string) {
  51. const download = (blob: Blob, fileName: string) => {
  52. const a = document.createElement('a')
  53. document.body.appendChild(a)
  54. a.style.display = 'none'
  55. // 使用获取到的blob对象创建的url
  56. const url = window.URL.createObjectURL(blob)
  57. a.href = url
  58. // 指定下载的文件名
  59. a.download = fileName
  60. a.click()
  61. document.body.removeChild(a)
  62. // 移除blob对象的url
  63. window.URL.revokeObjectURL(url)
  64. }
  65. return new Promise<void>((resolve, reject) => {
  66. if (url instanceof Blob) {
  67. download(url, fileName)
  68. resolve()
  69. } else {
  70. fetch(url).then((res) => res.blob()).then((blob) => download(blob, fileName)).finally(() => resolve())
  71. }
  72. })
  73. }
  74. /** token 续期 */
  75. async refresh(token: string) {
  76. this.post('', { token }).then((res) => {
  77. console.log('刷新token', res)
  78. })
  79. }
  80. /**
  81. * fetch
  82. * @param url
  83. * @param opt
  84. * @param timeout 0 默认不超时
  85. * @returns
  86. */
  87. private fetch(url: string, opt: RequestInit, timeout = 0): Promise<NetResult> {
  88. const satoken = this._store.token || `${sessionStorage.getItem('token')}`
  89. return new Promise((resolve) => {
  90. const controller = new AbortController()
  91. const { signal } = controller
  92. if (timeout) {
  93. setTimeout(() => {
  94. controller.abort()
  95. }, timeout)
  96. }
  97. fetch(this.url + url, {
  98. signal,
  99. ...opt,
  100. headers: opt.headers || { satoken, 'Content-Type': 'application/json' }
  101. }).then((res) => {
  102. if (res.status === 500) return { success: false, data: '请求错误' }
  103. return res.json()
  104. }).then((res) => {
  105. const obj = { data: res.data, success: res.data instanceof Object }
  106. if (!obj.success && res.message?.includes('Token')) {
  107. throw 401
  108. }
  109. resolve(obj)
  110. }).catch((er) => {
  111. throw er
  112. })
  113. })
  114. }
  115. }