request.uts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if(config.header['isToken'] == false){
  49. header["Authorization"] = ""
  50. }
  51. }
  52. // 发起请求
  53. uni.request({
  54. url: BASE_URL + config.url,
  55. method: config.method ?? "GET",
  56. data: config.data,
  57. header: header,
  58. timeout: config.timeout ?? 30000,
  59. success: (res) => {
  60. // 提取属性(any 类型不能直接访问属性)
  61. console.log(config.url, "==", res);
  62. const statusCode = res.statusCode as number;
  63. const resData = res.data;
  64. if (statusCode == 200) {
  65. // 将响应数据转换为 UTSJSONObject,然后手动提取属性
  66. const result = resData as UTSJSONObject;
  67. let code = result["code"] as number | null;
  68. let msg = result["msg"] as string | null;
  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. reject(new Error(errorMsg));
  81. } else {
  82. const errorMsg = msg != null ? msg : "请求失败";
  83. reject(new Error(errorMsg));
  84. }
  85. } else {
  86. reject(new Error(`请求失败:${statusCode}`));
  87. }
  88. },
  89. fail: (err: any) => {
  90. console.error("请求失败"+BASE_URL + config.url, err);
  91. reject(new Error("网络请求失败"));
  92. },
  93. });
  94. });
  95. };
  96. /**
  97. * 跳转登录页
  98. */
  99. const handleLoginRedirect = (): void => {
  100. uni.showToast({
  101. title: "登录已过期",
  102. icon: "none",
  103. });
  104. setTimeout(() => {
  105. uni.reLaunch({
  106. url: "/pages/login/index",
  107. });
  108. }, 1500);
  109. };