广丰卷烟厂数采质量分析系统
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json5 from 'json5';
 
/**
 * Create service config by current env
 *
 * @param env The current env
 */
export function createServiceConfig(env: Env.ImportMeta) {
  const { VITE_SERVICE_BASE_URL, VITE_OTHER_SERVICE_BASE_URL, VITE_APP_BASE_API, VITE_APP_WEBSOCKET } = env;
 
  let other = {} as Record<App.Service.OtherBaseURLKey, string>;
  try {
    if (VITE_OTHER_SERVICE_BASE_URL) {
      other = json5.parse(VITE_OTHER_SERVICE_BASE_URL);
    }
  } catch {
    // eslint-disable-next-line no-console
    console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid json5 string');
  }
 
  const httpConfig: App.Service.SimpleServiceConfig = {
    baseURL: VITE_SERVICE_BASE_URL,
    other
  };
 
  const otherHttpKeys = Object.keys(httpConfig.other) as App.Service.OtherBaseURLKey[];
 
  const otherConfig: App.Service.OtherServiceConfigItem[] = otherHttpKeys.map(key => {
    return {
      key,
      ws: false,
      baseURL: httpConfig.other[key],
      proxyPattern: createProxyPattern(key)
    };
  });
 
  const config: App.Service.ServiceConfig = {
    baseURL: httpConfig.baseURL,
    ws: VITE_APP_WEBSOCKET === 'Y',
    proxyPattern: VITE_APP_BASE_API,
    other: otherConfig
  };
 
  return config;
}
 
/**
 * get backend service base url
 *
 * @param env - the current env
 * @param isProxy - if use proxy
 */
export function getServiceBaseURL(env: Env.ImportMeta, isProxy: boolean = env.DEV && env.VITE_HTTP_PROXY === 'Y') {
  const { baseURL, other, proxyPattern } = createServiceConfig(env);
 
  const otherBaseURL = {} as Record<App.Service.OtherBaseURLKey, string>;
 
  other.forEach(item => {
    otherBaseURL[item.key] = isProxy ? item.proxyPattern : item.baseURL;
  });
 
  return {
    baseURL: isProxy ? proxyPattern : (baseURL || '') + proxyPattern,
    otherBaseURL
  };
}
 
/**
 * Get proxy pattern of backend service base url
 *
 * @param key If not set, will use the default key
 */
function createProxyPattern(key: App.Service.OtherBaseURLKey) {
  return `/proxy-${key}`;
}