chenyc
2024-12-23 7534287f7dc3824ff46476da2a0c7b9289ce0393
加入更新日志
3个文件已修改
1个文件已添加
41 ■■■■■ 已修改文件
app.js 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
logger.js 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
package.json 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
thirdPartyApi.js 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app.js
@@ -4,7 +4,7 @@
const fs = require('fs').promises;
const fs2=require('fs')
const path = require('path');
const moment = require('moment')
const logger = require('./logger'); // 根据实际情况调整路径
const axios = require('axios');
// 转成能识别的url
const ViapiUtil = require('@alicloud/viapi-utils');
@@ -77,6 +77,7 @@
  });
  app.get('/test', async (req, res) => {
    const token=await thirdPartyApi.getAccessToken()
    logger.info('返回token=='+JSON.stringify(token))
    res.json(token);
  });
// 定义 /webuser 路由处理器  用患者code 换信息
@@ -87,12 +88,14 @@
    const deviceID = req.body.deviceID;
    const type = req.body.type;
    const xid = req.body.xid;
    console.log(`action=${action},deviceID=${deviceID},type=${type},xid=${xid}`)
    console.log()
    logger.info(`req body action=${action},deviceID=${deviceID},type=${type},xid=${xid}`)
    if (action === 'doWebUser' && deviceID && type !== undefined) {
        try {
            const pathParams={code:xid}
            const data = await thirdPartyApi.fetchThirdPartyData('/patient/info/detail2', 'POST', {}, null,  pathParams,{});
            console.log('--------------获取到了患者信息',data.data)
            logger.info(`get patInfo data=${JSON.stringify(data)}`)
            console.log('get patInfo ----',data.data)
            // console.log(data)
            if(data?.code===200&&data?.message==='SUCCESS'&&data?.data){
                const user = data.data;
@@ -108,7 +111,8 @@
                    },
                    remsg: "success"
                };
                console.log('返回患者信息成功')
                logger.info('Successfully returned patient information')
                console.log('Successfully returned patient information')
            }else{
                response = {
                    retcode: 1001,
@@ -119,6 +123,7 @@
        } catch (err) {
            console.error(err);
            logger.error(err)
            res.status(500).json({ retCode: "-1", msg: err });
        }
    } else {
logger.js
New file
@@ -0,0 +1,20 @@
const pino = require('pino');
const pretty = require('pino-pretty');
// 创建日志记录器
const logger = pino({
  level: 'info',
  transport: {
    target: 'pino-pretty', // 可选:美化输出
    options: {
      translateTime: 'SYS:dd-mm-yyyy HH:MM:ss',
      ignore: 'pid,hostname'
    }
  }
});
// 或者简单地创建一个基础日志记录器
// const logger = pino();
// 将 logger 导出以便其他模块使用
module.exports = logger;
package.json
@@ -21,6 +21,8 @@
    "express": "^4.21.1",
    "moment": "^2.30.1",
    "mysql2": "^3.11.3",
    "pino": "^9.5.0",
    "pino-pretty": "^13.0.0",
    "pkg": "^5.8.1"
  },
  "devDependencies": {
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;
  }