app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import pinia from '~src/store/init'
  2. import { useAppStore } from '~src/store'
  3. import { getButtons } from '~api/menu'
  4. import projectApi from '~api/project'
  5. import { getStoreValue } from '~src/utils/storage'
  6. import { ArrToOneObj, getArrValue, getObjVal, getObjValue, isNullES } from 'js-fast-way'
  7. import website from '~src/config/index'
  8. const store = useAppStore(pinia)
  9. //项目合同段初始化
  10. export const initProjectContract = async () => {
  11. const value = getStoreValue('projectContract')
  12. const userRoleId = getStoreValue('userRoleId')
  13. if (userRoleId !== website.role_id) {
  14. const { error, data } = await getProjectContract()
  15. if (error) return Promise.reject('error')
  16. return Promise.resolve(data)
  17. } else {
  18. return Promise.resolve(value)
  19. }
  20. }
  21. //获取默认项目信息
  22. export const getProjectContract = async () => {
  23. const { error, data } = await projectApi.getProjectAndContract()
  24. let projectList = await getProjectArr(error, data)
  25. if (projectList.length <= 0) {
  26. return { error: true, data: [] }
  27. }
  28. //获取缓存的项目合同段数据
  29. const isStore = await getStoreProjecInfo(projectList)
  30. if (!isStore) {
  31. const isDefault = await getDefaultProject(projectList)
  32. if (!isDefault) return { error: true, data: [] }
  33. }
  34. //获取按钮权限
  35. await initButtons()
  36. //返回数据
  37. return { error: false, data: projectList }
  38. }
  39. //根据缓存获取项目合同段数据
  40. const getStoreProjecInfo = async (arr) => {
  41. const projectId = store.projectId //项目ID
  42. const contractId = store.contractId //合同段ID
  43. //查询缓存的选中ID是否存在
  44. const pid = arr.findIndex(item => Number(item.id) === Number(projectId))
  45. const contractList = getArrValue(arr[pid]?.contractInfoList)
  46. const cid = contractList.findIndex(item => Number(item.id) === Number(contractId))
  47. //如果缓存的选中ID不存在
  48. if (cid === -1) {
  49. return false
  50. }
  51. //获取项目合同段数据
  52. const projectInfo = await getProjectInfo(projectId)
  53. const contractInfo = await getContractInfo(contractId)
  54. if (isNullES(projectInfo) || isNullES(contractInfo)) {
  55. return false
  56. }
  57. setProjectStore(projectInfo, contractInfo)
  58. return true
  59. }
  60. //获取默认项目信息
  61. const getDefaultProject = async (projectList) => {
  62. const { error, status, data } = await projectApi.getDefaultProject()
  63. if (!error && status === 200 && !isNullES(data)) {
  64. const { projectId, contractId } = getObjValue(data)
  65. if (!projectId || !contractId) {
  66. return false
  67. }
  68. const projectInfo = await getProjectInfo(projectId)
  69. const contractInfo = await getContractInfo(contractId)
  70. if (isNullES(projectInfo) || isNullES(contractInfo)) {
  71. return false
  72. }
  73. setProjectStore(projectInfo, contractInfo)
  74. return true
  75. }
  76. //获取第一个项目的第一个合同段数据
  77. const contractList = getArrValue(projectList[0]?.contractInfoList)
  78. const projectInfo = projectList[0]
  79. const contractInfo = contractList[0]
  80. //缓存数据
  81. setProjectStore(projectInfo, contractInfo)
  82. return true
  83. }
  84. //缓存数据
  85. const setProjectStore = (project, contract) => {
  86. store.setProjectInfo(project)
  87. store.setProjectId(project.id)
  88. store.setContractInfo(contract)
  89. store.setContractId(contract.id)
  90. }
  91. const getProjectArr = async (error, data) => {
  92. let projectList = getArrValue(data)
  93. if (error || projectList.length <= 0) {
  94. window.$message?.error('没有相关项目权限')
  95. return []
  96. }
  97. //处理合同段的别名
  98. projectList.forEach(item => {
  99. let contractArr = item['contractInfoList'] || []
  100. contractArr.forEach(items => {
  101. items['projectAlias'] = items['name']
  102. })
  103. })
  104. //过滤空合同段的项目合同段数据
  105. const projectArr = projectList.filter(({ contractInfoList }) => {
  106. const contractList = getArrValue(contractInfoList)
  107. return contractList.length > 0
  108. })
  109. if (projectArr.length <= 0) {
  110. window.$message?.error('没有相关项目权限')
  111. return []
  112. }
  113. store.setProjectContract(projectArr)
  114. //返回数据
  115. return projectArr
  116. }
  117. //获取项目信息
  118. const getProjectInfo = async (projectId) => {
  119. const { data } = await projectApi.getProjectInfo(projectId)
  120. return getObjValue(data)
  121. }
  122. //获取合同段信息
  123. const getContractInfo = async (contractId) => {
  124. const { data } = await projectApi.getContractInfo(contractId)
  125. return getObjValue(data)
  126. }
  127. //按钮初始化
  128. export const initButtons = async () => {
  129. const value = getStoreValue('buttons')
  130. if (!getObjVal(value)) {
  131. const { error, data } = await getButtons()
  132. if (error) return Promise.reject('error')
  133. const buttons = getArrValue(data)
  134. const buttonsArr = await setButtonsData(buttons)
  135. store.setButtons(buttonsArr)
  136. return Promise.resolve(true)
  137. } else {
  138. return Promise.resolve(true)
  139. }
  140. }
  141. //设置按钮
  142. const setButtonsData = async (data) => {
  143. let buttonsArr = {}
  144. await ArrToOneObj(data, 'code', buttonsArr)
  145. return buttonsArr
  146. }