// post 请求
const postData = async (url, data) => {
  if (!sessionStorage.getItem('server_gateway')) {
    const jsonResponse = await fetch('./app_params.json');
    const content = await jsonResponse.json();
    sessionStorage.setItem('server_gateway', content.server_gateway);
  }
  // server_gateway
  const response = await fetch(sessionStorage.getItem('server_gateway') + url, {
    method: 'POST', // 设置请求方法为 "POST"
    headers: {
      'Content-Type': 'application/json', // 设置内容类型为 JSON
    },
    body: JSON.stringify(data), // 将参数对象序列化为 JSON 字符串
  });
  return await response.json(); // 解析响应并返回 JSON 数据
};
const jsonRpcObj = (method, params) => {
  return {
    jsonrpc: '2.0',
    method: method,
    params: [params],
    id: 1,
  };
};

// 发送客户端配置请求
const sendRequest = async (method, params) => {
  let token = '';
  if (sessionStorage.getItem('token')) {
    token = JSON.parse(sessionStorage.getItem('token'));
  }
  const data = await postData(
    '/IOrganizationService',
    jsonRpcObj(method, token ? { Token: token, ...params } : { ...params }),
  );
  return data;
};

// 客户端获取Server配置
const getServerSettingAsync = async () => {
  const data = await sendRequest('GetServerSettingAsync', {});
  return data;
};

export { getServerSettingAsync };
