小程序获取openid

小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系。

wx.login(Object object)

调用接口获取登录凭证(code)。通过凭证进而换取用户登录态信息,包括用户的唯一标识(openid)及本次登录的会话密钥(session_key)等。用户数据的加解密通讯需要依赖会话密钥完成。

通过wx.login获取登录凭证code  ,然后调用后台接口wx.request发送参数code调用接口换取openid和session_key,wx.checkSession检测当前用户登录态是否有效

onLaunch: function (options) {
// 检测版本的更新(添加)
var that = this;
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
})
// 查看是否授权
// that.getSetting()
that.getOpenid()
},
// 获取openid
getOpenid:function(){
var that = this
wx.login({
success: function (loginCode) {
// console.log(loginCode.code)
// 获取openid
wx.request({
url: ‘xxxxxxxxxxxx’,
data: {
js_code: loginCode.code
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'POST',
success: function (res) {
if (res.data.code == 1) {
that.globalData.openId = res.data.data.openid;
console.log(that.globalData.openId)
if (that.employIdCallback) {
that.globalData.openId = res.data.data.openid;
that.employIdCallback(that.globalData.openId);
}
}
},
// 获取openid失败
fail: function (res) { }
})
}
})
},
globalData: {
openId: '',
token:'',
},

小程序授权

方法1:getSetting获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限

获取用户nickName,avatarUrl,gender,city,province,country,language等各种信息
  // 查看是否授权
getSetting:function(){
var that = this
wx.getSetting({
success(res) {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success(res) {
console.log(res)
var avatarUrl = res.userInfo.avatarUrl;
var nickName = res.userInfo.nickName;
that.globalData.avatarUrl = avatarUrl;
that.globalData.nickName = nickName;
}
})
}else{
// 未授权
}
}
})
},

方法2:如果只是需要获取头像和昵称的话使用微信开放能力

<open-data type="userAvatarUrl"></open-data>
<open-data type="userGender" lang="zh_CN"></open-data> //城市和省份获取的都是字母拼写,例如:Bozhou Anhui
<open-data type="userCity"></open-data>
<open-data type="userProvince" lang="zh_CN"></open-data>

参考:https://developers.weixin.qq.com/community/develop/doc/0006026b3c83c0e244573a0025bc08

小程序获取openid 小程序授权的更多相关文章

  1. C# 微信小程序获取openid sessionkey

    项目介绍 1.微信小程序获取openid和session_key 2.后台使用C#开发 项目流程 准备工作 1 获取appid 1.1 下载微信web开发工具 https://developers.w ...

  2. C++ 一个程序获取另一个程序Edit控件的内容

    //一个程序获取另一个程序Edit控件的内容 //根据指定程序的标题名获取改程序窗口的句柄 HWND hWnd=::FindWindow(NULL,"zhang001"); if( ...

  3. 微信小程序 获取OpenId

    微信小程序 官方API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/ 首先 以下代码是 页面加载请求用户 是否同意授权 同意之后 用code 访问 微信 ...

  4. 小程序获取openid和unionid java实现

    官方api:https://developers.weixin.qq.com/miniprogram/dev/api/api-login.html#wxloginobject 参考文章:https:/ ...

  5. 微信小程序——获取openid

    1.在微信小程序后台得到appid.AppSecret 2.在任意小程序界面的[onLoad]中取得code(建议将代码写在index.js中) 3.用取得code换取openid 如后台无法将ope ...

  6. .NET开发微信小程序-获取OpenId

    注:获取当前用户信息只需要用GetUserInfo这个方法就行.这里就不需要提了 前端代码: CallBack:回调函数 function GetOpenID(CallBack){ var appIn ...

  7. 小程序获取 openid 和 session_key

    <?php //获取openid function getopenid(){//获取用户ID //code为前端通过 wx.login() 方式获取 $code = $_GET["co ...

  8. 小程序获取openId

    1.小程序获取微信openId   wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId / ...

  9. PHP 微信公众号/小程序获取openid,用户信息

    1.获取code (获得openid的前置条件) 地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redi ...

随机推荐

  1. Python3 tkinter基础 Canvas create_line 画实线与虚线

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. Java内存模型原理总结(转自51CTO)

    转载地址:http://developer.51cto.com/art/201811/587220.htm [51CTO.com原创稿件]这篇文章主要介绍模型产生的问题背景,解决的问题,处理思路,相关 ...

  3. Asp.Net 之 DropDownList的使用

    这里不细说,直接上案例 <td style="width: 30px;" align="right"> 年月: </td> <td ...

  4. (转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)

    干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码) 该博客来源自:https://mp.weixin.qq.com/s?__biz=MzA4NzE1NzYyMw==& ...

  5. Python实现机器学习算法:EM算法

    ''' 数据集:伪造数据集(两个高斯分布混合) 数据集长度:1000 ------------------------------ 运行结果: ---------------------------- ...

  6. Docker:Stacks

    Prerequisites Install Docker version 1.13 or higher. Get Docker Compose as described in Part 3 prere ...

  7. Katana的WebAPI集成Swagger 解决方案

    这位大哥写的博客很清楚了,我就不重复了. http://www.cnblogs.com/caodaiming/p/4156476.html 错误解决 http://blog.csdn.net/gold ...

  8. 【译】第8节---EF Code First中配置类

    原文:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx 前面的章节中我们知道 ...

  9. Perl中命令行参数以及打开管道文件

    打开管道文件   Linux提供了管道机制,可以方便应用程序之间的数据传递.在Perl中,扣开和使用管道可采用如下形式的open函数:   open(Filehandle,”丨 CMD”);   其中 ...

  10. Intellij idea 2017 图标含义

    File Type Icon Recognized in ActionScript files ActionScript files Ultimate Edition Active Server Pa ...