project-general-tools
Version:
项目开发通用工具类封装
143 lines (142 loc) • 4.98 kB
JavaScript
;
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月21日
* @explanatory: Api 接口 封装
*/
// @ts-nocheck
const axios_1 = __importDefault(require("./axios"));
/* eslint-disable */
const Api = {};
const likeGet = ['delete', 'get', 'head', 'options'];
const likePost = ['post', 'put', 'patch'];
// const baseUrl = process.env.NODE_ENV === 'development' ? '/api' : '/api';
Api.baseUrl = '';
const axios = new axios_1.default(Api.baseUrl);
const isElectron = () => {
try {
return typeof _electronAgent_ !== "undefined";
}
catch (e) {
console.error("-------我异常了........");
}
return false;
};
// 获取localStorage
const getItem = (name) => {
if (!name)
return '';
if (typeof window !== 'undefined')
return window.localStorage.getItem(name) || '';
};
// 存储localStorage
const setItem = (name, content) => {
if (!name)
return;
let contentTxt = content;
if (typeof content !== 'string') {
contentTxt = JSON.stringify(content);
}
if (typeof window !== 'undefined')
window.localStorage.setItem(name, contentTxt);
};
const getTokenToLocalStorage = () => {
return getItem("Authorization") || '';
};
const getTokenToElectron = () => {
let userInfo = _electronAgent_.getUser() || "";
const Authorization = userInfo && userInfo.user && userInfo.user.oauth && userInfo.user.oauth.accessToken || "";
setItem('Authorization', Authorization);
return Authorization || '';
};
const getUserAccessToken = () => {
if (isElectron()) {
return getTokenToElectron();
}
return getTokenToLocalStorage();
};
// 将请求数据的方式包装成一个对象
likeGet.forEach((method) => {
Api[method] = function (url, params) {
return axios.request({
url,
params,
method: 'get',
headers: {
Authorization: getUserAccessToken()
}
});
};
});
likePost.forEach((method) => {
Api[method] = function (url, data) {
return axios.request({
url,
// data: qs.stringify(data), from 标点传参
data,
method: 'post',
headers: {
Authorization: getUserAccessToken()
}
});
};
});
// axios本版本不支持jsonp 自己拓展一个
// @ts-ignore
Api.jsonp = (url) => {
if (!url) {
console.error('Axios.JSONP 至少需要一个url参数!');
return;
}
return new Promise((resolve, reject) => {
// @ts-ignore
window.jsonCallBack = (result) => {
resolve(result);
};
const JSONP = document.createElement('script');
JSONP.type = 'text/javascript';
JSONP.src = `${url}&callback=jsonCallBack`;
document.getElementsByTagName('head')[0].appendChild(JSONP);
setTimeout(() => {
document.getElementsByTagName('head')[0].removeChild(JSONP);
}, 500);
});
};
exports.default = Object.assign({}, Api);
/**
* _ooOoo_
* o8888888o
* 88' . '88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* .'' '< `.___\_<|>_/___.' >'''.
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永无BUG
* 佛曰:
* 写字楼里写字间,写字间里程序员;
* 程序人员写程序,又拿程序换酒钱。
* 酒醒只在网上坐,酒醉还来网下眠;
* 酒醉酒醒日复日,网上网下年复年。
* 但愿老死电脑间,不愿鞠躬老板前;
* 奔驰宝马贵者趣,公交自行程序员。
* 别人笑我忒疯癫,我笑自己命太贱;
* 不见满街漂亮妹,哪个归得程序员?
*
* Mad Dragon 395548460@qq.com
*/