123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import axios from 'axios'
- import qs from 'qs'
- const TIME_OUT_MS = 60 * 1000 // 默认请求超时时间
- /*
- * @param response 返回数据列表
- */
- function handleResults (response) {
- let remoteResponse = response.data;
- return remoteResponse
- }
- function handleUrl (url) {
- if(url.indexOf('tdatasizeinfowarehouseunits') > -1) {
- url = LINK_URL + url;
- } else {
- url = BASE_URL + url;
- }
- return url
- }
- /*
- * @param data 参数列表
- * @return
- */
- function handleParams (data) {
- return data
- }
- export default {
- /*
- * @param url
- * @param data
- * @param response 请求成功时的回调函数
- * @param exception 异常的回调函数
- */
- post (url, data, response, exception) {
- axios({
- method: 'post',
- url: handleUrl(url),
- data: handleParams(qs.stringify(data)),
- // timeout: TIME_OUT_MS,
- headers: {
- //'Content-Type': 'application/json; charset=UTF-8'
- 'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
- }
- }).then(
- (result) => {
- response(handleResults(result))
- }
- ).catch(
- (error) => {
- if (exception) {
- exception(error)
- } else {
- console.log(error)
- }
- }
- )
- },
- /*
- * get 请求
- * @param url
- * @param response 请求成功时的回调函数
- * @param exception 异常的回调函数
- */
- get (url , response, exception) {
- axios({
- method: 'get',
- url: handleUrl(url),
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- }
- }).then(
- (result) => {
- response(handleResults(result))
- }
- ).catch(
- (error) => {
- if (exception) {
- exception(error)
- } else {
- console.log(error)
- }
- }
- )
- },
- /*
- * 导入文件
- * @param url
- * @param data
- * @param response 请求成功时的回调函数
- * @param exception 异常的回调函数
- */
- uploadFile (url, data, response, exception) {
- axios({
- method: 'post',
- url: handleUrl(url),
- data: handleParams(data),
- dataType: 'json',
- processData: false,
- contentType: false
- }).then(
- (result) => {
- response(handleResults(result, data))
- }
- ).catch(
- (error) => {
- if (exception) {
- exception(error)
- } else {
- console.log(error)
- }
- }
- )
- },
- /*
- * 下载文件用,导出 Excel 表格可以用这个方法
- * @param url
- * @param param
- * @param fileName 如果是导出 Excel 表格文件名后缀最好用.xls 而不是.xlsx,否则文件可能会因为格式错误导致无法打开
- * @param exception 异常的回调函数
- */
- downloadFile (url, data, response, exception) {
- axios({
- method: 'post',
- url: handleUrl(url),
- data: handleParams(qs.stringify(data)),//handleParams(data),
- responseType: 'blob'
- }).then(
- (result) => {
- // const excelBlob = result.data
- // if ('msSaveOrOpenBlob' in navigator) {
- // window.navigator.msSaveOrOpenBlob(excelBlob, fileName)
- // } else {
- // const elink = document.createElement('a')
- // elink.download = fileName
- // elink.style.display = 'none'
- // const blob = new Blob([excelBlob])
- // elink.href = URL.createObjectURL(blob)
- // document.body.appendChild(elink)
- // elink.click()
- // document.body.removeChild(elink)
- // }
- response(handleResults(result,data))
- }
- ).catch(
- (error) => {
- if (exception) {
- exception(error)
- } else {
- console.log(error)
- }
- }
- )
- },
- uploadFileFormData (url, data, response, exception) {
- axios({
- method: 'post',
- url: handleUrl(url),
- data: data,
- // timeout: TIME_OUT_MS,
- headers: {
- 'Content-Type': 'multipart/form-data'
- }
- }).then(
- (result) => {
- response(handleResults(result))
- }
- ).catch(
- (error) => {
- if (exception) {
- exception(error)
- } else {
- console.log(error)
- }
- }
- )
- }
- }
|