Cascader.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div class="hc-header-cascader-box">
  3. <div class="project-name-box">
  4. {{ projectInfo.projectAlias }} / {{ contractInfo.name }}
  5. </div>
  6. <el-cascader
  7. ref="ElCascaderRef"
  8. v-model="projectValue" class="hc-header-cascader"
  9. :clearable="userInfo?.role_id === '1123598816738675201'"
  10. :filterable="userInfo?.role_id === '1123598816738675201'"
  11. :options="projectContract"
  12. :props="projectProps" placeholder="请选择项目"
  13. @change="projectContractChange"
  14. />
  15. </div>
  16. </template>
  17. <script setup>
  18. import { onMounted, ref, watch } from 'vue'
  19. import { useAppStore } from '~src/store'
  20. import { getArrValue } from 'js-fast-way'
  21. import { initButtons, initProjectContract } from '~sto/app'
  22. //事件
  23. const emit = defineEmits(['change', 'send'])
  24. //状态
  25. const store = useAppStore()
  26. const userInfo = ref(store.getUserInfo)
  27. //项目合同段
  28. const projectInfo = ref({})
  29. const contractInfo = ref({})
  30. const projectContract = ref([])
  31. const projectValue = ref(null)
  32. const projectProps = ref({
  33. value: 'id',
  34. label: 'projectAlias',
  35. children: 'contractInfoList',
  36. })
  37. //监听
  38. watch(() => store.getProjectContract, (val) => {
  39. projectContractData(getArrValue(val))
  40. })
  41. //渲染完成
  42. onMounted(() => {
  43. initButtons()
  44. initProjectContract()
  45. const info = store.getProjectContract || []
  46. projectContractData(info)
  47. })
  48. //处理项目合同段数据
  49. const projectContractData = (projectContractData) => {
  50. if (projectContractData.length > 0) {
  51. //处理别名
  52. projectContractData.forEach(item => {
  53. let contractArr = item['contractInfoList'] || []
  54. contractArr.forEach(items => {
  55. items['projectAlias'] = items['name']
  56. })
  57. })
  58. //处理其他数据
  59. projectContract.value = projectContractData
  60. const projectId = store.getProjectId //项目ID
  61. const contractId = store.getContractId //合同段ID
  62. const UserProjectInfo = store.getProjectInfo
  63. const UserContractInfo = store.getContractInfo
  64. //查询缓存的选中ID是否存在
  65. const pid = projectContractData.findIndex(item => Number(item.id) === Number(projectId))
  66. const contractList = projectContractData[pid]?.contractInfoList || []
  67. const cid = contractList.findIndex(item => Number(item.id) === Number(contractId))
  68. //如果缓存的选中ID不存在
  69. if (cid === -1) {
  70. //取项目数组中的第一个数据
  71. let letProjectInfo = projectContractData[0]
  72. let contractInfoList = letProjectInfo?.contractInfoList || []
  73. let letContractInfo = contractInfoList[0] || {}
  74. projectValue.value = letContractInfo?.id
  75. projectInfo.value = letProjectInfo
  76. contractInfo.value = letContractInfo
  77. //设置缓存
  78. store.setProjectInfo(letProjectInfo)
  79. store.setContractInfo(letContractInfo)
  80. store.setProjectId(letProjectInfo?.id)
  81. store.setContractId(letContractInfo?.id)
  82. emit('send', {
  83. projectId: letProjectInfo?.id,
  84. contractId: letContractInfo?.id,
  85. })
  86. } else {
  87. projectValue.value = String(contractId)
  88. projectInfo.value = UserProjectInfo
  89. contractInfo.value = UserContractInfo
  90. emit('send', {
  91. projectId: projectId,
  92. contractId: contractId,
  93. })
  94. }
  95. } else {
  96. projectContract.value = []
  97. projectValue.value = null
  98. projectInfo.value = {}
  99. contractInfo.value = {}
  100. emit('send', {
  101. projectId: '',
  102. contractId: '',
  103. })
  104. }
  105. }
  106. //项目被选择
  107. const ElCascaderRef = ref(null)
  108. const projectContractChange = (val) => {
  109. if (val) {
  110. const Nodes = ElCascaderRef.value.getCheckedNodes()
  111. const UserProjectInfo = Nodes[0].parent.data
  112. const UserContractInfo = Nodes[0].data
  113. //缓存项目数据
  114. store.setProjectId(val[0])
  115. store.setContractId(val[1])
  116. store.setProjectInfo(UserProjectInfo)
  117. store.setContractInfo(UserContractInfo)
  118. //更改界面更新
  119. projectInfo.value = UserProjectInfo
  120. contractInfo.value = UserContractInfo
  121. window.$message?.info('切换了项目,数据更新中')
  122. emit('send', {
  123. projectId: val[0],
  124. contractId: val[1],
  125. })
  126. emit('change')
  127. }
  128. }
  129. </script>
  130. <style lang="scss">
  131. .hc-header-cascader-box {
  132. position: relative;
  133. margin-right: 20px;
  134. .project-name-box {
  135. position: relative;
  136. max-width: 340px;
  137. padding-right: 20px;
  138. overflow: hidden;
  139. z-index: -1;
  140. }
  141. .el-cascader.hc-header-cascader {
  142. position: absolute;
  143. top: 2px;
  144. width: 100%;
  145. .el-input .el-input__wrapper {
  146. border-radius: 104px;
  147. height: 28px;
  148. }
  149. }
  150. }
  151. </style>