request.uts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * 网络请求封装
  3. */
  4. import { getAccessToken } from "./storage";
  5. // 请求配置类型
  6. export type RequestConfig = {
  7. url: string;
  8. method?: "GET" | "POST" | "PUT" | "DELETE";
  9. data?: UTSJSONObject | null;
  10. header?: UTSJSONObject | null;
  11. timeout?: number;
  12. };
  13. // 基础 URL
  14. // const BASE_URL = "http://192.168.110.105:8080";
  15. const BASE_URL = "http://222.243.138.146:5095";
  16. /**
  17. * 获取基础 URL
  18. */
  19. export const getBaseUrl = (): string => {
  20. return BASE_URL;
  21. };
  22. /**
  23. * 网络请求封装
  24. */
  25. export const request = (config: RequestConfig): Promise<any> => {
  26. return new Promise((resolve, reject) => {
  27. // 获取 AccessToken
  28. const accessToken = getAccessToken();
  29. // 设置请求头(先设置默认值,再合并自定义 header)
  30. const header: UTSJSONObject = {
  31. "Content-Type": "application/json",
  32. };
  33. // 添加 AccessToken
  34. if (accessToken != null) {
  35. header["Authorization"] = 'Bearer ' + accessToken;
  36. }
  37. // 合并自定义 header(会覆盖默认值)
  38. if (config.header != null) {
  39. const customHeader = config.header as UTSJSONObject;
  40. // 遍历自定义 header 的所有键值对
  41. for (const key in customHeader) {
  42. header[key] = customHeader[key];
  43. }
  44. }
  45. // 发起请求
  46. uni.request({
  47. url: BASE_URL + config.url,
  48. method: config.method ?? "GET",
  49. data: config.data,
  50. header: header,
  51. timeout: config.timeout ?? 30000,
  52. success: (res) => {
  53. // 提取属性(any 类型不能直接访问属性)
  54. console.log(res);
  55. const statusCode = res.statusCode as number;
  56. const resData = res.data;
  57. if (statusCode == 200) {
  58. // 将响应数据转换为 UTSJSONObject,然后手动提取属性
  59. const result = resData as UTSJSONObject;
  60. const success = result["success"] as boolean | null;
  61. const status = result["status"] as number | null;
  62. const code = result["code"] as number | null;
  63. const msg = result["msg"] as string | null;
  64. // 判断是否成功
  65. // success 为 true 表示成功
  66. // 或者没有 success 字段但 code 为 200 也表示成功
  67. const isSuccess = success == true || (success == null && code == 200)
  68. if (isSuccess) {
  69. console.log(config.url, result);
  70. // 返回整个 result 对象
  71. resolve(result as any);
  72. } else if (status != null && (status == 401 || status == 403)) {
  73. // Token 过期或无权限,跳转登录
  74. handleLoginRedirect();
  75. reject(new Error("登录已过期,请重新登录"));
  76. } else if (code != null && (code == 401 || code == 403)) {
  77. // 有些接口使用 code 字段
  78. handleLoginRedirect();
  79. reject(new Error("登录已过期,请重新登录"));
  80. } else {
  81. const errorMsg = msg != null ? msg : "请求失败";
  82. reject(new Error(errorMsg));
  83. }
  84. } else {
  85. reject(new Error(`请求失败:${statusCode}`));
  86. }
  87. },
  88. fail: (err: any) => {
  89. console.error("请求失败"+BASE_URL + config.url, err);
  90. reject(new Error("网络请求失败"));
  91. },
  92. });
  93. });
  94. };
  95. /**
  96. * 跳转登录页
  97. */
  98. const handleLoginRedirect = (): void => {
  99. uni.showToast({
  100. title: "登录已过期",
  101. icon: "none",
  102. });
  103. setTimeout(() => {
  104. uni.reLaunch({
  105. url: "/pages/login/index",
  106. });
  107. }, 1500);
  108. };