useCompany.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* eslint-disable prettier/prettier */
  2. import { computed, watch } from "vue";
  3. import { useUserInfo } from "./useUser";
  4. import { useUserStoreHook } from "/@/store/modules/user";
  5. export function useCompany() {
  6. const userInfo = useUserInfo();
  7. const userStore = useUserStoreHook();
  8. const licenseSet = computed(() => userStore.licenseSet)
  9. /** 当前用户的公司列表 */
  10. const companyList = computed(() => useUserStoreHook().companyList);
  11. /** 当前用户选择的公司 */
  12. const currentCompany = computed(() => useUserStoreHook().currentCompany || {});
  13. /** 设置账户的公司列表 */
  14. const setCompanies = (list) => useUserStoreHook().companyList = list
  15. function setCurrentCompany(value, list?: any) {
  16. const filter: any = ({ companyCode }) => companyCode === value;
  17. const company = (list || companyList.value).find(filter) || {}
  18. if (company && company.companyCode) { company.companyNo = company.companyCode }
  19. /** 超管用户可以查看所有 */
  20. if(userInfo.isSuperUser.value) {
  21. company.companyName = value?.companyName || '所有公司'
  22. company.companyCode = value?.companyNo || ''
  23. }
  24. userStore.setCurrentCompany(company);
  25. return company
  26. }
  27. watch(() => companyList.value, () => {
  28. setCompanies(companyList.value.map(item => {
  29. const lice = licenseSet.value.find(({ companyNo }) => companyNo === item.companyCode)
  30. return { ...item, company_license: lice?.company_license }
  31. }))
  32. }, {
  33. deep: true
  34. })
  35. return {
  36. licenseSet,
  37. companyList,
  38. setCompanies,
  39. currentCompany,
  40. setCurrentCompany,
  41. };
  42. }