Cascader.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { 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. initProjectContract()
  44. const info = store.getProjectContract || []
  45. projectContractData(info)
  46. })
  47. //处理项目合同段数据
  48. const projectContractData = (projectContractData) => {
  49. if (projectContractData.length > 0) {
  50. //处理别名
  51. projectContractData.forEach(item => {
  52. let contractArr = item['contractInfoList'] || []
  53. contractArr.forEach(items => {
  54. items['projectAlias'] = items['name']
  55. })
  56. })
  57. //处理其他数据
  58. projectContract.value = projectContractData
  59. const projectId = store.getProjectId //项目ID
  60. const contractId = store.getContractId //合同段ID
  61. const UserProjectInfo = store.getProjectInfo
  62. const UserContractInfo = store.getContractInfo
  63. //查询缓存的选中ID是否存在
  64. const pid = projectContractData.findIndex(item => Number(item.id) === Number(projectId))
  65. const contractList = projectContractData[pid]?.contractInfoList || []
  66. const cid = contractList.findIndex(item => Number(item.id) === Number(contractId))
  67. //如果缓存的选中ID不存在
  68. if (cid === -1) {
  69. //取项目数组中的第一个数据
  70. let letProjectInfo = projectContractData[0]
  71. let contractInfoList = letProjectInfo?.contractInfoList || []
  72. let letContractInfo = contractInfoList[0] || {}
  73. projectValue.value = letContractInfo?.id
  74. projectInfo.value = letProjectInfo
  75. contractInfo.value = letContractInfo
  76. //设置缓存
  77. store.setProjectInfo(letProjectInfo)
  78. store.setContractInfo(letContractInfo)
  79. store.setProjectId(letProjectInfo?.id)
  80. store.setContractId(letContractInfo?.id)
  81. emit('send', {
  82. projectId: letProjectInfo?.id,
  83. contractId: letContractInfo?.id,
  84. })
  85. } else {
  86. projectValue.value = String(contractId)
  87. projectInfo.value = UserProjectInfo
  88. contractInfo.value = UserContractInfo
  89. emit('send', {
  90. projectId: projectId,
  91. contractId: contractId,
  92. })
  93. }
  94. } else {
  95. projectContract.value = []
  96. projectValue.value = null
  97. projectInfo.value = {}
  98. contractInfo.value = {}
  99. emit('send', {
  100. projectId: '',
  101. contractId: '',
  102. })
  103. }
  104. }
  105. //项目被选择
  106. const ElCascaderRef = ref(null)
  107. const projectContractChange = (val) => {
  108. if (val) {
  109. const Nodes = ElCascaderRef.value.getCheckedNodes()
  110. const UserProjectInfo = Nodes[0].parent.data
  111. const UserContractInfo = Nodes[0].data
  112. //缓存项目数据
  113. store.setProjectId(val[0])
  114. store.setContractId(val[1])
  115. store.setProjectInfo(UserProjectInfo)
  116. store.setContractInfo(UserContractInfo)
  117. //更改界面更新
  118. projectInfo.value = UserProjectInfo
  119. contractInfo.value = UserContractInfo
  120. window.$message?.info('切换了项目,数据更新中')
  121. emit('send', {
  122. projectId: val[0],
  123. contractId: val[1],
  124. })
  125. emit('change')
  126. }
  127. }
  128. </script>
  129. <style lang="scss">
  130. .hc-header-cascader-box {
  131. position: relative;
  132. margin-right: 20px;
  133. .project-name-box {
  134. position: relative;
  135. max-width: 340px;
  136. padding-right: 20px;
  137. overflow: hidden;
  138. z-index: -1;
  139. height: 1px;
  140. }
  141. .el-cascader.hc-header-cascader {
  142. width: 100%;
  143. .el-input .el-input__wrapper {
  144. border-radius: 104px;
  145. height: 28px;
  146. }
  147. }
  148. }
  149. </style>