|
|
|
@ -1,5 +1,5 @@
|
|
|
|
import uviewPlus from 'uview-plus';
|
|
|
|
import { http } from 'uview-plus';
|
|
|
|
import { useAuthStore, useCommonStore} from '@/store';
|
|
|
|
import { useAuthStore, useCommonStore } from '@/store';
|
|
|
|
import { env } from '@/env';
|
|
|
|
import { env } from '@/env';
|
|
|
|
|
|
|
|
|
|
|
|
const HEADER_FORM = 'application/x-www-form-urlencoded'
|
|
|
|
const HEADER_FORM = 'application/x-www-form-urlencoded'
|
|
|
|
@ -8,14 +8,15 @@ const HEADER_JSON = 'application/json;charset=UTF-8';
|
|
|
|
const HEADER_TEXT = 'text/plain;charset=UTF-8';
|
|
|
|
const HEADER_TEXT = 'text/plain;charset=UTF-8';
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
export {
|
|
|
|
HEADER_FORM,HEADER_UPLOAD,HEADER_JSON,HEADER_TEXT
|
|
|
|
HEADER_FORM, HEADER_UPLOAD, HEADER_JSON, HEADER_TEXT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const HttpConfig = ()=>{
|
|
|
|
|
|
|
|
|
|
|
|
export const HttpConfig = () => {
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化请求配置
|
|
|
|
// 初始化请求配置
|
|
|
|
uni.$u.http.setConfig((config) => {
|
|
|
|
http.setConfig((config) => {
|
|
|
|
|
|
|
|
|
|
|
|
/* config 为默认全局配置*/
|
|
|
|
/* config 为默认全局配置*/
|
|
|
|
config.baseURL = env.baseApi;
|
|
|
|
config.baseURL = env.baseApi;
|
|
|
|
@ -31,13 +32,13 @@ export const HttpConfig = ()=>{
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 请求拦截
|
|
|
|
// 请求拦截
|
|
|
|
uni.$u.http.interceptors.request.use((config) => {
|
|
|
|
http.interceptors.request.use((config) => {
|
|
|
|
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
|
|
|
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
|
|
|
config.data = config.data || {};
|
|
|
|
config.data = config.data || {};
|
|
|
|
config.custom = config.custom || {};
|
|
|
|
config.custom = config.custom || {};
|
|
|
|
if(!config.header["Content-Type"]){
|
|
|
|
if (!config.header["Content-Type"]) {
|
|
|
|
let type = (config.custom.type || 'none').toLowerCase();
|
|
|
|
let type = (config.custom.type || 'none').toLowerCase();
|
|
|
|
switch(type){
|
|
|
|
switch (type) {
|
|
|
|
case 'form':
|
|
|
|
case 'form':
|
|
|
|
config.header["Content-Type"] = HEADER_FORM;
|
|
|
|
config.header["Content-Type"] = HEADER_FORM;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
@ -69,20 +70,20 @@ export const HttpConfig = ()=>{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截
|
|
|
|
// 响应拦截
|
|
|
|
uni.$u.http.interceptors.response.use(async (response) => {
|
|
|
|
http.interceptors.response.use(async (response) => {
|
|
|
|
useCommonStore().hideLoading();
|
|
|
|
useCommonStore().hideLoading();
|
|
|
|
const data = response.data;
|
|
|
|
const data = response.data;
|
|
|
|
const custom = response.config.custom || {};
|
|
|
|
const custom = response.config.custom || {};
|
|
|
|
|
|
|
|
|
|
|
|
if (data.code == 401) {
|
|
|
|
if (data.code == 401) {
|
|
|
|
if(custom.showError){
|
|
|
|
if (custom.showError) {
|
|
|
|
uni.showToast({title:'请重新登录',icon:'error',duration:3000});
|
|
|
|
uni.showToast({ title: '请重新登录', icon: 'error', duration: 3000 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAuthStore().$reset();
|
|
|
|
useAuthStore().$reset();
|
|
|
|
uni.reLaunch({
|
|
|
|
uni.reLaunch({
|
|
|
|
url:"/pages/index/login"
|
|
|
|
url: "/pages/index/login"
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}else if (data.code == 200) {
|
|
|
|
} else if (data.code == 200) {
|
|
|
|
if (!custom.returnData) {
|
|
|
|
if (!custom.returnData) {
|
|
|
|
return data
|
|
|
|
return data
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -90,7 +91,7 @@ export const HttpConfig = ()=>{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (custom.showError) {
|
|
|
|
if (custom.showError) {
|
|
|
|
uni.showToast({title: data.message || data.msg,icon:'error',duration:3000});
|
|
|
|
uni.showToast({ title: data.message || data.msg, icon: 'error', duration: 3000 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (custom.catch) {
|
|
|
|
if (custom.catch) {
|
|
|
|
return Promise.reject(data)
|
|
|
|
return Promise.reject(data)
|
|
|
|
@ -109,9 +110,9 @@ export const HttpConfig = ()=>{
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
errorData = message || '网络错误';
|
|
|
|
errorData = message || '网络错误';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uni.showToast({title: errorData,icon:'error',duration:3000});
|
|
|
|
uni.showToast({ title: errorData, icon: 'error', duration: 3000 });
|
|
|
|
return Promise.reject(response)
|
|
|
|
return Promise.reject(response)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const http = uni.$u.http;
|
|
|
|
export { http };
|