123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import axios from 'axios'
- import { getChromeStorage } from '@/uilts/chromeExtension.js'
- import { baseAPIUrl, appVersionCode } from '@/http/configAPI.js'
- let userInfo = '';
- let storage_mid = ''
- // 创建axios实例
- export const service = axios.create({
- baseURL: baseAPIUrl, // api的base_url
- timeout: 240000, // 请求超时时间
- headers: {
- 'Content-Type': 'application/json', //指定消息格式
- 'Accept': 'application/json',
- },
- })
- function checkParams(config) {
- const { accessToken: token = '', uid = '' } = userInfo || {};
- const { mid } = storage_mid || {};
- if (config.method === 'get') {
- let { baseInfo = null } = config.params || {};
- let params = {
- ...config.params
- }
- let {pageSource} = params.params || {};
- if(pageSource) {
- delete params.params.pageSource;
- }
- if (!baseInfo || !baseInfo.token) {
- params['baseInfo'] = {
- token: token,
- mid,
- appVersionCode,
- loginUid: uid,
- uid,
- appType:1,
- machineCode: mid,
- pageSource: pageSource || ''
- }
- }
- config['params'] = params;
- }
- if (config.method === 'post') {
- let { baseInfo = null } = config.data || {};
- let data = {
- ...config.data
- }
- console.log('data', data)
- let {pageSource} = data.params || {};
- if(pageSource) {
- delete data.params.pageSource;
- }
- if (!baseInfo || !baseInfo.token) {
- data['baseInfo'] = {
- token: token,
- mid,
- appVersionCode,
- loginUid: uid,
- uid,
- appType:1,
- machineCode: mid,
- pageSource: pageSource || ''
- }
- }
- config['data'] = data;
- }
- return config
- }
- // request拦截器
- service.interceptors.request.use(async (config) => {
- if (!userInfo) {
- userInfo = await getChromeStorage('userInfo') || ''
- }
- if (!storage_mid) {
- storage_mid = await getChromeStorage('mid') || ''
- }
- return checkParams(config)
- }, error => {
- // Do something with request error
- console.log(error) // for debug
- Promise.reject(error)
- })
- // respone拦截器
- service.interceptors.response.use(
- response => {
- const res = response.data;
- return res
- },
- error => {
- console.log('err' + error) // for debug
- return Promise.reject(error)
- }
- )
|