diff --git a/src/app/services/base.query.js b/src/app/services/base.query.js index 38afd257..15c5a6c6 100644 --- a/src/app/services/base.query.js +++ b/src/app/services/base.query.js @@ -40,7 +40,7 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => { const { token, refreshToken, - expireTime = new Date().getTime(), + expireTime = +new Date(), } = api.getState().authData; let result = null; if ( diff --git a/src/app/services/channel.js b/src/app/services/channel.js index 2af80b99..e0b9f16e 100644 --- a/src/app/services/channel.js +++ b/src/app/services/channel.js @@ -162,7 +162,7 @@ export const channelApi = createApi({ dispatch( updateChannel({ id: gid, - icon: `${BASE_URL}/resource/group_avatar?gid=${gid}&t=${new Date().getTime()}`, + icon: `${BASE_URL}/resource/group_avatar?gid=${gid}&t=${+new Date()}`, }) ); } catch (error) { diff --git a/src/app/services/handlers.js b/src/app/services/handlers.js index 3f80c5d8..f0180bd9 100644 --- a/src/app/services/handlers.js +++ b/src/app/services/handlers.js @@ -12,7 +12,7 @@ export const onMessageSendStarted = async ( type = "text", from_uid, reply_mid = null, - properties = { local_id: new Date().getTime() }, + properties = { local_id: +new Date() }, }, { dispatch, queryFulfilled }, from = "channel" @@ -22,7 +22,7 @@ export const onMessageSendStarted = async ( // id: who send to ,from_uid: who sent console.log("handlers data", content, type, properties, ignoreLocal, id); const isImage = properties.content_type?.startsWith("image"); - const ts = properties.local_id || new Date().getTime(); + const ts = properties.local_id || +new Date(); // let imageData = null; // if (type == "image") { // if (typeof content == "string" && content.startsWith("data:image")) { diff --git a/src/app/services/server.js b/src/app/services/server.js index 7ab67a55..3a5c03ee 100644 --- a/src/app/services/server.js +++ b/src/app/services/server.js @@ -4,218 +4,218 @@ import { updateInfo } from "../slices/server"; import baseQuery from "./base.query"; const defaultExpireDuration = 7 * 24 * 60 * 60; export const serverApi = createApi({ - reducerPath: "serverApi", - baseQuery, - endpoints: (builder) => ({ - getServer: builder.query({ - query: () => ({ url: `admin/system/organization` }), - transformResponse: (data) => { - data.logo = `${BASE_URL}/resource/organization/logo?t=${new Date().getTime()}`; - return data; - }, - async onQueryStarted(data, { dispatch, queryFulfilled }) { - try { - const { data: server } = await queryFulfilled; - dispatch(updateInfo(server)); - } catch { - console.log("get server info error"); - } - } + reducerPath: "serverApi", + baseQuery, + endpoints: (builder) => ({ + getServer: builder.query({ + query: () => ({ url: `admin/system/organization` }), + transformResponse: (data) => { + data.logo = `${BASE_URL}/resource/organization/logo?t=${+new Date()}`; + return data; + }, + async onQueryStarted(data, { dispatch, queryFulfilled }) { + try { + const { data: server } = await queryFulfilled; + dispatch(updateInfo(server)); + } catch { + console.log("get server info error"); + } + }, + }), + getThirdPartySecret: builder.query({ + query: () => ({ + // headers: { + // "content-type": "text/plain", + // accept: "text/plain", + // }, + url: `/admin/system/third_party_secret`, + responseHandler: (response) => response.text(), + }), + keepUnusedDataFor: 0, + }), + updateThirdPartySecret: builder.mutation({ + query: () => ({ + url: `/admin/system/third_party_secret`, + method: "POST", + responseHandler: (response) => response.text(), + }), + }), + getMetrics: builder.query({ + query: () => ({ url: `/admin/system/metrics` }), + }), + getServerVersion: builder.query({ + query: () => ({ + headers: { + // "content-type": "text/plain", + accept: "text/plain", + }, + url: `/admin/system/version`, + responseHandler: (response) => response.text(), + }), + }), + getFirebaseConfig: builder.query({ + query: () => ({ url: `admin/fcm/config` }), + }), + getGoogleAuthConfig: builder.query({ + query: () => ({ url: `admin/google_auth/config` }), + }), + updateGoogleAuthConfig: builder.mutation({ + query: (data) => ({ + url: `admin/google_auth/config`, + method: "POST", + body: data, + }), + }), + getGithubAuthConfig: builder.query({ + query: () => ({ url: `admin/github_auth/config` }), + }), + updateGithubAuthConfig: builder.mutation({ + query: (data) => ({ + url: `admin/github_auth/config`, + method: "POST", + body: data, + }), + }), + sendTestEmail: builder.mutation({ + query: (data) => ({ + url: `/admin/system/send_mail`, + method: "POST", + body: data, + }), + }), + updateFirebaseConfig: builder.mutation({ + query: (data) => ({ + url: `admin/fcm/config`, + method: "POST", + body: data, + }), + }), + getAgoraConfig: builder.query({ + query: () => ({ url: `admin/agora/config` }), + }), + updateAgoraConfig: builder.mutation({ + query: (data) => ({ + url: `admin/agora/config`, + method: "POST", + body: data, + }), + }), + getSMTPConfig: builder.query({ + query: () => ({ url: `admin/smtp/config` }), + }), + getSMTPStatus: builder.query({ + query: () => ({ url: `/admin/smtp/enabled` }), + }), + updateSMTPConfig: builder.mutation({ + query: (data) => ({ + url: `admin/smtp/config`, + method: "POST", + body: data, + }), + }), + getLoginConfig: builder.query({ + query: () => ({ url: `admin/login/config` }), + }), + updateLoginConfig: builder.mutation({ + query: (data) => ({ + url: `admin/login/config`, + method: "POST", + body: data, + }), + }), + updateLogo: builder.mutation({ + query: (data) => ({ + headers: { + "content-type": "image/png", + }, + url: `admin/system/organization/logo`, + method: "POST", + body: data, + }), + async onQueryStarted(data, { dispatch, queryFulfilled }) { + try { + await queryFulfilled; + dispatch( + updateInfo({ + logo: `${BASE_URL}/resource/organization/logo?t=${+new Date()}`, + }) + ); + } catch { + console.log("update server logo error"); + } + }, + }), + createInviteLink: builder.query({ + query: (expired_in = defaultExpireDuration) => ({ + headers: { + "content-type": "text/plain", + accept: "text/plain", + }, + url: `/admin/system/create_invite_link?expired_in=${expired_in}`, + responseHandler: (response) => response.text(), + }), + transformResponse: (link) => { + // 替换掉域名 + const invite = new URL(link); + return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`; + }, + }), + updateServer: builder.mutation({ + query: (data) => ({ + url: `admin/system/organization`, + method: "POST", + body: data, + }), + async onQueryStarted(data, { dispatch, queryFulfilled, getState }) { + const { name: prevName, description: prevDesc } = getState().server; + dispatch(updateInfo(data)); + try { + await queryFulfilled; + } catch { + dispatch(updateInfo({ name: prevName, description: prevDesc })); + } + }, + }), + createAdmin: builder.mutation({ + query: (data) => ({ + url: `/admin/system/create_admin`, + method: "POST", + body: data, + }), + }), + getInitialized: builder.query({ + query: () => ({ + url: `/admin/system/initialized`, + }), + }), }), - getThirdPartySecret: builder.query({ - query: () => ({ - // headers: { - // "content-type": "text/plain", - // accept: "text/plain", - // }, - url: `/admin/system/third_party_secret`, - responseHandler: (response) => response.text() - }), - keepUnusedDataFor: 0 - }), - updateThirdPartySecret: builder.mutation({ - query: () => ({ - url: `/admin/system/third_party_secret`, - method: "POST", - responseHandler: (response) => response.text() - }) - }), - getMetrics: builder.query({ - query: () => ({ url: `/admin/system/metrics` }) - }), - getServerVersion: builder.query({ - query: () => ({ - headers: { - // "content-type": "text/plain", - accept: "text/plain" - }, - url: `/admin/system/version`, - responseHandler: (response) => response.text() - }) - }), - getFirebaseConfig: builder.query({ - query: () => ({ url: `admin/fcm/config` }) - }), - getGoogleAuthConfig: builder.query({ - query: () => ({ url: `admin/google_auth/config` }) - }), - updateGoogleAuthConfig: builder.mutation({ - query: (data) => ({ - url: `admin/google_auth/config`, - method: "POST", - body: data - }) - }), - getGithubAuthConfig: builder.query({ - query: () => ({ url: `admin/github_auth/config` }) - }), - updateGithubAuthConfig: builder.mutation({ - query: (data) => ({ - url: `admin/github_auth/config`, - method: "POST", - body: data - }) - }), - sendTestEmail: builder.mutation({ - query: (data) => ({ - url: `/admin/system/send_mail`, - method: "POST", - body: data - }) - }), - updateFirebaseConfig: builder.mutation({ - query: (data) => ({ - url: `admin/fcm/config`, - method: "POST", - body: data - }) - }), - getAgoraConfig: builder.query({ - query: () => ({ url: `admin/agora/config` }) - }), - updateAgoraConfig: builder.mutation({ - query: (data) => ({ - url: `admin/agora/config`, - method: "POST", - body: data - }) - }), - getSMTPConfig: builder.query({ - query: () => ({ url: `admin/smtp/config` }) - }), - getSMTPStatus: builder.query({ - query: () => ({ url: `/admin/smtp/enabled` }) - }), - updateSMTPConfig: builder.mutation({ - query: (data) => ({ - url: `admin/smtp/config`, - method: "POST", - body: data - }) - }), - getLoginConfig: builder.query({ - query: () => ({ url: `admin/login/config` }) - }), - updateLoginConfig: builder.mutation({ - query: (data) => ({ - url: `admin/login/config`, - method: "POST", - body: data - }) - }), - updateLogo: builder.mutation({ - query: (data) => ({ - headers: { - "content-type": "image/png" - }, - url: `admin/system/organization/logo`, - method: "POST", - body: data - }), - async onQueryStarted(data, { dispatch, queryFulfilled }) { - try { - await queryFulfilled; - dispatch( - updateInfo({ - logo: `${BASE_URL}/resource/organization/logo?t=${new Date().getTime()}` - }) - ); - } catch { - console.log("update server logo error"); - } - } - }), - createInviteLink: builder.query({ - query: (expired_in = defaultExpireDuration) => ({ - headers: { - "content-type": "text/plain", - accept: "text/plain" - }, - url: `/admin/system/create_invite_link?expired_in=${expired_in}`, - responseHandler: (response) => response.text() - }), - transformResponse: (link) => { - // 替换掉域名 - const invite = new URL(link); - return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`; - } - }), - updateServer: builder.mutation({ - query: (data) => ({ - url: `admin/system/organization`, - method: "POST", - body: data - }), - async onQueryStarted(data, { dispatch, queryFulfilled, getState }) { - const { name: prevName, description: prevDesc } = getState().server; - dispatch(updateInfo(data)); - try { - await queryFulfilled; - } catch { - dispatch(updateInfo({ name: prevName, description: prevDesc })); - } - } - }), - createAdmin: builder.mutation({ - query: (data) => ({ - url: `/admin/system/create_admin`, - method: "POST", - body: data - }) - }), - getInitialized: builder.query({ - query: () => ({ - url: `/admin/system/initialized` - }) - }) - }) }); export const { - useGetServerVersionQuery, - useGetGithubAuthConfigQuery, - useUpdateGithubAuthConfigMutation, - useGetGoogleAuthConfigQuery, - useUpdateGoogleAuthConfigMutation, - useGetSMTPStatusQuery, - useSendTestEmailMutation, - useUpdateFirebaseConfigMutation, - useGetFirebaseConfigQuery, - useGetLoginConfigQuery, - useUpdateLoginConfigMutation, - useGetSMTPConfigQuery, - useUpdateSMTPConfigMutation, - useGetAgoraConfigQuery, - useUpdateAgoraConfigMutation, - useGetServerQuery, - useGetMetricsQuery, - useLazyGetServerQuery, - useUpdateServerMutation, - useUpdateLogoMutation, - useCreateInviteLinkQuery, - useLazyCreateInviteLinkQuery, - useGetThirdPartySecretQuery, - useUpdateThirdPartySecretMutation, - useCreateAdminMutation, - useGetInitializedQuery + useGetServerVersionQuery, + useGetGithubAuthConfigQuery, + useUpdateGithubAuthConfigMutation, + useGetGoogleAuthConfigQuery, + useUpdateGoogleAuthConfigMutation, + useGetSMTPStatusQuery, + useSendTestEmailMutation, + useUpdateFirebaseConfigMutation, + useGetFirebaseConfigQuery, + useGetLoginConfigQuery, + useUpdateLoginConfigMutation, + useGetSMTPConfigQuery, + useUpdateSMTPConfigMutation, + useGetAgoraConfigQuery, + useUpdateAgoraConfigMutation, + useGetServerQuery, + useGetMetricsQuery, + useLazyGetServerQuery, + useUpdateServerMutation, + useUpdateLogoMutation, + useCreateInviteLinkQuery, + useLazyCreateInviteLinkQuery, + useGetThirdPartySecretQuery, + useUpdateThirdPartySecretMutation, + useCreateAdminMutation, + useGetInitializedQuery, } = serverApi; diff --git a/src/app/slices/auth.data.js b/src/app/slices/auth.data.js index cafd2a22..e732269d 100644 --- a/src/app/slices/auth.data.js +++ b/src/app/slices/auth.data.js @@ -1,78 +1,89 @@ import { createSlice } from "@reduxjs/toolkit"; -import { KEY_PWA_INSTALLED, KEY_REFRESH_TOKEN, KEY_TOKEN, KEY_UID, KEY_EXPIRE } from "../config"; +import { + KEY_PWA_INSTALLED, + KEY_REFRESH_TOKEN, + KEY_TOKEN, + KEY_UID, + KEY_EXPIRE, +} from "../config"; const initialState = { - initialized: true, - uid: null, - token: localStorage.getItem(KEY_TOKEN), - expireTime: localStorage.getItem(KEY_EXPIRE) || new Date().getTime(), - refreshToken: localStorage.getItem(KEY_REFRESH_TOKEN) + initialized: true, + uid: null, + token: localStorage.getItem(KEY_TOKEN), + expireTime: localStorage.getItem(KEY_EXPIRE) || +new Date(), + refreshToken: localStorage.getItem(KEY_REFRESH_TOKEN), }; const emptyState = { - initialized: true, - uid: null, - token: null, - expireTime: new Date().getTime(), - refreshToken: null + initialized: true, + uid: null, + token: null, + expireTime: +new Date(), + refreshToken: null, }; const authDataSlice = createSlice({ - name: "authData", - initialState, - reducers: { - setAuthData(state, action) { - const { - initialized = true, - user: { uid }, - token, - refresh_token, - expired_in = 0 - } = action.payload; - state.initialized = initialized; - state.uid = uid; - state.token = token; - state.refreshToken = refresh_token; - // 当前时间往后推expire时长 - console.log("expire", expired_in); - const expireTime = new Date().getTime() + Number(expired_in) * 1000; - state.expireTime = expireTime; - // set local data - localStorage.setItem(KEY_EXPIRE, expireTime); - localStorage.setItem(KEY_TOKEN, token); - localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token); - localStorage.setItem(KEY_UID, uid); - }, - resetAuthData() { - console.log("clear auth data"); - // remove local data - localStorage.removeItem(KEY_EXPIRE); - localStorage.removeItem(KEY_TOKEN); - localStorage.removeItem(KEY_REFRESH_TOKEN); - localStorage.removeItem(KEY_UID); - localStorage.removeItem(KEY_PWA_INSTALLED); + name: "authData", + initialState, + reducers: { + setAuthData(state, action) { + const { + initialized = true, + user: { uid }, + token, + refresh_token, + expired_in = 0, + } = action.payload; + state.initialized = initialized; + state.uid = uid; + state.token = token; + state.refreshToken = refresh_token; + // 当前时间往后推expire时长 + console.log("expire", expired_in); + const expireTime = +new Date() + Number(expired_in) * 1000; + state.expireTime = expireTime; + // set local data + localStorage.setItem(KEY_EXPIRE, expireTime); + localStorage.setItem(KEY_TOKEN, token); + localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token); + localStorage.setItem(KEY_UID, uid); + }, + resetAuthData() { + console.log("clear auth data"); + // remove local data + localStorage.removeItem(KEY_EXPIRE); + localStorage.removeItem(KEY_TOKEN); + localStorage.removeItem(KEY_REFRESH_TOKEN); + localStorage.removeItem(KEY_UID); + localStorage.removeItem(KEY_PWA_INSTALLED); - return emptyState; + return emptyState; + }, + setUid(state, action) { + const uid = action.payload; + state.uid = uid; + console.log("set uid orginal"); + }, + updateInitialized(state, action) { + const isInitialized = action.payload; + state.initialized = isInitialized; + }, + updateToken(state, action) { + const { token, refresh_token, expired_in } = action.payload; + console.log("refresh token"); + state.token = token; + const et = +new Date() + Number(expired_in) * 1000; + state.expireTime = et; + state.refreshToken = refresh_token; + localStorage.setItem(KEY_EXPIRE, et); + localStorage.setItem(KEY_TOKEN, token); + localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token); + }, }, - setUid(state, action) { - const uid = action.payload; - state.uid = uid; - console.log("set uid orginal"); - }, - updateInitialized(state, action) { - const isInitialized = action.payload; - state.initialized = isInitialized; - }, - updateToken(state, action) { - const { token, refresh_token, expired_in } = action.payload; - console.log("refresh token"); - state.token = token; - const et = new Date().getTime() + Number(expired_in) * 1000; - state.expireTime = et; - state.refreshToken = refresh_token; - localStorage.setItem(KEY_EXPIRE, et); - localStorage.setItem(KEY_TOKEN, token); - localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token); - } - } }); -export const { updateInitialized, setAuthData, resetAuthData, setUid, updateToken } = - authDataSlice.actions; +export const { + updateInitialized, + setAuthData, + resetAuthData, + setUid, + updateToken, +} = authDataSlice.actions; export default authDataSlice.reducer; diff --git a/src/common/component/Send/index.js b/src/common/component/Send/index.js index 1fa18850..1f9453b6 100644 --- a/src/common/component/Send/index.js +++ b/src/common/component/Send/index.js @@ -76,7 +76,7 @@ function Send({ for await (const msg of msgs) { console.log("send msg", msg); const { type: content_type, content, properties = {} } = msg; - properties.local_id = properties.local_id ?? new Date().getTime(); + properties.local_id = properties.local_id ?? +new Date(); await sendMessage({ id, reply_mid: replying_mid, @@ -90,7 +90,7 @@ function Send({ // send files if (uploadFiles && uploadFiles.length !== 0) { uploadFiles.forEach((fileInfo) => { - const ts = new Date().getTime(); + const ts = +new Date(); const { url, name, size, type } = fileInfo; const tmpMsg = { mid: ts, @@ -118,7 +118,7 @@ function Send({ type: "markdown", content, from_uid, - properties: { local_id: new Date().getTime() }, + properties: { local_id: +new Date() }, }); }; const toggleMode = () => { diff --git a/src/common/hook/useStreaming/index.js b/src/common/hook/useStreaming/index.js index f5248c18..f64278c3 100644 --- a/src/common/hook/useStreaming/index.js +++ b/src/common/hook/useStreaming/index.js @@ -64,7 +64,7 @@ export default function useStreaming() { if (initialized || initializing) return; // 如果token快要过期,先renew const { - authData: { token, expireTime = new Date().getTime(), refreshToken }, + authData: { token, expireTime = +new Date(), refreshToken }, } = store.getState(); let api_token = token; const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000)); diff --git a/src/common/hook/useUploadFile.js b/src/common/hook/useUploadFile.js index ac55776e..a984aeaa 100644 --- a/src/common/hook/useUploadFile.js +++ b/src/common/hook/useUploadFile.js @@ -44,7 +44,7 @@ export default function useUploadFile(props = {}) { if (!file) return; setData(null); const { - name = `rustchat-${new Date().getTime()}.${file.type.split("/")[1]}`, + name = `rustchat-${+new Date()}.${file.type.split("/")[1]}`, type: file_type, size: file_size, } = file; diff --git a/src/routes/setting/Overview.js b/src/routes/setting/Overview.js index 8c89e991..6ddbf70c 100644 --- a/src/routes/setting/Overview.js +++ b/src/routes/setting/Overview.js @@ -128,7 +128,7 @@ export default function Overview() {