request.js 1.01 KB
const BASE_URL = 'http://course.zktljz.com';

/**
 * url
 * method
 * data
 * header
 */
export const request = (options) => {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync("token")
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: {
				'Content-Type': 'application/json',
				Authorization: token || "",
				...options.header,
			},
			success: (res) => {
				if (res?.data.code === 401) {
					uni.setStorageSync('token', "")
					uni.showToast({
						title: '登录过期',
						icon: 'none'
					});
					uni.navigateTo({
						url: '/pages/login/login'
					})
					reject(res);
				}
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					// uni.showToast({ title: '请求错误', icon: 'none' });
					reject(res);
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				});
				reject(err);
			},
		});
	});
};