IotInspectRoute.vue 12 KB

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