Advertisement
Guest User

Untitled

a guest
Apr 30th, 2023
282
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 1 0
  1. const html = `<!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <title>查询OpenAI-API密钥信息</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
  8. </head>
  9. <style>
  10. table th {
  11. width: 30%;
  12. white-space: nowrap;
  13. }
  14. </style>
  15. <body>
  16. <div class="container">
  17. <div class="row justify-content-center">
  18. <div class="col-md-6">
  19. <h1 class="text-center mt-5">查询API密钥信息</h1>
  20. <form class="mt-4">
  21. <div class="form-group mb-3">
  22. <label for="api-key">API密钥:</label>
  23. <input type="text" id="api-key" name="api-key" class="form-control" required autocomplete="off">
  24. </div>
  25. <div class="d-grid">
  26. <input type="submit" value="查询" class="btn btn-primary">
  27. </div>
  28. </form>
  29. <br>
  30. <div id="error-message" class="alert alert-danger d-none"></div>
  31. <br>
  32. <table id="result" class="table table-bordered d-none">
  33. <tbody>
  34. <tr>
  35. <th>账户名称</th>
  36. <td id="account_name"></td>
  37. </tr>
  38. <tr>
  39. <th>是否绑卡</th>
  40. <td id="payment_method_status"></td>
  41. </tr>
  42. <tr>
  43. <th>已消费额度</th>
  44. <td id="used"></td>
  45. </tr>
  46. <tr>
  47. <th>剩余额度</th>
  48. <td id="remaining"></td>
  49. </tr>
  50. <tr>
  51. <th>账户额度</th>
  52. <td id="subscription"></td>
  53. </tr>
  54. <tr>
  55. <th>额度上限</th>
  56. <td id="system_hard_limit_usd"></td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. <script>
  61. const form = document.querySelector('form')
  62. const apiKeyInput = document.querySelector('#api-key')
  63. const resultTable = document.querySelector('#result')
  64.  
  65. form.addEventListener('submit', async (event) => {
  66. event.preventDefault()
  67.  
  68. const apiKey = apiKeyInput.value
  69. const response = await fetch('/api?key=' + apiKey)
  70. const data = await response.json()
  71.  
  72. if (data.error) {
  73. const errorMessage = document.querySelector('#error-message')
  74. errorMessage.innerText = data.error.message || '出错了'
  75. errorMessage.classList.remove('d-none')
  76. resultTable.classList.add('d-none')
  77. } else {
  78. const errorMessage = document.querySelector('#error-message')
  79. errorMessage.classList.add('d-none')
  80. document.querySelector('#account_name').innerText = data.account_name
  81. document.querySelector('#payment_method_status').innerText = data.has_payment_method ? '已绑卡' : '未绑卡';
  82. document.querySelector('#system_hard_limit_usd').innerText = data.system_hard_limit_usd
  83. document.querySelector('#used').innerText = data.used
  84. document.querySelector('#subscription').innerText = data.subscription
  85. document.querySelector('#remaining').innerText = data.remaining;
  86. resultTable.classList.remove('d-none')
  87. }
  88. })
  89.  
  90. </script>
  91. </div>
  92. </div>
  93. </div>
  94. </body>
  95. </html>
  96.  
  97. `
  98.  
  99. addEventListener('fetch', event => {
  100. event.respondWith(handleRequest(event.request))
  101. })
  102.  
  103. async function handleRequest(request) {
  104. const url = new URL(request.url)
  105. if (url.pathname === '/api') {
  106. const apiKey = url.searchParams.get('key')
  107. if (!apiKey) {
  108. return new Response('缺少API密钥', { status: 400 })
  109. }
  110.  
  111. const queryUrl = 'https://api.openai.com/dashboard/billing/subscription'
  112. const headers = {
  113. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64',
  114. 'Authorization': `Bearer ${apiKey}`,
  115. 'Accept': '*/*',
  116. 'Host': 'api.openai.com',
  117. 'Connection': 'keep-alive'
  118. }
  119.  
  120. try {
  121. const response = await fetch(queryUrl, { headers })
  122. const data = await response.json()
  123. const formatDate = (d) =>
  124. `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d
  125. .getDate()
  126. .toString()
  127. .padStart(2, '0')}`;
  128. const startDate = formatDate(new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1));
  129. const endDate = formatDate(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1));
  130.  
  131. const usageResponse = await fetch(`https://api.openai.com/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`, { headers })
  132. const usageData = await usageResponse.json()
  133.  
  134. const used = usageData.total_usage ? Math.round(usageData.total_usage) / 100 : 0
  135. const subscription = data.hard_limit_usd ? Math.round(data.hard_limit_usd * 100) / 100 : 0
  136. data.used = used
  137. data.subscription = subscription
  138. const remaining = subscription - used;
  139. data.remaining = remaining;
  140.  
  141. return new Response(JSON.stringify(data, null, 2), { status: 200 })
  142. } catch (err) {
  143. if (data.error) {
  144. return new Response(JSON.stringify({ error: '出错了' }), { status: 500 })
  145. }
  146.  
  147. }
  148. }
  149.  
  150. return new Response(html, {
  151. headers: {
  152. 'content-type': 'text/html;charset=UTF-8',
  153. },
  154. })
  155. }
  156.  
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement