103 lines
2.2 KiB
TypeScript
103 lines
2.2 KiB
TypeScript
import type { LocaleType } from '@/types/common';
|
|
import { Locales } from '@/types/common';
|
|
import { useUserStore } from '@/stores/user.ts';
|
|
|
|
export const defaultLocale = Locales.en_US;
|
|
export const locales: Locales[] = [
|
|
Locales.ar_SA,
|
|
Locales.en_US,
|
|
Locales.es_ES,
|
|
Locales.ko_KR,
|
|
Locales.th_TH,
|
|
Locales.vi_VN,
|
|
Locales.zh_CN,
|
|
Locales.zh_TW,
|
|
Locales.hi_IN,
|
|
Locales.id_ID
|
|
];
|
|
|
|
export function setLocale(locale: LocaleType) {
|
|
// vantLocale.use(Locales[locale], vantLocales[locale]);
|
|
uni.setLocale(locale);
|
|
//#ifdef H5
|
|
window.location.reload();
|
|
//#endif
|
|
}
|
|
export const languages = {
|
|
'ar-SA': 'ar_SA',
|
|
'en-US': 'en_US',
|
|
'es-ES': 'es_ES',
|
|
'hi-IN': 'hi_IN',
|
|
'ko-KR': 'ko_KR',
|
|
'th-TH': 'th_TH',
|
|
'vi-VN': 'vi_VN',
|
|
'zh-CN': 'zh_CN',
|
|
'zh-TW': 'zh_TW',
|
|
'in-ID': 'id_ID'
|
|
};
|
|
|
|
export const UserLanguage: LocaleType = languages[uni.getLocale() as LocaleType] ?? 'en_US';
|
|
|
|
export function getToken() {
|
|
const userStore = useUserStore();
|
|
const token = userStore.token ?? uni.getStorageSync('access_token');
|
|
return token;
|
|
}
|
|
|
|
export function clearToken() {
|
|
const userStore = useUserStore();
|
|
userStore.clear();
|
|
uni.removeStorageSync('access_token');
|
|
}
|
|
|
|
export const patterns = {
|
|
/**
|
|
* 除英文字母、空格、-、'、@ 外
|
|
*/
|
|
name: /[^a-zA-Z\s\-'@/]/g,
|
|
/**
|
|
* 需包含8-15个字符
|
|
*/
|
|
passwordPattern1: /^.{8,15}$/,
|
|
/**
|
|
* 需包含至少一个大写和小写字母
|
|
*/
|
|
passwordPattern2: /(?=.*[a-z])(?=.*[A-Z]).+/,
|
|
/**
|
|
* 需包含至少一个数字
|
|
*/
|
|
passwordPattern3: /(?=.*\d).+/,
|
|
/**
|
|
* 需包含#,@,!其中一个字符
|
|
*/
|
|
passwordPattern4: /.*[@#!].*/,
|
|
/**
|
|
* 不允许包含空格
|
|
*/
|
|
noSpace: /^\S*$/
|
|
};
|
|
|
|
export function isValidNumber(val: string | number) {
|
|
if (isNaN(Number(val))) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export function getNumberLocaleIntegerPart(number: number | string) {
|
|
if (isNaN(parseInt(number?.toString()))) {
|
|
return 0;
|
|
} else {
|
|
return Math.floor(Number(number)).toLocaleString('en-US');
|
|
}
|
|
}
|
|
|
|
export function getNumberDecimalPart(number: number | string, precision = 2) {
|
|
if (isNaN(parseInt(number?.toString()))) {
|
|
return '00';
|
|
} else {
|
|
return Number(number).toFixed(precision).toString().split('.')[1];
|
|
}
|
|
}
|