import { useUserStore } from '@/stores/modules/user';
|
import Taro from '@tarojs/taro';
|
import { object2query, LocationUtils } from '@12333/utils';
|
import {
|
ParkOrHRStatus,
|
UserCertificationFrontStatus,
|
MatchMakingIdentityEnum,
|
} from '@12333/constants';
|
import * as userServices from '@12333/services/api/User';
|
import { useQuery } from '@tanstack/vue-query';
|
import { MaybeRef } from 'vue';
|
import { useRefeshDidShow } from '@12333/hooks/infiniteLoading';
|
|
export function useUser() {
|
const userStore = useUserStore();
|
|
const { userDetail, userInfo, matchMakingIdentity, isSetMatchMakingIdentity, locationCity } =
|
storeToRefs(userStore);
|
|
function updateUserInfo() {
|
return userStore.getCurrentUserInfo();
|
}
|
|
const isCompanyAudited = computed(() => {
|
return userDetail.value?.openHRSiteStatus === ParkOrHRStatus.Running;
|
});
|
|
/**
|
* 是否完善个人信息(企业名称,手机号)
|
*/
|
const isCompletePersonalInfo = computed(() => {
|
return (
|
!!userDetail.value?.customerName &&
|
!!userDetail.value?.contacter &&
|
!!userDetail.value?.cityName
|
);
|
});
|
|
const isCertified = computed(() => {
|
return userDetail.value?.frontStatus === UserCertificationFrontStatus.Certified;
|
});
|
|
return {
|
user: userInfo,
|
userDetail: userDetail,
|
updateUserInfo,
|
isCompletePersonalInfo,
|
isCompanyAudited,
|
isCertified,
|
matchMakingIdentity,
|
isSetMatchMakingIdentity,
|
locationCity,
|
};
|
}
|
|
export function useIsLogin() {
|
const { user, userDetail } = useUser();
|
return computed(() => !!user.value);
|
// return user?.isPersonal ? !!user : !!user && !!enterpriseInfo;
|
}
|
|
type UseAuthOptions = {
|
needAuth?: boolean;
|
};
|
|
export function useAuth(options: UseAuthOptions = {}) {
|
const { needAuth = true } = options;
|
const userStore = useUserStore();
|
|
const isLogin = useIsLogin();
|
|
const isAuth = computed(() => !needAuth || isLogin.value);
|
|
const router = Taro.useRouter();
|
|
Taro.useReady(async () => {
|
let res;
|
try {
|
res = await LocationUtils.getLocation();
|
} catch (error) {}
|
if (isLogin.value && userStore.firstGetUserDetail) {
|
userStore.firstGetUserDetail = false;
|
if (LocationUtils.isProvinceChange(userStore.locationProvince)) {
|
userStore.resetState();
|
} else {
|
userStore.getCurrentUserInfo();
|
}
|
}
|
if (res?.result?.ad_info?.city && userStore.firstSetLocation) {
|
userStore.setLocationCity(res.result.ad_info.city, res.result.ad_info.province);
|
}
|
if (needAuth && !isLogin.value) {
|
Taro.navigateTo({
|
url: `${RouterPath.authorization}?redirect=${router.path}&${object2query(router.params)}`,
|
});
|
}
|
});
|
|
return { isAuth };
|
}
|
|
export function useGoLogin() {
|
const router = Taro.useRouter();
|
|
function goLoginFn() {
|
Taro.navigateTo({
|
url: `${RouterPath.authorization}?redirect=${router.path}&${object2query(router.params)}`,
|
});
|
}
|
|
return {
|
goLoginFn,
|
};
|
}
|
|
type UseUserTotalInfoOptions = {
|
userId: MaybeRef<string>;
|
enabled?: MaybeRef<boolean>;
|
};
|
|
export function useUserTotalInfo({ userId, enabled = true }: UseUserTotalInfoOptions) {
|
const { data, refetch } = useQuery({
|
queryKey: ['userServices/getUserTotalInfo', userId],
|
queryFn: async () => {
|
return await userServices.getUserTotalInfo(
|
{
|
userId: unref(userId),
|
},
|
{ showLoading: false }
|
);
|
},
|
placeholderData: () => ({} as API.GetUserTotalInfoOutput),
|
enabled: computed(() => unref(enabled) && !!unref(userId)),
|
});
|
|
useRefeshDidShow({ queryKey: ['userServices/getUserTotalInfo', userId] });
|
|
return {
|
userTotalInfo: data,
|
refetch,
|
};
|
}
|
|
export function useUserSimpleInfo({ userId }: UseUserTotalInfoOptions) {
|
const { data, refetch } = useQuery({
|
queryKey: ['userServices/getUserSimpleInfo', userId],
|
queryFn: async () => {
|
return await userServices.getUserSimpleInfo(
|
{
|
userId: unref(userId),
|
},
|
{ showLoading: false }
|
);
|
},
|
placeholderData: () => ({} as API.UserSimpleInfo),
|
enabled: computed(() => !!unref(userId)),
|
});
|
|
return {
|
userSimpleInfo: data,
|
};
|
}
|
|
type UseToggleMatchMakingIdentityOnLaunchOptions = {
|
matchMakingIdentity: MatchMakingIdentityEnum;
|
};
|
|
export function useToggleMatchMakingIdentityOnLaunch({
|
matchMakingIdentity,
|
}: UseToggleMatchMakingIdentityOnLaunchOptions) {
|
const launchOptions = Taro.getEnterOptionsSync();
|
console.log('launchOptions: ', launchOptions);
|
|
const userStore = useUserStore();
|
const { userDetail } = useUser();
|
|
onMounted(async () => {
|
try {
|
if (launchOptions.scene === 1037) {
|
await userStore.setMatchMakingIdentity({
|
matchMakingIdentity: matchMakingIdentity,
|
userId: userDetail.value?.userId,
|
});
|
}
|
} catch (error) {}
|
});
|
}
|