modelDefine.vue 15 KB

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