Add currency auto transform (#83)

Co-authored-by: AceDataCloud <office@acedata.cloud>
This commit is contained in:
GermeyandAceDataCloud authored and GitHub committed 2024-07-14 15:35:04 +08:00
1 parent 3f16b60071
commit c83b4fcc9f
21 files changed
+245 -22

No files matched your search

+34 -2
View File
@@ -1,6 +1,6 @@
import { ActionContext } from 'vuex';
import { IRootState } from '../common/models';
import { userOperator, oauthOperator, siteOperator } from '@/operators';
import { userOperator, oauthOperator, siteOperator, exchangeOperator } from '@/operators';
import { log } from '@/utils/log';
import { IToken, IUser } from '@/models';
import { getSiteOrigin } from '@/utils/site';
@@ -31,6 +31,14 @@ export const setUser = ({ commit }: ActionContext<IRootState, IRootState>, paylo
commit('setUser', payload);
};
export const setCurrency = ({ commit }: ActionContext<IRootState, IRootState>, payload: string) => {
commit('setCurrency', payload);
};
export const setExchange = ({ commit }: ActionContext<IRootState, IRootState>, payload: any) => {
commit('setExchange', payload);
};
export const getUser = async ({ commit }: ActionContext<IRootState, IRootState>): Promise<IUser> => {
log(getUser, 'start to get user');
try {
@@ -62,6 +70,27 @@ export const getToken = async ({ commit }: ActionContext<IRootState, IRootState>
}
};
export const getExchangeRate = async (
{ state, commit }: ActionContext<IRootState, IRootState>,
payload: { source: string; target: string }
) => {
if (payload.source === payload.target) {
return;
}
const key = `${payload.source}-${payload.target}`;
if (!state.exchange || !state.exchange[key]) {
try {
const { data } = await exchangeOperator.rate(payload);
commit('setExchange', {
[key]: data.rate
});
return state.exchange![key];
} catch (e) {
log(getExchangeRate, 'get exchange rate failed');
}
}
};
export const initializeSite = async ({ state, commit, dispatch }: ActionContext<IRootState, IRootState>) => {
log(initializeSite, 'start to initialize site');
const origin = getSiteOrigin(state?.site);
@@ -113,9 +142,12 @@ export default {
resetUser,
resetSite,
setToken,
setCurrency,
setUser,
getExchangeRate,
getToken,
getUser,
initializeSite,
getSite
getSite,
setExchange
};
+7 -3
View File
@@ -4,15 +4,19 @@ import { IChatState } from '../chat/models';
import { IChatdocState } from '../chatdoc/models';
import { IQrartState } from '../qrart/models';
export interface ISetting {
navigationCollapsed?: boolean;
}
export interface ISetting {}
export interface ICommonState {
token: IToken;
user?: IUser;
setting?: ISetting;
site?: ISite;
currency: string;
exchange:
| {
[key: string]: number;
}
| undefined;
}
export interface IRootState extends ICommonState {
+13
View File
@@ -34,9 +34,22 @@ export const setSite = (state: IRootState, payload: any): void => {
};
};
export const setExchange = (state: IRootState, payload: any): void => {
state.exchange = {
...state.exchange,
...payload
};
};
export const setCurrency = (state: IRootState, payload: string): void => {
state.currency = payload;
};
export default {
setUser,
setSite,
setCurrency,
setExchange,
resetUser,
setToken,
resetToken,
+1 -1
View File
@@ -1 +1 @@
export default ['user', 'token', 'setting', 'locale', 'dark', 'site'];
export default ['user', 'token', 'setting', 'locale', 'dark', 'site', 'currency', 'exchange'];
+3 -3
View File
@@ -6,15 +6,15 @@ import qrartState from '../qrart/state';
export default (): IRootState => {
return {
currency: 'usd',
exchange: undefined,
user: {},
token: {
access: undefined,
refresh: undefined,
expiration: undefined
},
setting: {
navigationCollapsed: true
},
setting: {},
site: {},
chatdoc: chatdocState(),
chat: chatState(),