| | |
| | | import website from '@/config/website' |
| | | import { Base64} from 'js-base64' |
| | | |
| | | const http = new Request() |
| | | const httpGd = new Request() |
| | | |
| | | export function request (config) { |
| | | return new Promise((resolve, reject) => { |
| | | http |
| | | httpGd |
| | | .request(config) |
| | | .then(res => { |
| | | resolve(res) |
| | |
| | | |
| | | |
| | | // 请求拦截器 |
| | | function requestInterceptors(http) { |
| | | function requestInterceptors(httpGd) { |
| | | |
| | | http.interceptors.request.use((config) => { |
| | | httpGd.interceptors.request.use((config) => { |
| | | const {detail} = useUserStore().$state?.userInfo || {} |
| | | // 假设有token值需要在头部需要携带 |
| | | let accessToken = useUserStore()?.$state?.userInfo?.gd_access_token; |
| | | // let accessToken = useUserStore()?.$state?.userInfo?.gd_access_token; |
| | | let accessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiIwMDAwMDAiLCJ1c2VyX25hbWUiOiJ5ankiLCJyZWFsX25hbWUiOiJ5ankiLCJhdmF0YXIiOiIiLCJhdXRob3JpdGllcyI6WyJrZmpzIl0sImNsaWVudF9pZCI6InNhYmVyIiwicm9sZV9uYW1lIjoia2ZqcyIsImxpY2Vuc2UiOiJwb3dlcmVkIGJ5IGJsYWRleCIsInBvc3RfaWQiOiIiLCJ1c2VyX2lkIjoiMjAwODc5MzU1MDA1OTkyNTUwNSIsInJvbGVfaWQiOiIyMDA4NzMzNDY5MTg3MzY2OTE0Iiwic2NvcGUiOlsiYWxsIl0sIm5pY2tfbmFtZSI6InlqeSIsIm9hdXRoX2lkIjoiIiwiZGV0YWlsIjp7ImFyZWFDb2RlIjpudWxsLCJhcmVhTmFtZSI6Iuaxn-ilv-ecgSIsImFjdGl2ZUxvYW5zQXJlYUNvZGUiOltdfSwiZXhwIjoxNzY5MDc1MzY1LCJkZXB0X2lkIjoiMTEyMzU5ODgxMzczODY3NTIwMSIsImp0aSI6ImUwMGFhNzIzLTU5YjgtNDNmNy1hZmU2LTNiMjZlNTM5NTUxMiIsImFjY291bnQiOiJ5ankifQ.CJTDqgL2wu2KgrUNncBz34VYPoxUfKXxgScBUUE-BhM' |
| | | if (accessToken) { |
| | | config.header['Blade-Auth'] = 'bearer ' + accessToken; |
| | | } |
| | |
| | | // 安全请求header |
| | | config.header['Blade-Requested-With'] = 'BladeHttpRequest'; |
| | | // 客户端认证参数 |
| | | config.header['Authorization'] = 'Basic ' + Base64.encode(website.clientId + ':' + website.clientSecret); |
| | | // config.header['Authorization'] = 'Basic ' + Base64.encode(website.clientId + ':' + website.clientSecret); |
| | | config.header['Authorization'] = 'Basic ' + 'c2FiZXI6c2FiZXJfc2VjcmV0'; |
| | | return config |
| | | }, config => { // 可使用async await 做异步操作 |
| | | return Promise.reject(config) |
| | | }) |
| | | } |
| | | |
| | | function responseInterceptors(http) { |
| | | http.interceptors.response.use((response) => { |
| | | function responseInterceptors(httpGd) { |
| | | httpGd.interceptors.response.use((response) => { |
| | | let res = response |
| | | const status = res.data.error_code || res.data.code || res.statusCode |
| | | const message = res?.data?.msg || res?.data?.error_description || res?.data?.message || '系统错误' |
| | |
| | | |
| | | // 引入拦截器配置 |
| | | export function setupRequest () { |
| | | http.setConfig(defaultConfig => { |
| | | httpGd.setConfig(defaultConfig => { |
| | | |
| | | /* defaultConfig 为默认全局配置 */ |
| | | defaultConfig.baseURL = getEnvObj().VITE_API_BASE_URL_GD |
| | | // #ifdef H5 |
| | |
| | | // #endif |
| | | return defaultConfig |
| | | }) |
| | | requestInterceptors(http) |
| | | responseInterceptors(http) |
| | | requestInterceptors(httpGd) |
| | | responseInterceptors(httpGd) |
| | | } |
| | | |
| | | |