封装isBroswer()

This commit is contained in:
tlyong1992
2022-05-24 17:01:42 +08:00
parent a324cc127c
commit 8c19d88630
29 changed files with 110 additions and 114 deletions

View File

@@ -2,7 +2,7 @@ import zhCN from './lang/zh-CN'
import enUS from './lang/en-US'
import zhHK from './lang/zh-HK'
import zhTW from './lang/zh-TW'
import { mergeDeep } from './utils'
import { isBrowser, mergeDeep } from './utils'
const lang = {
'en-US': enUS,
'zh-CN': zhCN,
@@ -45,7 +45,7 @@ export function generateLocaleDict (langString) {
* 根据用户当前浏览器语言进行切换
*/
export function initLocale (locale, changeLocale) {
if (typeof window !== 'undefined') {
if (isBrowser()) {
const targetLocale = generateLocaleDict(window.navigator.language)
if (JSON.stringify(locale) !== JSON.stringify(targetLocale)) {
changeLocale(targetLocale)

View File

@@ -38,6 +38,7 @@ async function getPageWithRetry(id, from, retryAttempts = 3) {
console.info('[响应成功]:', `from:${from}`, `id:${id}`)
return pageData
} catch (e) {
console.warn('[响应异常]:', e)
await delay(1000)
const cacheKey = 'page_block_' + id
const pageBlock = await getDataFromCache(cacheKey)

View File

@@ -32,7 +32,7 @@ export function loadExternalResource(url, type) {
* @returns
*/
export function getQueryVariable(variable) {
const query = typeof window !== 'undefined' ? window.location.search.substring(1) : ''
const query = isBrowser() ? window.location.search.substring(1) : ''
const vars = query.split('&')
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
@@ -99,3 +99,9 @@ export function deepClone(obj) {
* @returns
*/
export const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
/**
* 判断是否客户端
* @returns {boolean}
*/
export const isBrowser = () => typeof window !== 'undefined'