import axios from 'axios' import PuppetMini from '../puppet-mini.js' import jwt from 'jsonwebtoken' import { log } from '../config.js' import CryptoJS from 'crypto-js' import * as crypto from 'crypto' export async function getUserInfo (token: string) { try { const verified = jwt.verify(token, PuppetMini.serviceSecret) // @ts-ignore if (verified.token) { const options = { data: { userId: '', }, headers: { // @ts-ignore token: verified.token, }, method: 'POST', url: PuppetMini.serviceUrl + '/user/info', } log.info('获取用户信息请求参数', JSON.stringify(options)) const res = await axios.request(options) if (res.data.code === 200) { return res.data.data } log.error('获取用户信息失败', JSON.stringify(res.data)) } log.error('token不存在') return null } catch (e) { log.error('请求报错', e) return null } } function getHash (content: string) { return crypto.createHash('sha512').update(content, 'utf8').digest('hex') } // 加密 export function aesEncrypt (data: any) { const jsonString = JSON.stringify(data) const encrypted = CryptoJS.AES.encrypt(jsonString, CryptoJS.enc.Utf8.parse(PuppetMini.encodeKey), { iv: CryptoJS.enc.Utf8.parse(PuppetMini.encodeIv) }) return encrypted.toString() } // 解码 export function aesDecrypt (content: string) { const decrypted = CryptoJS.AES.decrypt(content, CryptoJS.enc.Utf8.parse(PuppetMini.encodeKey), { iv: CryptoJS.enc.Utf8.parse(PuppetMini.encodeIv) }) const decryptedString = decrypted.toString(CryptoJS.enc.Utf8) return JSON.parse(decryptedString) } export async function getJsConfig (url: string) { try { const query = `appKey=${PuppetMini.bmpAppKey}&data={"url":"${url}"}${PuppetMini.bmpAppSecret}` const options = { data: { appKey: PuppetMini.bmpAppKey, data: JSON.stringify({ url }), sign: getHash(query), }, headers: { 'Content-Type': 'application/json', }, method: 'POST', url: PuppetMini.bmpUrl, } const res = await axios.request(options) return res.data.data } catch (e) { log.error('获取用户信息请求报错', e) return null } }