update
init
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
class TUIAegis {
|
||||
static reportEvent(arg0: { name: string; ext1: string; }) {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
private aegis:any;
|
||||
private options:any = {};
|
||||
static instance: any;
|
||||
constructor(Aegis?:any) {
|
||||
if (Aegis) {
|
||||
this.aegis = Aegis;
|
||||
} else {
|
||||
this.aegis = new (window as any).Aegis({
|
||||
id: 'iHWefAYqDTiNaZDjts', // 项目key
|
||||
uin: '', // 用户唯一 ID(可选)
|
||||
reportApiSpeed: true, // 接口测速
|
||||
reportAssetSpeed: true, // 静态资源测速
|
||||
spa: true, // spa 页面需要开启,页面切换的时候上报pv
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* install 挂载vue上
|
||||
*
|
||||
* @param {any} app Vue 实例
|
||||
*/
|
||||
public install(app:any) {
|
||||
app.config.globalProperties.$TUIAegis = TUIAegis.getInstance();
|
||||
}
|
||||
|
||||
static getInstance() {
|
||||
if (!TUIAegis.instance) {
|
||||
TUIAegis.instance = new TUIAegis();
|
||||
}
|
||||
return TUIAegis.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* setAegisOptions 设置上报公共参数
|
||||
*
|
||||
* @param {any} options 公共参数
|
||||
*/
|
||||
public setAegisOptions(options:any) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* reportEvent 上报事件
|
||||
*
|
||||
* @param {any} options 上报的内容
|
||||
* @returns {reportEvent}
|
||||
*/
|
||||
public reportEvent(options: any) {
|
||||
if (!(window as any)?.TUIKitTUICore?.isOfficial) {
|
||||
return;
|
||||
}
|
||||
return this.aegis.reportEvent({ ...options, ...this.options });
|
||||
}
|
||||
}
|
||||
|
||||
export default TUIAegis;
|
||||
@@ -0,0 +1,4 @@
|
||||
import mitt from "mitt";
|
||||
const emitter = mitt();
|
||||
|
||||
export default emitter;
|
||||
@@ -0,0 +1,231 @@
|
||||
export const COUNTRIES:Array<any> = [
|
||||
{ zh_cn: '阿富汗', en: 'Afghanistan', phone: '93' },
|
||||
{ zh_cn: '阿尔巴尼亚', en: 'Albania', phone: '355' },
|
||||
{ zh_cn: '阿尔及利亚', en: 'Algeria', phone: '213' },
|
||||
{ zh_cn: '美属萨摩亚', en: 'American Samoa', phone: '684' },
|
||||
{ zh_cn: '安道尔', en: 'Andorra', phone: '376' },
|
||||
{ zh_cn: '安哥拉', en: 'Angola', phone: '244' },
|
||||
{ zh_cn: '安圭拉', en: 'Anguilla', phone: '1264' },
|
||||
{ zh_cn: '南极洲', en: 'Antarctica', phone: '672' },
|
||||
{ zh_cn: '安提瓜和巴布达', en: 'Antigua and Barbuda', phone: '1268' },
|
||||
{ zh_cn: '阿根廷', en: 'Argentina', phone: '54' },
|
||||
{ zh_cn: '亚美尼亚', en: 'Armenia', phone: '374' },
|
||||
{ zh_cn: '阿鲁巴', en: 'Aruba', phone: '297' },
|
||||
{ zh_cn: '澳大利亚', en: 'Australia', phone: '61' },
|
||||
{ zh_cn: '奥地利', en: 'Austria', phone: '43' },
|
||||
{ zh_cn: '阿塞拜疆', en: 'Azerbaijan', phone: '994' },
|
||||
{ zh_cn: '巴林', en: 'Bahrain', phone: '973' },
|
||||
{ zh_cn: '孟加拉国', en: 'Bangladesh', phone: '880' },
|
||||
{ zh_cn: '巴巴多斯', en: 'Barbados', phone: '1246' },
|
||||
{ zh_cn: '白俄罗斯', en: 'Belarus', phone: '375' },
|
||||
{ zh_cn: '比利时', en: 'Belgium', phone: '32' },
|
||||
{ zh_cn: '伯利兹', en: 'Belize', phone: '501' },
|
||||
{ zh_cn: '贝宁', en: 'Benin', phone: '229' },
|
||||
{ zh_cn: '百慕大', en: 'Bermuda', phone: '1441' },
|
||||
{ zh_cn: '不丹', en: 'Bhutan', phone: '975' },
|
||||
{ zh_cn: '玻利维亚', en: 'Bolivia', phone: '591' },
|
||||
{ zh_cn: '波黑', en: 'Bosnia and Herzegovina', phone: '387' },
|
||||
{ zh_cn: '博茨瓦纳', en: 'Botswana', phone: '267' },
|
||||
{ zh_cn: '巴西', en: 'Brazil', phone: '55' },
|
||||
{ zh_cn: '英属维尔京群岛', en: 'British Virgin Islands', phone: '1284' },
|
||||
{ zh_cn: '文莱', en: 'Brunei Darussalam', phone: '673' },
|
||||
{ zh_cn: '保加利亚', en: 'Bulgaria', phone: '359' },
|
||||
{ zh_cn: '布基纳法索', en: 'Burkina Faso', phone: '226' },
|
||||
{ zh_cn: '缅甸', en: 'Burma', phone: '95' },
|
||||
{ zh_cn: '布隆迪', en: 'Burundi', phone: '257' },
|
||||
{ zh_cn: '柬埔寨', en: 'Cambodia', phone: '855' },
|
||||
{ zh_cn: '喀麦隆', en: 'Cameroon', phone: '237' },
|
||||
{ zh_cn: '加拿大', en: 'Canada', phone: '1' },
|
||||
{ zh_cn: '佛得角', en: 'Cape Verde', phone: '238' },
|
||||
{ zh_cn: '开曼群岛', en: 'Cayman Islands', phone: '1345' },
|
||||
{ zh_cn: '中非', en: 'Central African Republic', phone: '236' },
|
||||
{ zh_cn: '乍得', en: 'Chad', phone: '235' },
|
||||
{ zh_cn: '智利', en: 'Chile', phone: '56' },
|
||||
{ zh_cn: '中国', en: 'China', phone: '86' },
|
||||
{ zh_cn: '圣诞岛', en: 'Christmas Island', phone: '61' },
|
||||
{ zh_cn: '科科斯(基林)群岛', en: 'Cocos (Keeling) Islands', phone: '61' },
|
||||
{ zh_cn: '哥伦比亚', en: 'Colombia', phone: '57' },
|
||||
{ zh_cn: '科摩罗', en: 'Comoros', phone: '269' },
|
||||
{ zh_cn: '刚果(金)', en: 'Democratic Republic of the Congo', phone: '243' },
|
||||
{ zh_cn: '刚果(布)', en: 'Republic of the Congo', phone: '242' },
|
||||
{ zh_cn: '库克群岛', en: 'Cook Islands', phone: '682' },
|
||||
{ zh_cn: '哥斯达黎加', en: 'Costa Rica', phone: '506' },
|
||||
{ zh_cn: '科特迪瓦', en: 'Cote d\'Ivoire', phone: '225' },
|
||||
{ zh_cn: '克罗地亚', en: 'Croatia', phone: '385' },
|
||||
// { zh_cn: '古巴', en: 'Cuba', phone: '53' },
|
||||
{ zh_cn: '塞浦路斯', en: 'Cyprus', phone: '357' },
|
||||
{ zh_cn: '捷克', en: 'Czech Republic', phone: '420' },
|
||||
{ zh_cn: '丹麦', en: 'Denmark', phone: '45' },
|
||||
{ zh_cn: '吉布提', en: 'Djibouti', phone: '253' },
|
||||
{ zh_cn: '多米尼克', en: 'Dominica', phone: '1767' },
|
||||
{ zh_cn: '多米尼加', en: 'Dominican Republic', phone: '1809' },
|
||||
{ zh_cn: '厄瓜多尔', en: 'Ecuador', phone: '593' },
|
||||
{ zh_cn: '埃及', en: 'Egypt', phone: '20' },
|
||||
{ zh_cn: '萨尔瓦多', en: 'El Salvador', phone: '503' },
|
||||
{ zh_cn: '赤道几内亚', en: 'Equatorial Guinea', phone: '240' },
|
||||
{ zh_cn: '厄立特里亚', en: 'Eritrea', phone: '291' },
|
||||
{ zh_cn: '爱沙尼亚', en: 'Estonia', phone: '372' },
|
||||
{ zh_cn: '埃塞俄比亚', en: 'Ethiopia', phone: '251' },
|
||||
{ zh_cn: '福克兰群岛(马尔维纳斯)', en: 'Falkland Islands (Islas Malvinas)', phone: '500' },
|
||||
{ zh_cn: '法罗群岛', en: 'Faroe Islands', phone: '298' },
|
||||
{ zh_cn: '斐济', en: 'Fiji', phone: '679' },
|
||||
{ zh_cn: '芬兰', en: 'Finland', phone: '358' },
|
||||
{ zh_cn: '法国', en: 'France', phone: '33' },
|
||||
{ zh_cn: '法属圭亚那', en: 'French Guiana', phone: '594' },
|
||||
{ zh_cn: '法属波利尼西亚', en: 'French Polynesia', phone: '689' },
|
||||
{ zh_cn: '加蓬', en: 'Gabon', phone: '241' },
|
||||
{ zh_cn: '格鲁吉亚', en: 'Georgia', phone: '995' },
|
||||
{ zh_cn: '德国', en: 'Germany', phone: '49' },
|
||||
{ zh_cn: '加纳', en: 'Ghana', phone: '233' },
|
||||
{ zh_cn: '直布罗陀', en: 'Gibraltar', phone: '350' },
|
||||
{ zh_cn: '希腊', en: 'Greece', phone: '30' },
|
||||
{ zh_cn: '格陵兰', en: 'Greenland', phone: '299' },
|
||||
{ zh_cn: '格林纳达', en: 'Grenada', phone: '1473' },
|
||||
{ zh_cn: '瓜德罗普', en: 'Guadeloupe', phone: '590' },
|
||||
{ zh_cn: '关岛', en: 'Guam', phone: '1671' },
|
||||
{ zh_cn: '危地马拉', en: 'Guatemala', phone: '502' },
|
||||
{ zh_cn: '根西岛', en: 'Guernsey', phone: '1481' },
|
||||
{ zh_cn: '几内亚', en: 'Guinea', phone: '224' },
|
||||
{ zh_cn: '几内亚比绍', en: 'Guinea-Bissau', phone: '245' },
|
||||
{ zh_cn: '圭亚那', en: 'Guyana', phone: '592' },
|
||||
{ zh_cn: '海地', en: 'Haiti', phone: '509' },
|
||||
{ zh_cn: '梵蒂冈', en: 'Holy See (Vatican City)', phone: '379' },
|
||||
{ zh_cn: '洪都拉斯', en: 'Honduras', phone: '504' },
|
||||
{ zh_cn: '中国香港', en: 'Hong Kong (SAR)', phone: '852' },
|
||||
{ zh_cn: '匈牙利', en: 'Hungary', phone: '36' },
|
||||
{ zh_cn: '冰岛', en: 'Iceland', phone: '354' },
|
||||
{ zh_cn: '印度', en: 'India', phone: '91' },
|
||||
{ zh_cn: '印度尼西亚', en: 'Indonesia', phone: '62' },
|
||||
// { zh_cn: '伊朗', en: 'Iran', phone: '98' },
|
||||
{ zh_cn: '伊拉克', en: 'Iraq', phone: '964' },
|
||||
{ zh_cn: '爱尔兰', en: 'Ireland', phone: '353' },
|
||||
{ zh_cn: '以色列', en: 'Israel', phone: '972' },
|
||||
{ zh_cn: '意大利', en: 'Italy', phone: '39' },
|
||||
{ zh_cn: '牙买加', en: 'Jamaica', phone: '1876' },
|
||||
{ zh_cn: '日本', en: 'Japan', phone: '81' },
|
||||
{ zh_cn: '约旦', en: 'Jordan', phone: '962' },
|
||||
{ zh_cn: '哈萨克斯坦', en: 'Kazakhstan', phone: '73' },
|
||||
{ zh_cn: '肯尼亚', en: 'Kenya', phone: '254' },
|
||||
{ zh_cn: '基里巴斯', en: 'Kiribati', phone: '686' },
|
||||
// { zh_cn: '朝鲜', en: 'North Korea', phone: '850' },
|
||||
{ zh_cn: '韩国', en: 'South Korea', phone: '82' },
|
||||
{ zh_cn: '科威特', en: 'Kuwait', phone: '965' },
|
||||
{ zh_cn: '吉尔吉斯斯坦', en: 'Kyrgyzstan', phone: '996' },
|
||||
{ zh_cn: '老挝', en: 'Laos', phone: '856' },
|
||||
{ zh_cn: '拉脱维亚', en: 'Latvia', phone: '371' },
|
||||
{ zh_cn: '黎巴嫩', en: 'Lebanon', phone: '961' },
|
||||
{ zh_cn: '莱索托', en: 'Lesotho', phone: '266' },
|
||||
{ zh_cn: '利比里亚', en: 'Liberia', phone: '231' },
|
||||
{ zh_cn: '利比亚', en: 'Libya', phone: '218' },
|
||||
{ zh_cn: '列支敦士登', en: 'Liechtenstein', phone: '423' },
|
||||
{ zh_cn: '立陶宛', en: 'Lithuania', phone: '370' },
|
||||
{ zh_cn: '卢森堡', en: 'Luxembourg', phone: '352' },
|
||||
{ zh_cn: '中国澳门', en: 'Macao', phone: '853' },
|
||||
{ zh_cn: '前南马其顿', en: 'The Former Yugoslav Republic of Macedonia', phone: '389' },
|
||||
{ zh_cn: '马达加斯加', en: 'Madagascar', phone: '261' },
|
||||
{ zh_cn: '马拉维', en: 'Malawi', phone: '265' },
|
||||
{ zh_cn: '马来西亚', en: 'Malaysia', phone: '60' },
|
||||
{ zh_cn: '马尔代夫', en: 'Maldives', phone: '960' },
|
||||
{ zh_cn: '马里', en: 'Mali', phone: '223' },
|
||||
{ zh_cn: '马耳他', en: 'Malta', phone: '356' },
|
||||
{ zh_cn: '马绍尔群岛', en: 'Marshall Islands', phone: '692' },
|
||||
{ zh_cn: '马提尼克', en: 'Martinique', phone: '596' },
|
||||
{ zh_cn: '毛里塔尼亚', en: 'Mauritania', phone: '222' },
|
||||
{ zh_cn: '毛里求斯', en: 'Mauritius', phone: '230' },
|
||||
{ zh_cn: '马约特', en: 'Mayotte', phone: '269' },
|
||||
{ zh_cn: '墨西哥', en: 'Mexico', phone: '52' },
|
||||
{ zh_cn: '密克罗尼西亚', en: 'Federated States of Micronesia', phone: '691' },
|
||||
{ zh_cn: '摩尔多瓦', en: 'Moldova', phone: '373' },
|
||||
{ zh_cn: '摩纳哥', en: 'Monaco', phone: '377' },
|
||||
{ zh_cn: '蒙古', en: 'Mongolia', phone: '976' },
|
||||
{ zh_cn: '蒙特塞拉特', en: 'Montserrat', phone: '1664' },
|
||||
{ zh_cn: '摩洛哥', en: 'Morocco', phone: '212' },
|
||||
{ zh_cn: '莫桑比克', en: 'Mozambique', phone: '258' },
|
||||
{ zh_cn: '纳米尼亚', en: 'Namibia', phone: '264' },
|
||||
{ zh_cn: '瑙鲁', en: 'Nauru', phone: '674' },
|
||||
{ zh_cn: '尼泊尔', en: 'Nepal', phone: '977' },
|
||||
{ zh_cn: '荷兰', en: 'Netherlands', phone: '31' },
|
||||
{ zh_cn: '荷属安的列斯', en: 'Netherlands Antilles', phone: '599' },
|
||||
{ zh_cn: '新喀里多尼亚', en: 'New Caledonia', phone: '687' },
|
||||
{ zh_cn: '新西兰', en: 'New Zealand', phone: '64' },
|
||||
{ zh_cn: '尼加拉瓜', en: 'Nicaragua', phone: '505' },
|
||||
{ zh_cn: '尼日尔', en: 'Niger', phone: '227' },
|
||||
{ zh_cn: '尼日利亚', en: 'Nigeria', phone: '234' },
|
||||
{ zh_cn: '纽埃', en: 'Niue', phone: '683' },
|
||||
{ zh_cn: '诺福克岛', en: 'Norfolk Island', phone: '6723' },
|
||||
{ zh_cn: '北马里亚纳', en: 'Northern Mariana Islands', phone: '1' },
|
||||
{ zh_cn: '挪威', en: 'Norway', phone: '47' },
|
||||
{ zh_cn: '阿曼', en: 'Oman', phone: '968' },
|
||||
{ zh_cn: '巴基斯坦', en: 'Pakistan', phone: '92' },
|
||||
{ zh_cn: '帕劳', en: 'Palau', phone: '680' },
|
||||
{ zh_cn: '巴拿马', en: 'Panama', phone: '507' },
|
||||
{ zh_cn: '巴布亚新几内亚', en: 'Papua New Guinea', phone: '675' },
|
||||
{ zh_cn: '巴拉圭', en: 'Paraguay', phone: '595' },
|
||||
{ zh_cn: '秘鲁', en: 'Peru', phone: '51' },
|
||||
{ zh_cn: '菲律宾', en: 'Philippines', phone: '63' },
|
||||
{ zh_cn: '波兰', en: 'Poland', phone: '48' },
|
||||
{ zh_cn: '葡萄牙', en: 'Portugal', phone: '351' },
|
||||
{ zh_cn: '波多黎各', en: 'Puerto Rico', phone: '1809' },
|
||||
{ zh_cn: '卡塔尔', en: 'Qatar', phone: '974' },
|
||||
{ zh_cn: '留尼汪', en: 'Reunion', phone: '262' },
|
||||
{ zh_cn: '罗马尼亚', en: 'Romania', phone: '40' },
|
||||
{ zh_cn: '俄罗斯', en: 'Russia', phone: '7' },
|
||||
{ zh_cn: '卢旺达', en: 'Rwanda', phone: '250' },
|
||||
{ zh_cn: '圣赫勒拿', en: 'Saint Helena', phone: '290' },
|
||||
{ zh_cn: '圣基茨和尼维斯', en: 'Saint Kitts and Nevis', phone: '1869' },
|
||||
{ zh_cn: '圣卢西亚', en: 'Saint Lucia', phone: '1758' },
|
||||
{ zh_cn: '圣皮埃尔和密克隆', en: 'Saint Pierre and Miquelon', phone: '508' },
|
||||
{ zh_cn: '圣文森特和格林纳丁斯', en: 'Saint Vincent and the Grenadines', phone: '1784' },
|
||||
{ zh_cn: '萨摩亚', en: 'Samoa', phone: '685' },
|
||||
{ zh_cn: '圣马力诺', en: 'San Marino', phone: '378' },
|
||||
{ zh_cn: '圣多美和普林西比', en: 'Sao Tome and Principe', phone: '239' },
|
||||
{ zh_cn: '沙特阿拉伯', en: 'Saudi Arabia', phone: '966' },
|
||||
{ zh_cn: '塞内加尔', en: 'Senegal', phone: '221' },
|
||||
{ zh_cn: '塞尔维亚和黑山', en: 'Serbia and Montenegro', phone: '381' },
|
||||
{ zh_cn: '塞舌尔', en: 'Seychelles', phone: '248' },
|
||||
{ zh_cn: '塞拉利', en: 'Sierra Leone', phone: '232' },
|
||||
{ zh_cn: '新加坡', en: 'Singapore', phone: '65' },
|
||||
{ zh_cn: '斯洛伐克', en: 'Slovakia', phone: '421' },
|
||||
{ zh_cn: '斯洛文尼亚', en: 'Slovenia', phone: '386' },
|
||||
{ zh_cn: '所罗门群岛', en: 'Solomon Islands', phone: '677' },
|
||||
{ zh_cn: '索马里', en: 'Somalia', phone: '252' },
|
||||
{ zh_cn: '南非', en: 'South Africa', phone: '27' },
|
||||
{ zh_cn: '西班牙', en: 'Spain', phone: '34' },
|
||||
{ zh_cn: '斯里兰卡', en: 'Sri Lanka', phone: '94' },
|
||||
{ zh_cn: '苏丹', en: 'Sudan', phone: '249' },
|
||||
{ zh_cn: '苏里南', en: 'Suriname', phone: '597' },
|
||||
{ zh_cn: '斯瓦尔巴岛和扬马延岛', en: 'Svalbard', phone: '47' },
|
||||
{ zh_cn: '斯威士兰', en: 'Swaziland', phone: '268' },
|
||||
{ zh_cn: '瑞典', en: 'Sweden', phone: '46' },
|
||||
{ zh_cn: '瑞士', en: 'Switzerland', phone: '41' },
|
||||
// { zh_cn: '叙利亚', en: 'Syria', phone: '963' },
|
||||
{ zh_cn: '中国台湾', en: 'Taiwan', phone: '886' },
|
||||
{ zh_cn: '塔吉克斯坦', en: 'Tajikistan', phone: '992' },
|
||||
{ zh_cn: '坦桑尼亚', en: 'Tanzania', phone: '255' },
|
||||
{ zh_cn: '泰国', en: 'Thailand', phone: '66' },
|
||||
{ zh_cn: '巴哈马', en: 'The Bahamas', phone: '1242' },
|
||||
{ zh_cn: '冈比亚', en: 'The Gambia', phone: '220' },
|
||||
{ zh_cn: '多哥', en: 'Togo', phone: '228' },
|
||||
{ zh_cn: '托克劳', en: 'Tokelau', phone: '690' },
|
||||
{ zh_cn: '汤加', en: 'Tonga', phone: '676' },
|
||||
{ zh_cn: '特立尼达和多巴哥', en: 'Trinidad and Tobago', phone: '1868' },
|
||||
{ zh_cn: '突尼斯', en: 'Tunisia', phone: '216' },
|
||||
{ zh_cn: '土耳其', en: 'Turkey', phone: '90' },
|
||||
{ zh_cn: '土库曼斯坦', en: 'Turkmenistan', phone: '993' },
|
||||
{ zh_cn: '特克斯和凯科斯群岛', en: 'Turks and Caicos Islands', phone: '1649' },
|
||||
{ zh_cn: '图瓦卢', en: 'Tuvalu', phone: '688' },
|
||||
{ zh_cn: '乌干达', en: 'Uganda', phone: '256' },
|
||||
{ zh_cn: '乌克兰', en: 'Ukraine', phone: '380' },
|
||||
{ zh_cn: '阿拉伯联合酋长国', en: 'United Arab Emirates', phone: '971' },
|
||||
{ zh_cn: '英国', en: 'United Kingdom', phone: '44' },
|
||||
{ zh_cn: '美国', en: 'United States', phone: '1' },
|
||||
{ zh_cn: '乌拉圭', en: 'Uruguay', phone: '598' },
|
||||
{ zh_cn: '乌兹别克斯坦', en: 'Uzbekistan', phone: '998' },
|
||||
{ zh_cn: '瓦努阿图', en: 'Vanuatu', phone: '678' },
|
||||
{ zh_cn: '委内瑞拉', en: 'Venezuela', phone: '58' },
|
||||
{ zh_cn: '越南', en: 'Vietnam', phone: '84' },
|
||||
{ zh_cn: '美属维尔京群岛', en: 'Virgin Islands', phone: '1340' },
|
||||
{ zh_cn: '瓦利斯和富图纳', en: 'Wallis and Futuna', phone: '681' },
|
||||
{ zh_cn: '也门', en: 'Yemen', phone: '967' },
|
||||
{ zh_cn: '赞比亚', en: 'Zambia', phone: '260' },
|
||||
{ zh_cn: '津巴布韦', en: 'Zimbabwe', phone: '263' }];
|
||||
@@ -0,0 +1,9 @@
|
||||
import qs from 'qs';
|
||||
|
||||
const url = window.location.href;
|
||||
|
||||
const arr = url.indexOf('?') !== -1 ? url.split('?')[1].replace('#/', '') : '';
|
||||
|
||||
const hrefParams = qs.parse(arr);
|
||||
|
||||
export default hrefParams;
|
||||
@@ -0,0 +1,268 @@
|
||||
import type { AxiosRequestConfig, AxiosInstance, AxiosResponse, AxiosError } from 'axios';
|
||||
import type { RequestOptions, Result, UploadFileParams, UploadFileCallBack } from '/#/axios';
|
||||
import type { CreateAxiosOptions } from './axiosTransform';
|
||||
import axios from 'axios';
|
||||
import qs from 'qs';
|
||||
import { AxiosCanceler } from './axiosCancel';
|
||||
import { isFunction } from '/@/utils/is';
|
||||
import { cloneDeep } from 'lodash-es';
|
||||
import { ContentTypeEnum } from '/@/enums/httpEnum';
|
||||
import { RequestEnum } from '/@/enums/httpEnum';
|
||||
import { useGlobSetting } from '/@/hooks/setting';
|
||||
import { useMessage } from '/@/hooks/web/useMessage';
|
||||
|
||||
const { createMessage } = useMessage();
|
||||
export * from './axiosTransform';
|
||||
|
||||
/**
|
||||
* @description: axios module
|
||||
*/
|
||||
export class VAxios {
|
||||
private axiosInstance: AxiosInstance;
|
||||
private readonly options: CreateAxiosOptions;
|
||||
|
||||
constructor(options: CreateAxiosOptions) {
|
||||
this.options = options;
|
||||
this.axiosInstance = axios.create(options);
|
||||
this.setupInterceptors();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Create axios instance
|
||||
*/
|
||||
private createAxios(config: CreateAxiosOptions): void {
|
||||
this.axiosInstance = axios.create(config);
|
||||
}
|
||||
|
||||
private getTransform() {
|
||||
const { transform } = this.options;
|
||||
return transform;
|
||||
}
|
||||
|
||||
getAxios(): AxiosInstance {
|
||||
return this.axiosInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Reconfigure axios
|
||||
*/
|
||||
configAxios(config: CreateAxiosOptions) {
|
||||
if (!this.axiosInstance) {
|
||||
return;
|
||||
}
|
||||
this.createAxios(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Set general header
|
||||
*/
|
||||
setHeader(headers: any): void {
|
||||
if (!this.axiosInstance) {
|
||||
return;
|
||||
}
|
||||
Object.assign(this.axiosInstance.defaults.headers, headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Interceptor configuration
|
||||
*/
|
||||
private setupInterceptors() {
|
||||
const transform = this.getTransform();
|
||||
if (!transform) {
|
||||
return;
|
||||
}
|
||||
const { requestInterceptors, requestInterceptorsCatch, responseInterceptors, responseInterceptorsCatch } = transform;
|
||||
|
||||
const axiosCanceler = new AxiosCanceler();
|
||||
|
||||
// 请求侦听器配置处理
|
||||
this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {
|
||||
// If cancel repeat request is turned on, then cancel repeat request is prohibited
|
||||
// @ts-ignore
|
||||
const { ignoreCancelToken } = config.requestOptions;
|
||||
|
||||
const ignoreCancel = ignoreCancelToken !== undefined ? ignoreCancelToken : this.options.requestOptions?.ignoreCancelToken;
|
||||
|
||||
!ignoreCancel && axiosCanceler.addPending(config);
|
||||
if (requestInterceptors && isFunction(requestInterceptors)) {
|
||||
config = requestInterceptors(config, this.options);
|
||||
}
|
||||
return config;
|
||||
}, undefined);
|
||||
|
||||
// 请求拦截器错误捕获
|
||||
requestInterceptorsCatch &&
|
||||
isFunction(requestInterceptorsCatch) &&
|
||||
this.axiosInstance.interceptors.request.use(undefined, requestInterceptorsCatch);
|
||||
|
||||
// 响应结果拦截器处理
|
||||
this.axiosInstance.interceptors.response.use((res: AxiosResponse<any>) => {
|
||||
res && axiosCanceler.removePending(res.config);
|
||||
if (responseInterceptors && isFunction(responseInterceptors)) {
|
||||
res = responseInterceptors(res);
|
||||
}
|
||||
return res;
|
||||
}, undefined);
|
||||
|
||||
// 响应结果拦截器错误捕获
|
||||
responseInterceptorsCatch &&
|
||||
isFunction(responseInterceptorsCatch) &&
|
||||
this.axiosInstance.interceptors.response.use(undefined, responseInterceptorsCatch);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*/
|
||||
//--@updateBy-begin----author:liusq---date:20211117------for:增加上传回调参数callback------
|
||||
uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams, callback?: UploadFileCallBack) {
|
||||
//--@updateBy-end----author:liusq---date:20211117------for:增加上传回调参数callback------
|
||||
const formData = new window.FormData();
|
||||
const customFilename = params.name || 'file';
|
||||
|
||||
if (params.filename) {
|
||||
formData.append(customFilename, params.file, params.filename);
|
||||
} else {
|
||||
formData.append(customFilename, params.file);
|
||||
}
|
||||
const glob = useGlobSetting();
|
||||
config.baseURL = glob.uploadUrl;
|
||||
if (params.data) {
|
||||
Object.keys(params.data).forEach((key) => {
|
||||
const value = params.data![key];
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item) => {
|
||||
formData.append(`${key}[]`, item);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
formData.append(key, params.data[key]);
|
||||
});
|
||||
}
|
||||
|
||||
return this.axiosInstance
|
||||
.request<T>({
|
||||
...config,
|
||||
method: 'POST',
|
||||
data: formData,
|
||||
headers: {
|
||||
'Content-type': ContentTypeEnum.FORM_DATA,
|
||||
ignoreCancelToken: true,
|
||||
},
|
||||
})
|
||||
.then((res: any) => {
|
||||
//--@updateBy-begin----author:liusq---date:20210914------for:上传判断是否包含回调方法------
|
||||
if (callback?.success && isFunction(callback?.success)) {
|
||||
callback?.success(res?.data);
|
||||
//--@updateBy-end----author:liusq---date:20210914------for:上传判断是否包含回调方法------
|
||||
} else if (callback?.isReturnResponse) {
|
||||
//--@updateBy-begin----author:liusq---date:20211117------for:上传判断是否返回res信息------
|
||||
return Promise.resolve(res?.data);
|
||||
//--@updateBy-end----author:liusq---date:20211117------for:上传判断是否返回res信息------
|
||||
} else {
|
||||
if (res.data.success == true && res.data.code == 200) {
|
||||
createMessage.success(res.data.message);
|
||||
} else {
|
||||
createMessage.error(res.data.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 支持表单数据
|
||||
supportFormData(config: AxiosRequestConfig) {
|
||||
const headers = config.headers || this.options.headers;
|
||||
const contentType = headers?.['Content-Type'] || headers?.['content-type'];
|
||||
|
||||
if (contentType !== ContentTypeEnum.FORM_URLENCODED || !Reflect.has(config, 'data') || config.method?.toUpperCase() === RequestEnum.GET) {
|
||||
return config;
|
||||
}
|
||||
|
||||
return {
|
||||
...config,
|
||||
data: qs.stringify(config.data, { arrayFormat: 'brackets' }),
|
||||
};
|
||||
}
|
||||
|
||||
get<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
||||
return this.request({ ...config, method: 'GET' }, options);
|
||||
}
|
||||
|
||||
post<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
||||
return this.request({ ...config, method: 'POST' }, options);
|
||||
}
|
||||
|
||||
put<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
||||
return this.request({ ...config, method: 'PUT' }, options);
|
||||
}
|
||||
|
||||
delete<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
||||
return this.request({ ...config, method: 'DELETE' }, options);
|
||||
}
|
||||
|
||||
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
||||
let conf: CreateAxiosOptions = cloneDeep(config);
|
||||
const transform = this.getTransform();
|
||||
|
||||
const { requestOptions } = this.options;
|
||||
|
||||
const opt: RequestOptions = Object.assign({}, requestOptions, options);
|
||||
|
||||
const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {};
|
||||
if (beforeRequestHook && isFunction(beforeRequestHook)) {
|
||||
conf = beforeRequestHook(conf, opt);
|
||||
}
|
||||
conf.requestOptions = opt;
|
||||
|
||||
conf = this.supportFormData(conf);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.axiosInstance
|
||||
.request<any, AxiosResponse<Result>>(conf)
|
||||
.then((res: AxiosResponse<Result>) => {
|
||||
if (transformRequestHook && isFunction(transformRequestHook)) {
|
||||
try {
|
||||
const ret = transformRequestHook(res, opt);
|
||||
//zhangyafei---添加回调方法
|
||||
config.success && config.success(res.data);
|
||||
//zhangyafei---添加回调方法
|
||||
resolve(ret);
|
||||
} catch (err) {
|
||||
reject(err || new Error('request error!'));
|
||||
}
|
||||
return;
|
||||
}
|
||||
resolve(res as unknown as Promise<T>);
|
||||
})
|
||||
.catch((e: Error | AxiosError) => {
|
||||
if (requestCatchHook && isFunction(requestCatchHook)) {
|
||||
reject(requestCatchHook(e, opt));
|
||||
return;
|
||||
}
|
||||
if (axios.isAxiosError(e)) {
|
||||
// 在此处重写来自axios的错误消息
|
||||
}
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 【用于评论功能】自定义文件上传-请求
|
||||
* @param url
|
||||
* @param formData
|
||||
*/
|
||||
uploadMyFile<T = any>(url, formData) {
|
||||
const glob = useGlobSetting();
|
||||
return this.axiosInstance.request<T>({
|
||||
url: url,
|
||||
baseURL: glob.uploadUrl,
|
||||
method: 'POST',
|
||||
data: formData,
|
||||
headers: {
|
||||
'Content-type': ContentTypeEnum.FORM_DATA,
|
||||
ignoreCancelToken: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { AxiosRequestConfig, Canceler } from 'axios';
|
||||
import axios from 'axios';
|
||||
import { isFunction } from '/@/utils/is';
|
||||
|
||||
// Used to store the identification and cancellation function of each request
|
||||
let pendingMap = new Map<string, Canceler>();
|
||||
|
||||
export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&');
|
||||
|
||||
export class AxiosCanceler {
|
||||
/**
|
||||
* Add request
|
||||
* @param {Object} config
|
||||
*/
|
||||
addPending(config: AxiosRequestConfig) {
|
||||
this.removePending(config);
|
||||
const url = getPendingUrl(config);
|
||||
config.cancelToken =
|
||||
config.cancelToken ||
|
||||
new axios.CancelToken((cancel) => {
|
||||
if (!pendingMap.has(url)) {
|
||||
// If there is no current request in pending, add it
|
||||
pendingMap.set(url, cancel);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Clear all pending
|
||||
*/
|
||||
removeAllPending() {
|
||||
pendingMap.forEach((cancel) => {
|
||||
cancel && isFunction(cancel) && cancel();
|
||||
});
|
||||
pendingMap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removal request
|
||||
* @param {Object} config
|
||||
*/
|
||||
removePending(config: AxiosRequestConfig) {
|
||||
const url = getPendingUrl(config);
|
||||
|
||||
if (pendingMap.has(url)) {
|
||||
// If there is a current request identifier in pending,
|
||||
// the current request needs to be cancelled and removed
|
||||
const cancel = pendingMap.get(url);
|
||||
cancel && cancel(url);
|
||||
pendingMap.delete(url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: reset
|
||||
*/
|
||||
reset(): void {
|
||||
pendingMap = new Map<string, Canceler>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Data processing class, can be configured according to the project
|
||||
*/
|
||||
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
import type { RequestOptions, Result } from '/#/axios';
|
||||
|
||||
export interface CreateAxiosOptions extends AxiosRequestConfig {
|
||||
authenticationScheme?: string;
|
||||
transform?: AxiosTransform;
|
||||
requestOptions?: RequestOptions;
|
||||
}
|
||||
|
||||
export abstract class AxiosTransform {
|
||||
/**
|
||||
* @description: Process configuration before request
|
||||
* @description: Process configuration before request
|
||||
*/
|
||||
beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;
|
||||
|
||||
/**
|
||||
* @description: Request successfully processed
|
||||
*/
|
||||
transformRequestHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
|
||||
|
||||
/**
|
||||
* @description: 请求失败处理
|
||||
*/
|
||||
requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;
|
||||
|
||||
/**
|
||||
* @description: 请求之前的拦截器
|
||||
*/
|
||||
requestInterceptors?: (config: AxiosRequestConfig, options: CreateAxiosOptions) => AxiosRequestConfig;
|
||||
|
||||
/**
|
||||
* @description: 请求之后的拦截器
|
||||
*/
|
||||
responseInterceptors?: (res: AxiosResponse<any>) => AxiosResponse<any>;
|
||||
|
||||
/**
|
||||
* @description: 请求之前的拦截器错误处理
|
||||
*/
|
||||
requestInterceptorsCatch?: (error: Error) => void;
|
||||
|
||||
/**
|
||||
* @description: 请求之后的拦截器错误处理
|
||||
*/
|
||||
responseInterceptorsCatch?: (error: Error) => void;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { ErrorMessageMode } from '/#/axios';
|
||||
import { useMessage } from '/@/hooks/web/useMessage';
|
||||
import { useI18n } from '/@/hooks/web/useI18n';
|
||||
// import router from '/@/router';
|
||||
// import { PageEnum } from '/@/enums/pageEnum';
|
||||
import { useUserStoreWithOut } from '/@/store/modules/user';
|
||||
import projectSetting from '/@/settings/projectSetting';
|
||||
import { SessionTimeoutProcessingEnum } from '/@/enums/appEnum';
|
||||
|
||||
const { createMessage, createErrorModal } = useMessage();
|
||||
const error = createMessage.error!;
|
||||
const stp = projectSetting.sessionTimeoutProcessing;
|
||||
|
||||
export function checkStatus(status: number, msg: string, errorMessageMode: ErrorMessageMode = 'message'): void {
|
||||
const { t } = useI18n();
|
||||
const userStore = useUserStoreWithOut();
|
||||
let errMessage = '';
|
||||
|
||||
switch (status) {
|
||||
case 400:
|
||||
errMessage = `${msg}`;
|
||||
break;
|
||||
// 401: Not logged in
|
||||
// Jump to the login page if not logged in, and carry the path of the current page
|
||||
// Return to the current page after successful login. This step needs to be operated on the login page.
|
||||
case 401:
|
||||
userStore.setToken(undefined);
|
||||
errMessage = msg || t('sys.api.errMsg401');
|
||||
if (stp === SessionTimeoutProcessingEnum.PAGE_COVERAGE) {
|
||||
userStore.setSessionTimeout(true);
|
||||
} else {
|
||||
userStore.logout(true);
|
||||
}
|
||||
break;
|
||||
case 403:
|
||||
errMessage = t('sys.api.errMsg403');
|
||||
break;
|
||||
// 404请求不存在
|
||||
case 404:
|
||||
errMessage = t('sys.api.errMsg404');
|
||||
break;
|
||||
case 405:
|
||||
errMessage = t('sys.api.errMsg405');
|
||||
break;
|
||||
case 408:
|
||||
errMessage = t('sys.api.errMsg408');
|
||||
break;
|
||||
case 500:
|
||||
errMessage = t('sys.api.errMsg500');
|
||||
break;
|
||||
case 501:
|
||||
errMessage = t('sys.api.errMsg501');
|
||||
break;
|
||||
case 502:
|
||||
errMessage = t('sys.api.errMsg502');
|
||||
break;
|
||||
case 503:
|
||||
errMessage = t('sys.api.errMsg503');
|
||||
break;
|
||||
case 504:
|
||||
errMessage = t('sys.api.errMsg504');
|
||||
break;
|
||||
case 505:
|
||||
errMessage = t('sys.api.errMsg505');
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
if (errMessage) {
|
||||
if (errorMessageMode === 'modal') {
|
||||
createErrorModal({ title: t('sys.api.errorTip'), content: errMessage });
|
||||
} else if (errorMessageMode === 'message') {
|
||||
error({ content: errMessage, key: `global_error_message_status_${status}` });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { isObject, isString } from '/@/utils/is';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
|
||||
|
||||
export function joinTimestamp<T extends boolean>(join: boolean, restful: T): T extends true ? string : object;
|
||||
|
||||
export function joinTimestamp(join: boolean, restful = false): string | object {
|
||||
if (!join) {
|
||||
return restful ? '' : {};
|
||||
}
|
||||
const now = new Date().getTime();
|
||||
if (restful) {
|
||||
return `?_t=${now}`;
|
||||
}
|
||||
return { _t: now };
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Format request parameter time
|
||||
*/
|
||||
export function formatRequestDate(params: Recordable) {
|
||||
if (Object.prototype.toString.call(params) !== '[object Object]') {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const key in params) {
|
||||
// 判断是否是dayjs实例
|
||||
if (dayjs.isDayjs(params[key])) {
|
||||
params[key] = params[key].format(DATE_TIME_FORMAT);
|
||||
}
|
||||
if (isString(key)) {
|
||||
const value = params[key];
|
||||
if (value) {
|
||||
try {
|
||||
params[key] = isString(value) ? value.trim() : value;
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isObject(params[key])) {
|
||||
formatRequestDate(params[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
// axios配置 可自行根据项目进行更改,只需更改该文件即可,其他文件可以不动
|
||||
// The axios configuration can be changed according to the project, just change the file, other files can be left unchanged
|
||||
|
||||
import type { AxiosResponse } from 'axios';
|
||||
import type { RequestOptions, Result } from '/#/axios';
|
||||
import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform';
|
||||
import { VAxios } from './Axios';
|
||||
import { checkStatus } from './checkStatus';
|
||||
import { router } from '/@/router';
|
||||
import { useGlobSetting } from '/@/hooks/setting';
|
||||
import { useMessage } from '/@/hooks/web/useMessage';
|
||||
import { RequestEnum, ResultEnum, ContentTypeEnum, ConfigEnum } from '/@/enums/httpEnum';
|
||||
import { isString } from '/@/utils/is';
|
||||
import { getToken, getTenantId } from '/@/utils/auth';
|
||||
import { setObjToUrlParams, deepMerge } from '/@/utils';
|
||||
import signMd5Utils from '/@/utils/encryption/signMd5Utils';
|
||||
import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';
|
||||
import { useI18n } from '/@/hooks/web/useI18n';
|
||||
import { joinTimestamp, formatRequestDate } from './helper';
|
||||
import { useUserStoreWithOut } from '/@/store/modules/user';
|
||||
const globSetting = useGlobSetting();
|
||||
const urlPrefix = globSetting.urlPrefix;
|
||||
const { createMessage, createErrorModal } = useMessage();
|
||||
|
||||
/**
|
||||
* @description: 数据处理,方便区分多种处理方式
|
||||
*/
|
||||
const transform: AxiosTransform = {
|
||||
/**
|
||||
* @description: 处理请求数据。如果数据不是预期格式,可直接抛出错误
|
||||
*/
|
||||
transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => {
|
||||
const { t } = useI18n();
|
||||
const { isTransformResponse, isReturnNativeResponse } = options;
|
||||
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
|
||||
if (isReturnNativeResponse) {
|
||||
return res;
|
||||
}
|
||||
// 不进行任何处理,直接返回
|
||||
// 用于页面代码可能需要直接获取code,data,message这些信息时开启
|
||||
if (!isTransformResponse) {
|
||||
return res.data;
|
||||
}
|
||||
// 错误的时候返回
|
||||
|
||||
const { data } = res;
|
||||
if (!data) {
|
||||
// return '[HTTP] Request has no return value';
|
||||
throw new Error(t('sys.api.apiRequestFailed'));
|
||||
}
|
||||
// 这里 code,result,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式
|
||||
const { code, result, message, success } = data;
|
||||
// 这里逻辑可以根据项目进行修改
|
||||
const hasSuccess = data && Reflect.has(data, 'code') && (code === ResultEnum.SUCCESS || code === 200);
|
||||
if (hasSuccess) {
|
||||
if (success && message && options.successMessageMode === 'success') {
|
||||
//信息成功提示
|
||||
createMessage.success(message);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 在此处根据自己项目的实际情况对不同的code执行不同的操作
|
||||
// 如果不希望中断当前请求,请return数据,否则直接抛出异常即可
|
||||
let timeoutMsg = '';
|
||||
switch (code) {
|
||||
case ResultEnum.TIMEOUT:
|
||||
timeoutMsg = t('sys.api.timeoutMessage');
|
||||
const userStore = useUserStoreWithOut();
|
||||
userStore.setToken(undefined);
|
||||
userStore.logout(true);
|
||||
break;
|
||||
default:
|
||||
if (message) {
|
||||
timeoutMsg = message;
|
||||
}
|
||||
}
|
||||
|
||||
// errorMessageMode=‘modal’的时候会显示modal错误弹窗,而不是消息提示,用于一些比较重要的错误
|
||||
// errorMessageMode='none' 一般是调用时明确表示不希望自动弹出错误提示
|
||||
if (options.errorMessageMode === 'modal') {
|
||||
createErrorModal({ title: t('sys.api.errorTip'), content: timeoutMsg });
|
||||
} else if (options.errorMessageMode === 'message') {
|
||||
createMessage.error(timeoutMsg);
|
||||
}
|
||||
|
||||
throw new Error(timeoutMsg || t('sys.api.apiRequestFailed'));
|
||||
},
|
||||
|
||||
// 请求之前处理config
|
||||
beforeRequestHook: (config, options) => {
|
||||
const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true, urlPrefix } = options;
|
||||
|
||||
if (joinPrefix) {
|
||||
config.url = `${urlPrefix}${config.url}`;
|
||||
}
|
||||
|
||||
if (apiUrl && isString(apiUrl)) {
|
||||
config.url = `${apiUrl}${config.url}`;
|
||||
}
|
||||
const params = config.params || {};
|
||||
const data = config.data || false;
|
||||
formatDate && data && !isString(data) && formatRequestDate(data);
|
||||
if (config.method?.toUpperCase() === RequestEnum.GET) {
|
||||
if (!isString(params)) {
|
||||
// 给 get 请求加上时间戳参数,避免从缓存中拿数据。
|
||||
config.params = Object.assign(params || {}, joinTimestamp(joinTime, false));
|
||||
} else {
|
||||
// 兼容restful风格
|
||||
config.url = config.url + params + `${joinTimestamp(joinTime, true)}`;
|
||||
config.params = undefined;
|
||||
}
|
||||
} else {
|
||||
if (!isString(params)) {
|
||||
formatDate && formatRequestDate(params);
|
||||
if (Reflect.has(config, 'data') && config.data && Object.keys(config.data).length > 0) {
|
||||
config.data = data;
|
||||
config.params = params;
|
||||
} else {
|
||||
// 非GET请求如果没有提供data,则将params视为data
|
||||
config.data = params;
|
||||
config.params = undefined;
|
||||
}
|
||||
if (joinParamsToUrl) {
|
||||
config.url = setObjToUrlParams(config.url as string, Object.assign({}, config.params, config.data));
|
||||
}
|
||||
} else {
|
||||
// 兼容restful风格
|
||||
config.url = config.url + params;
|
||||
config.params = undefined;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
},
|
||||
|
||||
/**
|
||||
* @description: 请求拦截器处理
|
||||
*/
|
||||
requestInterceptors: (config: Recordable, options) => {
|
||||
// 请求之前处理config
|
||||
const token = getToken();
|
||||
let tenantid = getTenantId();
|
||||
if (token && (config as Recordable)?.requestOptions?.withToken !== false) {
|
||||
// jwt token
|
||||
config.headers.Authorization = options.authenticationScheme ? `${options.authenticationScheme} ${token}` : token;
|
||||
config.headers[ConfigEnum.TOKEN] = token;
|
||||
//--update-begin--author:liusq---date:20210831---for:将签名和时间戳,添加在请求接口 Header
|
||||
|
||||
// update-begin--author:taoyan---date:20220421--for: VUEN-410【签名改造】 X-TIMESTAMP牵扯
|
||||
config.headers[ConfigEnum.TIMESTAMP] = signMd5Utils.getTimestamp();
|
||||
// update-end--author:taoyan---date:20220421--for: VUEN-410【签名改造】 X-TIMESTAMP牵扯
|
||||
|
||||
config.headers[ConfigEnum.Sign] = signMd5Utils.getSign(config.url, config.params);
|
||||
//--update-end--author:liusq---date:20210831---for:将签名和时间戳,添加在请求接口 Header
|
||||
//--update-begin--author:liusq---date:20211105---for: for:将多租户id,添加在请求接口 Header
|
||||
if (!tenantid) {
|
||||
tenantid = 0;
|
||||
}
|
||||
config.headers[ConfigEnum.TENANT_ID] = tenantid;
|
||||
//--update-begin--author:liusq---date:20220325---for: 增加vue3标记
|
||||
config.headers[ConfigEnum.VERSION] = 'v3';
|
||||
//--update-end--author:liusq---date:20220325---for:增加vue3标记
|
||||
//--update-end--author:liusq---date:20211105---for:将多租户id,添加在请求接口 Header
|
||||
|
||||
// ========================================================================================
|
||||
// update-begin--author:sunjianlei---date:20220624--for: 添加低代码应用ID
|
||||
const routeParams = router.currentRoute.value.params;
|
||||
if (routeParams.appId) {
|
||||
config.headers[ConfigEnum.X_LOW_APP_ID] = routeParams.appId;
|
||||
// lowApp自定义筛选条件
|
||||
if (routeParams.lowAppFilter) {
|
||||
config.params = { ...config.params, ...JSON.parse(routeParams.lowAppFilter as string) };
|
||||
delete routeParams.lowAppFilter;
|
||||
}
|
||||
}
|
||||
// update-end--author:sunjianlei---date:20220624--for: 添加低代码应用ID
|
||||
// ========================================================================================
|
||||
}
|
||||
return config;
|
||||
},
|
||||
|
||||
/**
|
||||
* @description: 响应拦截器处理
|
||||
*/
|
||||
responseInterceptors: (res: AxiosResponse<any>) => {
|
||||
return res;
|
||||
},
|
||||
|
||||
/**
|
||||
* @description: 响应错误处理
|
||||
*/
|
||||
responseInterceptorsCatch: (error: any) => {
|
||||
const { t } = useI18n();
|
||||
const errorLogStore = useErrorLogStoreWithOut();
|
||||
errorLogStore.addAjaxErrorInfo(error);
|
||||
const { response, code, message, config } = error || {};
|
||||
const errorMessageMode = config?.requestOptions?.errorMessageMode || 'none';
|
||||
//scott 20211022 token失效提示信息
|
||||
//const msg: string = response?.data?.error?.message ?? '';
|
||||
const msg: string = response?.data?.message ?? '';
|
||||
const err: string = error?.toString?.() ?? '';
|
||||
let errMessage = '';
|
||||
|
||||
try {
|
||||
if (code === 'ECONNABORTED' && message.indexOf('timeout') !== -1) {
|
||||
errMessage = t('sys.api.apiTimeoutMessage');
|
||||
}
|
||||
if (err?.includes('Network Error')) {
|
||||
errMessage = t('sys.api.networkExceptionMsg');
|
||||
}
|
||||
|
||||
if (errMessage) {
|
||||
if (errorMessageMode === 'modal') {
|
||||
createErrorModal({ title: t('sys.api.errorTip'), content: errMessage });
|
||||
} else if (errorMessageMode === 'message') {
|
||||
createMessage.error(errMessage);
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
checkStatus(error?.response?.status, msg, errorMessageMode);
|
||||
return Promise.reject(error);
|
||||
},
|
||||
};
|
||||
|
||||
function createAxios(opt?: Partial<CreateAxiosOptions>) {
|
||||
return new VAxios(
|
||||
deepMerge(
|
||||
{
|
||||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes
|
||||
// authentication schemes,e.g: Bearer
|
||||
// authenticationScheme: 'Bearer',
|
||||
authenticationScheme: '',
|
||||
timeout: 10 * 1000,
|
||||
// 基础接口地址
|
||||
// baseURL: globSetting.apiUrl,
|
||||
headers: { 'Content-Type': ContentTypeEnum.JSON },
|
||||
// 如果是form-data格式
|
||||
// headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
|
||||
// 数据处理方式
|
||||
transform,
|
||||
// 配置项,下面的选项都可以在独立的接口请求中覆盖
|
||||
requestOptions: {
|
||||
// 默认将prefix 添加到url
|
||||
joinPrefix: true,
|
||||
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
|
||||
isReturnNativeResponse: false,
|
||||
// 需要对返回数据进行处理
|
||||
isTransformResponse: true,
|
||||
// post请求的时候添加参数到url
|
||||
joinParamsToUrl: false,
|
||||
// 格式化提交参数时间
|
||||
formatDate: true,
|
||||
// 异常消息提示类型
|
||||
errorMessageMode: 'message',
|
||||
// 成功消息提示类型
|
||||
successMessageMode: 'success',
|
||||
// 接口地址
|
||||
apiUrl: globSetting.apiUrl,
|
||||
// 接口拼接地址
|
||||
urlPrefix: urlPrefix,
|
||||
// 是否加入时间戳
|
||||
joinTime: true,
|
||||
// 忽略重复请求
|
||||
ignoreCancelToken: true,
|
||||
// 是否携带token
|
||||
withToken: true,
|
||||
},
|
||||
},
|
||||
opt || {}
|
||||
)
|
||||
);
|
||||
}
|
||||
export const defHttp = createAxios();
|
||||
|
||||
// other api url
|
||||
// export const otherHttp = createAxios({
|
||||
// requestOptions: {
|
||||
// apiUrl: 'xxx',
|
||||
// },
|
||||
// });
|
||||
@@ -0,0 +1,7 @@
|
||||
const title:any = {
|
||||
zh_cn: '腾讯云即时通信 Web-IM',
|
||||
en: 'Instant Messaging Web-IM',
|
||||
};
|
||||
export function switchTitle(locale:string) {
|
||||
return document.title = title[locale];
|
||||
}
|
||||
Reference in New Issue
Block a user