request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // 线上域名
  2. // const host = "https://www.xiaoyaotravel.com/api"
  3. // 测试服务器域名
  4. // const host = "http://test.xiaoyaotravel.com:8082";
  5. // 测试服务器ip
  6. const host = "http://101.126.146.250:8082";
  7. // 黄雯本地
  8. // const host ="http://192.168.1.109:8082"
  9. // const host ="http://q9943037p3.goho.co"
  10. // 李忠畅本地
  11. // const host = "http://192.168.1.100:8082"
  12. // const host ="http://cilicli.qicp.vip"
  13. const request = ({
  14. url,
  15. data,
  16. header,
  17. method = "GET",
  18. showLoading = true,
  19. showErrorToast = true,
  20. } = {}) => {
  21. return new Promise((resolve, reject) => {
  22. const token = getToken();
  23. if (showLoading) uni.showLoading({ title: "加载中" });
  24. uni.request({
  25. // url: `${import.meta.env.VITE_API_BASE_URL}${url}`,
  26. url: `${host}${url}`,
  27. method,
  28. data,
  29. header: { ...header, Authorization: token, version: "1.0" },
  30. success: (res) =>
  31. handleSuccessCallback({
  32. res,
  33. resolve,
  34. reject,
  35. showLoading,
  36. showErrorToast,
  37. }),
  38. fail: (_) => handleFailCallback({ reject }),
  39. });
  40. });
  41. };
  42. // 上传文件
  43. const upload = ({ url, filePath, name }) => {
  44. return new Promise((resolve, reject) => {
  45. uni.showLoading({ title: "加载中" });
  46. uni.uploadFile({
  47. url: `${host}${url}`,
  48. header: { token: "" },
  49. filePath,
  50. name,
  51. success: (res) => handleSuccessCallback({ res, resolve, reject }),
  52. fail: (_) => handleFailCallback({ reject }),
  53. });
  54. });
  55. };
  56. const handleSuccessCallback = ({
  57. res,
  58. resolve,
  59. reject,
  60. showLoading = true,
  61. showErrorToast = true,
  62. }) => {
  63. if (showLoading) uni.hideLoading();
  64. let { statusCode, data } = res;
  65. const isSuccess = isHTTPSuccess(statusCode);
  66. if (!isSuccess) {
  67. if (showErrorToast)
  68. uni.showToast({ icon: "none", title: "网络错误请重新尝试" });
  69. reject({ msg: `网络错误${statusCode}`, detail: res });
  70. }
  71. if (isJSONString(data)) data = JSON.parse(data);
  72. if (data.errorCode === "NO-ERROR") {
  73. // 业务处理成功
  74. resolve(data);
  75. } else if (data.errorCode === "UNAUTHORIZED_LOGIN") {
  76. uni.$u.toast('请登录!')
  77. setTimeout(()=>{
  78. uni.navigateTo({
  79. url:'/pages/index/login'
  80. })
  81. },1000)
  82. // 登录过期
  83. // const { gotoLogin, setToken } = useAuth();
  84. // setToken("");
  85. // gotoLogin();
  86. // reject({ msg: data.msg, detail: res });
  87. } else {
  88. if (showErrorToast) uni.showToast({ icon: "none", title: data.errorMessage });
  89. reject({ msg: data.errorMessage, detail: res });
  90. }
  91. };
  92. const handleFailCallback = ({
  93. reject,
  94. showLoading = true,
  95. showErrorToast = true,
  96. }) => {
  97. if (showLoading) uni.hideLoading();
  98. if (showErrorToast) {
  99. uni.showToast({ icon: "none", title: "网络错误请重新尝试" });
  100. }
  101. reject({ msg: "网络错误", detail: null });
  102. };
  103. /**
  104. * 判断请求状态是否成功
  105. * 参数:http状态码
  106. * 返回值:[Boolen]
  107. */
  108. const isHTTPSuccess = (status) => {
  109. return (status >= 200 && status < 300) || status === 304;
  110. };
  111. const isJSONString = (str) => {
  112. if (typeof str === "string") {
  113. try {
  114. JSON.parse(str);
  115. return true;
  116. } catch (error) {
  117. return false;
  118. }
  119. }
  120. return false;
  121. };
  122. export { request, upload };