request.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // 配置请求根路径
  2. const BASE_URL = process.env.UNI_BASE_URL;
  3. import { showToast } from "@/utils/index";
  4. const request = (requesUrl, data, method = "GET") => {
  5. let url = `${BASE_URL}${requesUrl}`;
  6. return new Promise((resolve, reject) => {
  7. let header = {
  8. "Content-Type": "application/json; charset=UTF-8",
  9. "Authorization-Business": uni.getStorageSync("storage_key"),
  10. };
  11. uni.request({
  12. url: url,
  13. data: data,
  14. method: method,
  15. header: header,
  16. success: (res) => {
  17. const { data } = res;
  18. console.log(res);
  19. if (data.code !== "") {
  20. // 判断是不是 token 过期了
  21. const tokenerr = [20003, "20003", 20004, "20004", 20005, "20005"];
  22. if (tokenerr.includes(data.code)) {
  23. // 清除本地缓存所有的数据
  24. uni.clearStorageSync();
  25. setTimeout(() => {
  26. // 跳转回登陆页面
  27. uni.redirectTo({
  28. url: "/pages/login/index"
  29. });
  30. }, 1000);
  31. showToast("登陆超时,请重新登陆", "none", 1000);
  32. return;
  33. }
  34. // showToast(data.message || "系统突然出差了,请稍后再试","none",2000)
  35. return resolve(data);
  36. }
  37. return resolve(data);
  38. },
  39. fail: (res) => {
  40. reject(res);
  41. showToast("系统突然出差了,请稍后再试", "none", 2000);
  42. },
  43. });
  44. });
  45. };
  46. //不带token接口请求,
  47. const request1 = (requesUrl, data, method = "GET") => {
  48. let url = `${BASE_URL}${requesUrl}`;
  49. return new Promise((resolve, reject) => {
  50. let header = {
  51. "Content-Type": "application/json; charset=UTF-8",
  52. };
  53. uni.request({
  54. url: url,
  55. data: data,
  56. method: method,
  57. header: header,
  58. success: (res) => {
  59. const { data } = res;
  60. if (data.code !== "") {
  61. // 判断是不是 token 过期了
  62. const tokenerr = [20003, "20003", 20004, "20004", 20005, "20005"];
  63. if (tokenerr.includes(data.code)) {
  64. // 清除本地缓存所有的数据
  65. uni.clearStorageSync();
  66. setTimeout(() => {
  67. // 跳转回登陆页面
  68. uni.redirectTo({
  69. url: "/pages/login/index",
  70. });
  71. }, 1000);
  72. showToast("登陆超时,请重新登陆", "none", 1000);
  73. return;
  74. }
  75. // showToast(data.message || "系统突然出差了,请稍后再试","none",2000)
  76. return resolve(data);
  77. }
  78. return resolve(data);
  79. },
  80. fail: (res) => {
  81. reject(res);
  82. showToast("系统突然出差了,请稍后再试", "none", 2000);
  83. },
  84. });
  85. });
  86. };
  87. export { request, request1 };