devices.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. {% extends "base.html" %}
  2. {% block content %}
  3. <section class="device-management">
  4. <h2>解密机配置</h2>
  5. {% if true %}
  6. <script>
  7. console.log('{{ success }}');
  8. console.log('{{ message }}');
  9. </script>
  10. {% endif %}
  11. <div class="batch-actions">
  12. <button id="batch-restart" class="batch-btn">批量重启</button>
  13. <button id="batch-upgrade" class="batch-btn">批量升级</button>
  14. <button id="batch-config" class="batch-btn">批量配置</button>
  15. </div>
  16. <form id="device-form" method="POST">
  17. <table class="data-table">
  18. <thead>
  19. <tr>
  20. <th><input type="checkbox" id="select-all"></th>
  21. <th>设备类型</th>
  22. <th>设备SN</th>
  23. <th>固件版本</th>
  24. <th>升级状态</th>
  25. <th>平台</th>
  26. <th>在线状态</th>
  27. <th>操作</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. {% for device in devices %}
  32. <tr>
  33. <td><input type="checkbox" name="dev_sn" value="{{ device.dev_sn }}"></td>
  34. <td>{{ device.dev_type }}</td>
  35. <td>{{ device.dev_sn }}</td>
  36. <td>{{ device.app_version }}</td>
  37. <td>{{ device.upgrade_status if device.upgrade_status is defined else '未知' }}</td>
  38. <td>{{ device.cloud_platform }}</td>
  39. <td style="color: {% if device.online == '在线' %}green{% else %}red{% endif %};">{{ device.online }}</td>
  40. <td>
  41. <a href="{{ url_for('devices', dev_sn=device.dev_sn) }}" class="config-btn">配置</a>
  42. <a href="{{ url_for('device_detail', dev_sn=device.dev_sn) }}" class="detail-btn">详情</a>
  43. </td>
  44. </tr>
  45. {% endfor %}
  46. </tbody>
  47. </table>
  48. </form>
  49. </section>
  50. {% endblock %}
  51. {% block scripts %}
  52. <script>
  53. // 全选功能
  54. const selectAll = document.getElementById('select-all');
  55. const checkboxes = document.querySelectorAll('input[name="dev_sn"]');
  56. selectAll.addEventListener('change', () => {
  57. checkboxes.forEach(checkbox => {
  58. checkbox.checked = selectAll.checked;
  59. });
  60. });
  61. // 批量重启
  62. document.getElementById('batch-restart').addEventListener('click', () => {
  63. const selectedCount = document.querySelectorAll('input[name="dev_sn"]:checked').length;
  64. if (selectedCount === 0) {
  65. alert('请至少选择一个设备');
  66. return;
  67. }
  68. if (confirm('确定要批量重启选中设备吗?')) {
  69. const form = document.getElementById('device-form');
  70. form.action = "{{ url_for('devices') }}";
  71. form.method = "POST";
  72. const input = document.createElement('input');
  73. input.type = 'hidden';
  74. input.name = 'action';
  75. input.value = 'restart';
  76. form.appendChild(input);
  77. form.submit();
  78. }
  79. });
  80. // 批量升级
  81. document.getElementById('batch-upgrade').addEventListener('click', () => {
  82. const selectedCount = document.querySelectorAll('input[name="dev_sn"]:checked').length;
  83. if (selectedCount === 0) {
  84. alert('请至少选择一个设备');
  85. return;
  86. }
  87. // 创建固件选择弹窗
  88. const modal = document.createElement('div');
  89. modal.className = 'modal';
  90. modal.innerHTML = `
  91. <div class="modal-content">
  92. <span class="close-btn">&times;</span>
  93. <h3>选择固件包</h3>
  94. <form id="firmware-select-form">
  95. <input type="hidden" name="action" value="upgrade">
  96. <div class="firmware-table-container">
  97. <table class="data-table">
  98. <thead>
  99. <tr>
  100. <th>选择</th>
  101. <th>文件名</th>
  102. <th>上传时间</th>
  103. <th>备注</th>
  104. </tr>
  105. </thead>
  106. <tbody id="firmware-list">
  107. <!-- 固件数据将通过JS加载 -->
  108. </tbody>
  109. </table>
  110. </div>
  111. <button type="submit" class="confirm-btn">确认升级</button>
  112. </form>
  113. </div>
  114. `;
  115. document.body.appendChild(modal);
  116. modal.style.display = 'block';
  117. // 加载固件列表
  118. fetch('{{ url_for('get_firmwares') }}')
  119. .then(response => response.json())
  120. .then(firmwares => {
  121. const firmwareList = document.getElementById('firmware-list');
  122. firmwares.forEach(firmware => {
  123. const row = document.createElement('tr');
  124. row.innerHTML = `
  125. <td><input type="radio" name="firmware_id" value="${firmware.id}" required></td>
  126. <td>${firmware.filename}</td>
  127. <td>${firmware.upload_time}</td>
  128. <td>${firmware.remark}</td>
  129. `;
  130. firmwareList.appendChild(row);
  131. });
  132. });
  133. // 关闭弹窗
  134. modal.querySelector('.close-btn').addEventListener('click', () => {
  135. modal.style.display = 'none';
  136. document.body.removeChild(modal);
  137. });
  138. // 提交表单
  139. document.getElementById('firmware-select-form').addEventListener('submit', (e) => {
  140. e.preventDefault();
  141. // 获取选中的固件ID
  142. const selectedFirmware = document.querySelector('input[name="firmware_id"]:checked');
  143. if (!selectedFirmware) {
  144. alert('请选择一个固件包');
  145. return;
  146. }
  147. const firmwareId = selectedFirmware.value;
  148. // 创建表单数据
  149. const formData = new FormData(document.getElementById('device-form'));
  150. formData.append('action', 'upgrade');
  151. formData.append('firmware_id', firmwareId);
  152. // 发送数据到后端
  153. fetch('{{ url_for('devices') }}', {
  154. method: 'POST',
  155. body: formData
  156. }).then(response => {
  157. if (response.ok) {
  158. window.location.reload();
  159. }
  160. });
  161. });
  162. });
  163. // 批量配置
  164. document.getElementById('batch-config').addEventListener('click', () => {
  165. const selectedCount = document.querySelectorAll('input[name="dev_sn"]:checked').length;
  166. if (selectedCount === 0) {
  167. alert('请至少选择一个设备');
  168. return;
  169. }
  170. if (confirm('确定要批量配置选中设备吗?')) {
  171. const form = document.getElementById('device-form');
  172. form.action = "{{ url_for('devices') }}";
  173. form.method = "POST";
  174. const input = document.createElement('input');
  175. input.type = 'hidden';
  176. input.name = 'action';
  177. input.value = 'config';
  178. form.appendChild(input);
  179. form.submit();
  180. }
  181. });
  182. // 每隔1秒钟更新device-form部分内容
  183. /*
  184. setInterval(function() {
  185. // 使用fetch异步获取表单内容
  186. fetch(window.location.href, {
  187. method: 'GET',
  188. headers: {
  189. 'X-Requested-With': 'XMLHttpRequest'
  190. }
  191. })
  192. .then(response => response.text())
  193. .then(html => {
  194. // 创建临时DOM元素来解析获取的HTML
  195. const tempDoc = document.createElement('div');
  196. tempDoc.innerHTML = html;
  197. // 获取新的表单内容
  198. const newFormContent = tempDoc.querySelector('#device-form').innerHTML;
  199. // 更新当前页面的表单内容
  200. document.querySelector('#device-form').innerHTML = newFormContent;
  201. })
  202. .catch(error => {
  203. console.error('更新表单内容失败:', error);
  204. });
  205. }, 1000);
  206. */
  207. </script>
  208. {% endblock %}