index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <div class="setting-container">
  3. <div class="setting-card">
  4. <div class="card-header">
  5. <h2 class="title">{{t('network_settings.title')}}</h2>
  6. <p class="subtitle">{{t('network_settings.description')}}</p>
  7. </div>
  8. <div class="content">
  9. <el-form
  10. ref="settingFormRef"
  11. :hide-required-asterisk="true"
  12. label-position="left"
  13. label-width="125px"
  14. :model="settingFormData"
  15. :rules="settingFormRules"
  16. @keyup.enter="handleSave"
  17. >
  18. <!-- 自动获取DHCP -->
  19. <div class="dhcp-section">
  20. <div class="dhcp-header">
  21. <span class="dhcp-label">{{t('network_settings.dhcp')}}</span>
  22. <el-switch
  23. v-model="settingFormData.enableDHCP"
  24. :active-value="1"
  25. :inactive-value="0"
  26. active-color="#409eff"
  27. inactive-color="#dcdfe4"
  28. @change="switchChange($event)"
  29. />
  30. </div>
  31. <p class="dhcp-tip">{{t('network_settings.dhcp_tip')}}</p>
  32. </div>
  33. <!-- IP版本 -->
  34. <el-form-item :label="t('network_settings.ip_version')" prop="IP_version" class="form-item-custom">
  35. <el-input v-model="IP_version" disabled></el-input>
  36. </el-form-item>
  37. <!-- IP地址 -->
  38. <el-form-item :label="t('network_settings.ip_address')" prop="IP" class="form-item-custom">
  39. <IPInputBox
  40. :disabled="settingFormData.enableDHCP"
  41. :ip-val="settingFormData.ipAddress"
  42. @update:ip-val="(updateVal) => (settingFormData.ipAddress = updateVal)"
  43. />
  44. </el-form-item>
  45. <!-- 子网掩码 -->
  46. <el-form-item :label="t('network_settings.subnet_mask')" prop="mask" class="form-item-custom">
  47. <IPInputBox
  48. :disabled="settingFormData.enableDHCP"
  49. :ip-val="settingFormData.subNetAddress"
  50. @update:ip-val="(updateVal) => (settingFormData.subNetAddress = updateVal)"
  51. />
  52. </el-form-item>
  53. <!-- 默认网关 -->
  54. <el-form-item :label="t('network_settings.gateway')" prop="gateway" class="form-item-custom">
  55. <IPInputBox
  56. :disabled="settingFormData.enableDHCP"
  57. :ip-val="settingFormData.gateWayAddress"
  58. @update:ip-val="(updateVal) => (settingFormData.gateWayAddress = updateVal)"
  59. />
  60. </el-form-item>
  61. <!-- 物理地址 -->
  62. <el-form-item :label="t('network_settings.mac_address')" class="form-item-custom">
  63. <el-input
  64. v-model="settingFormData.deviceMac"
  65. disabled
  66. class="mac-input"
  67. />
  68. </el-form-item>
  69. <!-- 操作按钮 -->
  70. <div class="button-group">
  71. <el-button
  72. :loading="loading"
  73. size="large"
  74. type="primary"
  75. @click="handleSave()"
  76. class="save-button"
  77. >
  78. <span v-if="!loading">{{t('common.save')}}</span>
  79. <span v-else>Saving...</span>
  80. </el-button>
  81. </div>
  82. </el-form>
  83. </div>
  84. </div>
  85. </div>
  86. </template>
  87. <script lang="ts" setup>
  88. import {
  89. type FormInstance,
  90. type FormRules,
  91. ElMessage,
  92. ElMessageBox
  93. } from 'element-plus'
  94. import { getUserSettingApi, putUserSettingApi } from '@/api/setting'
  95. import { type UpdateSettingRequestData } from '@/api/types/setting'
  96. import IPInputBox from './components/IPInputBox.vue'
  97. import {useI18n} from "vue-i18n";
  98. const { t } = useI18n();
  99. const IP_version = ref('IPV4')
  100. const options = [
  101. {
  102. value: 'IPV4',
  103. label: 'IPV4'
  104. }
  105. ]
  106. /** 按钮 Loading */
  107. const loading = ref(false)
  108. /** 表单数据 */
  109. const settingFormData: UpdateSettingRequestData = reactive({
  110. NIC: 1,
  111. enableDHCP: 0,
  112. ipAddress: '',
  113. subNetAddress: '',
  114. gateWayAddress: '',
  115. deviceMac: ''
  116. })
  117. /** 表单元素的引用 */
  118. const settingFormRef = ref<FormInstance | null>(null)
  119. /** 表单校验规则 */
  120. const settingFormRules: FormRules = {
  121. IP: [
  122. {
  123. validator: (rule, value, callback) => {
  124. if (settingFormData.ipAddress) {
  125. callback()
  126. } else {
  127. callback(new Error('Please enter the IP'))
  128. }
  129. },
  130. trigger: 'blur'
  131. }
  132. ],
  133. mask: [
  134. {
  135. validator: (rule, value, callback) => {
  136. if (settingFormData.subNetAddress) {
  137. callback()
  138. } else {
  139. callback(new Error('Please enter the subnet mask'))
  140. }
  141. },
  142. trigger: 'blur'
  143. }
  144. ],
  145. gateway: [
  146. {
  147. validator: (rule, value, callback) => {
  148. if (settingFormData.gateWayAddress) {
  149. callback()
  150. } else {
  151. callback(new Error('Please enter the default gateway'))
  152. }
  153. },
  154. trigger: 'blur'
  155. }
  156. ]
  157. }
  158. const switchChange = ($event: number) => {
  159. if ($event === 1) {
  160. settingFormRef.value?.clearValidate()
  161. }
  162. }
  163. const param = {
  164. NIC: 1
  165. }
  166. /** 获取表单数据 */
  167. const fetchData = () => {
  168. getUserSettingApi(param.NIC).then((res) => {
  169. settingFormData.enableDHCP = res.data.enableDHCP
  170. settingFormData.ipAddress = res.data.ipAddress
  171. settingFormData.subNetAddress = res.data.subNetAddress
  172. settingFormData.gateWayAddress = res.data.gateWayAddress
  173. settingFormData.deviceMac = res.data.deviceMac
  174. settingFormData.firstDNSAddress = res.data.firstDNSAddress
  175. settingFormData.secondDNSAddress = res.data.secondDNSAddress
  176. })
  177. }
  178. fetchData()
  179. /** 保存逻辑 */
  180. const handleSave = () => {
  181. settingFormRef.value?.validate((valid: boolean, fields) => {
  182. if (valid) {
  183. ElMessageBox.confirm(t('popup.network_tips'), t('popup.note'), {
  184. confirmButtonText: t('popup.confirm'),
  185. cancelButtonText: t('popup.cancel'),
  186. type: 'warning',
  187. confirmButtonClass: 'el-button--danger'
  188. }).then(() => {
  189. loading.value = true
  190. putUserSettingApi(param.NIC, settingFormData)
  191. .then(() => {
  192. ElMessage.success('Operation successful')
  193. })
  194. .finally(() => {
  195. loading.value = false
  196. })
  197. })
  198. }
  199. })
  200. }
  201. </script>
  202. <style lang="scss" scoped>
  203. .setting-container {
  204. padding: 20px;
  205. //background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  206. min-height: 85vh;
  207. display: flex;
  208. //align-items: flex-start;
  209. justify-content: center;
  210. align-items: center;
  211. }
  212. .setting-card {
  213. width: 100%;
  214. max-width: 510px;
  215. //background: white;
  216. border-radius: 12px;
  217. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  218. overflow: hidden;
  219. transition: all 0.3s ease;
  220. &:hover {
  221. box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  222. }
  223. }
  224. .card-header {
  225. //background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  226. color: #000104;
  227. padding: 20px 20px 0 20px;
  228. text-align: center;
  229. .title {
  230. margin: 0;
  231. font-size: 24px;
  232. font-weight: 600;
  233. margin-bottom: 8px;
  234. }
  235. .subtitle {
  236. margin: 0;
  237. font-size: 14px;
  238. opacity: 0.9;
  239. }
  240. }
  241. .content {
  242. padding: 30px;
  243. }
  244. .dhcp-section {
  245. background: #f0f9ff;
  246. border-left: 4px solid #409eff;
  247. padding: 16px;
  248. border-radius: 6px;
  249. margin-bottom: 24px;
  250. .dhcp-header {
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. margin-bottom: 8px;
  255. .dhcp-label {
  256. font-size: 14px;
  257. font-weight: 500;
  258. color: #333;
  259. }
  260. }
  261. .dhcp-tip {
  262. margin: 0;
  263. font-size: 12px;
  264. color: #909399;
  265. }
  266. }
  267. .form-item-custom {
  268. margin-bottom: 20px;
  269. :deep(.el-form-item__label) {
  270. color: #606266;
  271. font-weight: 500;
  272. padding-right: 12px;
  273. }
  274. }
  275. .mac-input {
  276. :deep(.el-input__wrapper) {
  277. background-color: #f5f7fa !important;
  278. }
  279. }
  280. .button-group {
  281. display: flex;
  282. gap: 12px;
  283. margin-top: 32px;
  284. padding-top: 20px;
  285. border-top: 1px solid #ebeef5;
  286. }
  287. .save-button {
  288. flex: 1;
  289. height: 40px;
  290. font-size: 16px;
  291. font-weight: 500;
  292. border-radius: 6px;
  293. transition: all 0.3s ease;
  294. &:hover {
  295. transform: translateY(-2px);
  296. box-shadow: 0 6px 16px rgba(64, 158, 255, 0.4);
  297. }
  298. &:active {
  299. transform: translateY(0);
  300. }
  301. }
  302. // 响应式设计
  303. @media (max-width: 600px) {
  304. .setting-container {
  305. padding: 12px;
  306. }
  307. .setting-card {
  308. max-width: 100%;
  309. }
  310. .content {
  311. padding: 20px;
  312. }
  313. .card-header {
  314. padding: 20px 16px;
  315. .title {
  316. font-size: 20px;
  317. }
  318. .subtitle {
  319. font-size: 12px;
  320. }
  321. }
  322. }
  323. // 深色主题支持(可选)
  324. @media (prefers-color-scheme: dark) {
  325. .setting-card {
  326. background: #1e1e1e;
  327. }
  328. .content {
  329. color: #e0e0e0;
  330. }
  331. .form-item-custom {
  332. :deep(.el-form-item__label) {
  333. color: #b0b0b0;
  334. }
  335. :deep(.el-input__wrapper) {
  336. background-color: #2a2a2a;
  337. border-color: #404040;
  338. }
  339. }
  340. }
  341. </style>