request.js 3.0 KB

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