nuiapp请求封装

Posted GHUIJS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nuiapp请求封装相关的知识,希望对你有一定的参考价值。

//token操作封装
// import * as $auth from "./auth"

//域名
import BASE_URL from './config.js'

//字符串处理插件
import qs from 'qs'


//请求异常处理
function error(res) {
	if (res.statusCode === 401) { // 超时自动刷新
		// console.log($auth)
		uni.request({
			url: BASE_URL + '/sys/refreshToken',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
			},
			data: {
				// refreshToken: $auth.getRefreshToken()
			},
			method: "GET",
			success: res => {
				if (res.data && res.data.success) {
					// $auth.setUserToken(res.data.token)
					// $auth.setRefreshToken('token', res.data.refreshToken)
				} else {
					// $auth.removeUserToken();
					// $auth.removeRefreshToken();
					uni.reLaunch({
						url: "/pages/login/login"
					});
				}
			},
			fail: (res) => {
				// $auth.removeUserToken();
				// $auth.removeRefreshToken();
				uni.reLaunch({
					url: "/pages/login/login"
				});
			},
			complete: () => {

			}
		});
	} else if (res.statusCode === 402 || res.statusCode === 403) { // 402 未登录或者refresh token过时, 403 账号在其他地方登录
		uni.showToast({
			title: '请登录!',
			icon: "none"
		})
		// $auth.removeUserToken();
		uni.reLaunch({
			url: "/pages/login/login"
		});
	} else if (res.statusCode === 404) {
		uni.showToast({
			title: "404,路径找不到:" + res.data.path,
			icon: "none"
		})
	} else if (res.statusCode === 504) {
		uni.showToast({
			title: "网络连接错误",
			icon: "none"
		})
	} else {
		uni.showToast({
			title: res.data.msg,
			icon: "none"
		})
	}
}




function httpService(url, method, data, header) {
	data = data || {};

	header = header || {
		'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
	};

	method = method || "GET";
	let token = uni.getStorage({
		key: 'token',
		success: function(ress) {
			token = ress.data
		}
	});

	if (token) {
		header.token = token; // 获取token值
	}
	if (method === 'POST') {
		data = qs.stringify(data, {
			allowDots: true,
			arrayFormat: 'indices'
		})
	} else if (method === 'GET' || method === 'DELETE') {
		data = qs.stringify(data, {
			allowDots: true,
			arrayFormat: 'indices'
		})
		data = qs.parse(data)
	}

    //异步处理
	return new Promise((resolve, reject) => {
		uni.showLoading()
		uni.request({
			url: BASE_URL + url,
			header: header,
			data: data,
			method: method,
			success: res => {
				uni.hideLoading()
				if (res.data && res.data.success === false) {
					error(res);
				}
				resolve(res);
			},
			fail: (res) => {
				uni.hideLoading();
				error(res);
				reject(res);
			},
			complete: () => {}
		});
	})
}

export default {
	get: function(url, data, header) {
		return httpService(url, 'GET', data, header);
	},
	post: function(url, data, header) {
		return httpService(url, 'POST', data, header);
	},
	delete: function(url, data, header) {
		return httpService(url, 'DELETE', data, header);
	},
	put: function(url, data, header) {
		return httpService(url, 'PUT', data, header);
	},
	// upload: function(url, filePath, filename, formData, header, success, fail) {
	// 	return httpUpload(url, filePath, filename, formData, header, success, fail)
	// },
	// download: function(url, header, success, fail) {
	// 	return downloadFile(url, header, success, fail);
	// }
}

 

以上是关于nuiapp请求封装的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

回归 | js实用代码片段的封装与总结(持续更新中...)

VsCode 代码片段-提升研发效率

常用Javascript代码片段集锦

Python 向 Postman 请求代码片段