一单四制优化及加入工作流
This commit is contained in:
56
utils/identitySwitch.js
Normal file
56
utils/identitySwitch.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import { switchIdentity } from '@/request/identity.js';
|
||||
import { getProfileDetail } from '@/request/three_one_api/info.js';
|
||||
import { saveUserInfoToStorage } from '@/utils/userInfo.js';
|
||||
|
||||
function unwrapPayload(res) {
|
||||
if (!res) return {};
|
||||
const data = res.data;
|
||||
if (data && typeof data === 'object' && (
|
||||
data.token ||
|
||||
data.identities ||
|
||||
data.currentIdentity ||
|
||||
data.roles
|
||||
)) {
|
||||
return data;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** 根据个人信息接口刷新本地 userInfo */
|
||||
export async function refreshUserInfoStorage() {
|
||||
const res = await getProfileDetail();
|
||||
if (res.code === 0 && res.data) {
|
||||
return saveUserInfoToStorage(res.data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 执行身份切换:更新 token,再通过 profile 接口刷新 userInfo */
|
||||
export async function performIdentitySwitch(identityId) {
|
||||
const res = await switchIdentity(identityId);
|
||||
const payload = unwrapPayload(res);
|
||||
|
||||
if (payload.token) {
|
||||
uni.setStorageSync('token', payload.token);
|
||||
}
|
||||
|
||||
if (payload.currentIdentity) {
|
||||
uni.setStorageSync('currentIdentity', JSON.stringify(payload.currentIdentity));
|
||||
}
|
||||
|
||||
await refreshUserInfoStorage();
|
||||
return payload;
|
||||
}
|
||||
|
||||
/** 切换成功后重启到首页,清空页面栈 */
|
||||
export function reLaunchAfterSwitch() {
|
||||
uni.showToast({
|
||||
title: '身份切换成功',
|
||||
icon: 'success'
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: '/pages/index/index'
|
||||
});
|
||||
}, 1500);
|
||||
}
|
||||
Reference in New Issue
Block a user