user.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { defineStore } from 'pinia';
  2. //import { useWsStore } from './ws';
  3. import type { RouteRecordRaw } from 'vue-router';
  4. import { store } from '@/store';
  5. import { login } from '@/api/login';
  6. import {ACCESS_DATA_PERMISSION_KEY, ACCESS_DATA_RANGE_KEY, ACCESS_TOKEN_KEY} from '@/enums/cacheEnum';
  7. import { Storage } from '@/utils/Storage';
  8. import { logout, getInfo, permmenu } from '@/api/account';
  9. import { generatorDynamicRouter } from '@/router/generator-router';
  10. import { resetRouter } from '@/router';
  11. import {oauthLogin} from "@/api/oauth";
  12. import dayjs from "dayjs";
  13. interface UserState {
  14. token: string;
  15. name: string;
  16. avatar: string;
  17. // like [ 'sys:user:add', 'sys:user:update' ]
  18. perms: string[];
  19. menus: RouteRecordRaw[];
  20. userInfo: Partial<API.AdminUserInfo>;
  21. dataRangeList: Partial<API.DataRange>[];
  22. avtImage: number;
  23. updateDayNum: number;
  24. }
  25. export const useUserStore = defineStore({
  26. id: 'user',
  27. state: (): UserState => ({
  28. token: Storage.get(ACCESS_TOKEN_KEY, null),
  29. name: 'amdin',
  30. avatar: '',
  31. perms: Storage.get(ACCESS_DATA_PERMISSION_KEY, null),
  32. menus: [],
  33. userInfo: {},
  34. dataRangeList: Storage.get(ACCESS_DATA_RANGE_KEY, null),
  35. avtImage: 0,
  36. updateDayNum: 0,
  37. }),
  38. getters: {
  39. getToken(): string {
  40. return this.token;
  41. },
  42. getAvatar(): string {
  43. return this.avatar;
  44. },
  45. getName(): string {
  46. return this.name;
  47. },
  48. getPerms(): string[] {
  49. return this.perms;
  50. },
  51. getDataRanges(): Partial<API.DataRange>[] {
  52. return this.dataRangeList;
  53. },
  54. getUserInfo():Partial<API.AdminUserInfo>{
  55. //return Storage.get(USER_INFO_KEY, null);
  56. return this.userInfo;
  57. },
  58. getAvtImage(): number {
  59. return this.avtImage;
  60. },
  61. getUpdateDayNum(): number {
  62. return this.updateDayNum;
  63. }
  64. },
  65. actions: {
  66. /** 清空token及用户信息 */
  67. resetToken() {
  68. this.avatar = this.token = this.name = '';
  69. this.perms = [];
  70. this.menus = [];
  71. this.userInfo = {};
  72. Storage.clear();
  73. },
  74. /** 登录成功保存token */
  75. setToken(token: string) {
  76. this.token = token ?? '';
  77. const ex = 7 * 24 * 60 * 60 * 1000;
  78. Storage.set(ACCESS_TOKEN_KEY, this.token, ex);
  79. },
  80. /** 登录成功保存dataRange */
  81. setDataRangeList(dataRangeList: Partial<API.DataRange>[]) {
  82. this.dataRangeList = dataRangeList;
  83. const ex = 7 * 24 * 60 * 60 * 1000;
  84. Storage.set(ACCESS_DATA_RANGE_KEY, this.dataRangeList, ex);
  85. },
  86. setUserInfo(userInfo:any){
  87. this.userInfo = userInfo ?? '';
  88. //const ex = 7 * 24 * 60 * 60 * 1000;
  89. //Storage.set(USER_INFO_KEY, this.userInfo,ex);
  90. },
  91. setPermissionList(dataList: string[]) {
  92. this.perms = dataList;
  93. const ex = 7 * 24 * 60 * 60 * 1000;
  94. Storage.set(ACCESS_DATA_PERMISSION_KEY, this.perms, ex);
  95. },
  96. /** 登录 */
  97. async login(params: API.LoginParams) {
  98. try {
  99. const result: any = await login(params);
  100. if (result.code == 200) {
  101. this.setToken(result.data.token);
  102. this.setDataRangeList(result.data.dataRangeList);
  103. this.setPermissionList(result.data.permissionList);
  104. return this.afterLogin();
  105. } else if (result.code == 20001 || result.code == 20002) {
  106. // 登录失败返回错误
  107. return {
  108. code: result.code,
  109. message: result.message,
  110. }
  111. }
  112. } catch (error) {
  113. return Promise.reject(error);
  114. }
  115. },
  116. /** 单点登录 */
  117. async oauthLogin(type: string, ticket: string,tm:string) {
  118. try {
  119. const data = await oauthLogin(type, ticket,tm);
  120. if (data != null && data.token != null) {
  121. this.setToken(data.token);
  122. this.setDataRangeList(data.dataRangeList);
  123. this.setPermissionList(data.permissionList);
  124. return this.afterLogin();
  125. }
  126. else{
  127. return Promise.reject(false);
  128. }
  129. } catch (error) {
  130. return Promise.reject(false);
  131. }
  132. },
  133. /** 登录成功之后, 获取用户信息以及生成权限路由 */
  134. async afterLogin() {
  135. try {
  136. //const wsStore = useWsStore();
  137. const [userInfo, { perms, menus }] = await Promise.all([getInfo(), permmenu()]);
  138. //this.perms = perms;
  139. this.name = userInfo.name;
  140. this.avatar = userInfo.headImg;
  141. this.userInfo = userInfo;
  142. this.setUserInfo(userInfo);
  143. // 生成路由
  144. const generatorResult = await generatorDynamicRouter(menus);
  145. this.menus = generatorResult.menus.filter((item) => !item.meta?.hideInMenu);
  146. //!wsStore.client && wsStore.initSocket();
  147. // 通过修改时间判断,上一次修改是否距离今天超过60天
  148. const today = dayjs();
  149. this.updateDayNum = today.diff(userInfo.updateTime, "day");
  150. console.log("上次修改是:" + this.updateDayNum + "天前");
  151. return { menus, perms, userInfo };
  152. } catch (error) {
  153. return Promise.reject(error);
  154. // return this.logout();
  155. }
  156. },
  157. /** 登出 */
  158. async logout() {
  159. await logout();
  160. //const wsStore = useWsStore();
  161. //wsStore.closeSocket();
  162. this.resetToken();
  163. this.setUpdateDayNum();
  164. resetRouter();
  165. },
  166. /** 修改头像通知 */
  167. setAvtImageNum() {
  168. this.avtImage += 1;
  169. console.log(this.avtImage);
  170. },
  171. /** 重置修改 */
  172. setUpdateDayNum() {
  173. Storage.remove("passwordUpdateTriggered");
  174. this.updateDayNum = 0;
  175. }
  176. },
  177. });
  178. // 在组件setup函数外使用
  179. export function useUserStoreWithOut() {
  180. return useUserStore(store);
  181. }