zhengyiming
21 小时以前 5cd618c9523ad30dccf858a00ff6d99a28de4187
packages/components/src/hooks/index.ts
@@ -11,49 +11,97 @@
  UserAccountListOutput,
  AddUpdateUserAccountInput,
  GasParValueResponse,
  ChannelRateOutput,
} from '@life-payment/core-vue';
import { useQuery, useQueryClient } from '@tanstack/vue-query';
import { computed, MaybeRef, reactive, unref } from 'vue';
import { useInfiniteLoading } from './infiniteLoading';
import { OrderInputType } from '../constants';
import { convertOrderFrontStatus } from '../utils';
import { useLifePayRateChannelAllList } from './rate';
export function useGetRate() {
  const { blLifeRecharge } = useLifeRechargeContext();
  const { data: lifePayRateList, isLoading } = useQuery({
    queryKey: ['blLifeRecharge/getRate'],
  // const { data: lifePayRateList, isLoading } = useQuery({
  //   queryKey: ['blLifeRecharge/getRate'],
  //   queryFn: async () => {
  //     return await blLifeRecharge.services.getRate({ showLoading: false });
  //   },
  //   placeholderData: () => [] as LifePayRateListOutput[],
  // });
  const { allRateChannelList } = useLifePayRateChannelAllList();
  const hasChannel = computed(() => !!blLifeRecharge.accountModel.channlesNum);
  const { data: channelRate } = useQuery({
    queryKey: ['blLifeRecharge/getChannelRate', blLifeRecharge.accountModel.channlesNum],
    queryFn: async () => {
      return await blLifeRecharge.services.getRate({ showLoading: false });
      return await blLifeRecharge.services.getChannelRate(
        {
          checkChannelId: blLifeRecharge.accountModel.channlesNum,
        },
        { showLoading: false }
      );
    },
    placeholderData: () => [] as LifePayRateListOutput[],
    placeholderData: () => ({} as ChannelRateOutput),
    enabled: hasChannel,
  });
  const lifePayPhoneRate = computed(
    () =>
      lifePayRateList.value.find(
        (x) => x.rateType === blLifeRecharge.constants.LifePayRateTypeEnum.默认话费折扣
      )?.rate ?? 100
  );
  const lifePayPhoneRate = computed(() => {
    if (hasChannel.value && channelRate.value.channlesRate) {
      return channelRate.value.channlesRate;
    }
    // return (
    //   lifePayRateList.value.find(
    //     (x) => x.rateType === blLifeRecharge.constants.LifePayRateTypeEnum.默认话费折扣
    //   )?.rate ?? 0
    // );
    return (
      allRateChannelList.value
        .filter((x) => x.lifePayOrderType == blLifeRecharge.constants.LifePayOrderTypeEnum.话费订单)
        .toSorted((a, b) => a.platformRate - b.platformRate)?.[0]?.platformRate ?? 0
    );
  });
  const lifePayElectricRate = computed(
    () =>
      lifePayRateList.value.find(
        (x) => x.rateType === blLifeRecharge.constants.LifePayRateTypeEnum.默认电费折扣
      )?.rate ?? 100
  );
  const lifePayElectricRate = computed(() => {
    if (hasChannel.value && channelRate.value.channlesRate) {
      return channelRate.value.channlesRate;
    }
    // return (
    //   lifePayRateList.value.find(
    //     (x) => x.rateType === blLifeRecharge.constants.LifePayRateTypeEnum.默认电费折扣
    //   )?.rate ?? 0
    // );
    return (
      allRateChannelList.value
        .filter((x) => x.lifePayOrderType == blLifeRecharge.constants.LifePayOrderTypeEnum.电费订单)
        .toSorted((a, b) => a.platformRate - b.platformRate)?.[0]?.platformRate ?? 0
    );
  });
  const lifePayGasRate = computed(
    () =>
      lifePayRateList.value.find(
        (x) => x.rateType === blLifeRecharge.constants.LifePayRateTypeEnum.默认燃气折扣
      )?.rate ?? 100
  );
  const lifePayGasRate = computed(() => {
    if (hasChannel.value && channelRate.value.channlesRate) {
      return channelRate.value.channlesRate;
    }
    // return (
    //   lifePayRateList.value.find(
    //     (x) => x.rateType === blLifeRecharge.constants.LifePayRateTypeEnum.默认燃气折扣
    //   )?.rate ?? 0
    // );
    return (
      allRateChannelList.value
        .filter((x) => x.lifePayOrderType == blLifeRecharge.constants.LifePayOrderTypeEnum.燃气订单)
        .toSorted((a, b) => a.platformRate - b.platformRate)?.[0]?.platformRate ?? 0
    );
  });
  return {
    lifePayRateList,
    // lifePayRateList,
    lifePayPhoneRate,
    lifePayElectricRate,
    lifePayGasRate,
    allRateChannelList,
  };
}
@@ -63,7 +111,7 @@
  const { data: phoneParValueList, isLoading } = useQuery({
    queryKey: ['blLifeRecharge/getPhoneParValue'],
    queryFn: async () => {
      return await blLifeRecharge.services.getPhoneParValue({ showLoading: false });
      return await blLifeRecharge.services.getPhoneParValue({}, { showLoading: false });
    },
    select(data) {
      return data?.phoneParValue ?? [];
@@ -82,7 +130,7 @@
  const { data: electricParValueList, isLoading } = useQuery({
    queryKey: ['blLifeRecharge/getElectricSupportArea'],
    queryFn: async () => {
      return await blLifeRecharge.services.getElectricSupportArea({ showLoading: false });
      return await blLifeRecharge.services.getElectricSupportArea({}, { showLoading: false });
    },
    select(data) {
      return data.electricAreaList ?? [];
@@ -109,7 +157,7 @@
  // });
  const { infiniteLoadingProps } = useInfiniteLoading(
    ({ pageParam }) => {
    async ({ pageParam }) => {
      let params: QueryLifePayOrderListInput = {
        pageModel: {
          rows: 20,
@@ -120,9 +168,18 @@
        userId: blLifeRecharge.accountModel.userId,
      };
      return blLifeRecharge.services.getUserLifePayOrderPage(params, {
      let res = await blLifeRecharge.services.getUserLifePayOrderPage(params, {
        showLoading: false,
      });
      res.data = res.data.map((x) => ({
        ...x,
        frontStatus: convertOrderFrontStatus(
          x.payStatus,
          x.lifePayOrderStatus,
          x.lifePayRefundStatus
        ),
      }));
      return res;
    },
    {
      queryKey: [
@@ -132,6 +189,12 @@
          userId: blLifeRecharge.accountModel.userId,
        },
      ],
      select(data) {
        console.log('data: ', data);
        //  data.pages.map((item) => item.data.map((x) => convertOrderFrontStatus(x)))
        return data;
      },
    }
  );
@@ -218,19 +281,27 @@
type UseSetUserAccountBySelectOptions = {
  lifePayOrderType: MaybeRef<LifeRechargeConstants.LifePayOrderTypeEnum>;
  onSetUserAccount: (currentUserAccount: UserAccountListOutput) => any;
  getDefaultUserAccount?: (
    userAccountList: UserAccountListOutput[]
  ) => Promise<UserAccountListOutput | undefined>;
};
export function useSetUserAccountBySelect({
  lifePayOrderType,
  onSetUserAccount,
  getDefaultUserAccount = (data) => Promise.resolve(data[0]),
}: UseSetUserAccountBySelectOptions) {
  const { userAccountAllList } = useUserAccountAllList({
    lifePayOrderType: lifePayOrderType,
    onSuccess(data) {
      if (data.length > 0) {
        const currentUserAccount = data[0];
        onSetUserAccount?.(currentUserAccount);
      }
    async onSuccess(data) {
      try {
        if (data.length > 0) {
          const currentUserAccount = await getDefaultUserAccount(data);
          if (currentUserAccount) {
            onSetUserAccount?.(currentUserAccount);
          }
        }
      } catch (error) {}
    },
  });
@@ -251,7 +322,7 @@
  const { data: gasParValueList, isLoading } = useQuery({
    queryKey: ['blLifeRecharge/getGasParValue'],
    queryFn: async () => {
      return await blLifeRecharge.services.getGasParValue({ showLoading: false });
      return await blLifeRecharge.services.getGasParValue({}, { showLoading: false });
    },
    select(data) {
      return data?.gasParValue ?? [];
@@ -263,3 +334,32 @@
    gasParValueList,
  };
}
type UseIntroInfoOptions = {
  lifePayOrderType: MaybeRef<LifeRechargeConstants.LifePayOrderTypeEnum>;
  onSuccess?: (data: API.LifePayIntroInfoOutput[]) => any;
};
export function useIntroInfo({ lifePayOrderType, onSuccess }: UseIntroInfoOptions) {
  const { blLifeRecharge } = useLifeRechargeContext();
  const { data: introInfo } = useQuery({
    queryKey: ['blLifeRecharge/getIntroInfo', lifePayOrderType],
    queryFn: async () => {
      return await blLifeRecharge.services.getIntroInfo(
        {
          type: unref(lifePayOrderType),
        },
        {
          showLoading: false,
        }
      );
    },
    placeholderData: () => [] as API.LifePayIntroInfoOutput[],
    onSuccess: (data) => {
      onSuccess?.(data);
    },
  });
  return { introInfo };
}