user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import pinia from "@/store/init"
  2. import {useAppStore} from "@/store";
  3. import {getStorage, setStorage} from "@/utils/storage";
  4. import userApi from '~api/user/index';
  5. import menuApi from '~api/menu';
  6. import projectApi from "~api/user/project";
  7. import {setToken, setRefreshToken} from '@/httpApi/util/auth';
  8. import {getArrValue, getObjVal, getObjValue, isNullES, ArrToOneObj} from "js-fast-way";
  9. const store = useAppStore(pinia)
  10. //账号密码登录
  11. export const userLogin = async (form) => {
  12. const {error, status, res, response, message} = await userApi.userLogin(form);
  13. if (!error && status === 200) {
  14. //设置全局状态
  15. const {access_token, refresh_token, tenant_id} = res;
  16. setToken(access_token)
  17. setRefreshToken(refresh_token)
  18. store.setTenantId(tenant_id)
  19. store.setUserInfo(res)
  20. //设置登录信息的缓存
  21. setStorage('login_user_info', {
  22. tenantId: tenant_id,
  23. username: form.username,
  24. password: form.password,
  25. type: form.type
  26. });
  27. await getProjectContract()
  28. return Promise.resolve(res);
  29. } else {
  30. uni.showToast({title: message, icon: 'error'});
  31. return Promise.reject({msg: message, res: response});
  32. }
  33. }
  34. //获取默认项目信息
  35. export const getProjectContract = async () => {
  36. const { error, data } = await projectApi.getProjectAndContract()
  37. const projectList = getArrValue(data)
  38. if (error || projectList.length <= 0) {
  39. uni.showToast({title: '没有相关项目权限', icon: 'error'});
  40. return false
  41. }
  42. //获取默认项目合同段数据
  43. const defaultProject = await getDefaultProject()
  44. let projectInfo = {}, contractInfo = {}
  45. if (defaultProject.code === 200) {
  46. projectInfo = defaultProject.project
  47. contractInfo = defaultProject.contract
  48. } else {
  49. //过滤空合同段的项目合同段数据
  50. const projectArr = projectList.filter(({contractInfoList}) => {
  51. const contractList = getArrValue(contractInfoList)
  52. return contractList.length > 0
  53. })
  54. if (projectArr.length <= 0) {
  55. uni.showToast({title: '没有相关项目权限', icon: 'error'});
  56. return false
  57. }
  58. //获取第一个项目的第一个合同段数据
  59. const contractList = projectArr[0].contractInfoList
  60. projectInfo = projectList[0]
  61. contractInfo = contractList[0]
  62. }
  63. //获取按钮权限
  64. await initButtons()
  65. //设置项目合同段数据
  66. store.setProjectInfo(projectInfo)
  67. store.setProjectId(projectInfo.id)
  68. store.setContractInfo(contractInfo)
  69. store.setContractId(contractInfo.id)
  70. return true
  71. }
  72. //获取默认项目信息
  73. const getDefaultProject = async () => {
  74. const {error, status, data} = await projectApi.getDefaultProject()
  75. if (!error && status === 200 && !isNullES(data)) {
  76. const {projectId, contractId} = getObjValue(data)
  77. if (!projectId || !contractId) {
  78. return {code: 300}
  79. }
  80. const projectInfo = await getProjectInfo(projectId)
  81. const contractInfo = await getContractInfo(contractId)
  82. if (isNullES(projectInfo) || isNullES(contractInfo)) {
  83. return {code: 300}
  84. }
  85. return {code: 200, project: projectInfo, contract: contractInfo}
  86. } else {
  87. return {code: 300}
  88. }
  89. }
  90. //获取项目信息
  91. const getProjectInfo = async (projectId) => {
  92. const {data} = await projectApi.getProjectInfo(projectId)
  93. return getObjValue(data)
  94. }
  95. //获取合同段信息
  96. const getContractInfo = async (contractId) => {
  97. const {data} = await projectApi.getContractInfo(contractId)
  98. return getObjValue(data)
  99. }
  100. //按钮初始化
  101. export const initButtons = async () => {
  102. const value = getStorage('buttons')
  103. if (!value) {
  104. const { error, data } = await menuApi.getButtons()
  105. if (error) return Promise.reject('error')
  106. const buttons = getArrValue(data)
  107. const buttonsArr = await setButtonsData(buttons)
  108. store.setButtons(buttonsArr)
  109. return Promise.resolve(true)
  110. } else {
  111. return Promise.resolve(true)
  112. }
  113. }
  114. //设置按钮
  115. const setButtonsData = async (data) => {
  116. let buttonsArr = {}
  117. await ArrToOneObj(data, 'code', buttonsArr)
  118. return buttonsArr
  119. }