zhengyiming
2 天以前 bd7dd96c732ded6854d47bf77f65e5c64d3d15e2
apps/taro/src/hooks/user.ts
@@ -6,8 +6,36 @@
import { useRefeshDidShow } from './infiniteLoading';
import { LoginFormTabs } from '@/subpackages/login/constants';
import { useLoginedJump } from './login';
import { useLifeRechargeContext, APIgetLifePayWxIndentityParams } from '@life-payment/core-vue';
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();
@@ -26,6 +54,11 @@
    return userStore.getCurrentUserInfo();
  }
  const { blLifeRecharge } = useLifeRechargeContext();
  // const { virtualUserInfo } = useVirtualUserInfo();
  const isChannelAccount = computed(() => blLifeRecharge.accountModel.isBackClientUser);
  return {
    user: userInfo,
    userDetail: userDetail,
@@ -35,6 +68,8 @@
    virtualPhoneNumber,
    wxCode,
    wxOpenId,
    isChannelAccount,
    // virtualUserInfo,
  };
}
@@ -83,6 +118,8 @@
  const { blLifeRecharge } = useLifeRechargeContext();
  const { getLifePayWxIndentity } = useLifePayWxIndentity();
  Taro.useReady(async () => {
    try {
      //@ts-ignore
@@ -93,23 +130,58 @@
      }
      if (!!code && !wxCode.value) {
        userStore.setWxCode(router.params.code ?? '');
        let res = await getLifePayWxIndentity();
        userStore.setWxOpenId(res.openId);
        getLifePayWxIndentity(code);
      }
    } catch (error) {}
  });
}
  async function getLifePayWxIndentity() {
let wxIndentityPromise: Promise<WxMiniAppIndentityInfo>;
export function useLifePayWxIndentity() {
  const { blLifeRecharge } = useLifeRechargeContext();
  const userStore = useUserStore();
  async function getLifePayWxIndentity(code: string) {
    try {
      let params: APIgetLifePayWxIndentityParams = {
        code: code,
      };
      let res = await blLifeRecharge.services.getLifePayWxIndentity(params, {
        showLoading: false,
      });
      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() {