有两种获取用户信息的方案。 
1、不包含敏感信息openId 的json对象(包含:nickname、avatarUrl等基本信息) 
2、包含敏感信息openId的基本信息。

第一种获取方案 
1、首先调用wx.login()接口 让用户授权验证,也就是我们肉眼观察到的,你是否对xxxxx授权这种信息。 
2、用户成功授权后,调用wx.getUserInfo() 接口获取用户信息。 
完整代码如下

wx.login({ success:function(){ wx.getUserInfo({ success:function(res){ var simpleUser = res.userInfo; console.log(simpleUser.nickName); } }); }
});

第二种比较复杂了,需要与后台进行交互才能获得userInfo,但是这种方案获得的数据是完整的(包含openId)。 
1、调用wx.login()接口 授权 在success 成功函数的参数中包含code。 
2、调用wx.getUserInfo()接口success 函数中包含encryptedData、iv 
3、将上述参数传给后台解析,生成userInfo 
代码如下 
js

var request = require("../../utils/request.js");

wx.login({
success:function(res_login){
if(res_login.code)
{
wx.getUserInfo({
withCredentials:true,
success:function(res_user){
var requestUrl = "/getUserApi/xxx.php";
var jsonData = {
code:res_login.code,
encryptedData:res_user.encryptedData,
iv:res_user.iv
};
request.httpsPostRequest(requestUrl,jsonData,function(res){
console.log(res.openId);
});
}
})
}
}
})

后台解析

/** * 获取粉丝信息 * 其中的参数就是前端传递过来的 */
public function wxUserInfo($code,$encryptedData,$iv) {
$apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wxConfig['appid']}&secret={$this->wxConfig['appsecret']}&js_code={$code}&grant_type=authorization_code"; $apiData = json_decode(curlHttp($apiUrl,true),true); if(!isset($apiData['session_key']))
{
echoJson(array(
"code" => 102,
"msg" => "curl error"
),true);
} $userInfo = getUserInfo($this->wxConfig['appid'],$apiData['session_key'],$encryptedData,$iv); if(!$userInfo)
{
echoJson(array(
"code" => 105,
"msg" => "userInfo not"
));
} //$userInfo = json_decode($userInfo,true); //载入用户服务
//$userService = load_service("User"); //$userService->checkUser($this->projectId,$userInfo); echo $userInfo; //微信响应的就是一个json数据
}

getUserInfo function 其中wxBizDataCrypt.php 就是微信官方提供的素材包

//获取粉丝信息
function getUserInfo($appid,$sessionKey,$encryptedData,$iv){
require_once ROOTPATH . "/extends/wxUser/wxBizDataCrypt.php";
$data = array();
$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) {
return $data;
} else {
return false;
}
}

自己写的小工具 request.js


var app = getApp(); //远程请求
var __httpsRequest = { //http 请求
https_request : function(obj){
wx.request(obj);
}, //文件上传
upload_request : function(dataSource){
wx.uploadFile(dataSource);
}
}; module.exports = {
//执行异步请求get
httpsRequest:function(obj){
var jsonUrl = {};
jsonUrl.url = obj.url;
if(obj.header)jsonUrl.header=obj.header;
if(obj.type)
jsonUrl.method = obj.type;
else
jsonUrl.method="GET";
if(obj.data)jsonUrl.data = obj.data;
obj.dataType?(jsonUrl.dataType=obj.dataType):(jsonUrl.dataType="json"); jsonUrl.success = obj.success; jsonUrl.data.projectId = app.globalData.projectId; __httpsRequest.https_request(jsonUrl);
}, //get 请求
httpsGetRequest:function(req_url,req_obj,res_func) {
var jsonUrl = {
url:app.globalData.host + req_url,
header:{"Content-Type":"application/json"},
dataType:"json",
method:"get",
success:function(res) {
typeof res_func == "function" && res_func(res.data);
}
} if(req_obj)
{
jsonUrl.data = req_obj;
} jsonUrl.data.projectId = app.globalData.projectId; __httpRequest.https_request(jsonUrl);
}, //post 请求
httpsPostRequest:function(req_url,req_obj,res_func) {
var jsonUrl = {
url:app.globalData.host + req_url,
header:{"Content-Type":"application/x-www-form-urlencoded"},
dataType:"json",
method:"post",
success:function(res) {
typeof res_func == "function" && res_func(res.data);
}
} if(req_obj)
{
jsonUrl.data = req_obj;
} jsonUrl.data.projectId = app.globalData.projectId; __httpsRequest.https_request(jsonUrl);
}, //文件上传
httpsUpload:function(uid,fileDataSource,res_func) {
dataSource = {
url:app.globalData.host + req_url,
header:{
"Content-Type":"multipart/form-data"
},
dataType:"json",
formData : {
"uid" : uid
},
filePath : fileDataSource,
name : "fileObj",
success:function(res){
typeof res_func == "function" && res_func(res);
}
} __httpsRequest.upload_request(dataSource);
}
};

