modelDefine.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <template>
  2. <a-row justify="end">
  3. <a-col >
  4. <a-space>
  5. <a-button v-if="route.query.type === 'model'" type="primary" @click="openModel('attrVisible', 'add')" >新增属性</a-button>
  6. </a-space>
  7. </a-col>
  8. </a-row>
  9. <!-- 模型属性 -->
  10. <a-table
  11. style="margin-top: 10px;"
  12. :columns="columns"
  13. :dataSource="state.dataSource"
  14. :loading="state.loading"
  15. :pagination="state.queryParams"
  16. @change="changeAttrPage"
  17. >
  18. <template #bodyCell="{column, record}">
  19. <template v-if="column.key === 'dataType'" >
  20. <span>{{ dataTypes.find(item => item.value === record.dataType)?.label }}</span>
  21. </template>
  22. <template v-if="column.key === 'action'">
  23. <a-space>
  24. <a @click="openModel('attrVisible', 'update', record)">修改</a>
  25. <a-popconfirm
  26. title="确实要删除吗?"
  27. ok-text="确定"
  28. cancel-text="取消"
  29. @confirm="confirmDel('attr', record.id)"
  30. >
  31. <a>删除</a>
  32. </a-popconfirm>
  33. </a-space>
  34. </template>
  35. </template>
  36. </a-table>
  37. <!-- 模型命令 -->
  38. <a-row style="margin-top: 20px;" justify="end" >
  39. <a-col >
  40. <a-space>
  41. <a-button v-if="route.query.type === 'model'" type="primary" @click="openModel('cmdVisible', 'add')" >新增命令</a-button>
  42. </a-space>
  43. </a-col>
  44. </a-row>
  45. <a-table
  46. style="margin-top: 10px;"
  47. :columns="columnsCmd"
  48. :dataSource="state.dataSourceCmd"
  49. :loading="state.loadingCmd"
  50. :pagination="state.queryParamsCmd"
  51. @change="changeCmdPage"
  52. >
  53. <template #bodyCell="{column, record}">
  54. <template v-if="column.key === 'cmdParams'">
  55. <span>{{ record.cmdParams.map(item => item.paramLabel).join(',')}}</span>
  56. </template>
  57. <template v-if="column.key === 'cmdResponses'">
  58. <span>{{record.cmdResponses.map(item => item.paramLabel).join(',')}}</span>
  59. </template>
  60. <template v-if="column.key === 'action'">
  61. <a-space>
  62. <a @click="openModel('cmdVisible', 'update', record)" >修改</a>
  63. <a-popconfirm
  64. v-if="record.canRemove"
  65. title="确实要删除吗?"
  66. ok-text="确定"
  67. cancel-text="取消"
  68. @confirm="confirmDel('cmd', record.id)"
  69. >
  70. <a>删除</a>
  71. </a-popconfirm>
  72. </a-space>
  73. </template>
  74. </template>
  75. </a-table>
  76. <a-modal
  77. :title="modalTitle"
  78. :open="state.attrVisible"
  79. @cancel="state.attrVisible = false"
  80. @ok="ok('attr')"
  81. ok-text="确定"
  82. cancel-text="取消"
  83. >
  84. <a-form :label-col="{span: 4}" :wrapper-col="{span: 14}" >
  85. <a-row>
  86. <a-col :span="12" >
  87. <a-form-item label="属性名称" :label-col="{span: 8}" v-bind="validateInfos.attributeLabel" >
  88. <a-input allowClear v-model:value="attrRef.attributeLabel" />
  89. </a-form-item>
  90. </a-col>
  91. <a-col :span="12" >
  92. <a-form-item label="属性key" :label-col="{span: 8}" v-bind="validateInfos.attributeKey" >
  93. <a-input allowClear v-model:value="attrRef.attributeKey" />
  94. </a-form-item>
  95. </a-col>
  96. </a-row>
  97. <a-form-item label="数据类型" v-bind="validateInfos.dataType" >
  98. <a-select allowClear v-model:value="attrRef.dataType" >
  99. <a-select-option :value="item.value" v-for="item in dataTypes" :key="item.value" >{{item.label}}</a-select-option>
  100. </a-select>
  101. </a-form-item>
  102. <a-form-item label="命令类型" v-bind="validateInfos.cmdType" >
  103. <a-select allowClear v-model:value="attrRef.cmdType" >
  104. <a-select-option :value="item.value" v-for="item in dataTypes" :key="item.value" >{{item.label}}</a-select-option>
  105. </a-select>
  106. </a-form-item>
  107. <a-form-item label="访问权限" v-bind="validateInfos.scope" >
  108. <a-checkbox-group v-model:value="attrRef.scope" name="checkboxgroup" :options="[{label: '读', value: 'R'}, {label: '写', value: 'W'}]" />
  109. </a-form-item>
  110. <a-form-item label="单位" >
  111. <a-input allowClear v-model:value="attrRef.dataUnit" />
  112. </a-form-item>
  113. <a-form-item label="计算表达式" >
  114. <a-input allowClear v-model:value="attrRef.expr" />
  115. </a-form-item>
  116. </a-form>
  117. </a-modal>
  118. <a-modal
  119. width="50%"
  120. :title="modalTitle"
  121. :open="state.cmdVisible"
  122. @cancel="state.cmdVisible = false"
  123. @ok="ok('cmd')"
  124. ok-text="确定"
  125. cancel-text="取消"
  126. >
  127. <a-form :label-col="{span: 4}" :wrapper-col="{span: 18}" >
  128. <a-form-item label="命令名称" v-bind="validateInfosCmd.cmdLabel" >
  129. <a-input allowClear v-model:value="cmdRef.cmdLabel" />
  130. </a-form-item>
  131. <a-form-item label="命令编码" v-bind="validateInfosCmd.cmdCode" >
  132. <a-input allowClear v-model:value="cmdRef.cmdCode" />
  133. </a-form-item>
  134. <a-form-item label="命令类型" >
  135. <a-select allowClear v-model:value="cmdRef.cmdType" >
  136. <a-select-option :value="item.value" v-for="item in ModelCmdController.cmdTypes" :key="item.key" >
  137. {{item.label}}
  138. </a-select-option>
  139. </a-select>
  140. </a-form-item>
  141. <a-form-item label="下发参数">
  142. <a-row style="width: 100%;">
  143. <a-col :span="24" ><a-button type="link" @click="openModalCmdp('request', 'add')" >+ 新增输入参数</a-button></a-col>
  144. <a-col :span="24">
  145. <a-table
  146. :columns="columnsCmdParams"
  147. :dataSource="state.dataCmdParams.requestData"
  148. >
  149. <template #bodyCell="{column, record}">
  150. <template v-if="column.key === 'action'">
  151. <a-space>
  152. <a @click="openModalCmdp('request', 'update', record)">修改</a>
  153. <a-popconfirm
  154. title="确实要删除吗?"
  155. ok-text="确定"
  156. cancel-text="取消"
  157. @confirm="delCmdParams('request', record.id)"
  158. >
  159. <a >删除</a>
  160. </a-popconfirm>
  161. </a-space>
  162. </template>
  163. </template>
  164. </a-table>
  165. </a-col>
  166. </a-row>
  167. </a-form-item>
  168. <a-form-item label="响应参数">
  169. <a-row style="width: 100%;">
  170. <a-col :span="24" ><a-button type="link" @click="openModalCmdp('response', 'add')" >+ 新增响应参数</a-button></a-col>
  171. <a-col :span="24">
  172. <a-table
  173. :columns="columnsCmdParams"
  174. :dataSource="state.dataCmdParams.responseData"
  175. >
  176. <template #bodyCell="{column, record}">
  177. <template v-if="column.key === 'action'">
  178. <a-space>
  179. <a @click="openModalCmdp('response', 'update', record)">修改</a>
  180. <a-popconfirm
  181. title="确实要删除吗?"
  182. ok-text="确定"
  183. cancel-text="取消"
  184. @confirm="delCmdParams('response', record.id)"
  185. >
  186. <a >删除</a>
  187. </a-popconfirm>
  188. </a-space>
  189. </template>
  190. </template>
  191. </a-table>
  192. </a-col>
  193. </a-row>
  194. </a-form-item>
  195. </a-form>
  196. </a-modal>
  197. <a-modal
  198. title="新增参数"
  199. :open="state.cmdParamsvisible"
  200. @cancel="state.cmdParamsvisible = false"
  201. @ok="okCmdParams"
  202. ok-text="确定"
  203. cancel-text="取消"
  204. >
  205. <a-form :label-col="{span: 4}" :wrapper-col="{span: 18}" >
  206. <a-form-item label="参数名称" >
  207. <a-input placeholder="请填写参数名称" v-model:value="cmdParamsRef.paramLabel" />
  208. </a-form-item>
  209. <a-form-item label="参数code" v-bind="validateInfosCmdP.paramCode" >
  210. <a-input placeholder="请填写参数名称" v-model:value="cmdParamsRef.paramCode" />
  211. </a-form-item>
  212. <a-form-item label="数据类型" v-bind="validateInfosCmdP.dataType" >
  213. <a-select allowClear v-model:value="cmdParamsRef.dataType" >
  214. <a-select-option :value="item.value" v-for="item in dataTypes" :key="item.value" >
  215. {{item.label}}
  216. </a-select-option>
  217. </a-select>
  218. </a-form-item>
  219. <a-form-item label="参数描述" v-bind="validateInfosCmdP.paramLabel" >
  220. <a-textarea placeholder="请填写参数描述" v-model:value="cmdParamsRef.description" />
  221. </a-form-item>
  222. </a-form>
  223. </a-modal>
  224. </template>
  225. <script lang="ts" setup >
  226. import { ModelAttrController, ModelCmdController } from '@/controller/iot/index'
  227. import { computed } from '@vue/reactivity'
  228. import { nextTick, onMounted, reactive } from 'vue'
  229. import { Form } from 'ant-design-vue'
  230. import { useRoute } from 'vue-router'
  231. import { useId } from '@/hooks'
  232. const columns = [
  233. {
  234. title: '属性名称',
  235. key: 'attributeLabel',
  236. dataIndex: 'attributeLabel'
  237. },
  238. {
  239. title: '属性key',
  240. key: 'attributeKey',
  241. dataIndex: 'attributeKey'
  242. },
  243. {
  244. title: '数据类型',
  245. key: 'dataType',
  246. dataIndex: 'dataType'
  247. },
  248. {
  249. title: '数据单位',
  250. key: 'dataUnit',
  251. dataIndex: 'dataUnit'
  252. },
  253. {
  254. title: '权限',
  255. key: 'scope',
  256. dataIndex: 'scope'
  257. },
  258. {
  259. title: '表达式',
  260. key: 'expr',
  261. dataIndex: 'expr'
  262. },
  263. {
  264. title: '操作',
  265. key: 'action'
  266. }
  267. ]
  268. const columnsCmd = [
  269. {
  270. title: '命令名称',
  271. key: 'cmdLabel',
  272. dataIndex: 'cmdLabel'
  273. },
  274. {
  275. title: '命令code',
  276. key: 'cmdCode',
  277. dataIndex: 'cmdCode'
  278. },
  279. {
  280. title: '下发参数',
  281. key: 'cmdParams',
  282. dataIndex: 'cmdParams'
  283. },
  284. {
  285. title: '响应参数',
  286. key: 'cmdResponses',
  287. dataIndex: 'cmdResponses'
  288. },
  289. {
  290. title: '操作',
  291. key: 'action'
  292. }
  293. ]
  294. const columnsCmdParams = [
  295. {
  296. title: '参数名称',
  297. key: 'paramLabel',
  298. dataIndex: 'paramLabel'
  299. },
  300. {
  301. title: '参数code',
  302. key: 'paramCode',
  303. dataIndex: 'paramCode'
  304. },
  305. {
  306. title: '数据类型',
  307. key: 'dataType',
  308. dataIndex: 'dataType'
  309. },
  310. {
  311. title: '参数描述',
  312. key: 'description',
  313. dataIndex: 'description'
  314. },
  315. {
  316. title: '操作',
  317. key: 'action'
  318. }
  319. ]
  320. const route = useRoute()
  321. const modelId = route.query.id! as string
  322. const useForm = Form.useForm
  323. const dataTypes = [
  324. {
  325. value: 'STRING',
  326. label: '字符串'
  327. },
  328. {
  329. value: 'LONG',
  330. label: '长整型'
  331. },
  332. {
  333. value: 'BOOLEAN',
  334. label: '布尔'
  335. },
  336. {
  337. value: 'DOUBLE',
  338. label: '双精度浮点数'
  339. },
  340. {
  341. value: 'JSON',
  342. label: 'JSON'
  343. }
  344. ]
  345. const state = reactive<{
  346. dataSource: IOT.API.MODELATTR.ModelAttr[],
  347. dataSourceCmd: IOT.API.CMD.CMD[]
  348. [key: string]: any
  349. }>({
  350. dataSource: [],
  351. loading: false,
  352. opraState: 'add',
  353. queryParams: {
  354. page: 1,
  355. pageSize: 10,
  356. modelId: '',
  357. total: 0
  358. },
  359. queryParamsCmd: {
  360. page: 1,
  361. pageSize: 10,
  362. modelId: '',
  363. total: 0
  364. },
  365. attrVisible: false,
  366. cmdVisible: false,
  367. cmdParamsvisible: false,
  368. cmdParamsOpraState: 'request',
  369. loadingCmd: false,
  370. dataSourceCmd: [],
  371. dataCmdParams: {
  372. requestData: [],
  373. responseData: []
  374. }
  375. })
  376. const attrRef = reactive({
  377. attributeLabel: '',
  378. dataType: '',
  379. dataUnit: '',
  380. scope: [],
  381. key: 'tem',
  382. expr: '',
  383. attributeKey: '',
  384. modelId: ''
  385. })
  386. const cmdRef = reactive({
  387. cmdCode: '',
  388. cmdLabel: '',
  389. cmdParams: [],
  390. cmdResponses: [],
  391. modelId: '',
  392. canRemove: false,
  393. cmdType: 'DEFAULT'
  394. })
  395. const cmdParamsRef = reactive({
  396. id: '',
  397. paramCode: '',
  398. paramLabel: '',
  399. description: '',
  400. dataType: ''
  401. })
  402. const { resetFields, validate: validateAttr, validateInfos } = useForm(attrRef, reactive({
  403. attributeLabel: [{ required: true, message: '请填写属性名称' }],
  404. attributeKey: [{ required: true, message: '请填写属性值' }],
  405. dataType: [{ required: true, message: '请选择数据类型' }],
  406. scope: [{ required: true, message: '请选择访问权限' }]
  407. }))
  408. const { resetFields: resetFieldsCmd, validate: validateCmd, validateInfos: validateInfosCmd } = useForm(cmdRef, reactive({
  409. cmdLabel: [{ required: true, message: '请填写模型命令' }],
  410. cmdCode: [{ required: true, message: '请填写模型code' }]
  411. }))
  412. const { resetFields: resetFieldsCmdP, validate: validateCmdP, validateInfos: validateInfosCmdP } = useForm(cmdParamsRef, reactive({
  413. paramCode: [{ required: true, message: '请填写参数code' }],
  414. dataType: [{ required: true, message: '请选择数据类型' }]
  415. }))
  416. const changeAttrPage = ({ current }) => {
  417. state.queryParams.page = current
  418. getModelAttr()
  419. }
  420. const changeCmdPage = ({ current }) => {
  421. state.queryParamsCmd.page = current
  422. getModelCmd()
  423. }
  424. const delCmdParams = (type: 'request' | 'response', id: string) => {
  425. const key = type === 'request' ? 'requestData' : 'responseData'
  426. const index = state.dataCmdParams[key].indexOf(id)
  427. state.dataCmdParams[key].splice(index, 1)
  428. }
  429. const openModalCmdp = (type: 'request' | 'response', opraState: 'add' | 'update', record: any = {}) => {
  430. state.cmdParamsOpraState = type
  431. state.cmdParamsvisible = true
  432. resetFieldsCmdP(record)
  433. }
  434. const okCmdParams = () => {
  435. validateCmdP().then(() => {
  436. console.log(cmdParamsRef)
  437. const key = state.cmdParamsOpraState === 'request' ? 'requestData' : 'responseData'
  438. console.log(state.dataCmdParams, key)
  439. const index = state.dataCmdParams[key].map(item => item.id).indexOf(cmdParamsRef.id)
  440. if (index >= 0) {
  441. state.dataCmdParams[key].splice(index, 1, { ...cmdParamsRef })
  442. } else {
  443. state.dataCmdParams[key].push({ ...cmdParamsRef, id: useId() })
  444. }
  445. nextTick(() => {
  446. resetFieldsCmdP({
  447. paramLabel: '',
  448. description: '',
  449. dataType: ''
  450. })
  451. })
  452. state.cmdParamsvisible = false
  453. })
  454. }
  455. const confirmDel = async (modalName: 'attr' | 'cmd', id: string) => {
  456. console.log('arreId:', id)
  457. modalName === 'attr' ? await ModelAttrController.del(id) : await ModelCmdController.del(id)
  458. modalName === 'attr' ? getModelAttr() : getModelCmd()
  459. }
  460. const ok = (modalName: 'attr' | 'cmd') => {
  461. if (modalName === 'attr') {
  462. validateAttr().then(async () => {
  463. state.opraState === 'add'
  464. ? await ModelAttrController.post({ ...attrRef, scope: typeof attrRef.scope === 'string' ? attrRef.scope : attrRef.scope.join(''), modelId })
  465. : await ModelAttrController.update({ ...attrRef, scope: typeof attrRef.scope === 'string' ? attrRef.scope : attrRef.scope.join(''), modelId })
  466. state.cmdVisible = false
  467. state.attrVisible = false
  468. getModelAttr()
  469. })
  470. } else {
  471. validateCmd().then(async () => {
  472. cmdRef.cmdParams = state.dataCmdParams.requestData
  473. cmdRef.cmdResponses = state.dataCmdParams.responseData
  474. cmdRef.modelId = modelId
  475. state.opraState === 'add'
  476. ? await ModelCmdController.post(cmdRef)
  477. : await ModelCmdController.update(cmdRef)
  478. state.cmdVisible = false
  479. state.attrVisible = false
  480. getModelCmd()
  481. })
  482. }
  483. }
  484. const openModel = (key: string, opraState: 'add' | 'update', record: any = {}) => {
  485. state[key] = true
  486. state.opraState = opraState
  487. if (key === 'attrVisible') {
  488. resetFields(record)
  489. } else {
  490. resetFieldsCmd(record)
  491. if (!record.cmdParams) return
  492. state.dataCmdParams.requestData = record.cmdParams.map(item => {
  493. return {
  494. ...item,
  495. id: item.id ? item.id : useId()
  496. }
  497. }) || []
  498. state.dataCmdParams.responseData = record.cmdResponses.map(item => {
  499. return {
  500. ...item,
  501. id: item.id ? item.id : useId()
  502. }
  503. }) || []
  504. }
  505. }
  506. const getModelCmd = async () => {
  507. state.loadingCmd = true
  508. state.dataSourceCmd = []
  509. const { data, sum } = await ModelCmdController.page(state.queryParamsCmd)
  510. state.loadingCmd = false
  511. state.dataSourceCmd = data.map(item => {
  512. return {
  513. ...item,
  514. id: item.id ? item.id : useId()
  515. }
  516. })
  517. console.log('state.dataSourceCmd:', state.dataSourceCmd)
  518. state.queryParamsCmd.total = sum
  519. }
  520. const getModelAttr = async () => {
  521. state.loading = true
  522. state.dataSource = []
  523. const { data, sum } = await ModelAttrController.page(state.queryParams)
  524. console.log(data)
  525. state.loading = false
  526. state.dataSource = data.map(item => {
  527. return {
  528. ...item,
  529. id: item.id ? item.id : useId()
  530. }
  531. })
  532. state.queryParams.total = sum
  533. }
  534. onMounted(() => {
  535. state.queryParams.modelId = route.query.id
  536. state.queryParamsCmd.modelId = route.query.id
  537. getModelAttr()
  538. getModelCmd()
  539. })
  540. const modalTitle = computed(() => {
  541. const t1 = state.opraState === 'add' ? '新增' : '编辑'
  542. const t2 = state.attrVisible ? '属性' : '命令'
  543. return t1 + t2
  544. })
  545. </script>
  546. <style>
  547. </style>