小程序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,
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值