最近在研究微信小程序怎么玩的。接触后发现好多的坑。 本文就教大家微信小程序获取用户信息如何实现。
比如在浏览器中我们可以通过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"
));
}
//$userinfo = json_decode($userinfo,true);
//载入用户服务
//$userservice = load_service("user");
//$userservice->checkuser($this->projectid,$userinfo);
echo $userinfo; //微信响应的就是一个json数据
}
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 = {
//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);
}
};
app.globaldata.host 就是域名地址如 https://xxxxx.com;
相关推荐:
thinkphp5如何实现微信小程序获取用户信息接口的案例
关于获取用户信息的10篇文章推荐
网页授权获取用户信息的方法
以上就是微信小程序获取用户信息如何实现的详细内容。