request.uts 3.8 KB

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