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; };