dasig/frontend/src/@dasig/api/functions/postApi.ts
2025-11-21 15:34:18 +08:00

35 lines
1,007 B
TypeScript

import { encryptRsa } from '../../scripts'
import { $backendUrl } from 'configs/config.api'
import { $tokenExpiration, $tokenName } from 'configs/config.security'
import dayjs from 'dayjs'
import { ofetch } from 'ofetch'
const URL = $backendUrl.get()
const TOKEN_NAME = $tokenName.get()
const TOKEN_EXPIRATION = $tokenExpiration.get()
export default async (api: string, body: Object) => {
const today = new Date()
const todayUnix = dayjs(today).unix()
const expiration = todayUnix + TOKEN_EXPIRATION
const aes = await encryptRsa(`${api.toString()}-${todayUnix.toString()}-${expiration.toString()}`)
const hash = `${TOKEN_NAME}=${aes}token`
try {
await ofetch(URL + api, {
headers: {
Accept: 'application/json',
'Cache-Control': 'no-cache',
'Dasig-Token': hash,
},
retry: 3,
retryDelay: 500,
retryStatusCodes: [400, 404, 405, 500, 502],
method: 'POST',
body: body,
})
return true
} catch {
return false
}
}