1
0

user.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 setToken = (token: string) => {
  53. user.value.token = token;
  54. changSaveLocal("token", () => token);
  55. };
  56. export const logout = async () => {
  57. await axios.post(userLogout);
  58. user.value.token = "";
  59. user.value.info = {} as any;
  60. };
  61. type UpdataPassowrdParams = {
  62. userName: string;
  63. code: string;
  64. password: string;
  65. };
  66. export const updatePassword = async (params: UpdataPassowrdParams) => {
  67. const password = encodePwd(params.password);
  68. await axios.post(updatePsw, {
  69. ...params,
  70. password,
  71. confirmPwd: password,
  72. });
  73. };
  74. type RegisterParams = Pick<
  75. UserInfo,
  76. "deptId" | "userName" | "nickName" | "password"
  77. > & {
  78. code: string;
  79. };
  80. export const register = async (params: RegisterParams) => {
  81. const password = encodePwd(params.password);
  82. await axios.post(userReg, {
  83. ...params,
  84. password,
  85. confirmPwd: password,
  86. });
  87. };
  88. export const addUser = async (
  89. params: Omit<UserInfo, "id">,
  90. deptPath: string[]
  91. ) => {
  92. await axios.post(userAdd, {
  93. ...params,
  94. deptIdList: deptPath.join(","),
  95. });
  96. };
  97. export const setUser = async (params: UserInfo, deptPath: string[]) => {
  98. await axios.post(userEdit, {
  99. ...params,
  100. deptIdList: deptPath.join(","),
  101. });
  102. };
  103. export const delUser = (id: string) => axios.post(deleUser, { id });
  104. export const changeUserStatus = (user: UserInfo) =>
  105. axios.post(changeUserStatusUrl, {
  106. status: Number(!user.status),
  107. id: user.id,
  108. });
  109. // 发送手机验证码
  110. export { CountdownStuts } from "@/hook/countdown";
  111. export type { CountdownStore } from "@/hook/countdown";
  112. const countdownStore = countdownFactory(60, "phoneCode");
  113. export const sendPhoneCode = async (phone: string) => {
  114. await countdownStore.set(phone, () =>
  115. axios.get(sendUserMsg, {
  116. params: {
  117. areaNum: 86,
  118. phoneNum: phone,
  119. type: 2,
  120. },
  121. })
  122. );
  123. return countdownStore.get(phone);
  124. };
  125. export const transformSWToken = async (scene: QuoteScene) => {
  126. const res = await axios.get(getSWToken, { params: { num: scene.num } });
  127. return res.data.token;
  128. };
  129. console.log('changSaveLocal', user.value);
  130. changSaveLocal(`token`, () => user.value.token);
  131. changSaveLocal("info", () => user.value.info);
  132. // 设置全局请求hook
  133. setAuthHook(() => {
  134. return {
  135. token: user.value.token,
  136. userId: user.value.info.id,
  137. clear: () => {
  138. user.value = {
  139. token: "",
  140. info: {} as UserInfo,
  141. };
  142. },
  143. };
  144. });