60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import type { OrgSnapshotResponse } from '@/types/api';
|
|
import { apiRequest } from './api';
|
|
import {
|
|
getAuth,
|
|
hasSelectedPosition,
|
|
normalizePosition,
|
|
saveSelectedPosition,
|
|
type UserPosition
|
|
} from './auth';
|
|
|
|
export const userTabPages = new Set([
|
|
'/pages/user/today/index',
|
|
'/pages/user/practice/index',
|
|
'/pages/user/sop/index',
|
|
'/pages/user/profile/index'
|
|
]);
|
|
|
|
export const isUserRoute = (url: string) => url.startsWith('/pages/user/');
|
|
|
|
export const positionSetupUrl = (redirect: string) =>
|
|
`/pages/auth/position/index?redirect=${encodeURIComponent(redirect || '/pages/user/today/index')}`;
|
|
|
|
export const goToMobileTarget = (url: string) => {
|
|
if (userTabPages.has(url)) {
|
|
uni.switchTab({ url });
|
|
return;
|
|
}
|
|
uni.redirectTo({ url });
|
|
};
|
|
|
|
export const hydratePositionFromOrg = async (phone = getAuth().phone): Promise<UserPosition | ''> => {
|
|
const keyword = phone.trim();
|
|
if (!keyword) return '';
|
|
try {
|
|
const snapshot = await apiRequest<OrgSnapshotResponse>({
|
|
url: `/api/aihr/org/snapshot?keyword=${encodeURIComponent(keyword)}&status=active&pageSize=1`,
|
|
method: 'GET',
|
|
timeout: 15000,
|
|
clearAuthOnUnauthorized: false
|
|
});
|
|
const officialPosition = snapshot.rows?.[0]?.positionName || '';
|
|
return saveSelectedPosition(officialPosition);
|
|
} catch {
|
|
return '';
|
|
}
|
|
};
|
|
|
|
export const nextUserRouteAfterLogin = async (redirect: string) => {
|
|
if (!isUserRoute(redirect) || hasSelectedPosition()) return redirect;
|
|
const officialPosition = await hydratePositionFromOrg();
|
|
return normalizePosition(officialPosition) ? redirect : positionSetupUrl(redirect);
|
|
};
|
|
|
|
export const ensureEmployeePosition = async (redirect: string) => {
|
|
const nextRoute = await nextUserRouteAfterLogin(redirect);
|
|
if (nextRoute === redirect) return true;
|
|
uni.navigateTo({ url: nextRoute });
|
|
return false;
|
|
};
|