container.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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" @menuTap="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 :disabled="tableCheckedKeys.length <= 0" hc-btn color="#e03997" @click="delTableModalClick">
  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. <HcDialog
  80. :show="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. </HcDialog>
  135. <!-- 新增/编辑 -->
  136. <HcDialog
  137. :loading="addEditFormLoading" :show="addEditFormModal" :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. </HcDialog>
  159. <!-- 导入 -->
  160. <HcDialog :show="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. </HcDialog>
  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 { delMessageV2 } from '~com/message/index.js'
  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 } = 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. }
  405. }
  406. //关闭分类编辑弹窗
  407. const addEditNodeFormModalClose = () => {
  408. addEditNodeFormModal.value = false
  409. }
  410. //删除分类
  411. const delNodeModalClick = (id) => {
  412. delMessageV2(async (action, instance, done) => {
  413. if (action === 'confirm') {
  414. instance.confirmButtonLoading = true
  415. removeClassification(id)
  416. instance.confirmButtonLoading = false
  417. done()
  418. } else {
  419. done()
  420. }
  421. })
  422. }
  423. //删除容器
  424. const removeClassification = async (id) => {
  425. //删除请求
  426. const { error, code } = await dataApi.removeClassification({
  427. projectId: projectId.value,
  428. contractId: contractId.value,
  429. id: id,
  430. })
  431. //处理数据
  432. if (!error && code === 200) {
  433. window?.$message?.success('操作成功')
  434. getMenusData()
  435. }
  436. }
  437. //新增/编辑 表单
  438. const addEditFormRef = ref(null)
  439. const addEditFormModel = ref({})
  440. const addEditFormRules = ref({})
  441. //处理表单校验
  442. const setAddEditFormRules = (fieldList) => {
  443. let newArr = {}
  444. for (let i = 0; i < fieldList.length; i++) {
  445. const item = fieldList[i]
  446. if (i === 0) {
  447. newArr[item.key] = {
  448. required: true,
  449. validator: async (rule, value, callback) => {
  450. if (!value) {
  451. callback(new Error('请输入' + item.name))
  452. } else {
  453. const ver = await verification(item.key, value)
  454. if (!ver) {
  455. callback(new Error(item.name + '必须是惟一的'))
  456. } else {
  457. callback()
  458. }
  459. }
  460. },
  461. trigger: 'blur',
  462. }
  463. }
  464. // else if (item.type === 1) {
  465. // newArr[item.key] = {
  466. // required: true,
  467. // trigger: 'blur',
  468. // message: "请输入" + item.name
  469. // }
  470. // } else if (item.type === 2) {
  471. // newArr[item.key] = {
  472. // required: true,
  473. // trigger: 'blur',
  474. // message: "请选择" + item.name
  475. // }
  476. // }
  477. }
  478. addEditFormRules.value = newArr
  479. }
  480. //校验材料编号是否唯一
  481. const verification = async (key, val) => {
  482. const { containerInitTabName } = menuItem.value
  483. const form = addEditFormModel.value
  484. const { error, code, data } = await dataApi.verification({
  485. projectId: projectId.value,
  486. contractId: contractId.value,
  487. id: form['id'] ?? '',
  488. bean: {
  489. fieldKey: key,
  490. fieldValue: val,
  491. },
  492. containerInitTabName: containerInitTabName,
  493. })
  494. if (!error && code === 200) {
  495. return data === '编号可以使用'
  496. } else {
  497. return false
  498. }
  499. }
  500. //新增/编辑
  501. const addEditFormModal = ref(false)
  502. const addFormModalClick = () => {
  503. addEditFormModel.value = {}
  504. addEditFormModal.value = true
  505. }
  506. //编辑表单
  507. const editFormModalClick = () => {
  508. const keys = tableCheckedKeys.value
  509. if (keys.length === 1) {
  510. addEditFormModel.value = keys[0]
  511. addEditFormModal.value = true
  512. } else if (keys.length > 1) {
  513. window?.$message?.warning('只能选择一条数据编辑')
  514. }
  515. }
  516. //关闭表单
  517. const addEditFormModalClose = () => {
  518. addEditFormModal.value = false
  519. }
  520. //新增/编辑 保存
  521. const addEditFormLoading = ref(false)
  522. const addEditFormClick = async () => {
  523. const validate = await formValidate(addEditFormRef.value)
  524. if (validate) {
  525. const { containerInitTabName } = menuItem.value
  526. const form = addEditFormModel.value
  527. addEditFormLoading.value = true
  528. //处理数据格式
  529. let beanList = []
  530. Object.keys(form).forEach(key => {
  531. if (key !== 'field_calibration_time') {
  532. beanList.push({
  533. fieldKey: key,
  534. fieldValue: form[key],
  535. })
  536. }
  537. })
  538. //发起请求
  539. const { error, code } = await dataApi.submitForm({
  540. projectId: projectId.value,
  541. contractId: contractId.value,
  542. beanList,
  543. containerInitTabName,
  544. fieldCalibrationTime: form['field_calibration_time'],
  545. id: form['id'] ?? '',
  546. })
  547. //处理数据
  548. if (!error && code === 200) {
  549. window?.$message?.success('操作成功')
  550. addEditFormModal.value = false
  551. addEditFormLoading.value = false
  552. await getTableData()
  553. console.log(addEditFormLoading.value, 'addEditFormLoading.value')
  554. }
  555. }
  556. }
  557. //删除数据
  558. const delTableModalClick = () => {
  559. delMessageV2(async (action, instance, done) => {
  560. if (action === 'confirm') {
  561. instance.confirmButtonLoading = true
  562. tableRemoveData()
  563. instance.confirmButtonLoading = false
  564. done()
  565. } else {
  566. done()
  567. }
  568. })
  569. }
  570. //批量删除
  571. const tableRemoveData = async () => {
  572. const rows = tableCheckedKeys.value
  573. if (rows.length > 0) {
  574. const ids = arrToId(rows)
  575. const { containerInitTabName } = menuItem.value
  576. //删除请求
  577. const { error, code } = await dataApi.removeData({
  578. projectId: projectId.value,
  579. contractId: contractId.value,
  580. containerInitTabName: containerInitTabName,
  581. ids: ids,
  582. })
  583. //处理数据
  584. if (!error && code === 200) {
  585. window?.$message?.success('操作成功')
  586. searchClick()
  587. }
  588. }
  589. }
  590. //导入
  591. const importModal = ref(false)
  592. const uploadData = ref({})
  593. const importModalClick = () => {
  594. const { containerInitTabName } = menuItem.value
  595. importModal.value = true
  596. importModalLoading.value = false
  597. uploadData.value = {
  598. containerInitTabName: containerInitTabName,
  599. }
  600. }
  601. const uploadRef = ref(null)
  602. //上传完成
  603. const uploadFinished = (res) => {
  604. importModalLoading.value = false
  605. importModal.value = false
  606. getTableData()
  607. }
  608. //上传进度
  609. const uploadprogress = (res) => {
  610. importModalLoading.value = res
  611. }
  612. //确认导入
  613. const importModalLoading = ref(false)
  614. const importModalYesClick = async () => {
  615. uploadRef.value?.submit()
  616. }
  617. //关闭导入
  618. const importModalClose = () => {
  619. importModal.value = false
  620. }
  621. const downloadLoading = ref(false)
  622. const download = async ()=>{
  623. downloadLoading.value = true
  624. const { error, disposition, res } = await dataApi.downloadTemplate({ id: menuKey.value })
  625. //处理数据
  626. downloadLoading.value = false
  627. if (!error) {
  628. if (disposition) {
  629. downloadBlob(res, disposition)
  630. } else {
  631. window.$message?.error('数据异常')
  632. }
  633. }
  634. }
  635. //左右拖动,改变树形结构宽度
  636. const leftWidth = ref(240)
  637. const onmousedown = () => {
  638. const leftNum = isCollapse.value ? 142 : 272
  639. document.onmousemove = (ve) => {
  640. let diffVal = ve.clientX - leftNum
  641. if (diffVal >= 220 && diffVal <= 400) {
  642. leftWidth.value = diffVal
  643. }
  644. }
  645. document.onmouseup = () => {
  646. document.onmousemove = null
  647. document.onmouseup = null
  648. }
  649. }
  650. </script>
  651. <style lang="scss" scoped>
  652. .hc-import-modal-table-box {
  653. position: relative;
  654. height: calc(100% - 228px);
  655. margin-top: 25px;
  656. }
  657. </style>