system.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 } from "vue";
  12. export type LoginProps = {
  13. phoneNum: string;
  14. code: string;
  15. password: string;
  16. };
  17. export const title = ref(appConstant.title);
  18. export const desc = ref(appConstant.desc);
  19. watchEffect(
  20. () => (document.title = title.value + (desc.value ? " | " + desc.value : ""))
  21. );
  22. const refreshUserInfo = async (data: any) => {
  23. user.value.info = data;
  24. await refreshRole();
  25. };
  26. export const login = async (props: LoginProps) => {
  27. const res = await axios.post(userLogin, {
  28. ...props,
  29. deptId: appConstant.deptId,
  30. password: encodePwd(props.password),
  31. });
  32. user.value.token = res.data.token;
  33. refreshUserInfo(res.data.tmUser);
  34. };
  35. if (user.value.token) {
  36. axios.get(userInfo).then((res) => {
  37. refreshUserInfo(res.data);
  38. });
  39. }
  40. export const uploadFile = async (file: File) => {
  41. return (await axios.post<string>(uploadFileUrl, { file })).data;
  42. };