request.js 1.1 KB
Newer Older
L
123  
linju 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
1.优雅访问指定路由地址
2.load自动显示与关闭
3.统一路由拦截
	3.1 读取云端接口权限配置,先验证本地token再访问
	3.2 处理因token过期等问题自动更新本地token,或token无效跳转至登陆页面
*/
const debug = true;//开启后,会alert错误信息
export default function request(name,params,callback,{showLoading=false,loadText='',fail=()=>{}}={}){
	console.log('request');
	showLoading||loadText? uni.showLoading({title:loadText}):'';
	
	let routers =  name.split('/');
	var action = false
	if (routers.length>1){
		name = routers[0]
		action =  routers[1]
	}
	console.log({name,data: {action,params}})
	return uniCloud.callFunction({name,data: {action,params},
		success(e){
			console.log(e);
			if(showLoading || loadText) uni.hideLoading()
			const res = e.result
			if (res.code === 0 ) {
				return callback(res.data, e.result, e)
			}
			debug? uni.showModal({content: JSON.stringify(e)}) :'';
		},
		fail(err){
			console.log(err);
			debug? uni.showModal({content: JSON.stringify(err)}) :'';
			fail(err)
		}
	})
}