IotInspectRoute.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <ContentWrap v-loading="formLoading">
  3. <ContentWrap>
  4. <el-form
  5. ref="formRef"
  6. :model="formData"
  7. :rules="formRules"
  8. style="margin-right: 4em; margin-left: 0.5em; margin-top: 1em"
  9. label-width="130px"
  10. >
  11. <div class="base-expandable-content">
  12. <el-row>
  13. <el-col :span="8">
  14. <el-form-item :label="t('route.RouteName')" prop="routeName">
  15. <el-input v-model="formData.routeName" :placeholder="t('route.RouteName')" />
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span="8">
  19. <el-form-item :label="t('monitor.category')" prop="deviceClassify">
  20. <el-tree-select
  21. filterable
  22. clearable
  23. v-model="formData.deviceClassify"
  24. :data="productClassifyList"
  25. :props="defaultProps"
  26. check-strictly
  27. node-key="id"
  28. :placeholder="t('deviceForm.categoryHolder')"
  29. />
  30. </el-form-item>
  31. </el-col>
  32. <el-col :span="8">
  33. <el-form-item :label="t('faultForm.device')" prop="deviceName">
  34. <el-input
  35. v-model="formData.deviceName"
  36. @clear="handleClear"
  37. :placeholder="t('iotMaintain.deviceHolder')"
  38. @click="openDevice"
  39. clearable
  40. />
  41. </el-form-item>
  42. </el-col>
  43. <el-col :span="24">
  44. <el-form-item :label="t('inspect.routeDesc')" prop="routeDesc">
  45. <el-input
  46. type="textarea"
  47. v-model="formData.routeDesc"
  48. :placeholder="t('inspect.routeDescHolder')"
  49. clearable
  50. />
  51. </el-form-item>
  52. </el-col>
  53. </el-row>
  54. </div>
  55. </el-form>
  56. </ContentWrap>
  57. <ContentWrap>
  58. <!-- 列表 -->
  59. <ContentWrap>
  60. <ContentWrap>
  61. <el-form
  62. class="-mb-15px"
  63. :model="queryParams"
  64. ref="queryFormRef"
  65. :inline="true"
  66. label-width="68px"
  67. >
  68. <el-form-item>
  69. <el-button @click="openForm" type="primary"
  70. ><Icon icon="ep:plus" class="mr-5px" />
  71. {{ t('route.selectInspectionItems') }}</el-button
  72. >
  73. </el-form-item>
  74. </el-form>
  75. </ContentWrap>
  76. <draggable
  77. v-model="items"
  78. item-key="id"
  79. tag="div"
  80. class="sortable-container"
  81. handle=".sortable-item"
  82. :animation="150"
  83. @start="dragStart"
  84. @end="dragEnd"
  85. >
  86. <template #item="{ element, index }">
  87. <div class="sortable-item">
  88. <!-- 序号显示 -->
  89. <div class="order-number">{{ index + 1 }}</div>
  90. <!-- 拖动手柄 -->
  91. <span class="drag-handle">≡</span>
  92. <!-- 组件内容 -->
  93. <div class="component-content">
  94. <span style="font-weight: bold">{{ t('inspect.InspectionItems') }}:</span
  95. ><span style="font-size: 14px">{{ element.item }}</span
  96. >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  97. <span style="font-weight: bold">{{ t('inspect.InspectionStandards') }}:</span
  98. ><span style="font-size: 14px">{{ element.standard }}</span>
  99. </div>
  100. <div>
  101. <el-button type="warning" @click="deleteDraggable(index)">{{ t('form.delete') }}</el-button>
  102. </div>
  103. </div>
  104. </template>
  105. </draggable>
  106. </ContentWrap>
  107. </ContentWrap>
  108. <ContentWrap>
  109. <el-form>
  110. <el-form-item style="float: right">
  111. <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('iotMaintain.save') }}</el-button>
  112. <el-button @click="close">{{ t('iotMaintain.cancel') }}</el-button>
  113. </el-form-item>
  114. </el-form>
  115. </ContentWrap>
  116. <InspectItemList
  117. ref="inspectItemFormRef"
  118. @choose="inspectItemChoose"
  119. />
  120. <DeviceList ref="deviceFormRef" @choose="deviceChoose" />
  121. </ContentWrap>
  122. </template>
  123. <script setup lang="ts">
  124. import * as UserApi from '@/api/system/user'
  125. import { ref } from 'vue'
  126. import { IotMaintainMaterialVO } from '@/api/pms/maintain/material'
  127. import { useTagsViewStore } from '@/store/modules/tagsView'
  128. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  129. import { defaultProps, handleTree } from '@/utils/tree'
  130. import * as ProductClassifyApi from '@/api/pms/productclassify'
  131. import draggable from 'vuedraggable'
  132. import InspectItemList from '@/views/pms/inspect/route/InspectItemList.vue'
  133. import DeviceList from '@/views/pms/failure/DeviceList.vue'
  134. import { IotInspectRouteApi, IotInspectRouteVO } from '@/api/pms/inspect/route'
  135. /** 维修工单 表单 */
  136. defineOptions({ name: 'RouteAdd' })
  137. const { t } = useI18n() // 国际化
  138. const message = useMessage() // 消息弹窗
  139. const { delView } = useTagsViewStore() // 视图操作
  140. const { currentRoute, push } = useRouter()
  141. const deptUsers = ref<UserApi.UserVO[]>([]) // 用户列表
  142. const dialogTitle = ref('') // 弹窗的标题
  143. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  144. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  145. const deviceLabel = ref('') // 表单的类型:create - 新增;update - 修改
  146. const drawerVisible = ref<boolean>(false)
  147. const showDrawer = ref()
  148. const list = ref<IotMaintainMaterialVO[]>([]) // 列表的数据
  149. const { params, name } = useRoute() // 查询参数
  150. const id = params.id
  151. const productClassifyList = ref<Tree[]>([]) // 树形结构
  152. const formData = ref({
  153. id: undefined,
  154. routeName: undefined,
  155. deviceClassify: undefined,
  156. deviceId: undefined,
  157. deviceClassifyName: undefined,
  158. deviceName: undefined,
  159. itemJson: undefined,
  160. remark: undefined,
  161. deptId: undefined,
  162. deviceCode: undefined
  163. })
  164. const handleClear = () => {
  165. formData.value.deviceId = undefined
  166. formData.value.deviceName = undefined
  167. formData.value.deptId = undefined
  168. formData.value.deviceCode = undefined
  169. }
  170. // 拖动状态管理
  171. const dragStart = () => {
  172. document.body.style.cursor = 'grabbing'
  173. }
  174. const deleteDraggable = async (index) => {
  175. await message.delConfirm()
  176. items.value.splice(index, 1)
  177. }
  178. const dragEnd = (event) => {
  179. document.body.style.cursor = ''
  180. console.log(
  181. '新顺序:',
  182. items.value.map((c) => c.id)
  183. )
  184. console.log('拖拽后的新位置:', event.newIndex + 1)
  185. }
  186. const items = ref([])
  187. const deviceChoose = (row) => {
  188. formData.value.deviceId = row.id
  189. formData.value.deviceName = row.deviceName
  190. formData.value.deptId = row.deptId
  191. formData.value.deviceCode = row.deviceCode
  192. //deviceLabel.value = row.deviceName
  193. }
  194. const formRules = reactive({
  195. routeName: [{ required: true, message: '路线名称不能为空', trigger: 'blur' }],
  196. deviceClassify: [{ required: true, message: '设备类别不能为空', trigger: 'blur' }]
  197. })
  198. const formRef = ref() // 表单 Ref
  199. const inspectItemChoose = (rows) => {
  200. rows.forEach((it) => {
  201. const ifExist = items.value.find((item) => item.id === it.id)
  202. if (!ifExist) {
  203. items.value.push(it)
  204. console.log(JSON.stringify(it))
  205. }
  206. })
  207. console.log(JSON.stringify(items.value))
  208. // items.value = rows
  209. }
  210. const inspectItemFormRef = ref()
  211. const openForm = () => {
  212. if (formData.value.deviceClassify === undefined) {
  213. message.error('请选择设备类别')
  214. return
  215. }
  216. inspectItemFormRef.value.open(formData.value.deviceClassify, formData.value.deviceId)
  217. }
  218. const deviceFormRef = ref()
  219. const openDevice = () => {
  220. deviceFormRef.value.open(formData.value.deviceClassify)
  221. }
  222. const close = () => {
  223. // delView(unref(currentRoute))
  224. // push({
  225. // name: 'IotInspectRoute',
  226. // query: {
  227. // date: new Date().getTime()
  228. // }
  229. // })
  230. delView(unref(currentRoute))
  231. push({ name: 'IotInspectRoute', params:{}})
  232. }
  233. const itemsWithIndex = computed(() => {
  234. return items.value.map((item, index) => ({
  235. itemId: item.id,
  236. item: item.item,
  237. standard: item.standard,
  238. index: index + 1 // 序号从1开始
  239. }))
  240. })
  241. const itemsWithIndexUpdate = computed(() => {
  242. return items.value.map((item, index) => ({
  243. itemId: item.itemId,
  244. item: item.item,
  245. standard: item.standard,
  246. index: index + 1 // 序号从1开始
  247. }))
  248. })
  249. const { wsCache } = useCache()
  250. /** 提交表单 */
  251. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  252. const submitForm = async () => {
  253. // 校验表单
  254. await formRef.value.validate()
  255. // 提交请求
  256. formLoading.value = true
  257. try {
  258. const user = wsCache.get(CACHE_KEY.USER)
  259. formData.value.deptId = user.user.deptId
  260. const data = formData.value as unknown as IotInspectRouteVO
  261. if (formType.value === 'create') {
  262. const newitems = itemsWithIndex
  263. formData.value.itemJson = JSON.stringify(newitems.value)
  264. await IotInspectRouteApi.createIotInspectRoute(data)
  265. message.success(t('common.createSuccess'))
  266. close()
  267. } else {
  268. const newitems = itemsWithIndexUpdate
  269. formData.value.itemJson = JSON.stringify(newitems.value)
  270. await IotInspectRouteApi.updateIotInspectRoute(data)
  271. message.success(t('common.updateSuccess'))
  272. close()
  273. }
  274. // 发送操作成功的事件
  275. emit('success')
  276. } finally {
  277. formLoading.value = false
  278. }
  279. }
  280. /** 重置表单 */
  281. onMounted(async () => {
  282. // const deptId = useUserStore().getUser.deptId
  283. // deptUsers.value = await UserApi.getDeptUsersByDeptId(deptId)
  284. if (id) {
  285. formType.value = 'update'
  286. const iotInspectRoute = await IotInspectRouteApi.getIotInspectRoute(id)
  287. deviceLabel.value = iotInspectRoute.deviceName
  288. formData.value = iotInspectRoute
  289. items.value = JSON.parse(iotInspectRoute.itemJson)
  290. } else {
  291. formData.value.type = 'in'
  292. formType.value = 'create'
  293. const { wsCache } = useCache()
  294. const userInfo = wsCache.get(CACHE_KEY.USER)
  295. formData.value.maintainPerson = userInfo.user.id
  296. }
  297. productClassifyList.value = handleTree(
  298. await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  299. )
  300. })
  301. const handleDelete = async (id: number) => {
  302. try {
  303. const index = list.value.findIndex((item) => item.code === id)
  304. if (index !== -1) {
  305. // 通过 splice 删除元素
  306. list.value.splice(index, 1)
  307. }
  308. } catch {}
  309. }
  310. </script>
  311. <style scoped>
  312. .base-expandable-content {
  313. overflow: hidden; /* 隐藏溢出的内容 */
  314. transition: max-height 0.3s ease; /* 平滑过渡效果 */
  315. }
  316. /* 横向布局容器 */
  317. .horizontal-list {
  318. display: flex;
  319. gap: 16px;
  320. padding: 20px;
  321. overflow-x: auto;
  322. flex-wrap: nowrap; /* 禁止换行:ml-citation{ref="5" data="citationList"} */
  323. }
  324. /* 拖拽项样式 */
  325. .horizontal-item {
  326. flex: 0 0 auto;
  327. min-width: 150px;
  328. padding: 20px;
  329. background: #d3a137;
  330. border: 2px solid #e0e0e0;
  331. border-radius: 8px;
  332. cursor: move;
  333. user-select: none; /* 防止文字选中:ml-citation{ref="7" data="citationList"} */
  334. transition:
  335. transform 0.3s,
  336. box-shadow 0.3s;
  337. display: flex;
  338. align-items: center;
  339. gap: 10px;
  340. }
  341. /* 拖拽手柄样式 */
  342. .drag-handle {
  343. opacity: 0.5;
  344. cursor: move;
  345. transition: opacity 0.3s;
  346. }
  347. .drag-handle:hover {
  348. opacity: 1;
  349. }
  350. /* 拖拽时的悬停效果 */
  351. .horizontal-item:hover {
  352. transform: translateY(-2px);
  353. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  354. }
  355. /* 滚动条样式 */
  356. .horizontal-list::-webkit-scrollbar {
  357. height: 8px;
  358. }
  359. .horizontal-list::-webkit-scrollbar-thumb {
  360. background: #888;
  361. border-radius: 4px;
  362. }
  363. .sortable-container {
  364. cursor: move;
  365. display: flex;
  366. flex-direction: column;
  367. gap: 9px;
  368. //max-height: 80vh;
  369. overflow-y: auto;
  370. padding: 7px;
  371. }
  372. .sortable-item {
  373. display: flex;
  374. align-items: center;
  375. padding: 8px;
  376. background: #fff;
  377. border: 1px solid #ebeef5;
  378. border-radius: 8px;
  379. transition:
  380. transform 0.3s,
  381. box-shadow 0.3s;
  382. user-select: none;
  383. height: 50%;
  384. }
  385. .sortable-item:hover {
  386. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  387. }
  388. .order-number {
  389. width: 24px;
  390. height: 24px;
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. background: #409eff;
  395. color: white;
  396. border-radius: 50%;
  397. margin-right: 7px;
  398. font-weight: bold;
  399. }
  400. .drag-handle {
  401. padding: 0 12px;
  402. opacity: 0.4;
  403. transition: opacity 0.3s;
  404. }
  405. .drag-handle:hover {
  406. opacity: 1;
  407. }
  408. .component-content {
  409. flex: 1;
  410. min-width: 0;
  411. }
  412. /* 优化滚动条 */
  413. .sortable-container::-webkit-scrollbar {
  414. width: 8px;
  415. }
  416. .sortable-container::-webkit-scrollbar-thumb {
  417. background: #c0c4cc;
  418. border-radius: 4px;
  419. }
  420. </style>