modelDefine.vue 17 KB

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