request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import store from "../store/index.js";
  2. import { errorCode } from "./commonDicts.js";
  3. export const timeout = 1000 * 60;
  4. import { showLoading,hideLoading } from "./loading.js";
  5. // 通用请求头设定
  6. // const ajaxHeader = 'x-ajax';
  7. const sessionIdHeader = 'Authorization';
  8. const install = (app) => {
  9. uni.$u.http.setConfig((config) => {
  10. // const { ip, port, folder } = store.state.user.netConfig;
  11. // /* config 为默认全局配置*/
  12. // config.baseURL = ip + ":" + port + folder;
  13. config.timeout = timeout;
  14. // #ifdef APP-PLUS
  15. config.sslVerify = false,
  16. // #endif
  17. config.custom = {
  18. loading : true,
  19. loadingText : "正在加载…",
  20. }
  21. return config
  22. })
  23. // 请求拦截
  24. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  25. const { ip, port, folder } = store.state.user.netConfig;
  26. const baseURL = ip + (port ? ":" + port : "") + folder; /* 根域名 */
  27. // 处理请求url的逻辑
  28. // 如果是完整的URL(包含http://或https://)或者是本地Mock服务器请求,则不拼接baseURL
  29. const isFullUrl = /^https?:\/\//i.test(config.url);
  30. const isMockRequest = config.url.includes('localhost:3001') ||
  31. config.url.includes('192.168.0.243:3001') ||
  32. config.url.startsWith('/prod-api');
  33. if (!isFullUrl && !isMockRequest) {
  34. config.url = baseURL + config.url;
  35. }
  36. if(config.custom.loading){
  37. const { loadingText } = config.custom;
  38. const { timeout } = config;
  39. showLoading({loadingText,loadingTime : timeout});
  40. }
  41. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  42. config.data = config.data || {}
  43. // config.header['Content-Type'] = 'application/json;charset=utf-8'
  44. // // 默认指定返回 JSON 数据
  45. // if (!config.header[ajaxHeader]){
  46. // config.header[ajaxHeader] = 'json';
  47. // }
  48. // 是否需要设置 token
  49. if (!config.header[sessionIdHeader]) {
  50. const token = store.state.user.token;
  51. const system = store.state.user.belongSystem;
  52. if(token){
  53. config.header[sessionIdHeader] = token;
  54. if (system) {
  55. if (system.id) config.header['belongSystemId'] = system.id
  56. if (system.systemName) config.header['belongSystemName'] = encodeURI(system.systemName)
  57. }
  58. }
  59. // else{
  60. // uni.reLaunch({
  61. // url : "/pages/login/index"
  62. // })
  63. // }
  64. }
  65. return config
  66. }, config => { // 可使用async await 做异步操作
  67. return Promise.reject(config)
  68. })
  69. // 响应拦截
  70. uni.$u.http.interceptors.response.use((res) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  71. hideLoading();
  72. // 未设置状态码则默认成功状态
  73. const code = res.data.code || 200;
  74. // 获取错误信息
  75. const msg = errorCode[code] || res.data.msg || errorCode['default'];
  76. // 登录过期
  77. if (code === 401) {
  78. uni.$u.toast("登录过期")
  79. uni.reLaunch({
  80. url : "/pages/login/index"
  81. });
  82. }else if(code === 500){
  83. uni.$u.toast(msg);
  84. if(["登录状态已过期","令牌不能为空"].includes(msg)){
  85. uni.reLaunch({
  86. url : "/pages/login/index"
  87. });
  88. }
  89. return Promise.reject(msg)
  90. }
  91. return res.data === undefined ? {} : res.data
  92. }, (response) => {
  93. const { errMsg } = response;
  94. if (errMsg.includes("timeout")) {
  95. uni.$u.toast("请求超时,请重试")
  96. }if(errMsg.includes("fail")){
  97. uni.$u.toast("服务器异常");
  98. }
  99. hideLoading();
  100. return Promise.reject(response)
  101. })
  102. }
  103. export default { install }