NetworkConfig.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div class="network-config">
  3. <a-card title="系统网络配置">
  4. <!-- 网络配置表单 -->
  5. <a-form
  6. :model="networkConfig"
  7. :label-col="{ span: 6 }"
  8. :wrapper-col="{ span: 16 }"
  9. @submit.prevent="handleSubmit"
  10. >
  11. <!-- IP获取方式选择 -->
  12. <a-form-item label="IP获取方式">
  13. <a-radio-group v-model:value="networkConfig.ipMethod" @change="onIpMethodChange">
  14. <a-radio value="dhcp">DHCP自动获取</a-radio>
  15. <a-radio value="static">静态IP配置</a-radio>
  16. </a-radio-group>
  17. </a-form-item>
  18. <!-- 自动获取的IP信息 (DHCP模式下显示) -->
  19. <template v-if="networkConfig.ipMethod === 'dhcp'">
  20. <a-form-item label="获取的IP地址">
  21. <a-input v-model:value="networkConfig.dhcpIp" disabled placeholder="自动获取的IP地址" />
  22. </a-form-item>
  23. <a-form-item label="获取的子网掩码">
  24. <a-input v-model:value="networkConfig.dhcpSubnetMask" disabled placeholder="自动获取的子网掩码" />
  25. </a-form-item>
  26. <a-form-item label="获取的网关">
  27. <a-input v-model:value="networkConfig.dhcpGateway" disabled placeholder="自动获取的网关" />
  28. </a-form-item>
  29. </template>
  30. <!-- 静态IP配置 (静态IP模式下显示) -->
  31. <template v-if="networkConfig.ipMethod === 'static'">
  32. <a-form-item label="IP地址" name="staticIp" :rules="[{ required: true, message: '请输入IP地址' }]">
  33. <a-input v-model:value="networkConfig.staticIp" placeholder="请输入IP地址" />
  34. </a-form-item>
  35. <a-form-item label="子网掩码" name="staticSubnetMask" :rules="[{ required: true, message: '请输入子网掩码' }]">
  36. <a-input v-model:value="networkConfig.staticSubnetMask" placeholder="请输入子网掩码" />
  37. </a-form-item>
  38. <a-form-item label="默认网关" name="staticGateway" :rules="[{ required: true, message: '请输入默认网关' }]">
  39. <a-input v-model:value="networkConfig.staticGateway" placeholder="请输入默认网关" />
  40. </a-form-item>
  41. </template>
  42. <!-- DNS配置 (两种模式都可配置) -->
  43. <a-form-item label="首选DNS服务器">
  44. <a-input v-model:value="networkConfig.primaryDns" placeholder="请输入首选DNS服务器" />
  45. </a-form-item>
  46. <a-form-item label="备用DNS服务器">
  47. <a-input v-model:value="networkConfig.secondaryDns" placeholder="请输入备用DNS服务器" />
  48. </a-form-item>
  49. <!-- 操作按钮 -->
  50. <a-form-item :wrapper-col="{ offset: 6, span: 16 }">
  51. <a-button type="primary" html-type="submit" :loading="submitting">
  52. 保存配置
  53. </a-button>
  54. <a-button style="margin-left: 8px" @click="handleReset">
  55. 重置
  56. </a-button>
  57. <a-button style="margin-left: 8px" type="default" @click="refreshConfig">
  58. 刷新配置
  59. </a-button>
  60. </a-form-item>
  61. </a-form>
  62. <!-- 网络状态信息 -->
  63. <a-divider>网络状态</a-divider>
  64. <a-descriptions column="1" bordered>
  65. <a-descriptions-item label="网络接口状态">
  66. <a-tag :color="networkStatus.connected ? 'green' : 'red'">
  67. {{ networkStatus.connected ? '已连接' : '未连接' }}
  68. </a-tag>
  69. </a-descriptions-item>
  70. <a-descriptions-item label="MAC地址">{{ networkStatus.macAddress || '未知' }}</a-descriptions-item>
  71. <a-descriptions-item label="网络接口">{{ networkStatus.interfaceName || '未知' }}</a-descriptions-item>
  72. <a-descriptions-item label="连接速度">{{ networkStatus.speed || '未知' }}</a-descriptions-item>
  73. <a-descriptions-item label="配置应用状态">
  74. <a-tag :color="configStatus === 'applied' ? 'green' : configStatus === 'pending' ? 'orange' : 'default'">
  75. {{ configStatusText }}
  76. </a-tag>
  77. </a-descriptions-item>
  78. </a-descriptions>
  79. <!-- 重启提示 -->
  80. <a-alert
  81. v-if="configStatus === 'pending'"
  82. message="配置已保存,需要重启网络服务才能生效"
  83. type="warning"
  84. show-icon
  85. action
  86. >
  87. <template #action>
  88. <a-button type="primary" size="small" @click="restartNetwork">
  89. 立即重启
  90. </a-button>
  91. </template>
  92. </a-alert>
  93. </a-card>
  94. </div>
  95. </template>
  96. <script setup>
  97. import { ref, reactive, onMounted, computed } from 'vue'
  98. import { message } from 'ant-design-vue'
  99. import { getNetworkConfig, updateNetworkConfig, restartNetworkService, getNetworkStatus } from '../api/network'
  100. // 网络配置数据
  101. const networkConfig = reactive({
  102. ipMethod: 'dhcp', // 'dhcp' 或 'static'
  103. // DHCP模式下获取的信息
  104. dhcpIp: '',
  105. dhcpSubnetMask: '',
  106. dhcpGateway: '',
  107. // 静态IP配置
  108. staticIp: '',
  109. staticSubnetMask: '',
  110. staticGateway: '',
  111. // DNS配置
  112. primaryDns: '',
  113. secondaryDns: ''
  114. })
  115. // 网络状态信息
  116. const networkStatus = reactive({
  117. connected: false,
  118. macAddress: '',
  119. interfaceName: '',
  120. speed: ''
  121. })
  122. // 配置状态
  123. const configStatus = ref('applied') // 'applied', 'pending', 'error'
  124. const submitting = ref(false)
  125. // 配置状态文本
  126. const configStatusText = computed(() => {
  127. switch (configStatus.value) {
  128. case 'applied':
  129. return '已应用'
  130. case 'pending':
  131. return '待应用'
  132. case 'error':
  133. return '应用失败'
  134. default:
  135. return '未知'
  136. }
  137. })
  138. // IP获取方式改变处理
  139. const onIpMethodChange = () => {
  140. // 重置表单校验状态
  141. // 如果需要,可以在这里添加一些额外的处理逻辑
  142. }
  143. // 提交表单处理
  144. const handleSubmit = async () => {
  145. submitting.value = true
  146. try {
  147. // 构建符合后端要求的数据结构
  148. const configData = {
  149. config_mode: networkConfig.ipMethod,
  150. interface: 'eth0', // 默认接口
  151. static_config: {
  152. ip_address: networkConfig.staticIp,
  153. subnet_mask: networkConfig.staticSubnetMask,
  154. gateway: networkConfig.staticGateway,
  155. dns_servers: [
  156. networkConfig.primaryDns || '8.8.8.8',
  157. networkConfig.secondaryDns || '8.8.4.4'
  158. ].filter(Boolean)
  159. }
  160. }
  161. const response = await updateNetworkConfig(configData)
  162. if (response && response.success) {
  163. message.success('网络配置保存成功')
  164. configStatus.value = 'pending' // 标记为待应用状态
  165. } else {
  166. message.error('保存失败: ' + (response?.message || '未知错误'))
  167. configStatus.value = 'error'
  168. }
  169. } catch (error) {
  170. console.error('保存网络配置失败:', error)
  171. message.error('保存失败,请检查网络连接')
  172. configStatus.value = 'error'
  173. } finally {
  174. submitting.value = false
  175. }
  176. }
  177. // 重置表单
  178. const handleReset = () => {
  179. // 重置为当前获取的配置
  180. fetchNetworkConfig()
  181. message.success('已重置为当前配置')
  182. }
  183. // 刷新配置
  184. const refreshConfig = () => {
  185. fetchNetworkConfig()
  186. fetchNetworkStatus()
  187. message.success('配置已刷新')
  188. }
  189. // 重启网络服务
  190. const restartNetwork = async () => {
  191. try {
  192. const response = await restartNetworkService()
  193. if (response.success) {
  194. message.success('网络服务正在重启,请等待...')
  195. // 重启后检查状态
  196. setTimeout(() => {
  197. fetchNetworkConfig()
  198. fetchNetworkStatus()
  199. configStatus.value = 'applied'
  200. }, 5001)
  201. } else {
  202. message.error('重启失败: ' + (response.message || '未知错误'))
  203. }
  204. } catch (error) {
  205. console.error('重启网络服务失败:', error)
  206. message.error('重启失败,请检查系统权限')
  207. }
  208. }
  209. // 获取网络配置
  210. const fetchNetworkConfig = async () => {
  211. try {
  212. const response = await getNetworkConfig()
  213. if (response && response.success) {
  214. // 根据后端返回的数据结构更新配置
  215. // 配置模式
  216. networkConfig.ipMethod = response.config_mode || 'dhcp'
  217. // 静态配置信息
  218. if (response.static_config) {
  219. networkConfig.staticIp = response.static_config.ip_address || ''
  220. networkConfig.staticSubnetMask = response.static_config.subnet_mask || ''
  221. networkConfig.staticGateway = response.static_config.gateway || ''
  222. // DNS配置
  223. if (response.static_config.dns_servers && Array.isArray(response.static_config.dns_servers)) {
  224. networkConfig.primaryDns = response.static_config.dns_servers[0] || ''
  225. networkConfig.secondaryDns = response.static_config.dns_servers[1] || ''
  226. }
  227. }
  228. // 如果是DHCP模式,尝试从静态配置中获取当前IP信息(开发环境)
  229. if (networkConfig.ipMethod === 'dhcp' && response.static_config) {
  230. networkConfig.dhcpIp = response.static_config.ip_address || ''
  231. networkConfig.dhcpSubnetMask = response.static_config.subnet_mask || ''
  232. networkConfig.dhcpGateway = response.static_config.gateway || ''
  233. }
  234. } else {
  235. message.error('获取配置失败: ' + (response?.message || '未知错误'))
  236. // 即使失败也设置一些默认值,避免界面空白
  237. setDefaultConfig()
  238. }
  239. } catch (error) {
  240. console.error('获取网络配置失败:', error)
  241. message.error('获取配置时发生错误')
  242. // 设置默认值
  243. setDefaultConfig()
  244. }
  245. }
  246. // 获取网络状态
  247. const fetchNetworkStatus = async () => {
  248. try {
  249. const response = await getNetworkStatus()
  250. if (response && response.success) {
  251. const ifs = response.interfaces || {}
  252. const activeIface = Object.entries(ifs).find(([name, v]) =>
  253. v.status === 'UP'
  254. && !name.startsWith('docker') && !name.startsWith('br-') && !name.startsWith('veth') && name !== 'lo'
  255. && v.ip_addresses?.some(ip => !ip.startsWith('127.'))
  256. )
  257. const iface = activeIface ? activeIface[1] : {}
  258. const ifaceName = activeIface ? activeIface[0] : '未知'
  259. networkStatus.connected = !!activeIface
  260. networkStatus.macAddress = iface.mac_address || iface.macAddress || '未知'
  261. networkStatus.interfaceName = ifaceName
  262. networkStatus.speed = '100Mbps'
  263. } else {
  264. message.error('获取网络状态失败: ' + (response?.message || '未知错误'))
  265. setDefaultStatus()
  266. }
  267. } catch (error) {
  268. console.error('获取网络状态失败:', error)
  269. message.error('获取网络状态时发生错误')
  270. setDefaultStatus()
  271. }
  272. }
  273. // 设置默认配置
  274. const setDefaultConfig = () => {
  275. networkConfig.ipMethod = 'dhcp'
  276. networkConfig.dhcpIp = '192.168.1.100'
  277. networkConfig.dhcpSubnetMask = '255.255.255.0'
  278. networkConfig.dhcpGateway = '192.168.1.1'
  279. networkConfig.staticIp = '192.168.1.100'
  280. networkConfig.staticSubnetMask = '255.255.255.0'
  281. networkConfig.staticGateway = '192.168.1.1'
  282. networkConfig.primaryDns = '8.8.8.8'
  283. networkConfig.secondaryDns = '8.8.4.4'
  284. }
  285. // 设置默认状态
  286. const setDefaultStatus = () => {
  287. networkStatus.connected = true
  288. networkStatus.macAddress = '00:11:22:33:44:55'
  289. networkStatus.interfaceName = 'eth0'
  290. networkStatus.speed = '100Mbps'
  291. }
  292. // 组件挂载时获取配置和状态
  293. onMounted(() => {
  294. fetchNetworkConfig()
  295. fetchNetworkStatus()
  296. })
  297. </script>
  298. <style scoped>
  299. .network-config {
  300. width: 100%;
  301. padding: 20px;
  302. }
  303. /* 确保表单元素间距合适 */
  304. .ant-form-item {
  305. margin-bottom: 20px;
  306. }
  307. /* 确保描述信息显示清晰 */
  308. .ant-descriptions-item-label {
  309. font-weight: 600;
  310. }
  311. /* 确保警告提示显示合理 */
  312. .ant-alert {
  313. margin-top: 20px;
  314. }
  315. /* 响应式设计 */
  316. @media (max-width: 768px) {
  317. .network-config {
  318. padding: 10px;
  319. }
  320. .ant-form-horizontal .ant-form-item-label {
  321. text-align: left;
  322. }
  323. }
  324. </style>