| | |
| | | import Cookies from "js-cookie"; |
| | | import { useUserStoreHook } from "@/store/modules/user"; |
| | | import { storageLocal, isString, isIncludeAllChildren } from "@pureadmin/utils"; |
| | | import type { nowRoleType } from "@/store/types"; |
| | | import type { LoginResult, RoleItem } from "@/api/types"; |
| | | |
| | | export interface DataInfo<T> { |
| | | /** token */ |
| | |
| | | username?: string; |
| | | /** 昵称 */ |
| | | nickname?: string; |
| | | /** 当前登录用户的角色列表 */ |
| | | exRoles?: RoleItem[]; |
| | | /** 当前登录用户的角色 */ |
| | | exRoles?: Array<string>; |
| | | roles?: Array<string>; |
| | | /** 当前登录用户的按钮级别权限 */ |
| | | permissions?: Array<string>; |
| | | isManager: boolean; |
| | | } |
| | | /** |
| | | * AdminResult_CusExtendDto,全局返回结果 |
| | | */ |
| | | export interface ResponseEnterprise { |
| | | /** |
| | | * 状态码 |
| | | */ |
| | | code?: number; |
| | | /** |
| | | * 附加数据 |
| | | */ |
| | | extras?: null; |
| | | /** |
| | | * 错误信息 |
| | | */ |
| | | message?: null | string; |
| | | result?: CusExtendDto; |
| | | /** |
| | | * 时间 |
| | | */ |
| | | time?: Date; |
| | | /** |
| | | * 类型success、warning、error |
| | | */ |
| | | type?: null | string; |
| | | } |
| | | |
| | | /** |
| | | * CusExtendDto,用户角色资料 |
| | | */ |
| | | export interface CusExtendDto { |
| | | /** |
| | | * 企业银行账号 |
| | | */ |
| | | bankAccount?: null | string; |
| | | /** |
| | | * 企业开户行 |
| | | */ |
| | | bankName?: null | string; |
| | | /** |
| | | * 营业执照文件路径 |
| | | */ |
| | | businessLicense?: null | string; |
| | | /** |
| | | * 企业邮箱 |
| | | */ |
| | | enterpriseEmail?: null | string; |
| | | /** |
| | | * 企业名称 |
| | | */ |
| | | enterpriseName?: null | string; |
| | | /** |
| | | * 企业联系电话 |
| | | */ |
| | | enterprisePhone?: null | string; |
| | | /** |
| | | * 企业类型 从企业类型中选择 来源企业类型 |
| | | */ |
| | | enterpriseType?: null | string; |
| | | /** |
| | | * 企业成立时间 |
| | | */ |
| | | establishmentTime?: Date; |
| | | /** |
| | | * 主键 |
| | | */ |
| | | id?: number | null; |
| | | /** |
| | | * 法定代表人身份证文件路径 |
| | | */ |
| | | legalRepresentativeIdCard?: null | string; |
| | | /** |
| | | * 法定代表人身份证号码 |
| | | */ |
| | | legalRepresentativeIdNumber?: null | string; |
| | | /** |
| | | * 法定代表人姓名 |
| | | */ |
| | | legalRepresentativeName?: null | string; |
| | | /** |
| | | * 法定代表人联系电话 |
| | | */ |
| | | legalRepresentativePhone?: null | string; |
| | | /** |
| | | * 企业主营业务 |
| | | */ |
| | | mainBusiness?: null | string; |
| | | /** |
| | | * 业务经办人身份证文件路径或标识(上传后存储信息) |
| | | */ |
| | | operatorIdCard?: null | string; |
| | | /** |
| | | * 业务经办人身份证号码 |
| | | */ |
| | | operatorIdNumber?: null | string; |
| | | /** |
| | | * 业务经办人姓名 |
| | | */ |
| | | operatorName?: null | string; |
| | | /** |
| | | * 业务经办人联系电话 |
| | | */ |
| | | operatorPhone?: null | string; |
| | | /** |
| | | * 注册资金(单位:万元 ) |
| | | */ |
| | | registeredCapital?: null | string; |
| | | /** |
| | | * 企业住所(下拉选择,需结合实际可选值,先字符串接收 ) |
| | | */ |
| | | residence?: null | string; |
| | | /** |
| | | * 交易主体 代码 同角色代码 |
| | | */ |
| | | transactionCode?: string; |
| | | /** |
| | | * 交易主体名称 |
| | | */ |
| | | transactionName?: null | string; |
| | | /** |
| | | * 统一社会信用代码 |
| | | */ |
| | | unifiedSocialCreditCode?: null | string; |
| | | } |
| | | |
| | | export const userKey = "user-info"; |
| | |
| | | * 将`accessToken`、`expires`、`refreshToken`这三条信息放在key值为authorized-token的cookie里(过期自动销毁) |
| | | * 将`avatar`、`username`、`nickname`、`exRoles`、`permissions`、`refreshToken`、`expires`这七条信息放在key值为`user-info`的localStorage里(利用`multipleTabsKey`当浏览器完全关闭后自动销毁) |
| | | */ |
| | | export function setToken(data: DataInfo<Date>) { |
| | | export function setToken(data: LoginResult) { |
| | | let expires = 0; |
| | | const { accessToken, refreshToken } = data; |
| | | const { isRemembered, loginDay } = useUserStoreHook(); |
| | |
| | | : {} |
| | | ); |
| | | |
| | | function setUserKey({ avatar, username, nickname, exRoles, permissions }) { |
| | | function setUserKey({ |
| | | avatar, |
| | | username, |
| | | nickname, |
| | | exRoles, |
| | | permissions, |
| | | roles, |
| | | isManager |
| | | }) { |
| | | useUserStoreHook().SET_AVATAR(avatar); |
| | | useUserStoreHook().SET_USERNAME(username); |
| | | useUserStoreHook().SET_NICKNAME(nickname); |
| | | useUserStoreHook().SET_ROLES(exRoles); |
| | | useUserStoreHook().SET_EXROLES(exRoles); |
| | | useUserStoreHook().SET_ROLES(roles); |
| | | useUserStoreHook().SET_PERMS(permissions); |
| | | useUserStoreHook().SET_ISMANAGER(isManager); |
| | | storageLocal().setItem(userKey, { |
| | | refreshToken, |
| | | expires, |
| | |
| | | username, |
| | | nickname, |
| | | exRoles, |
| | | permissions |
| | | roles, |
| | | permissions, |
| | | isManager |
| | | }); |
| | | } |
| | | |
| | | if (data.exRoles) { |
| | | const { username, exRoles } = data; |
| | | if (data.exRoles && data.roles) { |
| | | const { username, exRoles, roles, isManager } = data; |
| | | setUserKey({ |
| | | avatar: data?.avatar ?? "", |
| | | username, |
| | | nickname: data?.nickname ?? "", |
| | | exRoles, |
| | | permissions: data?.permissions ?? [] |
| | | roles, |
| | | permissions: data?.permissions ?? [], |
| | | isManager |
| | | }); |
| | | } else { |
| | | const avatar = |
| | |
| | | storageLocal().getItem<DataInfo<number>>(userKey)?.nickname ?? ""; |
| | | const exRoles = |
| | | storageLocal().getItem<DataInfo<number>>(userKey)?.exRoles ?? []; |
| | | const roles = |
| | | storageLocal().getItem<DataInfo<number>>(userKey)?.roles ?? []; |
| | | const permissions = |
| | | storageLocal().getItem<DataInfo<number>>(userKey)?.permissions ?? []; |
| | | const isManager = |
| | | storageLocal().getItem<DataInfo<number>>(userKey)?.isManager ?? false; |
| | | setUserKey({ |
| | | avatar, |
| | | username, |
| | | nickname, |
| | | exRoles, |
| | | permissions |
| | | roles, |
| | | permissions, |
| | | isManager |
| | | }); |
| | | } |
| | | } |
| | | |
| | | export function setEnterpriseInfo(data: DataInfo<Date>) { |
| | | export function setEnterpriseInfo(data: CusExtendDto) { |
| | | useUserStoreHook().SET_ENTERPRISEINFO(data); |
| | | useUserStoreHook().SET_USERNAME(data.enterpriseName); |
| | | storageLocal().setItem("enterpriseInfo", data); |
| | |
| | | storageLocal().setItem(userKey, obj); |
| | | } |
| | | |
| | | export function setRoleListInfo(data: DataInfo<Date>) { |
| | | useUserStoreHook().SET_ROLES_LIST(data); |
| | | export function setRoleListInfo(data: RoleItem[]) { |
| | | useUserStoreHook().SET_EXROLES_LIST(data); |
| | | storageLocal().setItem("rolesList", data); |
| | | } |
| | | |
| | | export function setNowRole(data: DataInfo<Date>) { |
| | | export function setNowRole(data: nowRoleType) { |
| | | const list = storageLocal().getItem<DataInfo<number>>(userKey)?.exRoles ?? []; |
| | | if (list.length > 0) { |
| | | const obj = list.find(item => { |
| | | return item.code == data.code; |
| | | }); |
| | | data = { ...obj, ...data }; |
| | | data = Object.assign(obj, data); |
| | | } |
| | | useUserStoreHook().SET_ROLES([data.name]); |
| | | useUserStoreHook().SET_NOW_ROLE(data); |
| | | storageLocal().setItem("nowRole", data); |
| | | } |