related-project.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div class="hc-related-project-box">
  3. <div class="add-btn" @click="addFormList">
  4. <i class="i-iconoir-plus" />
  5. <span class="ml-[1px]">添加项目</span>
  6. </div>
  7. <div v-for="(item, index) in formList" :key="index" class="hc-form-list">
  8. <el-row :gutter="20">
  9. <el-col :span="8">
  10. <el-select v-model="item.projectId" filterable block @change="projectChange($event, index)">
  11. <el-option v-for="(items) in projectList" :key="items.id" :label="items.projectName" :value="items.id" />
  12. </el-select>
  13. </el-col>
  14. <el-col :span="8">
  15. <el-select v-model="item.contractId" filterable block @change="contractChange($event, index)">
  16. <template v-for="(items) in projectList[item.projectIndex]?.contractInfoList" :key="items.id">
  17. <el-option :label="items.contractName" :value="items.id" />
  18. </template>
  19. </el-select>
  20. </el-col>
  21. <el-col :span="6">
  22. <el-select v-model="item.roleId" filterable block @change="roleChange($event, index)">
  23. <el-option v-for="(items) in roleList" :key="items.id" :label="items.roleName" :value="items.id" />
  24. </el-select>
  25. </el-col>
  26. <el-col :span="2">
  27. <el-button hc-btn type="danger" @click="delFormList(index)">删除</el-button>
  28. </el-col>
  29. </el-row>
  30. </div>
  31. </div>
  32. </template>
  33. <script setup>
  34. import { arrIndex, deepClone, getArrValue, isNullES } from 'js-fast-way'
  35. import { onMounted, ref } from 'vue'
  36. import mainApi from '~api/certificate/list'
  37. defineOptions({
  38. name: 'HcRelatedProject',
  39. })
  40. //双向绑定
  41. // eslint-disable-next-line no-undef
  42. const queryValue = defineModel('modelValue', {
  43. default: '',
  44. })
  45. //初始变量
  46. const formList = ref([])
  47. const formValue = ref([])
  48. //渲染完成
  49. onMounted(() => {
  50. getDataApi()
  51. })
  52. const getDataApi = async () => {
  53. await getProjectAndContract()
  54. await getRoleData()
  55. await setInitData()
  56. }
  57. //项目列表
  58. const projectList = ref([])
  59. const getProjectAndContract = async () => {
  60. const { data } = await mainApi.queryProjectAndContract()
  61. projectList.value = getArrValue(data)
  62. }
  63. //获取角色方
  64. const roleList = ref([])
  65. const getRoleData = async () => {
  66. const { data } = await mainApi.queryRole()
  67. roleList.value = getArrValue(data)
  68. }
  69. //添加项目
  70. const addFormList = () => {
  71. formList.value.push({ projectId: '', projectIndex: -1, contractId: '', contractIndex: -1, roleId: '' })
  72. }
  73. //设置初始数据
  74. const setInitData = async () => {
  75. const val = queryValue.value
  76. if (isNullES(val)) return
  77. const arr = val.split(','), project = projectList.value
  78. let newFormArr = []
  79. for (let i = 0; i < arr.length; i++) {
  80. const items = arr[i].split('-')
  81. const projectIndex = arrIndex(project, 'id', items[0])
  82. const contractArr = getArrValue(project[projectIndex]?.contractInfoList)
  83. const contractIndex = arrIndex(contractArr, 'id', items[1])
  84. newFormArr.push({
  85. projectId: items[0],
  86. projectIndex: projectIndex,
  87. contractId: items[1],
  88. contractIndex: contractIndex,
  89. roleId: items[2],
  90. })
  91. }
  92. formList.value = newFormArr
  93. formValue.value = arr
  94. }
  95. //项目选择
  96. const projectChange = (id, index) => {
  97. const pindex = arrIndex(projectList.value, 'id', id)
  98. formList.value[index].projectIndex = pindex ?? -1
  99. formList.value[index].contractIndex = null
  100. formList.value[index].contractId = null
  101. }
  102. //合同段选择
  103. const contractChange = (id, index) => {
  104. const contractList = projectList.value[formList.value[index].projectIndex]?.contractInfoList ?? []
  105. const cindex = arrIndex(contractList, 'id', id)
  106. formList.value[index].contractIndex = cindex ?? -1
  107. }
  108. //角色被选择
  109. const roleChange = (id, index) => {
  110. const form = deepClone(formList.value[index])
  111. const testValue = `${form.projectId}-${form.contractId}-${id}`
  112. const testIndex = formValue.value.indexOf(testValue)
  113. if (testIndex === -1) {
  114. formValue.value.push(testValue)
  115. queryValue.value = formValue.value.join(',')
  116. } else if (testIndex !== index) {
  117. window.$message.error('关联的项目参见方不可重复')
  118. formList.value[index].roleId = null
  119. }
  120. }
  121. //删除当前项目
  122. const delFormList = (index) => {
  123. const list = deepClone(formList.value)
  124. list.splice(index, 1)
  125. formList.value = list
  126. }
  127. </script>
  128. <style scoped lang="scss">
  129. .hc-related-project-box {
  130. position: relative;
  131. color: #606266;
  132. font-size: 14px;
  133. padding: 0 12px;
  134. min-height: 32px;
  135. width: 100%;
  136. border-radius: 4px;
  137. border: 1px solid #dddfe6;
  138. cursor: pointer;
  139. user-select: none;
  140. transition: border 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  141. &:hover {
  142. border-color: var(--el-color-primary);
  143. }
  144. .add-btn {
  145. position: relative;
  146. display: flex;
  147. align-items: center;
  148. i {
  149. font-size: 18px;
  150. }
  151. }
  152. .add-btn + .hc-form-list {
  153. margin-top: 10px;
  154. }
  155. .hc-form-list {
  156. position: relative;
  157. margin-bottom: 10px;
  158. }
  159. }
  160. </style>