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