123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- import axios from 'axios'
- import qs from 'qs'
- import imgPath from "@/assets/image/jiazai.gif"
- const TIME_OUT_MS = 180 * 1000 // 默认请求超时时间
- function prompt() {
- window.ELEMENT.Message({
- message: `<div style="display: flex;justify-content: center;flex-wrap: wrap;margin: 20px 0">
- <img src="${imgPath}" alt="" style="width: 122px;height: 85px;margin-bottom: 20px">
- <p style="width: 100%;text-align: center;color: #999"> 技术人员更新系统中,请一分钟后重试</p></div>`,
- type: 'info',
- dangerouslyUseHTMLString: true,
- center: true,
- duration: 10000,
- iconClass: 'el-icon'
- })
- console.log('执行多少次')
- }
- // 定时器
- let timer = null
- let timer2 = null
- let flgs = 0
- /*
- * @param response 返回数据列表
- */
- function handleResults (response) {
- let remoteResponse = response.data;
- return remoteResponse
- }
- function handleUrl (url) {
- 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) {
- let user = sessionStorage.getItem('user') , token = "";
- if(user != null){
- token = JSON.parse(user).id
- // data.token = token
- }
- if(localStorage.getItem('lang') == 'en') {
- data.lang = 'english'
- }
- 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',
- 'Token': token
- }
- }).then(
- (result) => {
- response(handleResults(result))
- }
- ).catch(
- (error) => {
- if (exception) {
- var str = error + ''
- if(str.indexOf('504') != '-1' || str.indexOf('502') != '-1') {
- if (flgs == 0) {
- timer = setTimeout(() => {
- prompt()
- }, 100)
- flgs++
- clearTimeout(timer2)
- timer2 = setTimeout(() => {
- flgs = 0
- }, 12000)
- }
- // exception(false)
- } else {
- exception(error)
- }
- } else {
- console.log(error, 3)
- }
- }
- )
- },
- WPGpost (url, data, response, exception) {
- let user = sessionStorage.getItem('user') , token = "";
- if(user != null){
- token = JSON.parse(user).id
- }
- axios({
- method: 'post',
- url: handleUrl(url),
-
- // timeout: TIME_OUT_MS,
- headers: {
- //'Content-Type': 'application/json; charset=UTF-8'
- 'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
- 'Token': token,
- 'app_id': '3bf356d5-bdba-48d4-b1f5-e91468beefa3',
- 'app_key': 'f2960f3bf3c5ca58ee0c6970c1242e87-1655272886142-371814'
- },
- data: {
- 'headers' : {
- 'Authorization' : ''
- },
- 'IT_TAB': {
- 'PSPHI': '',
- 'PROJK': '',
- 'GJAHR': '',
- 'MONAT': '',
- 'ZUNAM': '',
- 'ZID': '',
- 'ZDEP': '',
- 'DMBTR': '',
- }
- }
- }).then(
- (result) => {
- response(handleResults(result))
- }
- ).catch(
- (error) => {
- if (exception) {
- var str = error + ''
- if(str.indexOf('504') != '-1' || str.indexOf('502') != '-1') {
- if (flgs == 0) {
- timer = setTimeout(() => {
- prompt()
- }, 100)
- flgs++
- clearTimeout(timer2)
- timer2 = setTimeout(() => {
- flgs = 0
- }, 12000)
- }
- // exception(false)
- } else {
- exception(error)
- }
- } else {
- console.log(error, 3)
- }
- }
- )
- },
- /*
- * get 请求
- * @param url
- * @param response 请求成功时的回调函数
- * @param exception 异常的回调函数
- */
- get (url , response, exception) {
- let user = sessionStorage.getItem('user') , token = "";
- if(user != null){
- token = JSON.parse(user).id
- // data.token = token
- }
- axios({
- method: 'get',
- url: handleUrl(url),
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8',
- 'Token': token
- }
- }).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) {
- let user = sessionStorage.getItem('user') , token = "";
- if(user != null){
- token = JSON.parse(user).id
- // data.token = token
- }
- axios({
- method: 'post',
- url: handleUrl(url),
- data: handleParams(data),
- dataType: 'json',
- processData: false,
- contentType: false,
- headers: {
- 'Token': token
- }
- }).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, fileName, exception) {
- var user = sessionStorage.getItem('user') , token = "";
- if(user != null){
- token = JSON.parse(user).id
- }
- axios({
- method: 'post',
- url: handleUrl(url),
- data: handleParams(qs.stringify(data)),
- responseType: 'blob',
- headers: {
- 'Token': token
- }
- }).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)
- }
- }
- ).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)
- }
- }
- )
- }
- }
|