index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <StatisticsTemplate
  3. title="产品统计"
  4. :list="state.modelCount"
  5. />
  6. <a-card style="margin-top: 20px;" >
  7. <a-row justify="space-between" >
  8. <a-col>
  9. <a-input-search
  10. v-model:value="state.queryParams.modelLabel"
  11. placeholder="请输入产品模型名称"
  12. enter-button
  13. @search="getModel"
  14. />
  15. </a-col>
  16. <a-col>
  17. <a-space>
  18. <a-button type="primary" @click="openModel('add')" >
  19. 新增
  20. <template #icon>
  21. <plus-outlined />
  22. </template>
  23. </a-button>
  24. </a-space>
  25. </a-col>
  26. </a-row>
  27. <a-table
  28. style="margin-top: 10px;"
  29. :columns="columns"
  30. :dataSource="state.dataSource"
  31. :pagination="state.queryParams"
  32. :loading="state.loading"
  33. @change="change"
  34. >
  35. <template #name="{ text }">
  36. <a @click="goDetailPage(text)" >{{ text }}</a>
  37. </template>
  38. <template #bodyCell="{ column, record }">
  39. <template v-if="column.key === 'id'" >
  40. <a @click="goDetailPage(record.id)">{{record.id}}</a>
  41. </template>
  42. <template v-if="column.key === 'transportType'" >
  43. <a-tag >{{record.transportType}}</a-tag>
  44. </template>
  45. <template v-if="column.key === 'payloadType'" >
  46. <a-tag color="blue">{{record.payloadType}}</a-tag>
  47. </template>
  48. <template v-if="column.key === 'action'">
  49. <a-space>
  50. <a @click="goDetailPage(record.id)" >查看</a>
  51. <a-popconfirm
  52. title="确实要删除吗?"
  53. ok-text="确定"
  54. cancel-text="取消"
  55. @confirm="confirmDel(record.id)"
  56. >
  57. <a href="#">删除</a>
  58. </a-popconfirm>
  59. </a-space>
  60. </template>
  61. </template>
  62. </a-table>
  63. </a-card>
  64. <a-modal
  65. :title="modalTitle"
  66. :visible="state.visible"
  67. ok-text="确定"
  68. cancel-text="取消"
  69. @cancel="closeModel"
  70. @ok="ok"
  71. >
  72. <a-form :label-col="{span: 4}" :wrapper-col="{span: 14}">
  73. <a-form-item label="任务名称" v-bind="validateInfos.modelLabel">
  74. <a-input v-model:value="modelRef.modelLabel" />
  75. </a-form-item>
  76. <a-form-item label="协议类型" v-bind="validateInfos.transportType">
  77. <a-select v-model:value="modelRef.transportType" >
  78. <a-select-option :value="item.port" v-for="item in state.transport" :key="item.port" >{{item.port}}</a-select-option>
  79. </a-select>
  80. </a-form-item>
  81. <a-form-item label="数据格式" v-bind="validateInfos.payloadType">
  82. <a-select v-model:value="modelRef.payloadType" >
  83. <a-select-option :value="item" v-for="item in state.payloadType" :key="item" >{{item}}</a-select-option>
  84. </a-select>
  85. </a-form-item>
  86. <a-form-item label="设备类型" >
  87. <a-input v-model:value="modelRef.deviceType" />
  88. </a-form-item>
  89. <a-form-item label="厂商描述">
  90. <a-textarea v-model:value="modelRef.modelDescription" />
  91. </a-form-item>
  92. </a-form>
  93. </a-modal>
  94. </template>
  95. <script lang="ts" setup >
  96. import { CommonController, ModelController } from '@/controller/index'
  97. import { onMounted, reactive } from 'vue'
  98. import { computed } from '@vue/reactivity'
  99. import { Form } from 'ant-design-vue'
  100. import { useRouter } from 'vue-router'
  101. import StatisticsTemplate from '@/components/StatisticsTemplate/index.vue'
  102. const columns = [
  103. {
  104. title: '产品模型名称',
  105. dataIndex: 'modelLabel',
  106. key: 'modelLabel'
  107. },
  108. {
  109. title: '产品模型id',
  110. dataIndex: 'id',
  111. key: 'id'
  112. },
  113. {
  114. title: '传输协议',
  115. dataIndex: 'transportType',
  116. key: 'transportType'
  117. },
  118. {
  119. title: '数据格式',
  120. dataIndex: 'payloadType',
  121. key: 'payloadType'
  122. },
  123. {
  124. title: '设备类型',
  125. dataIndex: 'deviceType',
  126. key: 'deviceType'
  127. },
  128. {
  129. title: '模型描述',
  130. dataIndex: 'modelDescription',
  131. key: 'modelDescription'
  132. },
  133. {
  134. title: '操作',
  135. key: 'action'
  136. }
  137. ]
  138. const state = reactive<{
  139. dataSource: IOT.API.MODEL.ModelDot[],
  140. [key: string]: any
  141. }>({
  142. loading: false,
  143. opraState: 'add',
  144. visible: false,
  145. dataSource: [],
  146. queryParams: {
  147. page: 1,
  148. pageSize: 10,
  149. total: 0,
  150. modelLabel: ''
  151. },
  152. payloadType: ['JSON', '二进制流'],
  153. modelCount: []
  154. })
  155. const modalTitle = computed(() => state.opraState === 'add' ? '新增产品模型' : '编辑产品模型')
  156. const useForm = Form.useForm
  157. const router = useRouter()
  158. const modelRef = reactive({
  159. modelLabel: '',
  160. transportType: '',
  161. payloadType: '',
  162. deviceType: '',
  163. modelDescription: ''
  164. })
  165. const rulesRef = reactive({
  166. modelLabel: [{ required: true, message: '请填写标题' }],
  167. transportType: [{ required: true, message: '请选择协议' }],
  168. payloadType: [{ required: true, message: '请填写数据格式' }]
  169. })
  170. const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef)
  171. // 获取统计
  172. const getModelCount = async () => {
  173. const { data } = await ModelController.count()
  174. state.modelCount = Object.keys(data).map(key => {
  175. const item = ModelController.forwardStatisticMap.get(key)
  176. return {
  177. ...item,
  178. value: data[key]
  179. }
  180. })
  181. }
  182. const change = ({ current }) => {
  183. state.queryParams.page = current
  184. getModel()
  185. console.log('page:', current)
  186. }
  187. const ok = () => {
  188. validate().then(async () => {
  189. await opraModel()
  190. state.visible = false
  191. getModel()
  192. })
  193. }
  194. const goDetailPage = (id: string) => {
  195. router.push({ path: '/product/detail', query: { id } })
  196. }
  197. const confirmDel = async (id: string) => {
  198. await ModelController.del(id)
  199. getModel()
  200. }
  201. const openModel = (opraState: 'add' | 'update') => {
  202. state.opraState = opraState
  203. state.visible = true
  204. }
  205. const opraModel = async () => {
  206. console.log('opraModel')
  207. if (state.opraState === 'add') {
  208. await ModelController.post(modelRef)
  209. }
  210. }
  211. const closeModel = () => {
  212. state.visible = false
  213. }
  214. const getModel = async () => {
  215. state.loading = true
  216. const { data, sum } = await ModelController.page(state.queryParams)
  217. state.loading = false
  218. state.dataSource = data
  219. state.queryParams.total = sum
  220. }
  221. onMounted(async () => {
  222. getModel()
  223. state.transport = await CommonController.getTransport()
  224. getModelCount()
  225. })
  226. </script>
  227. <style></style>