import { useUserStore } from '@/stores/modules/user';
|
import Taro from '@tarojs/taro';
|
import { object2query } from '@life-payment/utils';
|
import { useQuery } from '@tanstack/vue-query';
|
import { MaybeRef } from 'vue';
|
import { useRefeshDidShow } from './infiniteLoading';
|
import { LoginFormTabs } from '@/subpackages/login/constants';
|
import { useLoginedJump } from './login';
|
import {
|
useLifeRechargeContext,
|
APIgetLifePayWxIndentityParams,
|
WxMiniAppIndentityInfo,
|
LifePayPhoneMesssageCodeLoginOutput,
|
} from '@life-payment/core-vue';
|
import { isInWeChat } from '@/utils/env';
|
|
export function useVirtualUserInfo() {
|
const { blLifeRecharge } = useLifeRechargeContext();
|
|
const { data: virtualUserInfo } = useQuery({
|
queryKey: ['blLifeRecharge/lifePayUserMesssageByIduser', blLifeRecharge.accountModel.userId],
|
queryFn: async () => {
|
return await blLifeRecharge.services.lifePayUserMesssageByIduser(
|
{
|
id: blLifeRecharge.accountModel.userId,
|
},
|
{
|
showLoading: false,
|
}
|
);
|
},
|
placeholderData: () => ({} as LifePayPhoneMesssageCodeLoginOutput),
|
enabled: computed(() => !!blLifeRecharge.accountModel.userId),
|
staleTime: Infinity,
|
});
|
|
return { virtualUserInfo };
|
}
|
|
export function useUser() {
|
const userStore = useUserStore();
|
|
const {
|
userDetail,
|
userInfo,
|
locationCity,
|
virtualUserId,
|
virtualPhoneNumber,
|
wxCode,
|
wxOpenId,
|
} = storeToRefs(userStore);
|
|
function updateUserInfo() {
|
return userStore.getCurrentUserInfo();
|
}
|
|
const { blLifeRecharge } = useLifeRechargeContext();
|
// const { virtualUserInfo } = useVirtualUserInfo();
|
|
const isChannelAccount = computed(() => blLifeRecharge.accountModel.isBackClientUser);
|
|
return {
|
user: userInfo,
|
userDetail: userDetail,
|
updateUserInfo,
|
locationCity,
|
virtualUserId,
|
virtualPhoneNumber,
|
wxCode,
|
wxOpenId,
|
isChannelAccount,
|
// virtualUserInfo,
|
};
|
}
|
|
export function useIsLogin() {
|
const { virtualUserId } = useUser();
|
// return computed(() => !!user.value);
|
return computed(() => !!virtualUserId.value);
|
}
|
|
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();
|
useInitWeixinJSBridge();
|
|
Taro.useReady(async () => {
|
if (isLogin.value && userStore.firstGetUserDetail) {
|
// userStore.getCurrentUserInfo();
|
}
|
if (needAuth && !isLogin.value) {
|
RouteHelper.navigateTo({
|
url: `${RouterPath.loginByForm}?redirect=${router.path}&${object2query(router.params)}`,
|
});
|
}
|
});
|
|
return { isAuth };
|
}
|
|
export function useInitWeixinJSBridge() {
|
const { wxCode } = useUser();
|
|
const userStore = useUserStore();
|
const router = Taro.useRouter();
|
|
const code = router.params.code ?? '';
|
|
const { blLifeRecharge } = useLifeRechargeContext();
|
|
const { getLifePayWxIndentity } = useLifePayWxIndentity();
|
|
Taro.useReady(async () => {
|
try {
|
//@ts-ignore
|
if (!code && isInWeChat && !wxCode.value && location?.replace) {
|
location.replace(
|
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf940ff1d35a98493&redirect_uri=${location.href}&response_type=code&scope=snsapi_base#wechat_redirect`
|
);
|
}
|
if (!!code && !wxCode.value) {
|
userStore.setWxCode(router.params.code ?? '');
|
getLifePayWxIndentity(code);
|
}
|
} catch (error) {}
|
});
|
}
|
|
let wxIndentityPromise: Promise<WxMiniAppIndentityInfo>;
|
|
export function useLifePayWxIndentity() {
|
const { blLifeRecharge } = useLifeRechargeContext();
|
const userStore = useUserStore();
|
|
async function getLifePayWxIndentity(code: string) {
|
try {
|
let params: APIgetLifePayWxIndentityParams = {
|
code: code,
|
};
|
if (!wxIndentityPromise) {
|
wxIndentityPromise = blLifeRecharge.services
|
.getLifePayWxIndentity(params, {
|
showLoading: false,
|
})
|
.finally(() => {
|
wxIndentityPromise = undefined;
|
});
|
}
|
|
let res = await wxIndentityPromise;
|
if (res.openId) {
|
userStore.setWxOpenId(res.openId);
|
}
|
return res;
|
} catch (error) {}
|
}
|
|
return { getLifePayWxIndentity };
|
}
|
|
export function useEnsureOpenId() {
|
const { wxCode, wxOpenId } = useUser();
|
const { getLifePayWxIndentity } = useLifePayWxIndentity();
|
|
async function ensureOpenId() {
|
if (wxOpenId.value) {
|
return wxOpenId.value;
|
} else {
|
let rea = await getLifePayWxIndentity(wxCode.value);
|
return rea.openId ? rea.openId : wxOpenId.value;
|
}
|
}
|
|
return { ensureOpenId };
|
}
|
|
export function useGoLogin() {
|
const router = Taro.useRouter();
|
const { redirectParams } = useLoginedJump();
|
function goLoginFn() {
|
// RouteHelper.navigateTo({
|
// url: `${RouterPath.authorization}?redirect=${router.path}&${object2query(router.params)}`,
|
// });
|
|
RouteHelper.navigateTo({
|
url: `${RouterPath.loginByForm}?redirect=${redirectParams.value}&tab=${LoginFormTabs.VerificationCodeLogin}`,
|
});
|
}
|
|
return {
|
goLoginFn,
|
};
|
}
|