addOrEditDialog.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <el-dialog
  3. :title="isEdit ? '修改设备' : '新增设备'"
  4. :visible.sync="dialogVisible"
  5. width="550px"
  6. @close="handleClose"
  7. >
  8. <el-form
  9. ref="editForm"
  10. :model="formData"
  11. :rules="formRules"
  12. label-width="100px"
  13. >
  14. <el-form-item label="设备名称" prop="deviceName">
  15. <el-input v-model="formData.deviceName" placeholder="请输入设备名称" clearable />
  16. </el-form-item>
  17. <el-form-item label="设备IP地址" prop="ipAddress">
  18. <el-input v-model="formData.ipAddress" placeholder="请输入设备IP地址" clearable />
  19. </el-form-item>
  20. <el-form-item label="设备端口号" prop="port">
  21. <el-input v-model="formData.port" placeholder="请输入设备端口号" clearable />
  22. </el-form-item>
  23. <el-form-item label="设备型号" prop="model">
  24. <el-input v-model="formData.model" placeholder="请输入设备型号" clearable />
  25. </el-form-item>
  26. <el-form-item label="账号" prop="account">
  27. <el-input v-model="formData.account" placeholder="请输入账号" clearable />
  28. </el-form-item>
  29. <el-form-item label="密码" prop="password">
  30. <el-input v-model="formData.password" placeholder="请输入密码" clearable show-password />
  31. </el-form-item>
  32. <el-form-item label="门数量" prop="doorCount">
  33. <el-input-number v-model="formData.doorCount" :min="0" :max="999" placeholder="请输入门数量" />
  34. </el-form-item>
  35. <el-form-item label="流名称" prop="stream">
  36. <el-input v-model="formData.stream" placeholder="请输入流名称" clearable />
  37. </el-form-item>
  38. <!-- <el-form-item label="启用禁用" prop="enable">
  39. <el-radio-group v-model="formData.enable">
  40. <el-radio :label="0">启用</el-radio>
  41. <el-radio :label="1">禁用</el-radio>
  42. </el-radio-group>
  43. </el-form-item> -->
  44. </el-form>
  45. <span slot="footer" class="dialog-footer">
  46. <el-button @click="dialogVisible = false">取 消</el-button>
  47. <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确 定</el-button>
  48. </span>
  49. </el-dialog>
  50. </template>
  51. <script>
  52. import { getDeviceDetail, saveDevice } from '@/api/‌doorSecurity‌';
  53. export default {
  54. props: {
  55. visible: {
  56. type: Boolean,
  57. default: false
  58. },
  59. // 编辑时的行数据
  60. data: {
  61. type: Object,
  62. default: null
  63. }
  64. },
  65. data() {
  66. return {
  67. isEdit: false,
  68. submitLoading: false,
  69. formData: {
  70. id: null,
  71. deviceName: '',
  72. ipAddress: '',
  73. port: '',
  74. model: '',
  75. account: '',
  76. password: '',
  77. doorCount: 0,
  78. stream: '',
  79. enable: 'true'
  80. },
  81. formRules: {
  82. deviceName: [
  83. { required: true, message: '请输入设备名称', trigger: 'blur' }
  84. ],
  85. ipAddress: [
  86. { required: true, message: '请输入设备IP地址', trigger: 'blur' }
  87. ],
  88. port: [
  89. { required: true, message: '请输入设备端口号', trigger: 'blur' }
  90. ],
  91. model: [
  92. { required: true, message: '请输入设备型号', trigger: 'blur' }
  93. ],
  94. account: [
  95. { required: true, message: '请输入账号', trigger: 'blur' }
  96. ],
  97. password: [
  98. { required: true, message: '请输入密码', trigger: 'blur' }
  99. ]
  100. }
  101. };
  102. },
  103. computed: {
  104. dialogVisible: {
  105. get() {
  106. return this.visible;
  107. },
  108. set(val) {
  109. this.$emit('update:visible', val);
  110. }
  111. }
  112. },
  113. watch: {
  114. visible(val) {
  115. if (val) {
  116. // 确保 prop data 已经传入后再初始化表单
  117. this.$nextTick(() => {
  118. this.initForm();
  119. });
  120. }
  121. }
  122. },
  123. methods: {
  124. /* 初始化表单数据 */
  125. async initForm() {
  126. console.log('data~~~', this.data);
  127. const row = this.data;
  128. this.isEdit = !!row?.id;
  129. if (row?.id) {
  130. // this.formData = {
  131. // id: row.id,
  132. // deviceName: row.deviceName || '',
  133. // ipAddress: row.ipAddress || '',
  134. // port: row.port || '',
  135. // model: row.model || '',
  136. // account: row.account || '',
  137. // password: row.password || '',
  138. // doorCount: row.doorCount ?? 0,
  139. // stream: row.stream || '',
  140. // enable: row.enable || 'true'
  141. // };
  142. const res = await getDeviceDetail(row.id);
  143. console.log(res, 'res');
  144. this.formData = res;
  145. } else {
  146. this.formData = {
  147. id: null,
  148. deviceName: '',
  149. ipAddress: '',
  150. port: '',
  151. model: '',
  152. account: '',
  153. password: '',
  154. doorCount: 0,
  155. stream: '',
  156. enable: 'true'
  157. };
  158. }
  159. this.$nextTick(() => {
  160. this.$refs.editForm?.clearValidate();
  161. });
  162. },
  163. /* 弹窗关闭 */
  164. handleClose() {
  165. this.$refs.editForm?.clearValidate();
  166. },
  167. /* 提交表单 */
  168. handleSubmit() {
  169. this.$refs.editForm.validate((valid) => {
  170. if (!valid) return;
  171. this.submitLoading = true;
  172. // 提交接口 - 可根据实际接口调整
  173. const loading = this.$loading({ lock: true });
  174. // TODO: 替换为实际的保存接口
  175. saveDevice(this.formData).then((res) => {
  176. loading.close();
  177. this.submitLoading = false;
  178. this.$message.success(this.isEdit ? '修改成功' : '新增成功');
  179. this.dialogVisible = false;
  180. this.$emit('done');
  181. });
  182. });
  183. }
  184. }
  185. };
  186. </script>