request.js 3.19 KB
import service from './service.js'
import config from '../common/config.js'
import auth from '@/common/auth.js'
import { redirectToLogin } from '@/common/auth-nav.js'
const get = function(url, data) {
	return request(url, 'GET', data);
}
const getp = function(url, data) {
	return request(url, 'GET');
}
const post = function(url, data, headers, timeoutMs) {
	return request(url, 'POST', data, headers, timeoutMs);
}
const put = function(url, data) {
	return request(url, 'put', data);
}
const postFormData = function(url, data) {
	return request(url, 'POST', data, {
		'Content-Type': 'application/x-www-form-urlencoded'
	});
}
const postPatchJson = function(url, data) {
	return request(url, 'POST', data, {
		'Content-Type': 'application/json-patch+json'
	});
}
const del = function(url, data) {
	return request(url, 'DELETE', data);
}

const request = function(url, method, data, headers, timeoutMs, options) {
	let header = {}
	var opts = options || {}
	if (!opts.anonymous) {
		const token = auth.getToken()
		if (token) {
			header['Authorization'] = token
		}
	}
	// 如果URL已经包含完整地址,则不添加BASE_URL
	if (url.startsWith('http')) {
		// URL已经是完整的,直接使用
	} else {
		url = config.getApiBaseUrl() + url;
	}
	
	// console.error(url)
	if (headers) {
		header = Object.assign({}, header, headers)
	}
	const timeout = timeoutMs != null ? timeoutMs : 50000
	return new Promise((resolve, reject) => { 
		uni.request({
			url,
			data, 
			method,
			header,
			timeout,
			success: (res) => {
				// console.error(url,res.data.code)
				if (res.statusCode === 200) {
					const biz = res.data || {}
					if (!opts.anonymous && (biz.code == 600 || biz.msg == '登录过期,请重新登录')) {
						redirectToLogin()
					}
					if (biz.code < 0 && biz.message) {
						uni.showToast({
							icon: 'none',
							title: biz.data && biz.data.message,
							duration: 2000
						});
						reject(res.data);
					} else if (biz.code === 500) {
						// uni.showToast({
						// 	icon: 'none',
						// 	title: res.data.msg || '操作失败,请联系管理员',
						// 	duration: 2000
						// });
					
						// uni.removeStorageSync('UserToken_KEY');
						resolve(biz);
					} else {
						resolve(biz);
					}
				} else if (res.statusCode === 403 || res.statusCode === 401) {
					service.addToken('');
					// uni.reLaunch({
					// 	url: '/pages/login/login'
					// });
					reject(res.data || { msg: '未授权' });
				} else {
					const body = res.data && typeof res.data === 'object' ? res.data : {}
					reject({
						msg: body.msg || body.message || ('请求失败 ' + res.statusCode),
						code: body.code,
						data: body
					});
					// uni.showToast({
					// 	icon:'none',
					//     title: res.data.msg || '系统繁忙!',
					//     duration: 2000
					// });
				}

			},
			fail: (error) => {
				uni.hideLoading();
				// uni.showToast({
				// 	icon:'none',
				//     title: '网络错误!',
				//     duration: 2000
				// });
				const errMsg = (error && (error.errMsg || error.message)) || '网络错误'
				reject({ msg: errMsg, raw: error });
				console.log(error);
			}
		});
	});
}

export default {
	get,
	post,
	del,
	request,
	postFormData,
	put,
	postPatchJson,
	getp
}