| | |
| | | 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'); |
| | |
| | | }); |
| | | app.get('/test', async (req, res) => { |
| | | const token=await thirdPartyApi.getAccessToken() |
| | | logger.info('返回token=='+JSON.stringify(token)) |
| | | res.json(token); |
| | | }); |
| | | // 定义 /webuser 路由处理器 用患者code 换信息 |
| | |
| | | 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; |
| | |
| | | }, |
| | | remsg: "success" |
| | | }; |
| | | console.log('返回患者信息成功') |
| | | logger.info('Successfully returned patient information') |
| | | console.log('Successfully returned patient information') |
| | | }else{ |
| | | response = { |
| | | retcode: 1001, |
| | |
| | | |
| | | } catch (err) { |
| | | console.error(err); |
| | | logger.error(err) |
| | | res.status(500).json({ retCode: "-1", msg: err }); |
| | | } |
| | | } else { |
| New file |
| | |
| | | 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; |
| | |
| | | "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 |
| | | const axios = require('axios'); |
| | | |
| | | const logger = require('./logger'); // 根据实际情况调整路径 |
| | | // BASIC_API = 'https://hemobs.icoldchain.cn/' |
| | | const BASIC_API = 'http://testbs.ihemodialysis.com/' |
| | | let cachedToken = null; |
| | |
| | | |
| | | // 获取缓存或新的 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 |
| | |
| | | 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); |
| | | |
| | |
| | | |
| | | 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; |
| | | } |