不同的用户获取自己唯一的openID
发布于 5 年前 作者 schen 18223 次浏览 来自 问答
  1. const APP_ID ='';//输入小程序appid  

  2. const APP_SECRET ='';//输入小程序app_secret  

  3. var OPEN_ID=''//储存获取到openid  

  4. var SESSION_KEY=''//储存获取到session_key  

  5. Page({  

  6.   getOpenIdTap:function(){  

  7.     var that=this;  

  8.     wx.login({  

  9.       success:function(res){  

  10.         wx.request({  

  11.             //获取openid接口  

  12.           url: 'https://api.weixin.qq.com/sns/jscode2session',  

  13.           data:{  

  14.             appid:APP_ID,  

  15.             secret:APP_SECRET,  

  16.             js_code:res.code,  

  17.             grant_type:'authorization_code'  

  18.           },  

  19.           method:'GET',  

  20.           success:function(res){  

  21.             console.log(res.data)  

  22.             OPEN_ID = res.data.openid;//获取到的openid  

  23.             SESSION_KEY = res.data.session_key;//获取到session_key  

  24.             console.log(OPEN_ID.length)  

  25.             console.log(SESSION_KEY.length)  

  26.             that.setData({  

  27.               openid: res.data.openid.substr(0, 10) + '********' + res.data.openid.substr(res.data.openid.length - 8, res.data.openid.length),  

  28.               session_key: res.data.session_key.substr(0, 8) + '********' + res.data.session_key.substr(res.data.session_key.length - 6, res.data.session_key.length)  

  29.             })  

  30.           }  

  31.         })  

  32.       }  

  33.     })  

  34.   }  

  35. })  

这个上面   输入appID 和appSecret 之后,只有注册小程序的关联的微信号登陆才能获取到openID,别的用户登录,就获取不到了。应该怎么写呢。不同的用户登录获取各自唯一的openID.

回到顶部