评论

微信小程序获取用户openid

首先调用wx.login() 获取登录凭证(code),再通过登录凭证(code)获取用户登录信息,包括用户的唯一标识(openid) 及本次登录的会话密钥(session_key)。

首先调用wx.login() 获取登录凭证(code),再通过登录凭证(code)获取用户登录信息,包括用户的唯一标识(openid) 及本次登录的会话密钥(session_key)。

app.js代码

//app.js

globalData: {

userInfo:null,

openid:null,

session_key:null,

apiUrl:'http://localhost:1234/api/index.aspx',//请求接口地址

},

getUserProfile(e) {

var that = this

wx.getUserProfile({

desc: '获取用户信息',

success: (res) => {

wx.showToast({

title: '获取中...',

duration: 2000,

icon: 'loading',

mask:true,

})

that.globalData.userInfo = res.userInfo

//登录

wx.login({

success: res => {

//根据code获取openid,session_key

if (res.code) {

//发起网络请求

wx.request({

url: that.globalData.apiUrl,

data: {

code: res.code,

parameter: 'onLogin'

},

header: {

'content-type': 'application/json'

},

success: function(res) {

console.log(res.data.openid);//openid

console.log(res.data.session_key);//session_key

that.globalData.openid= res.data.openid;

that.globalData.session_key= res.data.session_key;

wx.showToast({

title: '获取成功',

duration: 1500,

icon: 'success'

})

},

fail: function() {

wx.showToast({

title: '获取失败',

duration: 2000,

icon: 'error'

})

}

}

}else {

            console.log('登录失败:' + res.errMsg)

          }

})

}

})

},

后端api代码

protected void Page_Load(object sender, EventArgs e)

{

string parameter = Request["parameter"];

switch (parameter)

{

case "onLogin": onLogin(); break;//获取用户的openid、session_key

}

}

#region 通过code获取用户的openid、session_key

public void onLogin()

{

string code = Request["code"];

string Str = GetJson("https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=" + code + "&grant_type=authorization_code");//参数appid的值为小程序的appid,参数secret的值为小程序的密钥

if (Str != null)

{

Response.Write(Str);

Response.End();

}

else

{

Response.Write("");

Response.End();

}

}

/// <summary>

/// 接受信息

/// </summary>

/// <param name="url">请求地址</param>

/// <returns></returns>

public static string GetJson(string url)

{

WebClient wc = new WebClient();

wc.Credentials = CredentialCache.DefaultCredentials;

wc.Encoding = Encoding.UTF8;

string returnText = wc.DownloadString(url);

if (returnText.Contains("errcode"))

{

//错误

}

return returnText;

}

#endregion首先调用wx.login() 获取登录凭证(code),再通过登录凭证(code)获取用户登录信息,包括用户的唯一标识(openid) 及本次登录的会话密钥(session_key)。

app.js代码

//app.js

globalData: {

userInfo:null,

openid:null,

session_key:null,

apiUrl:'http://localhost:1234/api/index.aspx',//请求接口地址

},

getUserProfile(e) {

var that = this

wx.getUserProfile({

desc: '获取用户信息',

success: (res) => {

wx.showToast({

title: '获取中...',

duration: 2000,

icon: 'loading',

mask:true,

})

that.globalData.userInfo = res.userInfo

//登录

wx.login({

success: res => {

//根据code获取openid,session_key

if (res.code) {

//发起网络请求

wx.request({

url: that.globalData.apiUrl,

data: {

code: res.code,

parameter: 'onLogin'

},

header: {

'content-type': 'application/json'

},

success: function(res) {

console.log(res.data.openid);//openid

console.log(res.data.session_key);//session_key

that.globalData.openid= res.data.openid;

that.globalData.session_key= res.data.session_key;

wx.showToast({

title: '获取成功',

duration: 1500,

icon: 'success'

})

},

fail: function() {

wx.showToast({

title: '获取失败',

duration: 2000,

icon: 'error'

})

}

}

}else {

            console.log('登录失败:' + res.errMsg)

          }

})

}

})

},

后端api代码

protected void Page_Load(object sender, EventArgs e)

{

string parameter = Request["parameter"];

switch (parameter)

{

case "onLogin": onLogin(); break;//获取用户的openid、session_key

}

}

#region 通过code获取用户的openid、session_key

public void onLogin()

{

string code = Request["code"];

string Str = GetJson("https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=" + code + "&grant_type=authorization_code");//参数appid的值为小程序的appid,参数secret的值为小程序的密钥

if (Str != null)

{

Response.Write(Str);

Response.End();

}

else

{

Response.Write("");

Response.End();

}

}

/// <summary>

/// 接受信息

/// </summary>

/// <param name="url">请求地址</param>

/// <returns></returns>

public static string GetJson(string url)

{

WebClient wc = new WebClient();

wc.Credentials = CredentialCache.DefaultCredentials;

wc.Encoding = Encoding.UTF8;

string returnText = wc.DownloadString(url);

if (returnText.Contains("errcode"))

{

//错误

}

return returnText;

}

#endregion

最后一次编辑于  2022-11-09  
点赞 0
收藏
评论

3 个评论

  • Mr.Gud
    Mr.Gud
    2023-04-10

    这种方法只能够获取到开发者自己的openid呀,怎么能够获取到不同用户的openid,有好兄弟解答下的吗


    2023-04-10
    赞同
    回复
  • 老男孩
    老男孩
    2023-03-01

    其实还有个简单得办法 不需要登录能够拿到openid不知道说出来官方会不会又觉得不安全给封了

    2023-03-01
    赞同
    回复
  • ༉
    2022-11-23

    请问大佬解决了吗?我这也出问题

    2022-11-23
    赞同
    回复
登录 后发表内容