瀏覽代碼

登陆异常

Zimo 1 周之前
父節點
當前提交
227d81371c
共有 1 個文件被更改,包括 14 次插入3 次删除
  1. 14 3
      src/config/axios/index.ts

+ 14 - 3
src/config/axios/index.ts

@@ -4,16 +4,27 @@ import { config } from './config'
 
 const { default_headers } = config
 
+const DEFAULT_LANG = 'zh-CN'
+
+const getRequestLang = () => {
+  try {
+    const langStore = JSON.parse(localStorage.getItem('lang') || '{}')
+    const lang = typeof langStore === 'string' ? langStore : langStore?.v
+
+    return typeof lang === 'string' && lang ? lang.replace(/"/g, '') : DEFAULT_LANG
+  } catch {
+    return DEFAULT_LANG
+  }
+}
+
 const request = (option: any) => {
   const { headersType, headers, ...otherOption } = option
 
-  const langStore = JSON.parse(localStorage.getItem('lang') || '{}')
-
   return service({
     ...otherOption,
     headers: {
       'Content-Type': headersType || default_headers,
-      Lang: langStore.v.replace(/"/g, ''),
+      Lang: getRequestLang(),
       ...headers
     }
   })