system.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import UAParser from 'ua-parser-js';
  2. import { useAuthStore } from '@/store';
  3. import { isArray, isString } from '@/utils';
  4. interface AppInfo {
  5. /** 项目名称 */
  6. name: string;
  7. /** 项目标题 */
  8. title: string;
  9. /** 项目描述 */
  10. desc: string;
  11. }
  12. /** 项目信息 */
  13. export function useAppInfo(): AppInfo {
  14. const { VITE_APP_NAME: name, VITE_APP_TITLE: title, VITE_APP_DESC: desc } = import.meta.env;
  15. return {
  16. name,
  17. title,
  18. desc
  19. };
  20. }
  21. /** 获取设备信息 */
  22. export function useDeviceInfo() {
  23. const parser = new UAParser();
  24. const result = parser.getResult();
  25. return result;
  26. }
  27. /** 权限判断 */
  28. export function usePermission() {
  29. const auth = useAuthStore();
  30. function hasPermission(permission: Auth.RoleType | Auth.RoleType[]) {
  31. const { userRole } = auth.userInfo;
  32. let has = userRole === 'super';
  33. if (!has) {
  34. if (isArray(permission)) {
  35. has = (permission as Auth.RoleType[]).includes(userRole);
  36. }
  37. if (isString(permission)) {
  38. has = (permission as Auth.RoleType) === userRole;
  39. }
  40. }
  41. return has;
  42. }
  43. return {
  44. hasPermission
  45. };
  46. }