UNPKG

project-general-tools

Version:

项目开发通用工具类封装

167 lines (166 loc) 6.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* * @Author: Mad Dragon 395548460@qq.com * @Date: 2020年3月20日 * @explanatory: axios 接口封装 */ // @ts-nocheck const axios_1 = __importDefault(require("axios")); const log_1 = __importDefault(require("./log")); const isElectron = () => { try { // @ts-nocheck return typeof _electronAgent_ !== 'undefined'; } catch (e) { log_1.default.error('isElectron', '-------我异常了........'); } return false; }; const setItem = (name, content) => { if (!name) return; let contentTxt = content; if (typeof content !== 'string') { contentTxt = JSON.stringify(content); } window.localStorage.setItem(name, contentTxt); }; const logout = () => { if (isElectron()) { log_1.default.warn('退出登录 logout electron'); // @ts-nocheck _electronAgent_.bg.logout(); } log_1.default.warn('退出登录 logout localhostStorage'); setItem('electronUserInfo', ''); setItem('Authorization', ''); }; class HttpRequest { constructor(baseUrl) { this.baseUrl = baseUrl; } getInsideConfig() { return { // baseURL: this.baseUrl, timeout: 40000, responseType: 'json', headers: { 'If-Modified-Since': 0, 'Cache-Control': 'no-cache' } }; } static interceptors(instance) { // axios 请求拦截 instance.interceptors.request.use((config) => config, (error) => Promise.reject(error)); // axios 响应拦截 instance.interceptors.response.use((res) => { const { data, config } = res; if (data && data.status) { HttpRequest.setResponse({ config, data }, data.status, data.msg, config.url); } if (res && data && data.status !== 200) return ''; return data.data || ''; }, (error) => { const msg = (error.response && error.response.data && error.response.data.msg) || (error.response && error.response.data && JSON.stringify(error.response.data)) || (error.response && error.response.statusText) || ''; HttpRequest.setResponse(error.response, error.response && error.response.status, msg, error.response && error.response.config.url, error.response && error.response.config.data || {}); if ((error.response && error.response.status) === 403) return Promise.reject(error.response && error.response.data); return Promise.reject(error); }); } request(options) { const instance = axios_1.default.create(); HttpRequest.interceptors(instance); return instance(Object.assign(this.getInsideConfig(), options)); } static setResponse(response, status, msg, url, params = {}) { const messageTxt = HttpRequest.showLog(status, msg, url, JSON.stringify(params)); log_1.default.setFStorageLog('apiLogStorage', status, msg ? `${msg} ==》 ${url}` : url, JSON.stringify(response)); switch (status) { case 500: log_1.default.error('【错误拦截】', messageTxt); alert(messageTxt); break; case 501: log_1.default.error('【错误拦截】', messageTxt); // alert(messageTxt); break; case 502: log_1.default.error('【错误拦截】', messageTxt); // alert(messageTxt); break; case 503: log_1.default.error('【错误拦截】', messageTxt); // alert(messageTxt); break; case 404: log_1.default.error('【错误拦截】', messageTxt); // alert(messageTxt); break; case 400: log_1.default.error('【错误拦截】', messageTxt); // alert(messageTxt); break; case 403: log_1.default.error('【错误拦截】', messageTxt); // alert(messageTxt); break; case 401: { // alert(messageTxt); logout(); break; } default: break; } } static getTxt(msg, url, params) { return `请求参数:===>${params} 请求URL: ===>${url} 请求信息:===>${msg}`; } static showLog(type, msg, url, params) { return `请求状态${type}:请截图给管理员,以便快捷修复异常! ${HttpRequest.getTxt(msg, url, params)}`; } } exports.default = HttpRequest; /** * _ooOoo_ * o8888888o * 88" . "88 * (| -_- |) * O\ = /O * ____/`---'\____ * .' \\| |// `. * / \\||| : |||// \ * / _||||| -:- |||||- \ * | | \\\ - /// | | * | \_| ''\---/'' | | * \ .-\__ `-` ___/-. / * ___`. .' /--.--\ `. . __ * ."" '< `.___\_<|>_/___.' >'"". * | | : `- \`.;`\ _ /`;.`/ - ` : | | * \ \ `-. \_ __\ /__ _/ .-` / / * ======`-.____`-.___\_____/___.-`____.-'====== * `=---=' * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * 佛祖保佑 永无BUG * 佛曰: * 写字楼里写字间,写字间里程序员; * 程序人员写程序,又拿程序换酒钱。 * 酒醒只在网上坐,酒醉还来网下眠; * 酒醉酒醒日复日,网上网下年复年。 * 但愿老死电脑间,不愿鞠躬老板前; * 奔驰宝马贵者趣,公交自行程序员。 * 别人笑我忒疯癫,我笑自己命太贱; * 不见满街漂亮妹,哪个归得程序员? * * Mad Dragon 395548460@qq.com */