axios的再次封装

  1 import axios from "axios";
  2 import { message, Modal, notification } from "ant-design-vue";
  3 import moment from "moment";
  4 import store from "../store";
  5 import db from "utils/localstorage";
  6 moment.locale("zh-cn");
  7 
  8 // const baseURL = "http://api.yworth.com.cn/"
  9 // const baseURL = "http://mapi.qzdcloud.com/"
 10 
 11 const baseURL = "http://192.168.0.111:9998/"
 12 // const baseURL = "http://192.168.0.111:9998/"
 13 
 14 // 统一配置
 15 let QZDSOFT_REQUEST = axios.create({
 16   baseURL: baseURL,
 17   responseType: "json",
 18   validateStatus(status) {
 19     // 200 外的状态码都认定为失败
 20     return status === 200;
 21   }
 22 });
 23 
 24 // 拦截请求
 25 QZDSOFT_REQUEST.interceptors.request.use(
 26   config => {
 27     let expireTime = store.state.account.expireTime;
 28     let now = moment().format("YYYYMMDDHHmmss");
 29     // 让token早10秒种过期,提升“请重新登录”弹窗体验
 30     if (now - expireTime >= -10) {
 31       Modal.error({
 32         title: "登录已过期",
 33         content: "很抱歉,登录已过期,请重新登录",
 34         okText: "重新登录",
 35         mask: false,
 36         onOk: () => {
 37           return new Promise((resolve, reject) => {
 38             db.clear();
 39             location.reload();
 40           });
 41         }
 42       });
 43     }
 44     // 有 token就带上
 45     if (store.state.account.token) {
 46       config.headers.Authentication = store.state.account.token;
 47     }
 48     return config;
 49   },
 50   error => {
 51     return Promise.reject(error);
 52   }
 53 );
 54 
 55 // 拦截响应
 56 QZDSOFT_REQUEST.interceptors.response.use(
 57   config => {
 58     return config;
 59   },
 60   error => {
 61     if (error.response) {
 62       let errorMessage =
 63         error.response.data === null
 64           ? "系统内部异常,请联系网站管理员"
 65           : error.response.data.message;
 66       switch (error.response.status) {
67 case 404: 68 notification.error({ 69 message: "系统提示", 70 description: "很抱歉,资源未找到", 71 duration: 4 72 }); 73 break; 74 case 403: 75 case 401: 76 notification.warn({ 77 message: "系统提示", 78 description: 79 "很抱歉,您无法访问该资源,可能是因为没有相应权限或者登录已失效", 80 duration: 4 81 }); 82 break; 83 default: 84 notification.error({ 85 message: "系统提示", 86 description: errorMessage, 87 duration: 4 88 }); 89 break; 90 } 91 } 92 return Promise.reject(error); 93 } 94 ); 95 96 const request = { 97 post(url, params) { 98 return QZDSOFT_REQUEST.post(url, params, { 99 transformRequest: [ 100 params => { 101 let result = ""; 102 Object.keys(params).forEach(key => { 103 if ( 104 !Object.is(params[key], undefined) && 105 !Object.is(params[key], null) 106 ) { 107 result += 108 encodeURIComponent(key) + 109 "=" + 110 encodeURIComponent(params[key]) + 111 "&"; 112 } 113 }); 114 return result; 115 } 116 ], 117 headers: { 118 "Content-Type": "application/x-www-form-urlencoded" 119 } 120 }); 121 }, 122 put(url, params) { 123 return QZDSOFT_REQUEST.put(url, params, { 124 transformRequest: [ 125 params => { 126 let result = ""; 127 Object.keys(params).forEach(key => { 128 if ( 129 !Object.is(params[key], undefined) && 130 !Object.is(params[key], null) 131 ) { 132 result += 133 encodeURIComponent(key) + 134 "=" + 135 encodeURIComponent(params[key]) + 136 "&"; 137 } 138 }); 139 return result; 140 } 141 ], 142 headers: { 143 "Content-Type": "application/x-www-form-urlencoded" 144 } 145 }); 146 }, 147 get(url, params) { 148 let _params; 149 if (Object.is(params, undefined)) { 150 _params = ""; 151 } else { 152 _params = "?"; 153 for (let key in params) { 154 if (params.hasOwnProperty(key) && params[key] !== null) { 155 _params += `${key}=${params[key]}&`; 156 } 157 } 158 } 159 return QZDSOFT_REQUEST.get(`${url}${_params}`); 160 }, 161 delete(url, params) { 162 let _params; 163 if (Object.is(params, undefined)) { 164 _params = ""; 165 } else { 166 _params = "?"; 167 for (let key in params) { 168 if (params.hasOwnProperty(key) && params[key] !== null) { 169 _params += `${key}=${params[key]}&`; 170 } 171 } 172 } 173 return QZDSOFT_REQUEST.delete(`${url}${_params}`); 174 }, 175 export(url, params = {},fileType) { 176 message.loading("导出数据中"); 177 return QZDSOFT_REQUEST.post(url, params, { 178 transformRequest: [ 179 params => { 180 let result = ""; 181 Object.keys(params).forEach(key => { 182 if ( 183 !Object.is(params[key], undefined) && 184 !Object.is(params[key], null) 185 ) { 186 result += 187 encodeURIComponent(key) + 188 "=" + 189 encodeURIComponent(params[key]) + 190 "&"; 191 } 192 }); 193 console.log(result); 194 return result; 195 } 196 ], 197 responseType: "blob" 198 }) 199 .then(r => { 200 const content = r.data; 201 console.log("content") 202 console.log(content) 203 const blob = new Blob([content]); 204 console.log("blob") 205 console.log(blob) 206 // const fileName = `${new Date().getTime()}_导出结果.xlsx`; 207 let fileName = '' 208 if(fileType === 'zip'){ 209 fileName = `${moment().format('l')}.zip`; 210 }else if(fileType == 'pdf'){ 211 fileName = `${moment().format('l')}.pdf`; 212 }else{ 213 fileName = `${moment().format('l')}.xlsx`; 214 } 215 if('msSaveOrOpenBlob' in navigator){ 216 window.navigator.msSaveOrOpenBlob(blob, fileName); 217 return; 218 } 219 if ("download" in document.createElement("a")) { 220 const elink = document.createElement("a"); 221 elink.download = fileName; 222 elink.style.display = "none"; 223 elink.href = URL.createObjectURL(blob); 224 document.body.appendChild(elink); 225 elink.click(); 226 URL.revokeObjectURL(elink.href); 227 document.body.removeChild(elink); 228 } else { 229 // navigator.msSaveBlob(blob, fileName); 230 } 231 }) 232 .catch(r => { 233 console.error(r); 234 message.error("导出失败"); 235 }); 236 }, 237 download(url, params, filename) { 238 message.loading("文件传输中"); 239 return QZDSOFT_REQUEST.post(url, params, { 240 transformRequest: [ 241 params => { 242 let result = ""; 243 Object.keys(params).forEach(key => { 244 if ( 245 !Object.is(params[key], undefined) && 246 !Object.is(params[key], null) 247 ) { 248 result += 249 encodeURIComponent(key) + 250 "=" + 251 encodeURIComponent(params[key]) + 252 "&"; 253 } 254 }); 255 return result; 256 } 257 ], 258 responseType: "blob" 259 }) 260 .then(r => { 261 const content = r.data; 262 const blob = new Blob([content]); 263 if ("download" in document.createElement("a")) { 264 const elink = document.createElement("a"); 265 elink.download = filename; 266 elink.style.display = "none"; 267 elink.href = URL.createObjectURL(blob); 268 document.body.appendChild(elink); 269 elink.click(); 270 URL.revokeObjectURL(elink.href); 271 document.body.removeChild(elink); 272 } else { 273 navigator.msSaveBlob(blob, filename); 274 } 275 }) 276 .catch(r => { 277 console.error(r); 278 message.error("下载失败"); 279 }); 280 }, 281 upload(url, params) { 282 return QZDSOFT_REQUEST.post(url, params, { 283 headers: { 284 "Content-Type": "multipart/form-data" 285 } 286 }); 287 }, 288 uploadUrl() { 289 return baseURL 290 } 291 }; 292 293 export default request;

 

posted @ 2020-10-06 17:32  浪魔  阅读(243)  评论(0编辑  收藏  举报