user.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { getLocal, changSaveLocal } from "@/util/localUtil";
  2. import { ref, watchEffect } from "vue";
  3. import {
  4. PaggingReq,
  5. PaggingRes,
  6. axios,
  7. changeUserStatus as changeUserStatusUrl,
  8. deleUser,
  9. getSWToken,
  10. getUserList,
  11. getUserListSelect,
  12. sendUserMsg,
  13. setAuthHook,
  14. updatePsw,
  15. userAdd,
  16. userEdit,
  17. userLogout,
  18. userReg,
  19. } from "@/request/index";
  20. import { QuoteScene } from "./scene";
  21. import { encodePwd } from "@/util";
  22. import { countdownFactory } from "@/hook/countdown";
  23. import { appConstant } from "@/app";
  24. export type UserInfo = {
  25. avatar: string;
  26. deptId: string;
  27. deptName: string;
  28. id: string;
  29. departmentId: string;
  30. cameraSns: string[];
  31. status: 1 | 0;
  32. isAdmin: 1 | 0;
  33. permsList: string[];
  34. nickName: string;
  35. roleId: string;
  36. password: string;
  37. userName: string;
  38. };
  39. type Params = Pick<UserInfo, "nickName" | "status" | "userName"> & {
  40. deptId: string;
  41. };
  42. export const getUserPagging = async (params: PaggingReq<Params>) =>
  43. (await axios.get<PaggingRes<UserInfo>>(getUserList, { params })).data;
  44. export const getUsers = async (deptId?: string) =>
  45. (await axios.get<UserInfo[]>(getUserListSelect, { params: { deptId } })).data;
  46. // 当前用户的信息
  47. export const user = ref({
  48. token: getLocal(`${appConstant.deptId}Token`, ""),
  49. info: getLocal("info", {} as UserInfo),
  50. });
  51. export const logout = async () => {
  52. await axios.post(userLogout);
  53. user.value.token = "";
  54. user.value.info = {} as any;
  55. };
  56. type UpdataPassowrdParams = {
  57. userName: string;
  58. code: string;
  59. password: string;
  60. };
  61. export const updatePassword = async (params: UpdataPassowrdParams) => {
  62. const password = encodePwd(params.password);
  63. await axios.post(updatePsw, {
  64. ...params,
  65. password,
  66. confirmPwd: password,
  67. });
  68. };
  69. type RegisterParams = Pick<
  70. UserInfo,
  71. "deptId" | "userName" | "nickName" | "password"
  72. > & {
  73. code: string;
  74. };
  75. export const register = async (params: RegisterParams) => {
  76. const password = encodePwd(params.password);
  77. await axios.post(userReg, {
  78. ...params,
  79. password,
  80. confirmPwd: password,
  81. });
  82. };
  83. export const addUser = async (
  84. params: Omit<UserInfo, "id">,
  85. deptPath: string[]
  86. ) => {
  87. await axios.post(userAdd, {
  88. ...params,
  89. deptIdList: deptPath.join(","),
  90. });
  91. };
  92. export const setUser = async (params: UserInfo, deptPath: string[]) => {
  93. await axios.post(userEdit, {
  94. ...params,
  95. deptIdList: deptPath.join(","),
  96. });
  97. };
  98. export const delUser = (id: string) => axios.post(deleUser, { id });
  99. export const changeUserStatus = (user: UserInfo) =>
  100. axios.post(changeUserStatusUrl, {
  101. status: Number(!user.status),
  102. id: user.id,
  103. });
  104. // 发送手机验证码
  105. export { CountdownStuts } from "@/hook/countdown";
  106. export type { CountdownStore } from "@/hook/countdown";
  107. const countdownStore = countdownFactory(60, "phoneCode");
  108. export const sendPhoneCode = async (phone: string) => {
  109. await countdownStore.set(phone, () =>
  110. axios.get(sendUserMsg, {
  111. params: {
  112. areaNum: 86,
  113. phoneNum: phone,
  114. type: 2,
  115. },
  116. })
  117. );
  118. return countdownStore.get(phone);
  119. };
  120. export const transformSWToken = async (scene: QuoteScene) => {
  121. const res = await axios.get(getSWToken, { params: { num: scene.num } });
  122. return res.data.token;
  123. };
  124. changSaveLocal(`${appConstant.deptId}Token`, () => user.value.token);
  125. changSaveLocal("info", () => user.value.info);
  126. // 设置全局请求hook
  127. setAuthHook(() => {
  128. return {
  129. token: user.value.token,
  130. userId: user.value.info.id,
  131. clear: () => {
  132. user.value = {
  133. token: "",
  134. info: {} as UserInfo,
  135. };
  136. },
  137. };
  138. });