(token: string)
| 25 | } |
| 26 | |
| 27 | async function refreshToken(token: string) { |
| 28 | const decodedToken = parseJwt(token); |
| 29 | const currentTimestamp = Math.floor(Date.now() / 1000); // 当前时间的UNIX时间戳(秒) |
| 30 | if (decodedToken && currentTimestamp < decodedToken.exp) { |
| 31 | return token; |
| 32 | } |
| 33 | // 如果令牌无效或已过期,则尝试获取新令牌 |
| 34 | const resp = await fetch("https://edge.microsoft.com/translate/auth") |
| 35 | if (resp.ok) return resp.text(); |
| 36 | else throw new Error(`请求失败: ${resp}`); |
| 37 | } |
| 38 | |
| 39 | // 解析 jwt,返回解析后对象 |
| 40 | function parseJwt(token: string) { |