uni-app请求

144 阅读2分钟
import Vue from "vue";
import Request from '@/utils/luch-request/index.js'
import store from '@/store/index.js'
import {baseUrl, devApi} from '@/utils/config.js'
import {getToken} from '@/utils/auth'

const http = new Request();

let whitePath = ['/login'];

const errorCode = {
	'401': '认证失败,无法访问系统资源',
	'403': '当前操作没有权限',
	'404': '访问资源不存在',
	'default': '系统未知错误,请反馈给管理员'
}

// 全局配置
http.setConfig((config) => {
	/* config 为默认全局配置*/
	config.timeout = 60 * 1000; /* 超时时间 */
	return config
})

// 拦截器
http.interceptors.request.use(
	config => {
		let url = config.url;
		let isToken = true;
		whitePath.forEach(path => {
			if (url.includes(path)) {
				isToken = false;
			}
		})

		if (isToken) {
			// 设置 token
			let token = getToken();
			if (token) config.header['Authorization'] = 'Bearer ' + token;
		}
		return config
	},
	error => {
		Vue.prototype.$hideLoading();
		uni.showToast({
			title: "未知错误",
			icon: 'none'
		});
		return Promise.reject(error);
	}
)

// 响应器
http.interceptors.response.use((res) => {
	Vue.prototype.$hideLoading();
	// 未设置状态码则默认成功状态
	const code = res.data.code || 200;
	// 获取错误信息
	const msg = errorCode[code] || res.data.msg || errorCode['default']
	
	if (code === 200) {
		return res.data
	} else {
		if (code === 401 || msg === "登录状态已过期" || msg === "令牌不能为空") {
			uni.showToast({
				title: "登录状态已过期,请重新登录",
				icon: 'none'
			});
			store.dispatch('FedLogOut');
			uni.redirectTo({
				url: "/pages/login/index"
			});
		} else {
			uni.showToast({
				title: msg,
				icon: 'none'
			});
		}
		console.log(msg)
		return Promise.reject(msg);
	}
}, (response) => {
	Vue.prototype.$hideLoading();
	let {message} = response;

	if (message === "Network Error" || !message) {
		message = "后端接口连接异常";
	} else if (message.includes("timeout")) {
		message = "连接内网失败,请下载VPN。联系人:张建伟(18633809032)";
	} else if (message.includes("Request failed with status code")) {
		message = "系统接口" + message.substr(message.length - 3) + "异常";
	}

	uni.showToast({
		title: message,
		icon: 'none'
	});
	console.log(message)
	return Promise.reject(response);
})


const Http = {
	baseUrl: baseUrl,
	devApi: devApi,

	get(url, params, config = {}) {
		return http.get(url, {
			params: params,
			...config
		})
	},

	getForm(url, params, config = {}) {
		return http.get(url, {
			params: params,
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			...config
		})
	},

	post(url, params, config = {}) {
		return http.post(url, params, {
			...config
		})
	},

	postForm(url, params, config = {}) {
		return http.post(url, params, {
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			...config
		})
	},

	put(url, params, config = {}) {
		return http.put(url, params, {
			headers: {
				'content-type': "application/json"
			},
			...config
		})
	},

	putForm(url, params, config = {}) {
		return http.put(url, params, {
			headers: {
				'content-type': "application/x-www-form-urlencoded"
			},
			...config
		})
	},
	blobPostForm(url, params, config = {}) {
		return http.post(url, params, {
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			responseType:'arraybuffer',
			...config
		})
	},

	delete(url, params, config = {}) {
		return http.delete(url, params, {
			...config
		})
	},

	upload(url, params, config = {}) {
		return http.upload(url, {
			params: params,
			...config
		})
	}
}

export default Http

//使用
import { baseUrl, devApi } from '@/utils/config.js'
import Http from '@/utils/http.js'

const baseUrlApi = baseUrl + devApi;

/**
 * 投标前评估列表
 */
export function BdBidBeforeGetBidBeforeInfoListApi(data) {
	return Http.post(baseUrlApi + '/bid/bid_before/get_bid_before_info_list', data);
}