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