You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.5 KiB
61 lines
1.5 KiB
let commit; |
|
|
|
export const setCommit = (data) => { |
|
commit = data; |
|
}; |
|
export const post = (url, data) => { |
|
const t = new Date().getTime(); |
|
data.t = t; |
|
const options: any = { |
|
method: 'POST', |
|
credentials: 'same-origin', // 设置为 同源 以发送 Cookie |
|
body: JSON.stringify(data), |
|
headers: { |
|
'Content-Type': 'application/json', |
|
}, |
|
}; |
|
return new Promise((reslove, reject) => { |
|
fetch(url, options) |
|
.then(responseToJson) |
|
.then((data) => reslove(getResult(data))) |
|
.catch((error) => reslove(false)); |
|
}); |
|
}; |
|
export const get = (url, data?) => { |
|
const t = new Date().getTime(); |
|
if (data) { |
|
data.t = t; |
|
const params = new URLSearchParams(data); |
|
url += '?' + params; |
|
} else { |
|
url += '?t=' + t; |
|
} |
|
const options: any = { |
|
method: 'GET', |
|
credentials: 'same-origin', // 设置为 同源 以发送 Cookie |
|
headers: { |
|
'Content-Type': 'application/json', |
|
}, |
|
}; |
|
return new Promise((reslove, reject) => { |
|
fetch(url.toString(), options) |
|
.then(responseToJson) |
|
.then((data) => reslove(getResult(data))) |
|
.catch((error) => reslove(false)); |
|
}); |
|
}; |
|
|
|
const responseToJson = (response) => { |
|
if (response.ok) { |
|
return response.json(); |
|
} else { |
|
commit('set_sys_info', { msg: '网络错误', type: 'warning' }); |
|
} |
|
}; |
|
|
|
const getResult = (data) => { |
|
if (data.message) { |
|
commit('set_sys_info', { msg: data.message, type: data.success ? 'win' : 'warning' }); |
|
} |
|
return data.data; |
|
};
|
|
|