edit.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <div class="system-edit-dept-container">
  3. <el-dialog :title="(ruleForm.id ? '修改' : '添加') + '设备树'" v-model="isShowDialog" width="769px">
  4. <el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="120px">
  5. <el-row :gutter="35">
  6. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  7. <el-form-item label="名称" prop="name">
  8. <el-input v-model="ruleForm.name" placeholder="请输入名称" clearable></el-input>
  9. </el-form-item>
  10. </el-col>
  11. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  12. <el-form-item label="地址" prop="address">
  13. <el-input v-model="ruleForm.address" placeholder="请输入地址" clearable></el-input>
  14. </el-form-item>
  15. </el-col>
  16. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  17. <el-form-item label="经度" prop="lng">
  18. <el-input v-model="ruleForm.lng" placeholder="请输入经度" clearable></el-input>
  19. </el-form-item>
  20. </el-col>
  21. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  22. <el-form-item label="纬度" prop="lat">
  23. <el-input v-model="ruleForm.lat" placeholder="请输入纬度" clearable></el-input>
  24. </el-form-item>
  25. </el-col>
  26. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  27. <el-form-item label="联系人" prop="contact">
  28. <el-input v-model="ruleForm.contact" placeholder="请输入联系人" clearable></el-input>
  29. </el-form-item>
  30. </el-col>
  31. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  32. <el-form-item label="联系电话" prop="phone">
  33. <el-input v-model="ruleForm.phone" placeholder="请输入联系电话" clearable></el-input>
  34. </el-form-item>
  35. </el-col>
  36. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  37. <el-form-item label="开始日期" prop="startDate">
  38. <el-date-picker
  39. v-model="ruleForm.startDate"
  40. type="date"
  41. placeholder="选择开始日期"
  42. :size="'default'"
  43. />
  44. </el-form-item>
  45. </el-col>
  46. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  47. <el-form-item label="结束日期" prop="endDate">
  48. <el-date-picker
  49. v-model="ruleForm.endDate"
  50. type="date"
  51. placeholder="选择结束日期"
  52. :size="'default'"
  53. />
  54. </el-form-item>
  55. </el-col>
  56. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  57. <el-form-item label="设备标识" prop="deviceKey">
  58. <el-input v-model="ruleForm.deviceKey" placeholder="请输入设备标识" clearable></el-input>
  59. </el-form-item>
  60. </el-col>
  61. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  62. <el-form-item label="设备所属区域" prop="area">
  63. <el-input v-model="ruleForm.area" placeholder="请输入设备所属区域" clearable></el-input>
  64. </el-form-item>
  65. </el-col>
  66. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  67. <el-form-item label="所属公司" prop="company">
  68. <el-input v-model="ruleForm.company" placeholder="请输入所属公司" clearable></el-input>
  69. </el-form-item>
  70. </el-col>
  71. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  72. <el-form-item label="类型" prop="types">
  73. <el-select v-model="ruleForm.types" filterable clearable placeholder="请选择类型" class="w-35">
  74. <el-option v-for="dict in tree_types_2" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
  75. </el-select>
  76. <!-- <el-input v-model="ruleForm.types" placeholder="请输入所属公司" clearable></el-input> -->
  77. </el-form-item>
  78. </el-col>
  79. </el-row>
  80. </el-form>
  81. <template #footer>
  82. <span class="dialog-footer">
  83. <el-button @click="onCancel" size="default">取 消</el-button>
  84. <el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.id ? '修 改' : '添 加' }}</el-button>
  85. </span>
  86. </template>
  87. </el-dialog>
  88. </div>
  89. </template>
  90. <script lang="ts">
  91. import { reactive, toRefs, defineComponent, ref, unref, getCurrentInstance } from 'vue';
  92. import api from '/@/api/device';
  93. import { phoneValidate } from '/@/utils/validator';
  94. import { ElMessage } from 'element-plus';
  95. interface RuleFormState {
  96. id?: number;
  97. name: string;
  98. address: string;
  99. lat: string;
  100. lng: string;
  101. contact: string;
  102. phone: string;
  103. deviceKey: string;
  104. area: string;
  105. company: string;
  106. types: string;
  107. parentId: string;
  108. startDate: string;
  109. endDate: string;
  110. }
  111. const baseForm: RuleFormState = {
  112. name: '',
  113. address: '',
  114. lat: '',
  115. lng: '',
  116. contact: '',
  117. phone: '',
  118. deviceKey: '',
  119. area: '',
  120. company: '',
  121. types: '',
  122. parentId: '',
  123. startDate: '',
  124. endDate: '',
  125. };
  126. export default defineComponent({
  127. name: 'deviceEditCate',
  128. setup(prop, { emit }) {
  129. const { proxy } = getCurrentInstance() as any;
  130. const formRef = ref<HTMLElement | null>(null);
  131. const state = reactive({
  132. isShowDialog: false,
  133. ruleForm: {
  134. ...baseForm,
  135. },
  136. rules: {
  137. name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  138. address: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
  139. },
  140. });
  141. const { tree_types_2 } = proxy.useDict('tree_types_2');
  142. // 打开弹窗
  143. const openDialog = (type: string, row?: any) => {
  144. resetForm();
  145. if (type === 'add') {
  146. state.ruleForm.parentId = row.infoId
  147. } else if (type === 'edit') {
  148. state.ruleForm.id = row.infoId
  149. getDetail()
  150. }
  151. state.isShowDialog = true;
  152. };
  153. // 关闭弹窗
  154. const closeDialog = () => {
  155. state.isShowDialog = false;
  156. };
  157. // 取消
  158. const onCancel = () => {
  159. closeDialog();
  160. };
  161. const getDetail = () => {
  162. api.tree.detail({ infoId: state.ruleForm.id })
  163. .then((res: any) => {
  164. state.ruleForm = { ...res.data }
  165. })
  166. }
  167. // 新增
  168. const onSubmit = () => {
  169. const formWrap = unref(formRef) as any;
  170. if (!formWrap) return;
  171. formWrap.validate((valid: boolean) => {
  172. if (valid) {
  173. if (!state.ruleForm.id) {
  174. //添加
  175. api.tree.add(state.ruleForm).then(() => {
  176. ElMessage.success('设备树添加成功');
  177. closeDialog(); // 关闭弹窗
  178. emit('finish');
  179. });
  180. } else {
  181. //修改
  182. api.tree.edit(state.ruleForm).then(() => {
  183. ElMessage.success('设备树修改成功');
  184. closeDialog(); // 关闭弹窗
  185. emit('finish');
  186. });
  187. }
  188. }
  189. });
  190. };
  191. const resetForm = () => {
  192. state.ruleForm = {
  193. ...baseForm,
  194. };
  195. };
  196. return {
  197. openDialog,
  198. closeDialog,
  199. onCancel,
  200. onSubmit,
  201. formRef,
  202. tree_types_2,
  203. ...toRefs(state),
  204. };
  205. },
  206. });
  207. </script>