form.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <xn-form-container
  3. :title="formData.id ? '编辑点位' : '新增点位'"
  4. :width="'800px'"
  5. :visible="visible"
  6. :destroy-on-close="true"
  7. @close="onClose"
  8. >
  9. <a-form
  10. ref="formRef"
  11. :model="formData"
  12. :rules="formRules"
  13. layout="inline"
  14. :label-col="{ style: { width: '110px', justifyContent: 'end' } }"
  15. >
  16. <a-row :gutter="10">
  17. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  18. <a-form-item label="点位名称" name="name">
  19. <a-input v-model:value="formData.name" placeholder="请输入点位名称" allow-clear />
  20. </a-form-item>
  21. </a-col>
  22. <!-- <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  23. <a-form-item label="上级区域:" name="parentId">
  24. <a-tree-select
  25. v-model:value="formData.parentId"
  26. v-model:treeExpandedKeys="defaultExpandedKeys"
  27. class="xn-wd"
  28. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  29. placeholder="请选择区域"
  30. allow-clear
  31. :tree-data="treeData"
  32. :field-names="{
  33. children: 'children',
  34. label: 'name',
  35. value: 'id'
  36. }"
  37. selectable="false"
  38. treeLine
  39. />
  40. </a-form-item>
  41. </a-col> -->
  42. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  43. <a-form-item label="监控对象" name="monitorTargetId">
  44. <a-select
  45. ref="select"
  46. v-model:value="formData.monitorTargetId"
  47. :options="monitorTargetOptions"
  48. placeholder="请选择监控对象"
  49. @change="monitorTargetIdChange"
  50. />
  51. </a-form-item>
  52. </a-col>
  53. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  54. <a-form-item label="监控设备" name="monitorDeviceId">
  55. <a-select
  56. ref="select"
  57. v-model:value="formData.monitorDeviceId"
  58. :options="memListOptions"
  59. placeholder="请选择监控设备"
  60. @change="monitorDeviceIdChange"
  61. />
  62. </a-form-item>
  63. </a-col>
  64. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  65. <a-form-item label="传感器编号" name="sensorCode">
  66. <a-input v-model:value="formData.sensorCode" disabled placeholder="请输入传感器编号" allow-clear />
  67. </a-form-item>
  68. </a-col>
  69. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  70. <a-form-item label="传感器类型" name="sensorType">
  71. <a-select
  72. ref="select"
  73. v-model:value="formData.sensorType"
  74. :options="sensorOptions"
  75. placeholder="请选择传感器类型"
  76. />
  77. </a-form-item>
  78. </a-col>
  79. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  80. <a-form-item label="传感器路数" name="sensorRoute">
  81. <a-input-number
  82. v-model:value="formData.sensorRoute"
  83. style="width: 100%"
  84. :min="1"
  85. :max="sensorRouteMax"
  86. placeholder="请输入传感器路数"
  87. />
  88. </a-form-item>
  89. </a-col>
  90. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  91. <a-form-item label="报警上限" name="limitUp">
  92. <a-input-number
  93. id="inputNumber"
  94. v-model:value="formData.limitUp"
  95. placeholder="请输入报警上限"
  96. allow-clear
  97. style="width: 100%"
  98. />
  99. </a-form-item>
  100. </a-col>
  101. <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
  102. <a-form-item label="报警下限" name="limitDown">
  103. <a-input-number
  104. id="inputNumber"
  105. v-model:value="formData.limitDown"
  106. placeholder="请输入报警下限"
  107. allow-clear
  108. style="width: 100%"
  109. />
  110. </a-form-item>
  111. </a-col>
  112. </a-row>
  113. </a-form>
  114. <template #footer>
  115. <a-button class="xn-mr8" @click="onClose">退出</a-button>
  116. <a-button type="primary" @click="onSubmit">保存</a-button>
  117. </template>
  118. </xn-form-container>
  119. </template>
  120. <script setup>
  121. import tool from '@/utils/tool'
  122. import { required } from '@/utils/formRules'
  123. import locationApi from '@/api/basicset/locationApi.js'
  124. import setupApi from '@/api/basicset/setupApi'
  125. import memApi from '@/api/basicset/memApi'
  126. const emit = defineEmits({ successful: null })
  127. const visible = ref(false) // 默认是关闭状态
  128. const formRef = ref()
  129. const formData = ref() // 表单数据
  130. const sensorOptions = tool.dictList('SENSORTYPE') // 传感器类型
  131. const monitorTargetOptions = ref([]) // 监控对象
  132. const memListOptions = ref([]) // 监控设备数据
  133. const sensorRouteMax = ref(1) //传感器最大值默认1
  134. // 默认要校验的
  135. const formRules = {
  136. name: [required('请输入区域名称')],
  137. // parentId: [required('请选择上级区域')],
  138. monitorTargetId: [required('请选择监控对象')],
  139. monitorDeviceId: [required('请选择监控设备')],
  140. sensorRoute: [required('请输入传感器路数')],
  141. limitUp: [required('请选择报警上限')],
  142. limitDown: [required('请选择报警下限')]
  143. }
  144. // 默认展开的节点(顶级)
  145. const defaultExpandedKeys = ref([0])
  146. // 定义树元素
  147. const treeData = ref([])
  148. // 打开抽屉
  149. const onOpen = (record, parentId) => {
  150. loadData()
  151. visible.value = true
  152. if (record) {
  153. formData.value = Object.assign({}, record)
  154. } else {
  155. formData.value = {}
  156. }
  157. }
  158. const loadData = () => {
  159. // 获取监控对象
  160. setupApi.setupList({ isAll: false }).then((res) => {
  161. monitorTargetOptions.value = (res || []).map((item) => {
  162. return {
  163. ...item,
  164. value: item['id'],
  165. label: item['name']
  166. }
  167. })
  168. })
  169. // 获取监控设备
  170. memApi.memList({ isAll: false }).then((res) => {
  171. console.log(res)
  172. memListOptions.value = (res || []).map((item) => {
  173. return {
  174. ...item,
  175. value: item['id'],
  176. label: item['deviceName'],
  177. modelName: item['modelName'],
  178. deviceCode: item['deviceCode'],
  179. sensorCount: item['sensorCount']
  180. }
  181. })
  182. })
  183. }
  184. // 选中监控设备
  185. const monitorDeviceIdChange = (value) => {
  186. if (value) {
  187. const data = memListOptions.value.find((item) => item.value === value)
  188. // console.log(data, '1')
  189. formData.value.sensorCode = data.deviceCode //设备编号
  190. formData.value.modelName = data.modelName //监控设备型号
  191. formData.value.deviceCode = data.deviceCode //冷链编号
  192. sensorRouteMax.value = data.sensorCount //传感器路数的最大值
  193. }
  194. }
  195. // 选中监控对象
  196. const monitorTargetIdChange = (value) => {
  197. if (value) {
  198. const item = monitorTargetOptions.value.find((item) => item.value === value)
  199. formData.value.limitUp = item.limitUp //监控设备型号
  200. formData.value.limitDown = item.limitDown //冷链编号
  201. }
  202. }
  203. // 关闭抽屉
  204. const onClose = () => {
  205. formRef.value.resetFields()
  206. visible.value = false
  207. }
  208. // 验证并提交数据
  209. const onSubmit = () => {
  210. formRef.value
  211. .validate()
  212. .then(() => {
  213. locationApi.submitForm(formData.value, formData.value.id).then(() => {
  214. onClose()
  215. emit('successful')
  216. })
  217. })
  218. .catch((error) => {
  219. console.log(error)
  220. })
  221. }
  222. // 调用这个函数将子组件的一些数据和方法暴露出去
  223. defineExpose({
  224. onOpen
  225. })
  226. </script>
  227. <style lang="less" scoped>
  228. :deep(.ant-form-item) {
  229. width: 100%;
  230. margin-bottom: 10px;
  231. }
  232. </style>