user.js 3.6 KB

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