request.uts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { env, TIMEOUT } from '@/common/env.uts'
  2. import { logout } from '@/store/user.uts'
  3. export type ApiResponse<T> = {
  4. code : number
  5. message : string
  6. data : T | null
  7. }
  8. export type RequestOptions = {
  9. url : string
  10. method ?: string
  11. data ?: UTSJSONObject | null
  12. header ?: UTSJSONObject | null
  13. showLoading ?: boolean
  14. }
  15. function getToken() : string {
  16. const token = uni.getStorageSync('token')
  17. if (token == null) return ''
  18. return token as string
  19. }
  20. function toastError(msg : string) : void {
  21. uni.showToast({ title: msg, icon: 'none' })
  22. }
  23. function handleUnauthorized() : void {
  24. logout()
  25. toastError('登录已失效,请重新登录')
  26. uni.reLaunch({ url: '/pages/mine/mine' })
  27. }
  28. export function request<T>(options : RequestOptions) : Promise<T> {
  29. return new Promise<T>((resolve, reject) => {
  30. const header : UTSJSONObject = options.header ?? {}
  31. header['Content-Type'] = 'application/json'
  32. const token = getToken()
  33. if (token.length > 0) {
  34. header['Authorization'] = 'Bearer ' + token
  35. }
  36. if (options.showLoading == true) {
  37. uni.showLoading({ title: '加载中...' })
  38. }
  39. uni.request({
  40. url: env.baseUrl + options.url,
  41. method: options.method ?? 'GET',
  42. data: options.data,
  43. header: header,
  44. timeout: TIMEOUT,
  45. success: (res) => {
  46. if (options.showLoading == true) {
  47. uni.hideLoading()
  48. }
  49. const statusCode = res.statusCode
  50. if (statusCode == 401 || statusCode == 403) {
  51. handleUnauthorized()
  52. reject(new Error('登录已失效'))
  53. return
  54. }
  55. if (statusCode >= 200 && statusCode < 300) {
  56. const body = res.data
  57. if (body == null || !(body instanceof UTSJSONObject)) {
  58. reject(new Error('响应格式错误'))
  59. return
  60. }
  61. const json = body as UTSJSONObject
  62. const code = json.getNumber('code')
  63. if (code == null) {
  64. resolve(json as T)
  65. return
  66. }
  67. if (code == 200) {
  68. resolve(json.get('data') as T)
  69. } else {
  70. const msg = json.getString('message') ?? '请求失败'
  71. toastError(msg)
  72. reject(new Error(msg))
  73. }
  74. } else {
  75. const msg = '网络错误 ' + statusCode
  76. toastError(msg)
  77. reject(new Error(msg))
  78. }
  79. },
  80. fail: (err) => {
  81. if (options.showLoading == true) {
  82. uni.hideLoading()
  83. }
  84. const msg = err.errMsg != null && err.errMsg.length > 0 ? err.errMsg : '请求失败'
  85. toastError(msg)
  86. reject(new Error(msg))
  87. }
  88. })
  89. })
  90. }
  91. export function get<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
  92. return request<T>({ url: url, method: 'GET', data: data })
  93. }
  94. export function post<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
  95. return request<T>({ url: url, method: 'POST', data: data })
  96. }
  97. export function put<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
  98. return request<T>({ url: url, method: 'PUT', data: data })
  99. }
  100. export function del<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
  101. return request<T>({ url: url, method: 'DELETE', data: data })
  102. }