request.js 3.5 KB

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