微信公众号登录办法

小程序公众号统一后台解决办法

async login2(){
	if(uni.getStorageSync('token')){
	//这里填写验证token的请求
	//let istokenTime = await this.$u.post('/api/user/isToken');
		return;
	}
	//如果登录成功清除url上的code
	let code= this.getUrlKey("code");
	if(code){
	  console.log('已有code',code)
	  console.log('缓存的code',uni.getStorageSync('code'))
	if(code==uni.getStorageSync('code')){
	  let newUrl = this.delUrlKey('code')
	  window.location.href=newUrl;
	}else{
	  console.log('没有code,设置已有的code')
	  console.log('有code值:',code)
	  console.log('开始向服务器请求')
	  let _self = this;
	  var [error, res] =await uni.request({
	  url: _self.apiServer+'/api/wei/getUserOpentId?code='+code, //接口地址。
	  });
	  //用户token与昵称与头像
	  let getUserinfo = res.data.data
	  this.userData.username=getUserinfo.username;
	  this.userData.Heads=getUserinfo.Heads;
	  uni.setStorageSync('username', getUserinfo.username);
	  uni.setStorageSync('Heads', getUserinfo.Heads);
	  uni.setStorageSync('token', getUserinfo.token);
	  console.log('用户数据',this.userData)
	  console.log('用户token',uni.getStorageSync('token'));
	  this.is_login=true;
	 }else{
		console.log('没有code值:',code)
		console.log('调取微信接口')
		this.getaip();
	}	
	},
		//获取微信code
		async getaip(){
			console.log('获取当前域名')
			let redirect_uri=encodeURIComponent(window.location.href);
			
			console.log(redirect_uri)
			let appid = await this.$u.post('/api/wei/getappid');
			//let appid = "wx5a954fbcc9b55f79";
			let Urldata = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
			+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=sherk#wechat_redirect"
			window.location.href=Urldata;			
		},
		//得到code
		getUrlKey(name){
			return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
		},
		//清除code参数
		delUrlKey(name){
			 var loca = window.location;
			    var baseUrl = loca.origin + loca.pathname + "?";
			    var query = loca.search.substr(1);
			    if (query.indexOf(name)>-1) {
			        var obj = {}
			        var arr = query.split("&");
			        for (var i = 0; i < arr.length; i++) {
			            arr[i] = arr[i].split("=");
			            obj[arr[i][0]] = arr[i][1];
			        };
			        delete obj[name];
			        var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
			        return url
			    };
		}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