从上图可以看出。当我们通过wx.login()获取code的后,发送给我们的服务器,然后去请求微信服务器换取得到对应的openid与session_key,openid是该用户在小程序中的唯一标识用于模板通知之类的。session_key就是用来解密用户的敏感信息。unionid之类的。unionid是微信用户在所有微信平台下的唯一标识。下面我会讲解一下如何得到。
调用wx.login()得到code后请求服务器获取openid与session_key缓存在服务器当中。其中生成一个随机数为key,value为openid与session_key。然后返回到小程序通过wx.setStorageSync('LoginSessionKey',得到的随机数key)缓存在小程序当中。每当我们去请求服务器时带上LoginSessionKey即可给服务器读取从而判断用户是否在登录。是不是很简单呢?
通过wx.login()登录之后。我们可以通过wx.getUserInfo()获取用户信息。其中一些不敏感的信息在返回的 result中的userInfo里。如想要获取敏感信息。openid,unionid之类的。则需要从密文中去解密得到。
微信小程序 - 获取用户信息最近在研究微信小程序怎么玩的。接触后发现好多的坑。
比如在浏览器中我们可以通过document.getElementById 获取到页面的DOM对象。而在微信小程序中是获取不到DOM对象的。document.getElementById() 直接报错 getElementById not function 我也是醉了。不支持这个好多有趣的功能不能实现了。
言归正传,我谈下获取用户信息的感想。
有两种获取用户信息的方案。
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"
));
}
echo $userInfo;
}
getUserInfo function 其中wxBizDataCrypt.php 就是微信官方提供的素材包
curlHttp 函数是一个自定函数 该函数的源码查看我的这篇文章
curlHttp
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 = {
https_request : function(obj){
wx.request(obj);
},
upload_request : function(dataSource){
wx.uploadFile(dataSource);
}
};
module.exports = {
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);
},
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);
},
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);
}
};
app.globalData.host 就是域名地址如 https://xxxxx.com;
觉得不错请打赏,您的十分满意是笔者的无限动力
来自 http://blog.csdn.net/u014559227/article/details/71088788