Files
boursecu_user/src/views/base/wallet/composables/useWalletPage.ts
T

326 lines
9.5 KiB
TypeScript
Raw Normal View History

import { computed, inject, onMounted, provide, ref, watch, type InjectionKey, type Ref } from 'vue'
import OtpInput from 'vue3-otp-input'
import { formatCashWalletBalanceText } from '@/utils/productDealMethod'
import { usePriceWithoutType } from '@/utils/usePrice'
import { loading, loadingMore } from '@/utils'
import router from '@/router'
import { useWalletStore } from '@/store/walletStore'
import type { Transaction } from '@/models/transaction'
import { createCard, withdraw, confirmCode } from '@/api/wallet'
import { useInfiniteScroll } from '@vueuse/core'
import { message } from 'ant-design-vue'
import type { Card } from '@/models/wallet'
import { useUserStore } from '@/store/userStore'
import { isCustomerIdentityVerified } from '@/utils/customerIdentity'
import { useConfigStore } from '@/store/configStore'
2026-07-05 11:24:40 +02:00
import {
getWalletDepositQuickAmountsRial,
} from '@/utils/config'
import { useDesktopBreakpoint } from '@/layouts/composables/useAppLayout'
import { useSupportMessenger } from '@/composables/useSupportMessenger'
import { loadSupportMessengerFromSettings } from '@/utils/supportMessenger'
import {
initWalletPageData,
loadMoreWalletDeposits,
loadMoreWalletWithdrawals,
refreshWalletCards,
walletCardItems,
walletDepositItems,
walletWithdrawItems,
} from './useWalletPageData'
export interface WalletPageContext {
isDeposit: Ref<boolean>
walletStore: ReturnType<typeof useWalletStore>
config: ReturnType<typeof useConfigStore>
showAddCard: Ref<boolean>
chargeAccountModal: Ref<boolean>
showConfirmCode: Ref<boolean>
withdrawInfModal: Ref<boolean>
infoVisible: Ref<boolean>
identityRequiredVisible: Ref<boolean>
refOtpInput: Ref<InstanceType<typeof OtpInput> | null>
form: Ref<Transaction>
2026-06-18 19:09:50 +03:30
extraNote: Ref<string>
cardForm: Ref<Card>
disableButton: ReturnType<typeof computed<boolean>>
cardItems: typeof walletCardItems
depositItems: typeof walletDepositItems
withdrawItems: typeof walletWithdrawItems
loading: typeof loading
loadingMore: typeof loadingMore
mobileDepositScrollEl: Ref<HTMLElement | null>
desktopDepositScrollEl: Ref<HTMLElement | null>
supportMessengerText: ReturnType<typeof computed<string>>
2026-06-16 19:47:28 +03:30
depositGuideSupportLink: ReturnType<typeof computed<string>>
depositSmsSupportLink: ReturnType<typeof computed<string>>
openDepositSupportMessenger: () => Promise<boolean>
2026-07-05 11:24:40 +02:00
depositQuickAmounts: ReturnType<typeof computed<readonly number[]>>
usePriceWithoutType: typeof usePriceWithoutType
formatCashWalletBalanceText: typeof formatCashWalletBalanceText
setIsDeposit: (val: boolean) => void
useStyle: () => string
formatter: (value: number) => string
percentChange: (val: number) => void
ok: () => Promise<void>
addCard: () => Promise<void>
getCards: () => Promise<void>
handleOnComplete: (value: string) => Promise<void>
}
export const WALLET_PAGE_CONTEXT_KEY: InjectionKey<WalletPageContext> = Symbol('walletPageContext')
export function useWalletPage() {
const isDeposit = ref(true)
const walletStore = useWalletStore()
const userStore = useUserStore()
const config = useConfigStore()
const isDesktop = useDesktopBreakpoint()
const {
messengerLink: depositGuideSupportLink,
depositSmsLink: depositSmsSupportLink,
supportMessengerText,
openDepositSupportMessenger,
} = useSupportMessenger()
const showAddCard = ref(false)
const chargeAccountModal = ref(false)
const showConfirmCode = ref(false)
const withdrawInfModal = ref(false)
const identityRequiredVisible = ref(false)
function ensureIdentity(): boolean {
if (isCustomerIdentityVerified(userStore.userInfo?.is_legal)) return true
identityRequiredVisible.value = true
return false
}
const infoVisible = ref(false)
const refOtpInput = ref<InstanceType<typeof OtpInput> | null>(null)
const mobileDepositScrollEl = ref<HTMLElement | null>(null)
const desktopDepositScrollEl = ref<HTMLElement | null>(null)
const activeDepositScrollEl = computed(() =>
isDesktop.value ? desktopDepositScrollEl.value : mobileDepositScrollEl.value,
)
2026-07-05 11:24:40 +02:00
const depositQuickAmounts = computed(() => getWalletDepositQuickAmountsRial())
2026-06-18 19:09:50 +03:30
const extraNote = ref('')
const form = ref<Transaction>({
value: undefined,
percentWallet: 0,
card_id: undefined,
})
const cardForm = ref<Card>({
birth_date: 0,
account_number: '',
bank_name: '',
cart_number: '',
shaba_number: '',
})
const disableButton = computed(() => {
const minWithdrawAmount = 200_000
2026-06-29 11:20:02 +02:00
const minDepositAmount = 1_000_000
if (!loading.value) {
if (walletStore?.walletInfo?.cash && !isDeposit.value) {
if (walletCardItems.value.length > 0) {
return (
walletStore?.walletInfo?.cash <= 0
|| walletStore?.walletInfo?.cash < parseInt(form.value?.value)
|| isNaN(parseInt(form.value?.value))
2026-06-29 11:20:02 +02:00
|| parseInt(form.value?.value) < minWithdrawAmount
)
}
return true
}
if (isDeposit.value) {
2026-06-29 11:20:02 +02:00
return isNaN(parseInt(form.value?.value)) || parseInt(form.value?.value) < minDepositAmount
}
return false
}
return true
})
const unitCalculator = computed(() => (walletStore?.walletInfo?.cash ?? 0) / 100)
function resetData() {
form.value.value = undefined
form.value.percentWallet = 0
2026-06-18 19:09:50 +03:30
extraNote.value = ''
}
async function addCard() {
try {
loading.value = true
const res: any = await createCard(cardForm.value)
await getCards()
showAddCard.value = false
cardForm.value = {
birth_date: 0,
account_number: '',
bank_name: '',
cart_number: '',
shaba_number: '',
}
message.success(
res?.message || 'اطلاعات کارت بانکی شما ثبت شد و پس از تایید ادمین فعال سازی خواهد شد',
)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
}
async function getCards() {
await refreshWalletCards()
form.value.card_id = walletCardItems.value.length ? walletCardItems.value[0].id : undefined
}
function setIsDeposit(val: boolean) {
isDeposit.value = val
form.value.value = undefined
form.value.percentWallet = 0
}
function useStyle() {
if (isDeposit.value) return 'right: 2%;'
return ' right: 98%; transform: translate(100%, 0);'
}
const formatter = (value: number) => `${value}%`
function percentChange(val: number) {
if (val !== 0) form.value.value = parseInt((unitCalculator.value * val).toFixed())
else form.value.value = undefined
}
async function ok() {
if (!ensureIdentity()) return
try {
if (form.value.value !== undefined) {
loading.value = true
if (!isDeposit.value) {
await confirmCode({ value: form.value.value })
showConfirmCode.value = true
}
if (isDeposit.value) {
chargeAccountModal.value = true
}
} else {
message.error('ابتدا مبلغ را وارد کنید')
}
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
}
async function handleOnComplete(value: string) {
if (!ensureIdentity()) return
try {
infoVisible.value = true
showConfirmCode.value = false
2026-06-18 19:09:50 +03:30
await withdraw({
...form.value,
otp: value,
extra_note: extraNote.value.trim() || undefined,
})
await walletStore.getMyWallet()
await message.success('عملیات با موفقیت انجام شد')
withdrawInfModal.value = true
resetData()
} catch (e) {
console.log(e)
} finally {
infoVisible.value = false
refOtpInput.value?.clearInput()
}
}
onMounted(async () => {
const walletState = router.currentRoute.value.query.state
if (walletState === 'deposit') {
isDeposit.value = true
} else if (walletState === 'withdraw') {
isDeposit.value = false
}
cardForm.value.birth_date = userStore.userInfo?.birth_date
await initWalletPageData()
form.value.card_id = walletCardItems.value.length ? walletCardItems.value[0].id : undefined
})
watch(chargeAccountModal, async (open) => {
if (!open) return
await loadSupportMessengerFromSettings(true)
})
if (!userStore.isSingle) {
useInfiniteScroll(
activeDepositScrollEl,
async () => {
if (isDeposit.value) {
await loadMoreWalletDeposits()
} else {
await loadMoreWalletWithdrawals()
}
},
{ distance: 12 },
)
}
const context: WalletPageContext = {
isDeposit,
walletStore,
config,
showAddCard,
chargeAccountModal,
showConfirmCode,
withdrawInfModal,
infoVisible,
identityRequiredVisible,
refOtpInput,
form,
2026-06-18 19:09:50 +03:30
extraNote,
cardForm,
disableButton,
cardItems: walletCardItems,
depositItems: walletDepositItems,
withdrawItems: walletWithdrawItems,
loading,
loadingMore,
mobileDepositScrollEl,
desktopDepositScrollEl,
supportMessengerText,
2026-06-16 19:47:28 +03:30
depositGuideSupportLink,
depositSmsSupportLink,
openDepositSupportMessenger,
2026-07-05 11:24:40 +02:00
depositQuickAmounts,
usePriceWithoutType,
formatCashWalletBalanceText,
setIsDeposit,
useStyle,
formatter,
percentChange,
ok,
addCard,
getCards,
handleOnComplete,
}
provide(WALLET_PAGE_CONTEXT_KEY, context)
return context
}
export function useWalletPageContext() {
const context = inject(WALLET_PAGE_CONTEXT_KEY)
if (!context) {
throw new Error('useWalletPageContext must be used within a page that calls useWalletPage()')
}
return context
}