mirror of
https://github.com/BewlyBewly/BewlyBewly.git
synced 2025-04-14 13:15:29 +00:00
refactor: utils file
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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
47
src/utils/main.ts
Normal 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')
|
||||
}
|
||||
Reference in New Issue
Block a user