chenyc
2024-12-23 7534287f7dc3824ff46476da2a0c7b9289ce0393
thirdPartyApi.js
@@ -1,5 +1,7 @@
// thirdPartyApi.js
const axios = require('axios');
const logger = require('./logger'); // 根据实际情况调整路径
// BASIC_API = 'https://hemobs.icoldchain.cn/'
const BASIC_API = 'http://testbs.ihemodialysis.com/'
let cachedToken = null;
@@ -28,11 +30,13 @@
// 获取缓存或新的 Token
async function getCachedOrNewToken() {
  logger.log('Check if the token exists')
  if (cachedToken && tokenExpiresAt > Date.now()) {
    return cachedToken;
  }
  // 如果 token 已经过期或不存在,则重新获取
  logger.log('If the token has expired or does not exist, retrieve it again')
  const { data, expires_in } = await getAccessToken();
  cachedToken = data;
  tokenExpiresAt = Date.now() + (expires_in - 60) * 1000; // 提前 60 秒刷新 token
@@ -73,6 +77,7 @@
async function fetchThirdPartyData(url, method = 'GET', headers = {}, data = null, queryParams = {}, pathParams = {}) {
  try {
    const token = await getCachedOrNewToken();
    logger.info(`get token====${token}`)
    console.log('得到了token------------------------',token)
    const fullUrl = buildUrl(url, pathParams);
@@ -89,6 +94,7 @@
    return response.data;
  } catch (error) {
    logger.error('Error fetching third-party data:', error.message)
    console.error('Error fetching third-party data:', error.message);
    throw error;
  }