index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Function to show logs in a modal
  2. function showLogs(userId) {
  3. fetch(`/client/${userId}`)
  4. .then(response => response.json())
  5. .then(data => {
  6. const logs = data.data.logs;
  7. const logText = logs.map(log => `[${log[0]}] -- ${log[1]}`).join('<br>');
  8. document.getElementById('logText').innerHTML = logText;
  9. document.getElementById('logsModal').style.display = 'block';
  10. })
  11. .catch(error => console.error('Error:', error));
  12. }
  13. // 更新连接数量的统计
  14. function updateOnlineCount(onlineCount, allCount) {
  15. // 获取显示连接数量的<span>元素
  16. var onlineCountElement = document.getElementById("onlineCount");
  17. var allCountElement = document.getElementById("allCount");
  18. onlineCountElement.textContent = onlineCount;
  19. allCountElement.textContent = allCount;
  20. }
  21. // 弹出对话框输入 user_id 和 proxy
  22. function addUser() {
  23. var userId = prompt("请输入UserID:");
  24. if (!userId) {
  25. return
  26. }
  27. var proxy = prompt("设置代理 (不使用代理点击取消)");
  28. if (userId) {
  29. // 构建带参数的 URL
  30. var baseUrl = '/client/';
  31. var url = new URL(baseUrl, window.location.href);
  32. url.searchParams.append('user_id', userId);
  33. if (proxy) {
  34. url.searchParams.append('proxy_url', proxy);
  35. }
  36. fetch(url.href, {
  37. method: 'POST',
  38. headers: {
  39. 'Content-Type': 'application/json'
  40. },
  41. })
  42. .then(response => {
  43. if (response.ok) {
  44. alert('账号添加成功');
  45. fetchData();
  46. } else {
  47. alert('账号添加失败');
  48. }
  49. })
  50. .catch(error => {
  51. alert('请求错误:', error);
  52. });
  53. }
  54. }
  55. function deleteUser(userId) {
  56. fetch(`/client/${userId}`, {
  57. method: 'DELETE'
  58. })
  59. .then(response => response.json())
  60. .then(data => {
  61. if (data.message === 'success') {
  62. alert('删除成功');
  63. // If deletion is successful, reload all data
  64. fetchData();
  65. }
  66. })
  67. .catch(error => console.error('Error:', error));
  68. }
  69. function deleteAllUser() {
  70. showLoading();
  71. fetch(`/client/`, {
  72. method: 'DELETE'
  73. })
  74. .then(response => response.json())
  75. .then(data => {
  76. if (data.message === 'success') {
  77. alert('删除成功');
  78. // If deletion is successful, reload all data
  79. fetchData();
  80. }
  81. hideLoading();
  82. })
  83. .catch(error => {
  84. console.error('Error:', error);
  85. hideLoading();
  86. });
  87. }
  88. function uploadFile() {
  89. const input = document.createElement('input');
  90. input.type = 'file';
  91. input.onchange = function() {
  92. const file = input.files[0];
  93. const formData = new FormData();
  94. formData.append('file', file);
  95. showLoading();
  96. fetch('/upload/', {
  97. method: 'POST',
  98. body: formData
  99. })
  100. .then(response => {
  101. if (response.ok) {
  102. alert('文件上传成功');
  103. fetchData()
  104. } else {
  105. alert('文件上传失败!');
  106. }
  107. hideLoading();
  108. })
  109. .catch(error => {
  110. console.error('Error:', error);
  111. hideLoading();
  112. });
  113. };
  114. input.click();
  115. }
  116. // Function to close the modal
  117. function closeModal() {
  118. document.getElementById('logsModal').style.display = 'none';
  119. }
  120. // 模拟数据请求的函数 fetchData,您需要根据实际情况替换为您的数据请求逻辑
  121. function fetchData() {
  122. return new Promise((resolve, reject) => {
  123. // Make a GET request to the API endpoint
  124. fetch('/client/')
  125. .then(response => response.json())
  126. .then(data => {
  127. const statusMap = {
  128. 0: '未连接',
  129. 1: '连接中',
  130. 2: '已连接',
  131. 3: '已停止'
  132. };
  133. let counter = 0; // 初始编号为1
  134. let onlineCounter = 0;
  135. const tableBody = document.querySelector('#data-table tbody');
  136. // Clear existing data in the table
  137. tableBody.innerHTML = '';
  138. data.data.forEach(item => {
  139. const row = document.createElement('tr');
  140. row.innerHTML = `
  141. <td>${counter+1}</td>
  142. <td>${item.user_id}</td>
  143. <td class="proxy">${item.proxy_url || ''}</td>
  144. <td class="status-${item.status}">${statusMap[item.status]}</td>
  145. <td>
  146. <button onclick="showLogs('${item.id}')">日志</button>
  147. <button onclick="deleteUser('${item.id}')">删除</button>
  148. </td>
  149. `;
  150. tableBody.appendChild(row);
  151. counter++;
  152. if (item.status == 2){
  153. onlineCounter++
  154. }
  155. });
  156. updateOnlineCount(onlineCounter, counter);
  157. resolve();
  158. })
  159. .catch(error => console.error('Error:', error))
  160. });
  161. }
  162. function fetchDataInterval(){
  163. fetchData().then(() => {
  164. // 数据请求完成后等待5秒再发起下一次请求
  165. setTimeout(fetchDataInterval, 5000);
  166. });
  167. }
  168. function showLoading() {
  169. document.getElementById('loading').style.display = 'block';
  170. }
  171. function hideLoading() {
  172. document.getElementById('loading').style.display = 'none';
  173. }
  174. fetchDataInterval()