mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-20 10:36:37 +08:00
[update]两阶段编译代码
This commit is contained in:
parent
54c6e6ade4
commit
5199639d8c
@ -1,259 +1,260 @@
|
|||||||
import { enqueueSnackbar } from 'notistack';
|
import {enqueueSnackbar} from 'notistack';
|
||||||
import { snackbarConstants } from 'constants/SnackbarConstants';
|
import {snackbarConstants} from 'constants/SnackbarConstants';
|
||||||
import { API } from './api';
|
import {API} from './api';
|
||||||
|
|
||||||
export function getSystemName() {
|
export function getSystemName() {
|
||||||
let system_name = localStorage.getItem('system_name');
|
let system_name = localStorage.getItem('system_name');
|
||||||
if (!system_name) return 'One API';
|
if (!system_name) return 'One API';
|
||||||
return system_name;
|
return system_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isMobile() {
|
export function isMobile() {
|
||||||
return window.innerWidth <= 600;
|
return window.innerWidth <= 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
export function SnackbarHTMLContent({ htmlContent }) {
|
export function SnackbarHTMLContent({htmlContent}) {
|
||||||
return <div dangerouslySetInnerHTML={{ __html: htmlContent }} />;
|
return <div dangerouslySetInnerHTML={{__html: htmlContent}}/>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSnackbarOptions(variant) {
|
export function getSnackbarOptions(variant) {
|
||||||
let options = snackbarConstants.Common[variant];
|
let options = snackbarConstants.Common[variant];
|
||||||
if (isMobile()) {
|
if (isMobile()) {
|
||||||
// 合并 options 和 snackbarConstants.Mobile
|
// 合并 options 和 snackbarConstants.Mobile
|
||||||
options = { ...options, ...snackbarConstants.Mobile };
|
options = {...options, ...snackbarConstants.Mobile};
|
||||||
}
|
}
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showError(error) {
|
export function showError(error) {
|
||||||
if (error.message) {
|
if (error.message) {
|
||||||
if (error.name === 'AxiosError') {
|
if (error.name === 'AxiosError') {
|
||||||
switch (error.response.status) {
|
switch (error.response.status) {
|
||||||
case 429:
|
case 429:
|
||||||
enqueueSnackbar('错误:请求次数过多,请稍后再试!', getSnackbarOptions('ERROR'));
|
enqueueSnackbar('错误:请求次数过多,请稍后再试!', getSnackbarOptions('ERROR'));
|
||||||
break;
|
break;
|
||||||
case 500:
|
case 500:
|
||||||
enqueueSnackbar('错误:服务器内部错误,请联系管理员!', getSnackbarOptions('ERROR'));
|
enqueueSnackbar('错误:服务器内部错误,请联系管理员!', getSnackbarOptions('ERROR'));
|
||||||
break;
|
break;
|
||||||
case 405:
|
case 405:
|
||||||
enqueueSnackbar('本站仅作演示之用,无服务端!', getSnackbarOptions('INFO'));
|
enqueueSnackbar('本站仅作演示之用,无服务端!', getSnackbarOptions('INFO'));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
enqueueSnackbar('错误:' + error.message, getSnackbarOptions('ERROR'));
|
enqueueSnackbar('错误:' + error.message, getSnackbarOptions('ERROR'));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
enqueueSnackbar('错误:' + error, getSnackbarOptions('ERROR'));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
enqueueSnackbar('错误:' + error, getSnackbarOptions('ERROR'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showNotice(message, isHTML = false) {
|
export function showNotice(message, isHTML = false) {
|
||||||
if (isHTML) {
|
if (isHTML) {
|
||||||
enqueueSnackbar(<SnackbarHTMLContent htmlContent={message} />, getSnackbarOptions('NOTICE'));
|
enqueueSnackbar(<SnackbarHTMLContent htmlContent={message}/>, getSnackbarOptions('NOTICE'));
|
||||||
} else {
|
} else {
|
||||||
enqueueSnackbar(message, getSnackbarOptions('NOTICE'));
|
enqueueSnackbar(message, getSnackbarOptions('NOTICE'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showWarning(message) {
|
export function showWarning(message) {
|
||||||
enqueueSnackbar(message, getSnackbarOptions('WARNING'));
|
enqueueSnackbar(message, getSnackbarOptions('WARNING'));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showSuccess(message) {
|
export function showSuccess(message) {
|
||||||
enqueueSnackbar(message, getSnackbarOptions('SUCCESS'));
|
enqueueSnackbar(message, getSnackbarOptions('SUCCESS'));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showInfo(message) {
|
export function showInfo(message) {
|
||||||
enqueueSnackbar(message, getSnackbarOptions('INFO'));
|
enqueueSnackbar(message, getSnackbarOptions('INFO'));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getOAuthState() {
|
export async function getOAuthState() {
|
||||||
const res = await API.get('/api/oauth/state');
|
const res = await API.get('/api/oauth/state');
|
||||||
const { success, message, data } = res.data;
|
const {success, message, data} = res.data;
|
||||||
if (success) {
|
if (success) {
|
||||||
return data;
|
return data;
|
||||||
} else {
|
} else {
|
||||||
showError(message);
|
showError(message);
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function onGitHubOAuthClicked(github_client_id, openInNewTab = false) {
|
export async function onGitHubOAuthClicked(github_client_id, openInNewTab = false) {
|
||||||
const state = await getOAuthState();
|
const state = await getOAuthState();
|
||||||
if (!state) return;
|
if (!state) return;
|
||||||
let url = `https://github.com/login/oauth/authorize?client_id=${github_client_id}&state=${state}&scope=user:email`;
|
let url = `https://github.com/login/oauth/authorize?client_id=${github_client_id}&state=${state}&scope=user:email`;
|
||||||
if (openInNewTab) {
|
if (openInNewTab) {
|
||||||
window.open(url);
|
window.open(url);
|
||||||
} else {
|
} else {
|
||||||
window.location.href = url;
|
window.location.href = url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function onLarkOAuthClicked(lark_client_id) {
|
export async function onLarkOAuthClicked(lark_client_id) {
|
||||||
const state = await getOAuthState();
|
const state = await getOAuthState();
|
||||||
if (!state) return;
|
if (!state) return;
|
||||||
let redirect_uri = `${window.location.origin}/oauth/lark`;
|
let redirect_uri = `${window.location.origin}/oauth/lark`;
|
||||||
window.open(`https://accounts.feishu.cn/open-apis/authen/v1/authorize?redirect_uri=${redirect_uri}&client_id=${lark_client_id}&state=${state}`);
|
window.open(`https://accounts.feishu.cn/open-apis/authen/v1/authorize?redirect_uri=${redirect_uri}&client_id=${lark_client_id}&state=${state}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function onOidcClicked(auth_url, client_id, openInNewTab = false) {
|
export async function onOidcClicked(auth_url, client_id, openInNewTab = false) {
|
||||||
const state = await getOAuthState();
|
const state = await getOAuthState();
|
||||||
if (!state) return;
|
if (!state) return;
|
||||||
const redirect_uri = `${window.location.origin}/oauth/oidc`;
|
const redirect_uri = `${window.location.origin}/oauth/oidc`;
|
||||||
const response_type = "code";
|
const response_type = "code";
|
||||||
const scope = "openid profile email";
|
const scope = "openid profile email";
|
||||||
const url = `${auth_url}?client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=${response_type}&scope=${scope}&state=${state}`;
|
const url = `${auth_url}?client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=${response_type}&scope=${scope}&state=${state}`;
|
||||||
if (openInNewTab) {
|
if (openInNewTab) {
|
||||||
window.open(url);
|
window.open(url);
|
||||||
} else
|
} else {
|
||||||
{
|
window.location.href = url;
|
||||||
window.location.href = url;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isAdmin() {
|
export function isAdmin() {
|
||||||
let user = localStorage.getItem('user');
|
let user = localStorage.getItem('user');
|
||||||
if (!user) return false;
|
if (!user) return false;
|
||||||
user = JSON.parse(user);
|
user = JSON.parse(user);
|
||||||
return user.role >= 10;
|
return user.role >= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function timestamp2string(timestamp) {
|
export function timestamp2string(timestamp) {
|
||||||
let date = new Date(timestamp * 1000);
|
let date = new Date(timestamp * 1000);
|
||||||
let year = date.getFullYear().toString();
|
let year = date.getFullYear().toString();
|
||||||
let month = (date.getMonth() + 1).toString();
|
let month = (date.getMonth() + 1).toString();
|
||||||
let day = date.getDate().toString();
|
let day = date.getDate().toString();
|
||||||
let hour = date.getHours().toString();
|
let hour = date.getHours().toString();
|
||||||
let minute = date.getMinutes().toString();
|
let minute = date.getMinutes().toString();
|
||||||
let second = date.getSeconds().toString();
|
let second = date.getSeconds().toString();
|
||||||
if (month.length === 1) {
|
if (month.length === 1) {
|
||||||
month = '0' + month;
|
month = '0' + month;
|
||||||
}
|
}
|
||||||
if (day.length === 1) {
|
if (day.length === 1) {
|
||||||
day = '0' + day;
|
day = '0' + day;
|
||||||
}
|
}
|
||||||
if (hour.length === 1) {
|
if (hour.length === 1) {
|
||||||
hour = '0' + hour;
|
hour = '0' + hour;
|
||||||
}
|
}
|
||||||
if (minute.length === 1) {
|
if (minute.length === 1) {
|
||||||
minute = '0' + minute;
|
minute = '0' + minute;
|
||||||
}
|
}
|
||||||
if (second.length === 1) {
|
if (second.length === 1) {
|
||||||
second = '0' + second;
|
second = '0' + second;
|
||||||
}
|
}
|
||||||
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
|
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calculateQuota(quota, digits = 2) {
|
export function calculateQuota(quota, digits = 2) {
|
||||||
let quotaPerUnit = localStorage.getItem('quota_per_unit');
|
let quotaPerUnit = localStorage.getItem('quota_per_unit');
|
||||||
quotaPerUnit = parseFloat(quotaPerUnit);
|
quotaPerUnit = parseFloat(quotaPerUnit);
|
||||||
|
|
||||||
return (quota / quotaPerUnit).toFixed(digits);
|
return (quota / quotaPerUnit).toFixed(digits);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderQuota(quota, digits = 2) {
|
export function renderQuota(quota, digits = 2) {
|
||||||
let displayInCurrency = localStorage.getItem('display_in_currency');
|
let displayInCurrency = localStorage.getItem('display_in_currency');
|
||||||
displayInCurrency = displayInCurrency === 'true';
|
displayInCurrency = displayInCurrency === 'true';
|
||||||
if (displayInCurrency) {
|
if (displayInCurrency) {
|
||||||
return '$' + calculateQuota(quota, digits);
|
return '$' + calculateQuota(quota, digits);
|
||||||
}
|
}
|
||||||
return renderNumber(quota);
|
return renderNumber(quota);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const verifyJSON = (str) => {
|
export const verifyJSON = (str) => {
|
||||||
try {
|
try {
|
||||||
JSON.parse(str);
|
JSON.parse(str);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function renderNumber(num) {
|
export function renderNumber(num) {
|
||||||
if (num >= 1000000000) {
|
if (num >= 1000000000) {
|
||||||
return (num / 1000000000).toFixed(1) + 'B';
|
return (num / 1000000000).toFixed(1) + 'B';
|
||||||
} else if (num >= 1000000) {
|
} else if (num >= 1000000) {
|
||||||
return (num / 1000000).toFixed(1) + 'M';
|
return (num / 1000000).toFixed(1) + 'M';
|
||||||
} else if (num >= 10000) {
|
} else if (num >= 10000) {
|
||||||
return (num / 1000).toFixed(1) + 'k';
|
return (num / 1000).toFixed(1) + 'k';
|
||||||
} else {
|
} else {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderQuotaWithPrompt(quota, digits) {
|
export function renderQuotaWithPrompt(quota, digits) {
|
||||||
let displayInCurrency = localStorage.getItem('display_in_currency');
|
let displayInCurrency = localStorage.getItem('display_in_currency');
|
||||||
displayInCurrency = displayInCurrency === 'true';
|
displayInCurrency = displayInCurrency === 'true';
|
||||||
if (displayInCurrency) {
|
if (displayInCurrency) {
|
||||||
return `(等价金额:${renderQuota(quota, digits)})`;
|
return `(等价金额:${renderQuota(quota, digits)})`;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function downloadTextAsFile(text, filename) {
|
export function downloadTextAsFile(text, filename) {
|
||||||
let blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
|
let blob = new Blob([text], {type: 'text/plain;charset=utf-8'});
|
||||||
let url = URL.createObjectURL(blob);
|
let url = URL.createObjectURL(blob);
|
||||||
let a = document.createElement('a');
|
let a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
a.download = filename;
|
a.download = filename;
|
||||||
a.click();
|
a.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeTrailingSlash(url) {
|
export function removeTrailingSlash(url) {
|
||||||
if (url.endsWith('/')) {
|
if (url.endsWith('/')) {
|
||||||
return url.slice(0, -1);
|
return url.slice(0, -1);
|
||||||
} else {
|
} else {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let channelModels = undefined;
|
let channelModels = undefined;
|
||||||
|
|
||||||
export async function loadChannelModels() {
|
export async function loadChannelModels() {
|
||||||
const res = await API.get('/api/models');
|
const res = await API.get('/api/models');
|
||||||
const { success, data } = res.data;
|
const {success, data} = res.data;
|
||||||
if (!success) {
|
if (!success) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
channelModels = data;
|
channelModels = data;
|
||||||
localStorage.setItem('channel_models', JSON.stringify(data));
|
localStorage.setItem('channel_models', JSON.stringify(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getChannelModels(type) {
|
export function getChannelModels(type) {
|
||||||
if (channelModels !== undefined && type in channelModels) {
|
if (channelModels !== undefined && type in channelModels) {
|
||||||
return channelModels[type];
|
return channelModels[type];
|
||||||
}
|
}
|
||||||
let models = localStorage.getItem('channel_models');
|
let models = localStorage.getItem('channel_models');
|
||||||
if (!models) {
|
if (!models) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
channelModels = JSON.parse(models);
|
||||||
|
if (type in channelModels) {
|
||||||
|
return channelModels[type];
|
||||||
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
|
||||||
channelModels = JSON.parse(models);
|
|
||||||
if (type in channelModels) {
|
|
||||||
return channelModels[type];
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function copy(text, name = '') {
|
export function copy(text, name = '') {
|
||||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||||
navigator.clipboard.writeText(text).then(() => {
|
navigator.clipboard.writeText(text).then(() => {
|
||||||
showNotice(`复制${name}成功!`, true);
|
showNotice(`复制${name}成功!`, true);
|
||||||
}, () => {
|
}, () => {
|
||||||
text = `复制${name}失败,请手动复制:<br /><br />${text}`;
|
text = `复制${name}失败,请手动复制:<br /><br />${text}`;
|
||||||
enqueueSnackbar(<SnackbarHTMLContent htmlContent={text} />, getSnackbarOptions('COPY'));
|
enqueueSnackbar(<SnackbarHTMLContent htmlContent={text}/>, getSnackbarOptions('COPY'));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const textArea = document.createElement("textarea");
|
const textArea = document.createElement("textarea");
|
||||||
textArea.value = text;
|
textArea.value = text;
|
||||||
document.body.appendChild(textArea);
|
document.body.appendChild(textArea);
|
||||||
textArea.select();
|
textArea.select();
|
||||||
try {
|
try {
|
||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
showNotice(`复制${name}成功!`, true);
|
showNotice(`复制${name}成功!`, true);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
text = `复制${name}失败,请手动复制:<br /><br />${text}`;
|
text = `复制${name}失败,请手动复制:<br /><br />${text}`;
|
||||||
enqueueSnackbar(<SnackbarHTMLContent htmlContent={text} />, getSnackbarOptions('COPY'));
|
enqueueSnackbar(<SnackbarHTMLContent htmlContent={text}/>, getSnackbarOptions('COPY'));
|
||||||
|
}
|
||||||
|
document.body.removeChild(textArea);
|
||||||
}
|
}
|
||||||
document.body.removeChild(textArea);
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user