有两种获取用户信息的方案。 
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. January 23 2017 Week 4 Monday

    Knowledge is long, life is short. 吾生也有涯,而知也无涯. I often feel that I have a lot of things to learn, ne ...

  2. ArcSde服务启动后又停止

    问题:突然发现ArcSde服务无法启动,“ArcSde服务启动后又停止,......” 环境:Win7+ArcSDE10 +Oracle11g 由于本人使用ArcSde时间不长,基本没有遇到过什么问题 ...

  3. SVN安装操作流程

    SVN 安装操作流程 1.服务端安装流程 1.1 双击打开svn-server安装包 1.2 点击Next 1.3 勾选上“I accert the terms in the License Agre ...

  4. [转]How to Leak a Context: Handlers & Inner Classes

    Consider the following code: public class SampleActivity extends Activity { private final Handler mL ...

  5. 前端面试题总结(一)HTML篇

    前端面试题总结(一)HTML篇 一.iframe的优缺点? 缺点: 1.会阻塞主页面的onload事件(iframe和主页面共享链接池,而浏览器对相同域的链接有限制,所以会影响页面的并行加载). 解决 ...

  6. Junit4所需jar包

    在eclipse中新建一个Junit类,运行时出现java.lang.NoClassdeffounderror:org/apache/commons/logging/LogFactory错误,原来是缺 ...

  7. Java中类继承、接口实现的一些要注意的细节问题

    1.接口A和接口B有相同的方法,只是返回值不同,则实现类不能同时实现这两个接口中的方法. 接口A有void C()方法,接口B有int C()方法,则无法同时实现这两个接口. Java为了弥补类单继承 ...

  8. 折腾apt源的时候发生的错误

    在折腾Ubuntu源的时候,把新的源替换进去,然后 sudo apt-get update 之后报错: W: Unknown Multi-Arch type 'no' for package 'com ...

  9. react 开发中的问题简记

    1.什么时候用props 什么时候用state ? 不能使用props:当页面组件存在URL跳转问题时候,原因:若单独刷新,他会报错,拿不到前面的数据: 使用props场景:当组件为页面组件的一部分即 ...

  10. datagrid和combobox简单应用

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...