user.ts 3.7 KB

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