container.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <template>
  2. <div class="hc-page-layout-box">
  3. <div :style="`width:${leftWidth}px;`" class="hc-layout-left-box menu">
  4. <div class="hc-menu-header-box">
  5. <div class="text-xl name">试验容器</div>
  6. <HcTooltip keys="tentative_parameter_container_menu_add">
  7. <el-button _icon hc-btn size="small" type="primary" @click="addEditNodeFormModalClick">
  8. <HcIcon name="add" />
  9. </el-button>
  10. </HcTooltip>
  11. </div>
  12. <div class="hc-menu-contents-box">
  13. <el-scrollbar>
  14. <HcMenuSimple
  15. :datas="menus" :keys="menuKey" :menus="contextMenu" :props="menuProps"
  16. @change="menuChange" @menu-tap="contextMenuClick"
  17. />
  18. </el-scrollbar>
  19. </div>
  20. <!-- 左右拖动 -->
  21. <div class="horizontal-drag-line" @mousedown="onmousedown" />
  22. </div>
  23. <div class="hc-page-content-box">
  24. <HcNewCard>
  25. <template #header>
  26. <div class="w-72">
  27. <el-input v-model="searchForm.queryValue" clearable placeholder="请输入容器编号查询" @keyup="keyUpEvent" />
  28. </div>
  29. <div class="ml-2">
  30. <el-button type="primary" @click="searchClick">
  31. <HcIcon name="search-2" />
  32. <span>搜索</span>
  33. </el-button>
  34. </div>
  35. </template>
  36. <template #extra>
  37. <HcTooltip keys="tentative_parameter_container_add">
  38. <el-button hc-btn type="primary" @click="addFormModalClick">
  39. <HcIcon name="add-circle" />
  40. <span>新增</span>
  41. </el-button>
  42. </HcTooltip>
  43. <HcTooltip keys="tentative_parameter_container_edit">
  44. <el-button :disabled="tableCheckedKeys.length <= 0" hc-btn type="primary" color="#12C060" style="color: white;" @click="editFormModalClick">
  45. <HcIcon name="edit" />
  46. <span>编辑</span>
  47. </el-button>
  48. </HcTooltip>
  49. <HcTooltip keys="tentative_parameter_container_del">
  50. <el-button v-del-com:[delTableModalClick] :disabled="tableCheckedKeys.length <= 0" hc-btn color="#e03997">
  51. <HcIcon name="delete-bin-2" />
  52. <span>删除</span>
  53. </el-button>
  54. </HcTooltip>
  55. <HcTooltip keys="tentative_parameter_container_printer">
  56. <el-button :disabled="tableCheckedKeys.length <= 0" hc-btn color="#567722">
  57. <HcIcon name="printer" />
  58. <span>打印</span>
  59. </el-button>
  60. </HcTooltip>
  61. <HcTooltip keys="tentative_parameter_container_import">
  62. <el-button hc-btn color="#A16222" @click="importModalClick">
  63. <HcIcon name="folder-upload" />
  64. <span>导入</span>
  65. </el-button>
  66. </HcTooltip>
  67. </template>
  68. <HcTable
  69. ref="tableRef" :column="tableColumn" :datas="tableData" :loading="tableLoading"
  70. is-new :index-style="{ width: 60 }" is-check :check-style="{ width: 29 }"
  71. @selection-change="tableSelection"
  72. />
  73. <template #action>
  74. <HcPages :pages="searchForm" @change="pageChange" />
  75. </template>
  76. </HcNewCard>
  77. </div>
  78. <!-- 新增/编辑 节点 -->
  79. <hc-new-dialog
  80. v-model="addEditNodeFormModal" :title="`${addEditNodeFormModel.id ? '编辑' : '新增'}试验容器`"
  81. is-row-footer widths="30rem" @close="addEditNodeFormModalClose"
  82. >
  83. <el-form :model="addEditNodeFormModel" label-position="top" label-width="auto" size="large">
  84. <el-form-item label="试验容器名称">
  85. <el-input v-model="addEditNodeFormModel.containerName" placeholder="请输入试验容器名称" />
  86. </el-form-item>
  87. <template v-for="(item, index) in addEditNodeFormModel.fieldList">
  88. <div class="hc-form-item">
  89. <el-form-item class="w-32" label="数据类型" style="flex: initial;">
  90. <el-select v-model="item.fieldType" block>
  91. <el-option :value="1" label="文本" />
  92. <el-option :value="2" label="日期框" />
  93. </el-select>
  94. </el-form-item>
  95. <el-form-item label="字段名">
  96. <template #label>
  97. <div class="solt-label">
  98. <span class="mr-1">字段名</span>
  99. <span
  100. v-if="index === 0"
  101. class="text-xs text-slate-400"
  102. >第一个作为编号,是唯一的</span>
  103. </div>
  104. <div class="solt-extra">
  105. <el-button
  106. plain size="small" type="primary"
  107. @click="addEditNodeDataDel(item, index)"
  108. >
  109. 删除
  110. </el-button>
  111. </div>
  112. </template>
  113. <el-input v-model="item.fieldName" block />
  114. </el-form-item>
  115. </div>
  116. </template>
  117. </el-form>
  118. <template #leftRowFooter>
  119. <el-button size="large" @click="addEditNodeDataAdd">
  120. <HcIcon name="add" />
  121. <span>加字段</span>
  122. </el-button>
  123. </template>
  124. <template #rightRowFooter>
  125. <el-button size="large" @click="addEditNodeFormModalClose">
  126. <HcIcon name="close" />
  127. <span>取消</span>
  128. </el-button>
  129. <el-button :loading="addEditNodeFormLoading" hc-btn type="primary" @click="addEditNodeFormModalSave">
  130. <HcIcon name="check" />
  131. <span>提交</span>
  132. </el-button>
  133. </template>
  134. </hc-new-dialog>
  135. <!-- 新增/编辑 -->
  136. <hc-new-dialog
  137. v-model="addEditFormModal" :loading="addEditFormLoading" :title="`${addEditFormModel.id ? '编辑' : '新增'}数据`"
  138. widths="30rem" @close="addEditFormModalClose" @save="addEditFormClick"
  139. >
  140. <el-form
  141. ref="addEditFormRef" :model="addEditFormModel" :rules="addEditFormRules" label-width="auto"
  142. size="large"
  143. >
  144. <template v-for="(item, index) in tableColumn">
  145. <el-form-item :label="item.name" :prop="item.key">
  146. <el-input
  147. v-if="item.type === 1"
  148. v-model="addEditFormModel[item.key]"
  149. :placeholder="`${index === 0 ? '作为编号,是唯一的' : `请输入${item.name}`}`"
  150. />
  151. <el-date-picker
  152. v-if="item.type === 2" v-model="addEditFormModel[item.key]" :clearable="false"
  153. class="block" type="date" value-format="YYYY-MM-DD"
  154. />
  155. </el-form-item>
  156. </template>
  157. </el-form>
  158. </hc-new-dialog>
  159. <!-- 导入 -->
  160. <hc-new-dialog v-model="importModal" is-row-footer title="导入" widths="38rem" @close="importModalClose">
  161. <HcDragUpload ref="uploadRef" :datas="uploadData" action="/api/blade-business/container/data/import-excel" @finished="uploadFinished" @progress="uploadprogress" />
  162. <template #leftRowFooter>
  163. <el-button size="large" :loading="downloadLoading" @click="download">
  164. <HcIcon name="download-2" />
  165. <span>下载模板</span>
  166. </el-button>
  167. </template>
  168. <template #rightRowFooter>
  169. <el-button size="large" @click="importModalClose">
  170. <HcIcon name="close" />
  171. <span>取消导入</span>
  172. </el-button>
  173. <el-button :loading="importModalLoading" hc-btn type="primary" @click="importModalYesClick">
  174. <HcIcon name="folder-upload" />
  175. <span>确认导入</span>
  176. </el-button>
  177. </template>
  178. </hc-new-dialog>
  179. </div>
  180. </template>
  181. <script setup>
  182. import { onMounted, ref, watch } from 'vue'
  183. import { useAppStore } from '~src/store'
  184. import { HcIsButton } from '~src/plugins/IsButtons'
  185. import { arrToId, downloadBlob, formValidate, getArrValue } from 'js-fast-way'
  186. import HcDragUpload from './components/HcDragUpload.vue'
  187. import dataApi from '~api/tentative/parameter/container'
  188. import { HcDelMsg } from 'hc-vue3-ui'
  189. //初始变量
  190. const useAppState = useAppStore()
  191. const projectId = ref(useAppState.getProjectId)
  192. const contractId = ref(useAppState.getContractId)
  193. const isCollapse = ref(useAppState.getCollapse)
  194. //监听
  195. watch(() => [
  196. useAppState.getCollapse,
  197. ], ([Collapse]) => {
  198. isCollapse.value = Collapse
  199. })
  200. //渲染完成
  201. onMounted(() => {
  202. getMenusData()
  203. setContextMenu()
  204. })
  205. //左侧菜单
  206. const menuProps = {
  207. key: 'id',
  208. label: 'containerName',
  209. }
  210. const menus = ref([])
  211. const getMenusData = async () => {
  212. const { data } = await dataApi.queryClassification({
  213. projectId: projectId.value,
  214. contractId: contractId.value,
  215. })
  216. const arr = getArrValue(data)
  217. menus.value = arr
  218. if (arr.length > 0) {
  219. const item = arr[0]
  220. menuItem.value = item
  221. menuKey.value = item?.id
  222. setMenuTableColumn(item?.fieldList)
  223. }
  224. }
  225. //菜单被点击
  226. const menuKey = ref('')
  227. const menuItem = ref({})
  228. const menuChange = (item) => {
  229. menuItem.value = item
  230. menuKey.value = item?.id
  231. setMenuTableColumn(item?.fieldList)
  232. }
  233. //设置表格头
  234. const setMenuTableColumn = (data) => {
  235. let newArr = []
  236. for (let i = 0; i < data.length; i++) {
  237. newArr.push({
  238. key: data[i].fieldKey,
  239. name: data[i].fieldName,
  240. type: data[i].fieldType,
  241. })
  242. }
  243. newArr.push({
  244. key: 'field_calibration_time',
  245. name: '校准日期',
  246. type: 2,
  247. })
  248. tableColumn.value = newArr
  249. //取列表数据
  250. searchClick()
  251. setAddEditFormRules(newArr)
  252. }
  253. //菜单的右键菜单
  254. const contextMenu = ref([])
  255. const setContextMenu = () => {
  256. let newArr = []
  257. if (HcIsButton('tentative_parameter_container_menu_edit')) {
  258. newArr.push({ icon: 'draft', label: '编辑容器', key: 'edit' })
  259. }
  260. if (HcIsButton('tentative_parameter_container_menu_del')) {
  261. newArr.push({ icon: 'delete-bin', label: '删除容器', key: 'del' })
  262. }
  263. contextMenu.value = newArr
  264. }
  265. //菜单的右键菜单被点击
  266. const contextMenuClick = ({ key, item }) => {
  267. if (key === 'edit') {
  268. addEditNodeFormModel.value = item
  269. addEditNodeFormModal.value = true
  270. } else if (key === 'del') {
  271. delNodeModalClick(item.id)
  272. }
  273. }
  274. //搜索表单
  275. const searchForm = ref({
  276. queryValue: null, current: 1, size: 20, total: 0,
  277. })
  278. //回车搜索
  279. const keyUpEvent = (e) => {
  280. if (e.key === 'Enter') {
  281. searchForm.value.current = 1
  282. getTableData()
  283. }
  284. }
  285. //搜索
  286. const searchClick = () => {
  287. searchForm.value.current = 1
  288. getTableData()
  289. }
  290. //分页被点击
  291. const pageChange = ({ current, size }) => {
  292. searchForm.value.current = current
  293. searchForm.value.size = size
  294. getTableData()
  295. }
  296. //表格数据
  297. const tableRef = ref(null)
  298. const tableColumn = ref([])
  299. const tableData = ref([])
  300. //获取数据
  301. const tableLoading = ref(false)
  302. const getTableData = async () => {
  303. const { id, fieldList } = menuItem.value
  304. const fieldLists = getArrValue(fieldList)
  305. if (fieldLists.length > 0) {
  306. tableLoading.value = true
  307. const { error, code, data } = await dataApi.queryPage({
  308. projectId: projectId.value,
  309. contractId: contractId.value,
  310. containerId: id,
  311. ...searchForm.value,
  312. fieldKey: fieldList[0].fieldKey,
  313. id: id,
  314. })
  315. //处理数据
  316. tableLoading.value = false
  317. if (!error && code === 200) {
  318. tableData.value = getArrValue(data['records'])
  319. searchForm.value.total = data.total || 0
  320. } else {
  321. tableData.value = []
  322. searchForm.value.total = 0
  323. }
  324. } else {
  325. window.$message.warning('请先设置容器字段')
  326. }
  327. }
  328. //多选
  329. const tableCheckedKeys = ref([])
  330. const tableSelection = (rows) => {
  331. tableCheckedKeys.value = rows
  332. }
  333. //新增/编辑 分类
  334. const addEditNodeFormModal = ref(false)
  335. const addEditNodeFormModel = ref({
  336. containerName: '', fieldList: [{
  337. fieldType: 1,
  338. fieldName: '',
  339. }],
  340. })
  341. const addEditNodeFormModalClick = () => {
  342. addEditNodeFormModel.value = {
  343. containerName: '', fieldList: [{
  344. fieldType: 1,
  345. fieldName: '',
  346. }],
  347. }
  348. addEditNodeFormModal.value = true
  349. }
  350. //加字段
  351. const addEditNodeDataAdd = () => {
  352. if (addEditNodeFormModel.value.fieldList.length > 4) {
  353. window.$message?.warning('最多只能添加5个字段')
  354. } else {
  355. addEditNodeFormModel.value.fieldList.push({
  356. fieldType: 1,
  357. fieldName: '',
  358. })
  359. }
  360. }
  361. //删除当前行
  362. const addEditNodeDataDel = async ({ fieldId }, index) => {
  363. if (!fieldId) {
  364. addEditNodeFormModel.value.fieldList.splice(index, 1)
  365. } else {
  366. const { error, code } = await dataApi.removeField({ fieldId })
  367. if (!error && code === 200) {
  368. addEditNodeFormModel.value.fieldList.splice(index, 1)
  369. }
  370. }
  371. }
  372. //保存节点信息
  373. const addEditNodeFormLoading = ref(false)
  374. const addEditNodeFormModalSave = () => {
  375. const form = addEditNodeFormModel.value
  376. if (!form.containerName) {
  377. window.$message.warning('请输入容器名称')
  378. } else if (form.fieldList.length <= 0) {
  379. window.$message.warning('请添加容器字段')
  380. } else {
  381. //判断是否满足条件
  382. const result = form.fieldList.every(({ fieldName }) => {
  383. return fieldName !== ''
  384. })
  385. if (!result) {
  386. window.$message.warning('请先完善字段数据')
  387. } else {
  388. form.projectId = projectId.value
  389. form.contractId = contractId.value
  390. submitClassification(form)
  391. }
  392. }
  393. }
  394. //提交保存分类
  395. const submitClassification = async (form) => {
  396. addEditNodeFormLoading.value = true
  397. const { error, code, msg } = await dataApi.submitClassification(form)
  398. //处理数据
  399. addEditNodeFormLoading.value = false
  400. if (!error && code === 200) {
  401. window?.$message?.success('操作成功')
  402. addEditNodeFormModal.value = false
  403. await getMenusData()
  404. } else {
  405. window?.$message?.error(msg)
  406. }
  407. }
  408. //关闭分类编辑弹窗
  409. const addEditNodeFormModalClose = () => {
  410. addEditNodeFormModal.value = false
  411. }
  412. //删除分类
  413. const delNodeModalClick = (id) => {
  414. HcDelMsg({}, async (instance, resolve) => {
  415. instance.confirmButtonLoading = true
  416. instance.confirmButtonText = '删除中...'
  417. await removeClassification(id)
  418. resolve() //关闭弹窗的回调
  419. instance.confirmButtonLoading = false
  420. })
  421. }
  422. //删除容器
  423. const removeClassification = async (id) => {
  424. //删除请求
  425. const { error, code, msg } = await dataApi.removeClassification({
  426. projectId: projectId.value,
  427. contractId: contractId.value,
  428. id: id,
  429. })
  430. //处理数据
  431. if (!error && code === 200) {
  432. window?.$message?.success('操作成功')
  433. getMenusData()
  434. } else {
  435. window?.$message?.error(msg)
  436. }
  437. }
  438. //新增/编辑 表单
  439. const addEditFormRef = ref(null)
  440. const addEditFormModel = ref({})
  441. const addEditFormRules = ref({})
  442. //处理表单校验
  443. const setAddEditFormRules = (fieldList) => {
  444. let newArr = {}
  445. for (let i = 0; i < fieldList.length; i++) {
  446. const item = fieldList[i]
  447. if (i === 0) {
  448. newArr[item.key] = {
  449. required: true,
  450. validator: async (rule, value, callback) => {
  451. if (!value) {
  452. callback(new Error('请输入' + item.name))
  453. } else {
  454. const ver = await verification(item.key, value)
  455. if (!ver) {
  456. callback(new Error(item.name + '必须是惟一的'))
  457. } else {
  458. callback()
  459. }
  460. }
  461. },
  462. trigger: 'blur',
  463. }
  464. }
  465. // else if (item.type === 1) {
  466. // newArr[item.key] = {
  467. // required: true,
  468. // trigger: 'blur',
  469. // message: "请输入" + item.name
  470. // }
  471. // } else if (item.type === 2) {
  472. // newArr[item.key] = {
  473. // required: true,
  474. // trigger: 'blur',
  475. // message: "请选择" + item.name
  476. // }
  477. // }
  478. }
  479. addEditFormRules.value = newArr
  480. }
  481. //校验材料编号是否唯一
  482. const verification = async (key, val) => {
  483. const { containerInitTabName } = menuItem.value
  484. const form = addEditFormModel.value
  485. const { error, code, data } = await dataApi.verification({
  486. projectId: projectId.value,
  487. contractId: contractId.value,
  488. id: form['id'] ?? '',
  489. bean: {
  490. fieldKey: key,
  491. fieldValue: val,
  492. },
  493. containerInitTabName: containerInitTabName,
  494. })
  495. if (!error && code === 200) {
  496. return data === '编号可以使用'
  497. } else {
  498. return false
  499. }
  500. }
  501. //新增/编辑
  502. const addEditFormModal = ref(false)
  503. const addFormModalClick = () => {
  504. addEditFormModel.value = {}
  505. addEditFormModal.value = true
  506. }
  507. //编辑表单
  508. const editFormModalClick = () => {
  509. const keys = tableCheckedKeys.value
  510. if (keys.length === 1) {
  511. addEditFormModel.value = keys[0]
  512. addEditFormModal.value = true
  513. } else if (keys.length > 1) {
  514. window?.$message?.warning('只能选择一条数据编辑')
  515. }
  516. }
  517. //关闭表单
  518. const addEditFormModalClose = () => {
  519. addEditFormModal.value = false
  520. }
  521. //新增/编辑 保存
  522. const addEditFormLoading = ref(false)
  523. const addEditFormClick = async () => {
  524. const validate = await formValidate(addEditFormRef.value)
  525. if (validate) {
  526. const { containerInitTabName } = menuItem.value
  527. const form = addEditFormModel.value
  528. addEditFormLoading.value = true
  529. //处理数据格式
  530. let beanList = []
  531. Object.keys(form).forEach(key => {
  532. if (key !== 'field_calibration_time') {
  533. beanList.push({
  534. fieldKey: key,
  535. fieldValue: form[key],
  536. })
  537. }
  538. })
  539. //发起请求
  540. const { error, code, msg } = await dataApi.submitForm({
  541. projectId: projectId.value,
  542. contractId: contractId.value,
  543. beanList,
  544. containerInitTabName,
  545. fieldCalibrationTime: form['field_calibration_time'],
  546. id: form['id'] ?? '',
  547. })
  548. //处理数据
  549. if (!error && code === 200) {
  550. window?.$message?.success('操作成功')
  551. addEditFormModal.value = false
  552. addEditFormLoading.value = false
  553. await getTableData()
  554. console.log(addEditFormLoading.value, 'addEditFormLoading.value')
  555. } else {
  556. window?.$message?.error(msg)
  557. }
  558. }
  559. }
  560. //删除数据
  561. const delTableModalClick = async (_, resolve) => {
  562. await tableRemoveData()
  563. resolve()
  564. }
  565. //批量删除
  566. const tableRemoveData = async () => {
  567. const rows = tableCheckedKeys.value
  568. if (rows.length > 0) {
  569. const ids = arrToId(rows)
  570. const { containerInitTabName } = menuItem.value
  571. //删除请求
  572. const { error, code, msg } = await dataApi.removeData({
  573. projectId: projectId.value,
  574. contractId: contractId.value,
  575. containerInitTabName: containerInitTabName,
  576. ids: ids,
  577. })
  578. //处理数据
  579. if (!error && code === 200) {
  580. window?.$message?.success('操作成功')
  581. searchClick()
  582. } else {
  583. window?.$message?.error(msg)
  584. }
  585. }
  586. }
  587. //导入
  588. const importModal = ref(false)
  589. const uploadData = ref({})
  590. const importModalClick = () => {
  591. const { containerInitTabName } = menuItem.value
  592. importModal.value = true
  593. importModalLoading.value = false
  594. uploadData.value = {
  595. containerInitTabName: containerInitTabName,
  596. }
  597. }
  598. const uploadRef = ref(null)
  599. //上传完成
  600. const uploadFinished = (res) => {
  601. importModalLoading.value = false
  602. importModal.value = false
  603. getTableData()
  604. }
  605. //上传进度
  606. const uploadprogress = (res) => {
  607. importModalLoading.value = res
  608. }
  609. //确认导入
  610. const importModalLoading = ref(false)
  611. const importModalYesClick = async () => {
  612. uploadRef.value?.submit()
  613. }
  614. //关闭导入
  615. const importModalClose = () => {
  616. importModal.value = false
  617. }
  618. const downloadLoading = ref(false)
  619. const download = async ()=>{
  620. downloadLoading.value = true
  621. const { error, disposition, res } = await dataApi.downloadTemplate({ id: menuKey.value })
  622. //处理数据
  623. downloadLoading.value = false
  624. if (!error) {
  625. if (disposition) {
  626. downloadBlob(res, disposition)
  627. } else {
  628. window.$message?.error('数据异常')
  629. }
  630. }
  631. }
  632. //左右拖动,改变树形结构宽度
  633. const leftWidth = ref(240)
  634. const onmousedown = () => {
  635. const leftNum = isCollapse.value ? 142 : 272
  636. document.onmousemove = (ve) => {
  637. let diffVal = ve.clientX - leftNum
  638. if (diffVal >= 220 && diffVal <= 400) {
  639. leftWidth.value = diffVal
  640. }
  641. }
  642. document.onmouseup = () => {
  643. document.onmousemove = null
  644. document.onmouseup = null
  645. }
  646. }
  647. </script>
  648. <style lang="scss" scoped>
  649. .hc-import-modal-table-box {
  650. position: relative;
  651. height: calc(100% - 228px);
  652. margin-top: 25px;
  653. }
  654. </style>