modelDefine.vue 16 KB

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