token.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import { API } from './api';
  16. /**
  17. * 获取可用的token keys
  18. * @returns {Promise<string[]>} 返回active状态的token key数组
  19. */
  20. export async function fetchTokenKeys() {
  21. try {
  22. const response = await API.get('/api/token/?p=1&size=10');
  23. const { success, data } = response.data;
  24. if (!success) throw new Error('Failed to fetch token keys');
  25. const tokenItems = Array.isArray(data) ? data : data.items || [];
  26. const activeTokens = tokenItems.filter((token) => token.status === 1);
  27. return activeTokens.map((token) => token.key);
  28. } catch (error) {
  29. console.error('Error fetching token keys:', error);
  30. return [];
  31. }
  32. }
  33. /**
  34. * 获取服务器地址
  35. * @returns {string} 服务器地址
  36. */
  37. export function getServerAddress() {
  38. let status = localStorage.getItem('status');
  39. let serverAddress = '';
  40. if (status) {
  41. try {
  42. status = JSON.parse(status);
  43. serverAddress = status.server_address || '';
  44. } catch (error) {
  45. console.error('Failed to parse status from localStorage:', error);
  46. }
  47. }
  48. if (!serverAddress) {
  49. serverAddress = window.location.origin;
  50. }
  51. return serverAddress;
  52. }