Files
robot_manager/src/utils/passwordEncrypt.ts
huaqiang fd753291fe refactor: vite.config.ts 类型修复与 Vite 8 迁移适配
- 修复 vite.config.ts 中 command 隐式 any 类型错误
- 修复 proxy 中 path 参数隐式 any 类型错误
- 将无效的 oxc.drop 配置迁移为 Vite 8 原生方式(build.rolldownOptions.output.minify.compress)
- 引入 defineConfig + ConfigEnv 提供完整类型推导
- JS 文件迁移为 TypeScript(user.ts, routerGuard.ts 等)
- 更新 API 层类型定义
2026-08-12 14:13:45 +08:00

42 lines
1.2 KiB
TypeScript

import { JSEncrypt } from 'jsencrypt'
import { getPublicKey } from '@/api/user'
export async function getPasswordPublicKey(): Promise<string> {
const res = await getPublicKey()
return res.data
}
export function encryptPassword(plainText: string, publicKey: string): string {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(formatPublicKey(publicKey))
const encrypted = encryptor.encrypt(plainText)
if (!encrypted) {
throw new Error('密码加密失败,请刷新页面后重试')
}
return encrypted
}
export async function encryptPasswordPayload<T extends Record<string, unknown>>(
payload: T,
fields: Array<keyof T>,
): Promise<T> {
const publicKey = await getPasswordPublicKey()
const nextPayload = { ...payload }
for (const field of fields) {
nextPayload[field] = encryptPassword(String(payload[field] || ''), publicKey) as T[keyof T]
}
return nextPayload
}
function formatPublicKey(publicKey: string): string {
if (!publicKey) {
return ''
}
if (publicKey.includes('BEGIN PUBLIC KEY')) {
return publicKey
}
const normalized = publicKey.replace(/\s+/g, '')
const lines = normalized.match(/.{1,64}/g) || []
return ['-----BEGIN PUBLIC KEY-----', ...lines, '-----END PUBLIC KEY-----'].join('\n')
}