uni-app微信公众号登录

155 阅读1分钟

获取url参数的方法:这个方法url必须是带着#/的

 			  let pages =  getCurrentPages()
				   let len = pages.length
				   let curParam = pages[len - 1].options //获取当前页面参数

所以完整的代码就是下面的

 			  let pages =  getCurrentPages()
				   let len = pages.length
				   let curParam = pages[len - 1].options //获取当前页面参数
				  let token = uni.getStorageSync('user_token');
		 
		 
				  if(token==""){
					  console.log(curParam["code"]);
					 
					  if((curParam["code"]== null || curParam["code"] === '')){
					  	 // 这是获取后端接口的方法,可以写成自己的
					  	 //url是同步回调页面、platform是登录方式 
					  		wxGetCode({"url":"域名/h5/#/","platform":"wechat"})
					  		.then(result=>{
					  		  //后端返回获取code的url,前端跳转
					  		  window.location.href =result.data;
					  		}).catch(error => {
					  		  console.log("错误",error.data);
					  			uni.showToast({
					  				title: error.data.msg,
					  				icon:'none',
					  				duration: 2000
					  			});
					  		});
					   
					  }else{
					  		
					  		//如果回调中亦code的参数,走下面同样是获取后端接口的方法,可以写成自己的
					  		wxloging({"state":curParam["state"],"code":curParam["code"],"platform":"wechat"})
					  		.then(result=>{
					  		 	//后端接口成功后,保存状态
					  			 uni.setStorageSync("user_token" , result.data.token);
					  			 uni.setStorageSync("user" , result.data);
					  		}).catch(error => {
								uni.showToast({
									title: error.data.msg,
									icon:'none',
									duration: 2000
								});
						 
					  		});
					  }
				  }