user.js 4.3 KB

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