system.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {
  2. axios,
  3. userLogin,
  4. uploadFile as uploadFileUrl,
  5. userInfo,
  6. } from "@/request";
  7. import { encodePwd } from "@/util";
  8. import { user } from "./user";
  9. import { refreshRole } from "./role";
  10. import { appConstant } from "@/app";
  11. import { ref, watchEffect, reactive } from "vue";
  12. export type LoginProps = {
  13. phoneNum: string;
  14. code: string;
  15. password: string;
  16. deptId?: string;
  17. };
  18. export const title = ref(appConstant.title);
  19. export const desc = ref(appConstant.desc);
  20. console.log('appConstant', appConstant, title.value + (desc.value ? " | " + desc.value : ""));
  21. watchEffect(
  22. () => (document.title = title.value + (desc.value ? " | " + desc.value : ""))
  23. );
  24. const refreshUserInfo = async (data: any) => {
  25. user.value.info = data;
  26. await refreshRole();
  27. };
  28. export const login = async (props: LoginProps) => {
  29. const res = await axios.post(userLogin, {
  30. deptId: appConstant.deptId,
  31. ...props,
  32. password: encodePwd(props.password),
  33. });
  34. user.value.token = res.data.token;
  35. refreshUserInfo(res.data.tmUser);
  36. };
  37. if (user.value.token) {
  38. axios.get(userInfo).then((res) => {
  39. refreshUserInfo(res.data);
  40. });
  41. }
  42. export const uploadFile = async (file: File) => {
  43. return (await axios.post<string>(uploadFileUrl, { file })).data;
  44. };
  45. export const appEl = ref<HTMLDivElement | null>(null);
  46. let currentTempIndex = 0;
  47. export const isTemploraryID = (id: string) =>
  48. id.includes("__currentTempIndex__");
  49. export const createTemploraryID = () =>
  50. `__currentTempIndex__${currentTempIndex++}`;
  51. export const recordFragments = ref<any>([])
  52. export const getRecordFragments = (record: any) => recordFragments.value.filter(fragment => fragment.recordId === record.id)
  53. export const getRecordFragmentBlobs = (record) => getRecordFragments(record)
  54. .filter(fragment => typeof fragment.url !== 'string')
  55. .map(fragment => fragment.url as Blob)
  56. export const createRecordFragment = (recordFragment: Partial<any> = {}): any => ({
  57. id: createTemploraryID(),
  58. recordId: '',
  59. cover: '',
  60. url: '',
  61. sort: Math.min(...recordFragments.value.map(item => item.sort)) + 1,
  62. ...recordFragment
  63. })
  64. // 字符串转params对象
  65. export const strToParams = (str: string) => {
  66. if (str[0] === '?') {
  67. str = str.substr(1)
  68. }
  69. const result: { [key: string]: string } = {}
  70. const splitRG = /([^=&]+)(?:=([^&]*))?&?/
  71. let rgRet
  72. while ((rgRet = str.match(splitRG))) {
  73. result[rgRet[1]] = rgRet[2] === undefined ? '' : rgRet[2]
  74. str = str.substr(rgRet[0].length)
  75. }
  76. return result
  77. }
  78. export const params = reactive(
  79. strToParams(location.search)
  80. ) as any
  81. export const baseURL = params.baseURL ? params.baseURL : "";
  82. export const getResource = (uri: string) => {
  83. if (~uri.indexOf("base64") || ~uri.indexOf("bolb") || ~uri.indexOf("//"))
  84. return uri;
  85. if (uri[0] === "/") {
  86. return `${baseURL}${uri}`;
  87. } else {
  88. return `${baseURL}/${uri}`;
  89. }
  90. };