小程序get,post,图片上传 封装

/**
 * common network methonds
 *
 */

const app = getApp();
const host = app.config.host;
const host_img = 'https://upload-z2.qiniup.com';


// get 
const get = (url,params,succ,fail) => {
	//let access_token = app.globalData.accesstoken;
	let access_token = wx.getStorageSync('accesstoken');

	//开始请求
	wx.request({
		url: host+url,
		data: params,
		method: 'GET',
		header: {
			"content-type": "application/x-www-form-urlencoded",	
			'source': 'weapp',
			'accesstoken': access_token
		},
		success:function(res){
			res = res.data;
			const {code ,msg,data} = res;

			if(code !== 0){
				wx.showToast({title: msg,icon: 'none'});
				return;
			}

			succ(res);
		},
		fail:function(){
			if(fail){
				fail();
			}
		}
	});
};


// post 
const post = (url,postdata,succ,fail) => {

	//let access_token = app.globalData.accesstoken;
	let access_token = wx.getStorageSync('accesstoken');

	wx.request({
		url: host+url,
		data: postdata,
		method: 'POST',
		header: {
			"content-type": "application/x-www-form-urlencoded",	
			'source': 'weapp',
			'accesstoken': access_token
		},
		success:function(res){
			res = res.data;
			const {code ,msg,data} = res;

			if(code !== 0){
				wx.showToast({title: msg,icon: 'none'});
				return;
			}

			succ(res);
		},
		fail:function(){
			if(fail){
				fail();
			}
		}
	});
};

// upload single file to qiniu cloud
const upload_qiniu = (filepath,succ,fail) => {
	const qiniutoken = app.globalData.qiniutoken;
	wx.uploadFile({
		url: host_img, 
		filePath: filepath,
		name: 'file',
		formData:{
			'token': qiniutoken,
		},
		success: function(res){
			//@todo request filed 
			succ(res.data);
		},
		fail:function(){
			fail();
		}
	});
};

// 批量上传图片
const uploadAll = (files,callback) => {
	let uploadedArr = [];
	for(let i = 0; i < files.length; i++){
		upload_qiniu(files[i],res=>{
			res = JSON.parse(res);
			uploadedArr.push({index:i,val:res.key});

			// 图片全部上传完成
			if(uploadedArr.length == files.length){
				uploadedArr.sort(function(x,y){
					return x.index - y.index
				})

				const newList = uploadedArr.map(item=>{
					return item.val
				});

				callback(newList);
			}
		});
	}
};

//***==================== (version 2.0 ) begin =========== ***/

const upload = (file) => {
	return new Promise((resolve,reject)=>{
		const qiniutoken = app.globalData.qiniutoken;
		wx.uploadFile({
			url: host_img, 
			filePath: file,
			name: 'file',
			formData:{
				'token': qiniutoken,
			},
			success: function(res){
				resolve(res.data);
			},
			fail:function(err){
				reject(err);
			}
		});

	})
}

const uploadBatch = (files) => {
	const promises = files.map((file)=>{
		return upload(file)
	})
	return Promise.all(promises)
}

const base = (options) => {
	return new Promise((resolve,reject)=>{
		const access_token = wx.getStorageSync('accesstoken');
		wx.request({
			url: host+'/'+options.url,
			data: options.data,
			method: options.method,
			header: {
				"content-type": "application/x-www-form-urlencoded",	
				"source": "weapp",
				"accesstoken": access_token
			},
			success:function(res){
				resolve(res.data);
			},
			fail:function(err){
				reject(err)
			}
		})
	})
}

const newGet = (url,data) => {
	const options = {
		url: url,
		data: data,
		method: 'GET'
	}
	return base(options)
}

const newPost = (url,data) => {
	const options = {
		url: url,
		data: data,
		method: 'POST'
	}
	return base(options)
}

//***==================== (version 2.0 ) end =========== ***/

module.exports = {
	get: get,
	post: post,
	upload_qiniu: upload_qiniu,
	uploadAll: uploadAll,
	upload: upload,
	uploadBatch: uploadBatch,
	newGet: newGet,
	newPost: newPost,
}

 

你可能感兴趣的:(小程序get,post,图片上传 封装)