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