You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
customAgent/src/utils/api.js

56 lines
1.7 KiB

import { Get, Post } from './https.js'
import { useAppStore } from '@/store/app.js'
import CryptoJS from 'crypto-js'
// AES 加密配置
const AES_KEY = import.meta.env.VITE_AES_KEY || '3a7b9c2d5e8f1a4b6c9d8e7f5a3b2c1d' // 从环境变量读取密钥,默认值作为 fallback
const ENCRYPTION_ENABLED = true // 加密开关,默认关闭
/**
* AES 加密函数(ECB 模式,PKCS7 填充,Base64 输出)
* @param {Object} data - 需要加密的数据
* @returns {string} Base64 编码的密文
*/
export function encryptPayload(data) {
const key = CryptoJS.enc.Utf8.parse(AES_KEY)
const dataStr = JSON.stringify(data)
const encrypted = CryptoJS.AES.encrypt(dataStr, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
})
return encrypted.toString()
}
/**
* 判断事件类型是否需要加密
* @param {string} eventType - 事件类型
* @returns {boolean}
*/
function shouldEncrypt(eventType) {
return ENCRYPTION_ENABLED && (eventType === 'CARD_ACTION' || eventType === 'PAGE_ACTION')
}
const getCurrentISOTime = () => {
return new Date().toISOString()
}
const getTimestamp = () => {
return Date.now()
}
// 获取 app store 实例(延迟初始化,避免循环依赖)
function getAppStore() {
return useAppStore()
}
// 用户输入:统一复用 liics 迁移后的发送动作。
// 该动作在机器人模式下调用 /liics_api/semantic/qaChat,人工模式下走原有 WebSocket。
export function messageSend(text, options = {}) {
const appStore = getAppStore()
return appStore.sendingRobot({
reqAndResType: '1',
msgText: text,
msgSource: appStore.manualService,
quoteData: options.quoteData || null,
questionType: options.questionType || '',
})
}