1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { defineStore } from 'pinia';
- import { Cookie, Session } from '@/utils/storage';
- import { getUserInfo } from '@/api/login/user';
- import { JSEncrypt } from 'jsencrypt';
- import { encryptionLoginKey } from '@/utils/constants';
- /**
- * @description 用户信息
- * @methods getUserInfos 获取用户信息
- * @methods setUserInfos 设置用户信息
- */
- export const useUserInfo = defineStore('userInfo', {
- state: (): UserInfosStates => ({
- userInfos: {
- account: '', // 账号
- name: '', // 姓名
- phoneNo: '', //电话
- staffNo: '', // 工号
- id: '',
- photo: '', //头像
- authBtnList: [], // 授权按钮数组
- defaultTelNo: '', // 默认分机号
- defaultTelGroup: '', // 默认分机组
- token: '',
- showTelControl: false, // 是否展示坐席操作电话面板
- orgName: '', // 组织名称
- roles: [], // 角色
- isCenter: false, // 当前本部门是否是中心
- monitor: false, // 是否是班长
- encryptionHeader: '', // 加密用户信息(用户数据共享平台加密header)
- },
- }),
- actions: {
- async setUserInfos(buttons: string[]) {
- if (Session.get('userInfo')) {
- //有缓存 取缓存
- this.userInfos = Session.get('userInfo');
- } else {
- this.userInfos = (await this.getApiUserInfo(buttons)) as any;
- }
- },
- async getApiUserInfo(buttons: string[]) {
- try {
- // 个人信息
- let userInfo: any = await getUserInfo();
- this.userInfos.name = userInfo.result?.user.name ?? '暂无名称';
- this.userInfos.account = userInfo.result?.user.account ?? '';
- this.userInfos.phoneNo = userInfo.result?.user.phoneNo ?? '';
- this.userInfos.staffNo = userInfo.result?.user.staffNo ?? '';
- this.userInfos.defaultTelNo = userInfo.result.user.defaultTelNo ?? '';
- this.userInfos.defaultTelGroup = userInfo.result.user.defaultTelGroup ?? '';
- this.userInfos.id = userInfo.result?.user.id ?? '';
- this.userInfos.roles = userInfo.result?.user.roles ?? [];
- this.userInfos.token = Cookie.get('token') ?? '';
- this.userInfos.photo = '';
- this.userInfos.orgName = userInfo.result?.user.organization?.name ?? '';
- this.userInfos.isCenter = userInfo.result?.user.organization?.isCenter ?? false;
- this.userInfos.monitor = userInfo.result?.monitor ?? false;
- //授权按钮
- this.userInfos.showTelControl = buttons.includes('public:seat:panel'); // 查询是否有展示面板权限
- this.userInfos.authBtnList = buttons;
- // 新建一个JSEncrypt对象
- const encryptor = new JSEncrypt({ default_key_size: '2048' });
- encryptor.setPublicKey(encryptionLoginKey); // publicKey为公钥
- // 加密数据
- const submitObj: { userId: string; userName: string; orgName: string } = {
- userId: this.userInfos.id,
- userName: this.userInfos.name,
- orgName: this.userInfos.orgName,
- };
- this.userInfos.encryptionHeader = <string>encryptor.encrypt(JSON.stringify(submitObj));
- Session.set('userInfo', this.userInfos);
- return this.userInfos;
- } catch (error) {
- this.userInfos = {
- account: '暂无名称', // 账号
- name: '', // 姓名
- phoneNo: '', //电话
- staffNo: '', // 工号
- id: '',
- photo: '', //头像
- authBtnList: [], // 授权按钮数组
- defaultTelNo: '', // 默认分机号
- defaultTelGroup: '', // 默认分机组
- token: '',
- showTelControl: false, // 是否展示坐席操作电话面板
- orgName: '', // 组织名称
- roles: [], // 角色
- isCenter: false, // 当前部门是否是中心
- monitor: false, // 是否是班长
- encryptionHeader: '', // 加密用户信息(用户数据共享平台加密header)
- };
- Session.set('userInfo', this.userInfos);
- return this.userInfos;
- }
- },
- },
- });
|