536 lines
14 KiB
JavaScript
536 lines
14 KiB
JavaScript
import * as tools from './tools';
|
|
import appconsts from "../service/appconsts";
|
|
import { message } from 'antd';
|
|
|
|
|
|
function checkStatus(response) {
|
|
if (response.status >= 200 && response.status < 300) {
|
|
return response;
|
|
}
|
|
|
|
const error = new Error(response.statusText);
|
|
error.response = response;
|
|
throw error;
|
|
}
|
|
|
|
function getToken() {
|
|
return localStorage.getItem('TOKEN');
|
|
}
|
|
|
|
function request(url, options,type) {
|
|
const opt = { ...options };
|
|
opt.headers = opt.headers || {};
|
|
opt.headers.Accept = 'application/json';
|
|
// opt.credentials = opt.credentials || 'include';
|
|
|
|
return fetch(url, opt)
|
|
.then(checkStatus)
|
|
.then(response => type=='blob'?response.blob():response.json())
|
|
.then(data => ({ data })) // { data: 接口实际返回的数据 }
|
|
.catch(err => ({ err })); // { err: 错误信息 }
|
|
}
|
|
|
|
async function send(url, options) {
|
|
try {
|
|
const res = await request(url, options);
|
|
|
|
if (!('data' in res) && 'err' in res) {
|
|
//message.error(getErrCode(res.err.message));
|
|
return null;
|
|
}
|
|
|
|
const { code } = res.data;
|
|
if (code === 405) {
|
|
// debugger;
|
|
//window.location.href = '/mgr/home';// /mgr/home /login
|
|
window.location.href = '/tsg/#/login';
|
|
// window.location.hash = '#/login';
|
|
}
|
|
return res.data;
|
|
} catch (e) {
|
|
//message.error(e);
|
|
}
|
|
|
|
return {};
|
|
}
|
|
export function xyt_httpput(url, data = {}) {
|
|
const options = {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'adcd': localStorage.getItem('ADCD6'),
|
|
"authorization":"Bearer" + ' ' + localStorage.getItem('access_token')
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
export function httpget(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
if (tools.strIsEmpty(v)) { continue; }
|
|
|
|
params.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
|
|
}
|
|
if (!data._) {
|
|
params.push(`_=${Date.now()}`);
|
|
}
|
|
const token = getToken();
|
|
if (token) {
|
|
params.push(`token=${token}`);
|
|
}
|
|
const strparams = params.join('&');
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${strparams}`;
|
|
} else {
|
|
url += `?${strparams}`;
|
|
}
|
|
}
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"Authorization": localStorage.getItem('access_token')
|
|
},
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httpget2(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
if (tools.strIsEmpty(v)) { continue; }
|
|
|
|
params.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
|
|
}
|
|
// if (!data._) {
|
|
// params.push(`_=${Date.now()}`);
|
|
// }
|
|
const strparams = params.join('&');
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${strparams}`;
|
|
} else {
|
|
url += `?${strparams}`;
|
|
}
|
|
}
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"gs-token": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
"SHOW_LOADING":true
|
|
},
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httpgetExport(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
if (tools.strIsEmpty(v)) { continue; }
|
|
|
|
params.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
|
|
}
|
|
// if (!data._) {
|
|
// params.push(`_=${Date.now()}`);
|
|
// }
|
|
const strparams = params.join('&');
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${strparams}`;
|
|
} else {
|
|
url += `?${strparams}`;
|
|
}
|
|
}
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"gs-token": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
"SHOW_LOADING":true
|
|
},
|
|
};
|
|
|
|
return sendFile(url, options);
|
|
}
|
|
export function xyt_httpget2(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
if (tools.strIsEmpty(v)) { continue; }
|
|
|
|
params.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
|
|
}
|
|
// if (!data._) {
|
|
// params.push(`_=${Date.now()}`);
|
|
// }
|
|
const strparams = params.join('&');
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${strparams}`;
|
|
} else {
|
|
url += `?${strparams}`;
|
|
}
|
|
}
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"cookie":'Token=' + localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
"SHOW_LOADING": true,
|
|
"authorization":"Bearer" + ' ' + localStorage.getItem('access_token')
|
|
},
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
export function httpget3(url, data = {}) {
|
|
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${data}`;
|
|
} else {
|
|
url += `/${data}`;
|
|
}
|
|
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"gs-token": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
"SHOW_LOADING":true
|
|
},
|
|
};
|
|
return send(url, options);
|
|
}
|
|
export function httpget6(url) {
|
|
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"gs-token": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
"SHOW_LOADING":true
|
|
},
|
|
};
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httpget4(url, data = {}) {
|
|
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${data}`;
|
|
} else {
|
|
url += `/${data}`;
|
|
}
|
|
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
//'Content-Type': 'application/pdf'
|
|
},
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
//预案
|
|
export function httpgetPlan(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
if (tools.strIsEmpty(v)) { continue; }
|
|
|
|
params.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
|
|
}
|
|
// if (!data._) {
|
|
// params.push(`_=${Date.now()}`);
|
|
// }
|
|
const strparams = params.join('&');
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${strparams}`;
|
|
} else {
|
|
url += `?${strparams}`;
|
|
}
|
|
}
|
|
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"Authorization": localStorage.getItem('access_token'),
|
|
"dataScope":"COUNTY",
|
|
"countyAdcd":localStorage.getItem('ADCD6') + '000000'+"000",
|
|
},
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
export function httpgetPlan1(url, data = {}) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${data}`;
|
|
} else {
|
|
url += `/${data}`;
|
|
}
|
|
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
"Authorization": localStorage.getItem('access_token'),
|
|
"dataScope":"COUNTY",
|
|
"countyAdcd":localStorage.getItem('ADCD6') + '000000'+"000",
|
|
},
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httppost(url, data = {}) {
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httppost1(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
/*if (tools.strIsEmpty(v)) { continue; }*/
|
|
|
|
params.push(`${encodeURIComponent(v)}`);
|
|
}
|
|
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${params}`;
|
|
} else {
|
|
url += `/${params}`;
|
|
}
|
|
}
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httppost2(url, data = {}) {
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'adcd': localStorage.getItem('ADCD6'),
|
|
"gs-token": localStorage.getItem('access_token'),
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httppost5(url, data = {}) {
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'adcd': localStorage.getItem('ADCD6'),
|
|
"gs-token": localStorage.getItem('access_token'),
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return sendFile(url, options);
|
|
}
|
|
|
|
|
|
export function httppost3(url, data = {}) {
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
"Content-Type":'application/json',
|
|
"Authorization": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httppost4(url, data = {}) {
|
|
const params = [];
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
/*if (tools.strIsEmpty(v)) { continue; }*/
|
|
|
|
params.push(`${k}=${encodeURIComponent(v)}`);
|
|
}
|
|
|
|
const strparams = params.join('&');
|
|
if (params.length > 0) {
|
|
if (url.indexOf('?') > 0) {
|
|
url += `&${strparams}`;
|
|
} else {
|
|
url += `?${strparams}`;
|
|
}
|
|
}
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
"Content-Type":'application/json',
|
|
"Authorization": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httpPostFile(url, data = {}) {
|
|
const form = new FormData();
|
|
for (const k in data) {
|
|
const v = data[k];
|
|
if (tools.strIsEmpty(v)) { continue; }
|
|
form.append(k, data[k]);
|
|
console.log(k,data[k])
|
|
}
|
|
|
|
const token = getToken();
|
|
if (token) {
|
|
form.append('token', token);
|
|
}
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
// 'Content-Type': 'multipart/form-data',
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: form,
|
|
};
|
|
return send(url, options);
|
|
}
|
|
// export function httpPostFile(url, data = {}) {
|
|
// const form = new FormData();
|
|
// form.append("file", data.file.originFileObj);
|
|
|
|
// const options = {
|
|
// method: 'POST',
|
|
// // responseType: 'blob',
|
|
// 'Content-Type': 'multipart/form-data',
|
|
// body: form,
|
|
// };
|
|
|
|
// return sendFile(url, options);
|
|
// }
|
|
export function httppostAddfile(url, data = {},key,fileKey) {
|
|
const form = new FormData();
|
|
let json=new Blob([JSON.stringify(data)],{ type: "application/json" })
|
|
// form.append(key, json);
|
|
if(data.file){
|
|
form.append("file", data.file.originFileObj??data.file);
|
|
}
|
|
const options = {
|
|
method: 'POST',
|
|
body: form,
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
async function sendFile(url, options) {
|
|
try {
|
|
const res = await request(url, options,'blob');
|
|
return res;
|
|
} catch (e) {
|
|
message.error(e);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
|
|
export function httpput(url, data = {}) {
|
|
const options = {
|
|
method: 'PUT',
|
|
headers: {
|
|
"Content-Type":'application/json',
|
|
"Authorization": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
|
|
export function httpdelete(url, data = {}) {
|
|
const options = {
|
|
method: 'DELETE',
|
|
headers: {
|
|
"Content-Type":'application/json',
|
|
"Authorization": localStorage.getItem('access_token'),
|
|
"dataScope":"CUSTOM_WATERSHED",
|
|
"customWatershedCode":localStorage.getItem('customWatershedCode'),
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
|
|
return send(url, options);
|
|
}
|
|
// export function httpPostFile(url, options = {}) {
|
|
// options.method = 'POST';
|
|
// options.processData = false;
|
|
// options.headers = options.headers || {};
|
|
|
|
// if (options.headers instanceof Headers) {
|
|
// options.headers.append('enctype', 'multipart/form-data');
|
|
// } else {
|
|
// options.headers.enctype = 'multipart/form-data';
|
|
// }
|
|
|
|
// return send(url, options);
|
|
// }
|
|
|
|
|
|
// export function download(url, params) {
|
|
// let parts = [];
|
|
// if (typeof params !== 'string') {
|
|
// for (const key in params) {
|
|
// const value = params[key];
|
|
// if (tools.strIsEmpty(value)) {
|
|
// continue;
|
|
// }
|
|
// parts.push(`${key}=${encodeURIComponent(`${value}`)}`);
|
|
// }
|
|
// } else {
|
|
// parts.push(params);
|
|
// }
|
|
// window.open(`${url}?${parts.join('&')}&token=${getToken()}`);
|
|
// }
|
|
|
|
|