Skip to content

Commit a414568

Browse files
author
Kerwin
committed
feat: 显示AccessToken过期时间(Close #145)
1 parent 1104844 commit a414568

File tree

8 files changed

+50
-4
lines changed

8 files changed

+50
-4
lines changed

service/src/chatgpt/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { SocksProxyAgent } from 'socks-proxy-agent'
66
import httpsProxyAgent from 'https-proxy-agent'
77
import fetch from 'node-fetch'
88
import type { AuditConfig } from 'src/storage/model'
9+
import jwt_decode from 'jwt-decode'
10+
import dayjs from 'dayjs'
911
import type { TextAuditService } from '../utils/textAudit'
1012
import { textAuditServices } from '../utils/textAudit'
1113
import { getCacheConfig, getOriginConfig } from '../storage/config'
1214
import { sendResponse } from '../utils'
1315
import { isNotEmptyString } from '../utils/is'
14-
import type { ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'
16+
import type { ChatContext, ChatGPTUnofficialProxyAPIOptions, JWT, ModelConfig } from '../types'
1517
import { getChatByMessageId } from '../storage/mongo'
1618
import type { RequestOptions } from './types'
1719

@@ -145,6 +147,15 @@ async function containsSensitiveWords(audit: AuditConfig, text: string): Promise
145147
}
146148
return false
147149
}
150+
151+
async function fetchAccessTokenExpiredTime() {
152+
const config = await getCacheConfig()
153+
const jwt = jwt_decode(config.accessToken) as JWT
154+
if (jwt.exp)
155+
return dayjs.unix(jwt.exp).format('YYYY-MM-DD HH:mm:ss')
156+
return '-'
157+
}
158+
148159
let cachedBalance: number | undefined
149160
let cacheExpiration = 0
150161

@@ -230,7 +241,10 @@ function formatDate(date) {
230241

231242
async function chatConfig() {
232243
const config = await getOriginConfig() as ModelConfig
233-
config.balance = await fetchBalance()
244+
if (config.apiModel === 'ChatGPTAPI')
245+
config.balance = await fetchBalance()
246+
else
247+
config.accessTokenExpiredTime = await fetchAccessTokenExpiredTime()
234248
return sendResponse<ModelConfig>({
235249
type: 'Success',
236250
data: config,

service/src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ export interface ModelConfig {
3535
httpsProxy?: string
3636
allowRegister?: boolean
3737
balance?: string
38+
accessTokenExpiredTime?: string
3839
}
3940

4041
export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined
42+
43+
export interface JWT {
44+
'https://api.openai.com/profile': {
45+
'email': string
46+
'email_verified': boolean
47+
}
48+
'https://api.openai.com/auth': {
49+
'user_id': string
50+
}
51+
'iss': string
52+
'sub': string
53+
'aud': []
54+
'iat': number
55+
'exp': number
56+
'azp': string
57+
'scope': string
58+
}

src/components/common/Setting/About.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang='ts'>
22
import { onMounted, ref, watch } from 'vue'
3-
import { NButton, NInput, NSelect, NSpin, useMessage } from 'naive-ui'
3+
import { NButton, NInput, NSelect, NSpin, NTag, useMessage } from 'naive-ui'
44
import { ConfigState } from './model'
55
import { fetchChatConfig, fetchUpdateBaseSetting } from '@/api'
66
import { t } from '@/locales'
@@ -110,8 +110,17 @@ onMounted(() => {
110110
<div class="flex-1">
111111
<NInput :value="config.accessToken" placeholder="" @input="(val) => { config.accessToken = val }" />
112112
</div>
113+
</div>
114+
<div v-if="!isChatGPTAPI" class="flex items-center space-x-4">
115+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.accessTokenExpiredTime') }}</span>
116+
<div class="flex-1">
117+
{{ config.accessTokenExpiredTime }}
118+
<NTag v-if="config.accessTokenExpiredTime && config.accessTokenExpiredTime !== '-' && new Date(config.accessTokenExpiredTime as string) < new Date()" :bordered="false" type="warning">
119+
{{ new Date(config.accessTokenExpiredTime as string) > new Date() ? '' : 'Expired' }}
120+
</NTag>
121+
</div>
113122
<p>
114-
<a target="_blank" href="https://chat.openai.com/api/auth/session">Get Token</a>
123+
<a target="_blank" href="https://chat.openai.com/api/auth/session">Goto Refresh Token</a>
115124
</p>
116125
</div>
117126
<div v-if="!isChatGPTAPI" class="flex items-center space-x-4">

src/components/common/Setting/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export class ConfigState {
22
timeoutMs?: number
33
apiKey?: string
44
accessToken?: string
5+
accessTokenExpiredTime?: string
56
apiBaseUrl?: string
67
apiModel?: ApiModel
78
chatModel?: CHATMODEL

src/locales/en-US.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export default {
124124
auditBaiduLabelLink: 'Goto Label Detail',
125125
auditCustomizeEnabled: 'Customize',
126126
auditCustomizeWords: 'Sensitive Words',
127+
accessTokenExpiredTime: 'Expired Time',
127128
},
128129
store: {
129130
siderButton: 'Prompt Store',

src/locales/ko-KR.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export default {
122122
auditBaiduLabelLink: '레이블 세부 정보로 이동',
123123
auditCustomizeEnabled: '맞춤화하다',
124124
auditCustomizeWords: '단어 맞춤설정',
125+
accessTokenExpiredTime: '만료된 시간',
125126
},
126127
store: {
127128
siderButton: '프롬프트 스토어',

src/locales/zh-CN.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export default {
124124
auditBaiduLabelLink: '查看细分类型',
125125
auditCustomizeEnabled: '自定义',
126126
auditCustomizeWords: '敏感词',
127+
accessTokenExpiredTime: '过期时间',
127128
},
128129
store: {
129130
siderButton: '提示词商店',

src/locales/zh-TW.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export default {
124124
auditBaiduLabelLink: '查看细分类型',
125125
auditCustomizeEnabled: '自定义',
126126
auditCustomizeWords: '敏感词',
127+
accessTokenExpiredTime: '过期时间',
127128
},
128129
store: {
129130
siderButton: '提示詞商店',

0 commit comments

Comments
 (0)