Commit 50e56559 by Caiyijun

change

parent 8435b1cc
import axios from 'axios';//引入axios import axios from 'axios'//引入axios
export function fetch(options) { export function fetch(options) {
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
if (!window['userData']) { if (!window['userData']) {
let user_data_str = localStorage.getItem('walk_userData'); let user_data_str = localStorage.getItem('walk_userData')
if (user_data_str) { if (user_data_str) {
window['userData'] = JSON.parse(user_data_str); window['userData'] = JSON.parse(user_data_str)
} }
} }
if (!window['userData']) { if (!window['userData']) {
localStorage.setItem('last_url', location.hash.slice(1)); localStorage.setItem('last_url', location.hash.slice(1))
location.replace('#/Author'); location.replace('#/Author')
} }
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -18,28 +18,28 @@ export function fetch(options) { ...@@ -18,28 +18,28 @@ export function fetch(options) {
//所有的请求都会带上这些配置,比如全局都要用的身份信息等。 //所有的请求都会带上这些配置,比如全局都要用的身份信息等。
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': process.env.NODE_ENV === 'production' ? (window['userData']?window['userData'].token:'') : "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAiLCJvcGVuX2lkIjoiMTExMTExMTExMTExMTExMTExMTExMTExMTExMSIsInVuaW9uX2lkIjoiMTExMTExMTExMTExMTExMTExMTExMTExMTExMSIsImV4cCI6MTU1NDQ1NzU3Mn0.ehCAENl8zjdMyF4y44Z2-XEascaDhEbUF3BaNsWbfY8" 'Authorization': process.env.NODE_ENV === 'production' ? (window['userData'] ? window['userData'].token : '') : "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAiLCJvcGVuX2lkIjoiMTExMTExMTExMTExMTExMTExMTExMTExMTExMSIsInVuaW9uX2lkIjoiMTExMTExMTExMTExMTExMTExMTExMTExMTExMSIsImV4cCI6MTU1NDQ1NzU3Mn0.ehCAENl8zjdMyF4y44Z2-XEascaDhEbUF3BaNsWbfY8"
// 'token_in_header': global_.token,//token从全局变量那里传过来 // 'token_in_header': global_.token,//token从全局变量那里传过来
}, },
timeout: 30 * 1000 // 30秒超时 timeout: 30 * 1000 // 30秒超时
}); })
instance(options) instance(options)
.then(response => { //then 请求成功之后进行什么操作 .then(response => { //then 请求成功之后进行什么操作
console.log('raw response',response); console.log('raw response', response)
response.data['date'] = response.headers.date; response.data['date'] = response.headers.date
resolve(response.data);//把请求到的数据发到引用请求的地方 resolve(response.data)//把请求到的数据发到引用请求的地方
}) })
.catch(error => { .catch(error => {
console.log('请求异常信息:', error); console.log('请求异常信息:', error)
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
if (error.response.status == 401) { if (error.response.status == 401) {
localStorage.setItem('last_url', location.hash.slice(1)); localStorage.setItem('last_url', location.hash.slice(1))
localStorage.setItem('walk_userData', ''); localStorage.setItem('walk_userData', '')
location.replace('#/Author'); location.replace('#/Author')
} else { } else {
reject(error); reject(error)
} }
} }
}); })
}); })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment