2021-10-20 request.js

// const baseurl = "/"; //公共路径 测试
// const baseurl = ""; //公共路径 测试
const baseurl = " "; //公共路径 测试

// 请求封装
function request(option) {
	const userInfo = uni.getStorageSync('token') || {};
	let url = baseurl + option.url; //地址
	let data = option.data || {};
	let success = option.success;
	let method = option.method || 'POST';
	uni.request({
		url: url,
		data: data,
		header: {
			'User-Token': userInfo.Token
			// 'User-Token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoi5rWL6K-V5LyB5LiaIiwiaWQiOiI4IiwidXNlcnR5cGUiOiIyIiwiY29tcGFueWlkIjoiMSIsImNvbXBhbnluYW1lIjoi5rWL6K-V5LyB5LiaIiwibmJmIjoxNTk1MzgxNDY1LCJleHAiOjE1OTc4MDA2NjUsImlzcyI6InJlbGlnaW9uYXBpIiwiYXVkIjoiYXBpIn0.HBMi3RKAJYviIDLX5W-ZHeEC4UNZtd8goOcAm7kzCFQ"
		},
		method: method,
		success: function(res) {
			// 判断token是否过期
			if (res.data.status == 601) {
				uni.showModal({
					title: '提示',
					content: res.data.statusMsg,
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							uni.redirectTo({
								url: '../Login/register'
							})
						}
					}
				})
				return
			}
			success(res)
		},
		fail: function(res) {},
		complete: function(res) {
			// console.log("请求成功")
		},
	})
}


// 请求封装
function Request1(option) {
	const userInfo = uni.getStorageSync('token') || {};
	let url = baseurl + option.url; //地址
	let data = option.data || {};
	let success = option.success;
	let method = option.method || 'POST';
	uni.request({
		url: url,
		data: data,
		header: {
			'content-type': 'application/x-www-form-urlencoded',
			"from-source":'app'
			//'User-Token': userInfo.Token,
			// 'User-Token':"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoi5rWL6K-V5LyB5LiaIiwiaWQiOiI4IiwidXNlcnR5cGUiOiIyIiwiY29tcGFueWlkIjoiMSIsImNvbXBhbnluYW1lIjoi5rWL6K-V5LyB5LiaIiwibmJmIjoxNTk1OTI4MzExLCJleHAiOjE1OTgzNDc1MTEsImlzcyI6InJlbGlnaW9uYXBpIiwiYXVkIjoiYXBpIn0.sQKXgyNVwGVSqp9phxi4CZfl5UslqmYyoSKyDwC_QeU"
		},
		method: method,
		success: function(res) {
			// 判断token是否过期
			if (res.data.status == 601) {
				uni.showModal({
					title: '提示',
					content: res.data.statusMsg,
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							// uni.redirectTo({
							// 	// url: '../Login/register'
							// })
						}
					}
				})
				return
			}
			success(res)
		},
		fail: function(res) {},
		complete: function(res) {
			console.log("请求成功")
		},
	})
}
function Request2(option) {
	const userInfo = uni.getStorageSync('token') || {};
	let url = option.url; //地址
	let data = option.data || {};
	let success = option.success;
	let method = option.method || 'POST';
	uni.request({
		url: url,
		data: data,
		header: {
			'content-type': 'application/x-www-form-urlencoded',
			'User-Token': userInfo.Token,
			// 'User-Token':"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoi5rWL6K-V5LyB5LiaIiwiaWQiOiI4IiwidXNlcnR5cGUiOiIyIiwiY29tcGFueWlkIjoiMSIsImNvbXBhbnluYW1lIjoi5rWL6K-V5LyB5LiaIiwibmJmIjoxNTk1OTI4MzExLCJleHAiOjE1OTgzNDc1MTEsImlzcyI6InJlbGlnaW9uYXBpIiwiYXVkIjoiYXBpIn0.sQKXgyNVwGVSqp9phxi4CZfl5UslqmYyoSKyDwC_QeU"
		},
		method: method,
		success: function(res) {
			// 判断token是否过期
			if (res.data.status == 601) {
				uni.showModal({
					title: '提示',
					content: res.data.statusMsg,
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							uni.redirectTo({
								url: '../Login/register'
							})
						}
					}
				})
				return
			}
			success(res)
		},
		fail: function(res) {},
		complete: function(res) {
			console.log("请求成功")
		},
	})
}
// 上传图片封装
function uploadFile(option) {
	let url = baseurl + 'file-list/uploadImage'; //服务器地址
	let filePath = option.filePath; //要上传文件资源的路径。
	let formData = option.formData || {
		'user': 'test'
	};
	let success = option.success;
	let name = option.name || 'filePath'; //文件对应的 key 
	let method = option.method || 'POST'; //默认post请求
	uni.uploadFile({
		url: url,
		filePath: filePath,
		name: name,
		formData: formData,
		header: {
			'token': token,
		},
		success: function(res) {
			success(res)
		},
		fail: function(res) {},
		complete: function(res) {},
	})
}
// // 添加图片     上传多张图片  到服务器
// 		upload_pictures() {
// 			let that = this
// 			uni.chooseImage({
// 				count:9,  //图片数量
// 				sizeType: ['copressed', 'original'], //原图货压缩图
// 				sourceType: ['album', 'camera'], //相册或者拍照
// 				success: (chooseImageRes) => {
// 					const arrimg = chooseImageRes.tempFilePaths  //临时地址
// 					for (var i = 0; i < arrimg.length; i++) { 
// 						console.log(arrimg[i]) 
// 						httpsTypes.uploadFile({  //上传服务器
// 							filePath: arrimg[i],
// 							success: (res) => {
// 								let arr = JSON.parse(res.data);  //转换格式
// 								let imgurl = arr.msg 
// 								that.img.push(imgurl) //push进数组
// 							},
// 						});
// 					}
// 				}
// 			});
// 		},
export default {
	request: request,
	uploadFile: uploadFile,
	Request1: Request1,
	Request2:Request2
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sundy。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值