uniapp微信公众号授权登录,本地调试

在项目如何集中微信公众号授权登录

后端跳转获取code码,最后拼接,

                    //保留登录前的页面参数。为了不让参数传到后台,并且在微信授权链接带来带去,可以将参数和登录前的地址存到本地缓存
					let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
					let curRoute = routes[routes.length - 1].route //获取当前页面路由
					let curParam = routes[routes.length - 1].options; //获取路由参数
					console.log(routes)
						console.log(curRoute);
							console.log(curParam);
					if ("pages/login/login" != curRoute) {
					
					//保存登录前的地址  
					    uni.setStorageSync('curRoute','/'+curRoute );
					    
					        //保存登录前的参数  
					  uni.setStorageSync('curParam',curParam );
					     
					
					}
                    	window.location.href =
					          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=sdfsdfgsaa194f7ca6" 
					          +
					          "&redirect_uri=" +
					          encodeURIComponent("http://sdfa.test.com/pages/login/login") +
					          "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";		

然后会跳转前端login这个页面

在这个页面发起请求,用code去获取授权。得到token之后拼接登录前的页面

uni.setStorage({//将用户信息保存在本地
						  key: 'token',  
						 data: res.data.payload
					 });
let curRoute = uni.getStorageSync('curRoute');
					 let curParam =  uni.getStorageSync('curParam');
					 let url =  curRoute;
					 if(Object.keys(curParam).length > 0){  
					 
					       url += '?';  
					 
					      for(let i in curParam){  
					 
					             url += i + '=' + curParam[i] + '&';  
					 
					      }  
					 
					      url = url.substring(0, url.length - 1);  
					 
					 }  
					 uni.reLaunch({  
					 
					        url: url  
					 
					 });

注意,模式最好用history模式

如果本地调试,这个问题很多人是很头痛的,已经弄好的请略过,因为uniapp自带服务器,微信公众号的校验文件,很多人不知道放那个位置 。

我的做法是,用花生壳通过内网穿透,先映射到apache服务器,把文件放大apache服务器下面, 让微信校验成功。成功之后,将内网映射成uniapp服务器的地址,这样就可以进行本地调试了。

有不同的,请在下方提问 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值