ServerForm.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="名称" prop="name">
  11. <el-input v-model="formData.name" placeholder="请输入名称" />
  12. </el-form-item>
  13. <el-form-item label="类型" prop="type">
  14. <el-select v-model="formData.type" placeholder="请选择类型">
  15. <el-option
  16. v-for="dict in getStrDictOptions(DICT_TYPE.SAFE_DEVICE_TYPE)"
  17. :key="dict.value"
  18. :label="dict.label"
  19. :value="dict.value"
  20. />
  21. </el-select>
  22. </el-form-item>
  23. <el-form-item label="品牌" prop="brand">
  24. <el-input v-model="formData.brand" placeholder="请输入品牌" />
  25. </el-form-item>
  26. <el-form-item label="型号" prop="model">
  27. <el-input v-model="formData.model" placeholder="请输入型号" />
  28. </el-form-item>
  29. <el-form-item label="所属机构" prop="orgId">
  30. <el-tree-select
  31. v-model="formData.orgId"
  32. :data="deptList"
  33. :props="defaultProps"
  34. check-strictly
  35. node-key="id"
  36. placeholder="请选择归属机构"
  37. />
  38. </el-form-item>
  39. <el-form-item label="接入协议" prop="protocol">
  40. <el-select v-model="formData.protocol" placeholder="请选择接入协议">
  41. <el-option
  42. v-for="dict in getStrDictOptions(DICT_TYPE.SAFE_DEVICE_PROTOCOL)"
  43. :key="dict.value"
  44. :label="dict.label"
  45. :value="dict.value"
  46. />
  47. </el-select>
  48. </el-form-item>
  49. <el-form-item label="SRS地址" prop="ip">
  50. <el-input v-model="formData.ip" placeholder="请输入SRS地址" />
  51. </el-form-item>
  52. <el-form-item label="SRS端口" prop="port">
  53. <el-input v-model="formData.port" placeholder="请输入SRS端口" />
  54. </el-form-item>
  55. <el-form-item label="SRS服务域" prop="domain">
  56. <el-input v-model="formData.domain" placeholder="请输入SRS服务域" />
  57. </el-form-item>
  58. <el-form-item label="用户名" prop="username">
  59. <el-input v-model="formData.username" placeholder="请输入用户名" />
  60. </el-form-item>
  61. <el-form-item label="密码" prop="password">
  62. <el-input type="password" v-model="formData.password" placeholder="请输入密码" show-password/>
  63. </el-form-item>
  64. <el-form-item label="描述" prop="des">
  65. <el-input v-model="formData.des" type="textarea" placeholder="请输入描述" />
  66. </el-form-item>
  67. </el-form>
  68. <template #footer>
  69. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  70. <el-button @click="dialogVisible = false">取 消</el-button>
  71. </template>
  72. </Dialog>
  73. </template>
  74. <script setup lang="ts">
  75. import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
  76. import { ServerApi, ServerVO } from '@/api/safe/server'
  77. import * as DeptApi from '@/api/system/dept'
  78. import { defaultProps, handleTree } from '@/utils/tree'
  79. /** 安防后端设备 表单 */
  80. defineOptions({ name: 'ServerForm' })
  81. const { t } = useI18n() // 国际化
  82. const message = useMessage() // 消息弹窗
  83. const deptList = ref<Tree[]>([]) // 树形结构
  84. const dialogVisible = ref(false) // 弹窗的是否展示
  85. const dialogTitle = ref('') // 弹窗的标题
  86. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  87. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  88. const formData = ref({
  89. id: undefined,
  90. name: undefined,
  91. type: undefined,
  92. brand: undefined,
  93. model: undefined,
  94. orgId: undefined,
  95. protocol: undefined,
  96. ip: undefined,
  97. port: undefined,
  98. domain: undefined,
  99. username: undefined,
  100. password: undefined,
  101. des: undefined,
  102. })
  103. const formRules = reactive({
  104. name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  105. protocol: [{ required: true, message: '接入协议不能为空', trigger: 'change' }],
  106. ip: [{ required: true, message: 'SRS地址不能为空', trigger: 'blur' }],
  107. port: [{ required: true, message: 'SRS端口不能为空', trigger: 'blur' }],
  108. domain: [{ required: true, message: 'SRS服务域不能为空', trigger: 'blur' }],
  109. })
  110. const formRef = ref() // 表单 Ref
  111. /** 打开弹窗 */
  112. const open = async (type: string, id?: number) => {
  113. dialogVisible.value = true
  114. dialogTitle.value = t('action.' + type)
  115. formType.value = type
  116. resetForm()
  117. // 修改时,设置数据
  118. if (id) {
  119. formLoading.value = true
  120. try {
  121. formData.value = await ServerApi.getServer(id)
  122. } finally {
  123. formLoading.value = false
  124. }
  125. }
  126. // 加载部门树
  127. deptList.value = handleTree(await DeptApi.getSimpleDeptList())
  128. }
  129. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  130. /** 提交表单 */
  131. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  132. const submitForm = async () => {
  133. // 校验表单
  134. await formRef.value.validate()
  135. // 提交请求
  136. formLoading.value = true
  137. try {
  138. const data = formData.value as unknown as ServerVO
  139. if (formType.value === 'create') {
  140. await ServerApi.createServer(data)
  141. message.success(t('common.createSuccess'))
  142. } else {
  143. await ServerApi.updateServer(data)
  144. message.success(t('common.updateSuccess'))
  145. }
  146. dialogVisible.value = false
  147. // 发送操作成功的事件
  148. emit('success')
  149. } finally {
  150. formLoading.value = false
  151. }
  152. }
  153. /** 重置表单 */
  154. const resetForm = () => {
  155. formData.value = {
  156. id: undefined,
  157. name: undefined,
  158. type: undefined,
  159. brand: undefined,
  160. model: undefined,
  161. orgId: undefined,
  162. protocol: undefined,
  163. ip: undefined,
  164. port: undefined,
  165. domain: undefined,
  166. username: undefined,
  167. password: undefined,
  168. des: undefined,
  169. }
  170. formRef.value?.resetFields()
  171. }
  172. </script>