123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import accounting from 'accounting'
- const setIntervalImmediately = (fn, duration) => setInterval((() => (fn(), fn))(), duration)
- async function needLogin() {
- const route = useRoute()
- const useAuth = useAuthStore()
- const { token } = storeToRefs(useAuth)
- if (!token.value) {
- await navigateTo({
- path: '/login',
- query: {
- redirect: route.fullPath,
- ...route.query
- },
- replace: true
- })
- return
- }
- }
- function jumpToLoginPage() {
- const route = useRoute()
- navigateTo({
- path: '/login',
- query: {
- redirect: route.path,
- ...route.query
- },
- replace: true
- })
- }
- function formatImgSrc(srcArr) {
- if (Array.isArray(srcArr) && srcArr.length > 0) {
- return srcArr[0] ?? ''
- }
- return ''
- }
- function priceToArray(price) {
- if (!price) return ['0', '00']
- const priceStr = price.toString()
- if (!priceStr.includes('.')) {
- return [priceStr, '00']
- }
- return [priceStr.split('.')[0], priceStr.split('.')[1]]
- }
- function isEmail(email) {
- if (!email) return false
- const value = email.trim()
- if (
- /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
- value
- )
- ) {
- return true
- }
- return false
- }
- const isEmptyValue = (value) => {
- if (value == null) return true
- if (Array.isArray(value) && value.length === 0) return true
- if (typeof value === 'object' && Object.keys(value).length === 0) return true
- return false
- }
- const formatNumber = (n) => {
- let num = Number(n ?? '')
-
- if (num >= 10000 * 10000) {
- return '9999万+'
- }
-
- else if (num >= 10000) {
- let w = Math.floor(num / 10000)
- let remainder = num % 10000
-
- let decimalPart = ''
- if (remainder > 0) {
-
- decimalPart = ('.' + Math.floor(remainder / 100)).slice(0, 2)
-
- decimalPart = decimalPart.replace(/\.?0+$/, '')
- }
- return `${w}${decimalPart}万`
- }
-
- else {
-
- }
- }
- const transferCount = (num = 0) => {
- if (isNaN(num)) return 0
- if (num > 10000) {
- return (num / 10000).toFixed(1) + 'w'
- } else {
- return num
- }
- }
- const transferCountChinese = (num = 0) => {
- if (isNaN(num)) return 0
- if (num > 10000) {
- return (num / 10000).toFixed(1) + '万'
- } else {
- return num
- }
- }
- export {
- setIntervalImmediately,
- needLogin,
- jumpToLoginPage,
- formatImgSrc,
- isEmail,
- formatNumber,
- isEmptyValue,
- priceToArray,
- transferCount,
- transferCountChinese
- }
|