微信小程序获取用户openid

news/2024/7/20 0:59:18 标签: 微信小程序, 小程序, javascript, openid

openid_0">小程序>微信小程序获取用户openid

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

app.js代码

javascript">//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

http://www.niftyadmin.cn/n/500480.html

相关文章

windows一键切换mysql版本_window10同时安装多个版本的Mysql

我的电脑已经安装mysql5.5.24&#xff0c;现在再安装个mysql8.0.111、下载mysql8.0.11,解压到目录下2、新建my.ini[mysql]# 设置mysql客户端默认字符集default-character-setutf8[mysqld]#设置3306端口port 3307# 设置mysql的安装目录basedirD:\mysql-5.5.54# 设置mysql数据库…

Windows 2012 Hyper –V 3.0 New Functions

1-Hyper –V 复制Hyper-V 3.0提供的复制特性&#xff0c;允许管理员为现有的虚拟机创建副本&#xff0c;提供了一种简单而实用的故障转移和灾难恢复的方案 防火墙允许HV2 启用复制,指定HV1 ,复制到HV1察看复制状态故障转移方式:在HV2 上 ,选择计划故障转移在HV1上也可以选择即…

我心中想念那位偷吃玉米的老朋友

明天是我的76岁生日&#xff0c;与小孙女恰好是同一天过生日&#xff0c;此时&#xff0c;我心中回忆起在《五七干校》的美好时光。 记得&#xff0c;在1969年的夏天&#xff0c;我开着“铁牛”拖拉机到黑龙江德都市办事&#xff0c;路上经过一片玉米地&#xff0c;从里面突然钻…

mysql error code 145_Mysql ERROR 145 (HY000) Table crashed

Errors about the table:The cause behind the errorLow disk space could be a reason for MySQL table being marked as “crashed”. When you have low space on your disk, some of the tables in the database may get crashed. It usually happens when you get space i…

IIS实现http跳转https的重定向方法

IIS实现http跳转https的重定向方法 一、安装部署证书 首先再控制台申请SSL证书&#xff0c;阿里云与腾讯云控制台可以领取一次数量为20的免费证书&#xff0c;期限一年。 在IIS服务器上安装IIS证书&#xff08;阿里云&#xff09; IIS 服务器 SSL 证书安装部署&#xff08;腾…

mysql 按条件join_MySQL Join竟然可以这么玩?根据条件进行复制

原标题&#xff1a;MySQL Join竟然可以这么玩&#xff1f;根据条件进行复制本文节选自松华老师的《SQL优化专栏》郑松华&#xff0c;知数堂SQL 优化班老师现任 CCmediaService DBA&#xff0c;主要负责数据库优化相关工作擅长SQL优化 &#xff0c;数据核对大家好&#xff0c;我…

实验二报告

北京电子科技学院&#xff08;BESTI&#xff09; 实 验 报 告 课程&#xff1a;Java   班级&#xff1a; 1352    姓名&#xff1a;谈愈敏   学号&#xff1a;20135220 成绩&#xff1a;    指导教师&#xff1a;娄嘉鹏        实验…

Win+R命令,你了解多少?

在windows系统中经常经常需要使用一些快捷键快速打开某些设置&#xff0c;在windows系统中由于开始运行命令很难找到&#xff0c;通过WinR组合快捷键即可快速打开运行命令框。 1、启动计算器 calc2、启动字符映射表 charmap3、Chkdsk磁盘检查 chkdsk4、磁盘清理 cleanmgr5、…