request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = (Vue, vm) => {
  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. config.url = baseURL + config.url;
  29. if(config.custom.loading){
  30. const { loadingText } = config.custom;
  31. const { timeout } = config;
  32. showLoading({loadingText,loadingTime : timeout});
  33. }
  34. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  35. config.data = config.data || {}
  36. // config.header['Content-Type'] = 'application/json;charset=utf-8'
  37. // // 默认指定返回 JSON 数据
  38. // if (!config.header[ajaxHeader]){
  39. // config.header[ajaxHeader] = 'json';
  40. // }
  41. // 是否需要设置 token
  42. if (!config.header[sessionIdHeader]) {
  43. const token = store.state.user.token;
  44. const system = store.state.user.belongSystem;
  45. if(token){
  46. config.header[sessionIdHeader] = token;
  47. if (system) {
  48. if (system.id) config.header['belongSystemId'] = system.id
  49. if (system.systemName) config.header['belongSystemName'] = encodeURI(system.systemName)
  50. }
  51. }
  52. // else{
  53. // uni.reLaunch({
  54. // url : "/pages/login/index"
  55. // })
  56. // }
  57. }
  58. return config
  59. }, config => { // 可使用async await 做异步操作
  60. return Promise.reject(config)
  61. })
  62. // 响应拦截
  63. uni.$u.http.interceptors.response.use((res) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  64. hideLoading();
  65. // 未设置状态码则默认成功状态
  66. const code = res.data.code || 200;
  67. // 获取错误信息
  68. const msg = errorCode[code] || res.data.msg || errorCode['default'];
  69. // 登录过期
  70. if (code === 401) {
  71. uni.$u.toast("登录过期")
  72. uni.reLaunch({
  73. url : "/pages/login/index"
  74. });
  75. }else if(code === 500){
  76. uni.$u.toast(msg);
  77. if(["登录状态已过期","令牌不能为空"].includes(msg)){
  78. uni.reLaunch({
  79. url : "/pages/login/index"
  80. });
  81. }
  82. return Promise.reject(msg)
  83. }
  84. return res.data === undefined ? {} : res.data
  85. }, (response) => {
  86. const { errMsg } = response;
  87. if (errMsg.includes("timeout")) {
  88. uni.$u.toast("请求超时,请重试")
  89. }if(errMsg.includes("fail")){
  90. uni.$u.toast("服务器异常");
  91. }
  92. hideLoading();
  93. return Promise.reject(response)
  94. })
  95. }
  96. export default { install }