对于微信小程序登录的理解图的更多相关文章

  1. 微信小程序登录方案

    微信小程序登录方案 登录程序 app.js 调用wx.login获取code 将code作为参数请求自己业务登录接口获取session_key 存储session_key 如果有回调执行回调 App( ...

  2. 微信小程序登录,获取code,获取openid,获取session_key

    微信小程序登录 wx.login(Object object) 调用接口获取登录凭证(code).通过凭证进而换取用户登录态信息,包括用户的唯一标识(openid)及本次登录的会话密钥(session ...

  3. 基于Shiro,JWT实现微信小程序登录完整例子

    小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html ...

  4. 微信小程序登录JAVA后台

    代码地址如下:http://www.demodashi.com/demo/12736.html 登录流程时序登录流程时序 具体的登录说明查看 小程序官方API 项目的结构图: springboot项目 ...

  5. 微信小程序登录对接Django后端实现JWT方式验证登录

    先上效果图 点击授权按钮后可以显示部分资料和头像,点击修改资料可以修改部分资料. 流程 1.使用微信小程序登录和获取用户信息Api接口 2.把Api获取的用户资料和code发送给django后端 3. ...

  6. 全栈项目|小书架|微信小程序-登录及token鉴权

    小程序登录 之前也写过微信小程序登录的相关文章: 微信小程序~新版授权用户登录例子 微信小程序-携带Token无感知登录的网络请求方案 微信小程序开通云开发并利用云函数获取Openid 也可以通过官方 ...

  7. Flask与微信小程序登录(后端)

    开发微信小程序时,接入小程序的授权登录可以快速实现用户注册登录的步骤,是快速建立用户体系的重要一步.这篇文章将介绍 python + flask + 微信小程序实现用户快速注册登录方案(本文主要进行后 ...

  8. Taro -- 微信小程序登录

    Taro微信小程序登录 1.调用Taro.login()获取登录凭证code: 2.调用Taro.request()将code传到服务器: 3.服务器端调用微信登录校验接口(appid+appsecr ...

  9. Spring Security 整合 微信小程序登录的思路探讨

    1. 前言 原本打算把Spring Security中OAuth 2.0的机制讲完后,用小程序登录来实战一下,发现小程序登录流程和Spring Security中OAuth 2.0登录的流程有点不一样 ...

随机推荐

  1. 基于SAP Kyma的订单编排增强介绍

    尽管有一万个舍不得,2018年还是无可挽回地离我们远去了. 唯有SAP成都研究院的同事和我去年在网络上留下的这些痕迹,能证明2018年我们曾经很认真地去度过每一天: SAP成都研究院2018年总共87 ...

  2. 关于HiddenHttpMethodFilter

    这个类的代码比较少,所以把整个类的代码都复制过来.在注释中添加上自己的理解. public class HiddenHttpMethodFilter extends OncePerRequestFil ...

  3. logback配置详解和使用

    最近知道一种打印日志的新方法,在此做一下学习总结. 转自:行走在云端的愚公 https://www.cnblogs.com/warking/p/5710303.html 一.logback的介绍   ...

  4. sublime text 插件及快捷键的使用

    安装插件准备步骤: 1.先安装管理插件,插件必备:package control 1.按ctrl+` 调出console 2.在底部代码行粘贴以下代码并回车: import urllib2,os;pf ...

  5. Charles Proxy v4.1.3 Mac、Win64、Win32破解版

    http://charles.iiilab.com/ 1. 下载Charles Proxy 4.1.3版本,百度云盘下载 或 去官网下载 2. 安装后先打开Charles一次(Windows版可以忽略 ...

  6. python查看微信消息撤回

    准备环境 python语言环境 python解释器-pycharm itchat介绍 itchat是一个开源的微信个人号接口,通过itchat可以实现微信(好友或微信群)的信息处理,包括文本.图片.小 ...

  7. rabbitmq+topic+java

    可参照github代码:https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java/EmitLogTopic.java 1. 新建m ...

  8. Subnetting

    Subnet Addressing To better utilize IP address Subnet addressing introduces another hierarchical(分层) ...

  9. Java 编码规范(转)

    本文转自:http://www.javaranger.com/archives/390 文章总结出了java编码过程中的一些规范,以便参考. 1.合理组织代码层次,分层清晰:controller.lo ...

  10. CF考古活动

    Codeforces Beta Round #1 http://codeforces.com/contest/1 A.测试用水题,呵呵.给三个数nma,求ceil(n/a)*ceil(m/a). 长整 ...