refactor: utils file

This commit is contained in:
Hakadao
2023-03-15 02:28:12 +08:00
parent 0d2be4b30c
commit 475bdb0b0e
3 changed files with 52 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
import { language } from '~/logic'
import { i18n } from '~/utils'
import { i18n } from '~/utils/i18n'
export const { t } = i18n.global
export const numFormatter = (num: number) => {

View File

@@ -1,54 +1,5 @@
import { grantAccessKey, revokeAccessKey } from './authProvider'
import { SVG_ICONS } from './svgIcons'
import { i18n } from './i18n'
export { grantAccessKey, revokeAccessKey, SVG_ICONS, i18n }
export * from './authProvider'
export * from './svgIcons'
export * from './i18n'
export * from './dataFormatter'
/**
* get cookie by name
* @param name cookie name
* @returns cookie value
*/
export const getCookie = (name: string) => {
const value = `; ${document.cookie}`
const parts: Array<string> = value.split(`; ${name}=`)
if (parts.length === 2)
return parts?.pop()?.split(';').shift()
}
/**
* set cookie
* @param name cookie name
* @param value cookie value
*/
export const setCookie = (name: string, value: any, expDays: number) => {
const date = new Date()
date.setTime(date.getTime() + expDays * 24 * 60 * 60 * 1000)
const expires = `expires=${date.toUTCString()}`
document.cookie = `${name}=${value}; ${expires}; domain=.bilibili.com; path=/`
}
/**
* get current login user id
* @returns userId
*/
export const getUserID = () => getCookie('DedeUserID')
/**
* get csrf token
*/
export const getCSRF = () => getCookie('bili_jct')
/**
* remove 'http://' or 'https://' from a URL
* @param url
* @returns
*/
export const removeHttpFromUrl = (url: string) => {
return url.replace(/^https?:/, '')
}
export const openLinkToNewTab = (url: string) => {
window.open(url, '_blank')
}
export * from './main'

47
src/utils/main.ts Normal file
View File

@@ -0,0 +1,47 @@
/**
* get cookie by name
* @param name cookie name
* @returns cookie value
*/
export const getCookie = (name: string) => {
const value = `; ${document.cookie}`
const parts: Array<string> = value.split(`; ${name}=`)
if (parts.length === 2)
return parts?.pop()?.split(';').shift()
}
/**
* set cookie
* @param name cookie name
* @param value cookie value
*/
export const setCookie = (name: string, value: any, expDays: number) => {
const date = new Date()
date.setTime(date.getTime() + expDays * 24 * 60 * 60 * 1000)
const expires = `expires=${date.toUTCString()}`
document.cookie = `${name}=${value}; ${expires}; domain=.bilibili.com; path=/`
}
/**
* get current login user id
* @returns userId
*/
export const getUserID = () => getCookie('DedeUserID')
/**
* get csrf token
*/
export const getCSRF = () => getCookie('bili_jct')
/**
* remove 'http://' or 'https://' from a URL
* @param url
* @returns
*/
export const removeHttpFromUrl = (url: string) => {
return url.replace(/^https?:/, '')
}
export const openLinkToNewTab = (url: string) => {
window.open(url, '_blank')
}