原创-微信H5支付后页面不跳转问题解决方案

原创微信H5支付类参考>>>

其中用到了Jquery.

/*
微信H5支付页面回跳问题解决方案
作者:七歌工作室(V:sevstudio,Q:10287093)

微信H5支付是通过访问一个URL来实现唤起微信支付,
支付后不会自动跳转到结果页面,
常规使用弹窗引导用户点击支付结果再进行处理,
这里写个小方法模拟下让他实现支付成功后自动跳转到自定义页面。
苹果安卓都适用!!!
*/

/*
调用方式
H5支付获取URL后,跳转之前,执行

SevOrder.add('订单号');

//监听
$(function(){
	setInterval(function(){
		SevOrder.watch();
	},100); //间隔大小依据实际情况调整
});
*/

//处理任务函数示例
function testFunc(){
	return new Promise(function(resolve, reject) {
		setTimeout(function(){
			resolve('hello');
		},1500);
	});
}


//主要代码
var SevOrder = {
	KEY : 'order_waiting',
	locked : false, //避免处理函数耗时 > watch间隔
	watch : function(){
		if(this.locked){
			//console.log('locked');
			return;
		}
		
		var list = this.get();
		if(list.length < 1){
			//console.log('no wait');
			return; //没有记录
		}
		
		var item = list[0];
		//console.log('task',item);
		this.locked = true;
		var that = this;
		testFunc().then(res=>{
			//如果成功了
			//console.log('remove',item);
			that.remove(item);
			that.locked = false;
			
			//如果需要做跳转或者其他操作的话,写到这里,
			//或者直接写到testFunc里
		});
	},
	get : function(){
		var json = localStorage.getItem(this.KEY);
		if(!json) return [];
		var list = $.parseJSON(json);
		return list ? list : [];
	},
	add : function(item){
		var list = this.get();
		if(list.indexOf(item) == -1){
			list.push(item);
			localStorage.setItem(this.KEY,JSON.stringify(list));
		}
	},
	remove: function(item){
		var list = this.get();
		var index = list.indexOf(item);
		if(index != -1){
			list.splice(index,1);
		}
		localStorage.setItem(this.KEY,JSON.stringify(list));
	}
}

使用起来还是比较简单,如果使用过程遇到bug可以联系我升级下。