import 'whatwg-fetch' import Cookies from 'universal-cookie'; export const cookies = new Cookies(); export const csrf_token = cookies.get('XSRF-TOKEN'); /* ================= 工具方法 ================== */ /** * 拼接get请求 * @param data * @returns {string} */ export function object2string(o) { const arr = [] for (let i in o) { if (Array.isArray(o[i])) { o[i].forEach((item) => { arr.push(`${i}[]=${item}`) }) } else { arr.push(`${i}=${o[i]}`) } } return arr.join('&') } export async function get(path, query = {}) { const queryString = object2string(query) if (queryString.length > 0) { path += `?${queryString}` } const response = await fetch(path, { method: 'GET', credentials: 'same-origin' }) if (!response.ok) { throw new Error(response.status) } let res try { res = await response.json() } catch (err) { throw new Error('服务器错误') } if (res.success === false) { throw new Error(res.msg || '请求失败') } else { (res.code === -401) && window.location.reload(); return res } } /** * 网络数据请求工具方法 : fetch POST 请求 * @param dispatch redux派发action * @param path 路径 * @param data post参数 * @param successFun 请求成功回调 */ export async function post(path, data = {}, query = {}, type = 'form') { const queryString = object2string(query) if (queryString.length > 0) { path += `?${queryString}` } let response; if (type === 'json') { response = await fetch(path, { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-XSRF-TOKEN': csrf_token }, body: JSON.stringify(data) }) } else { response = await fetch(path, { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-XSRF-TOKEN': csrf_token }, body: object2string(data) }) } if (!response.ok) { throw new Error(response.status) } let res try { res = await response.json() } catch (err) { throw new Error('服务器错误') } if (res.success === false) { throw new Error(res.msg || '请求失败') } else { (res.code === -401) && window.location.reload(); return res } } /** * 跨域访问 * @param {[type]} path [description] * @param {Object} query [description] * @return {[type]} [description] */ export async function getCors(path, query = {}) { const queryString = object2string(query) if (queryString.length > 0) { path += `?${queryString}` } const response = await fetch(path, { method: 'GET', credentials: 'same-origin', mode: 'cors' }) if (!response.ok) { throw new Error(response.status) } let res try { res = await response.json() } catch (err) { throw new Error('服务器错误') } if (res.success === false) { throw new Error(res.msg || '请求失败') } else { (res.code === -401) && window.location.reload(); return res } } export async function postCors(path, data = {}, query = {}, type = 'form') { const queryString = object2string(query) if (queryString.length > 0) { path += `?${queryString}` } let response; if (type === 'json') { response = await fetch(path, { method: 'POST', credentials: 'same-origin', mode: 'cors', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) } else { response = await fetch(path, { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: object2string(data) }) } if (!response.ok) { throw new Error(response.status) } let res try { res = await response.json() } catch (err) { return // throw new Error('服务器错误') } if (res.success === false) { throw new Error(res.msg || '请求失败') } else { (res.code === -401) && window.location.reload(); return res } }