对于微信小程序登录的理解图
2024-09-17 09:25:23
有两种获取用户信息的方案。
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);
}
};
最新文章
- Java-URL类详解
- 贪心 POJ 2586 Y2K Accounting Bug
- Sky数[HDU2097]
- Writing Text File From A Tabular Block In Oracle Forms
- ios下input focus弹出软键盘造成fixed元素位置移位
- [转]tftp在put上传的时候显示File not found的解决办法
- Retrofit – Java(Android) 的REST 接口封装类库
- treeview OnSelectedNodeChanged js的方法
- BroadCastReceiver中耗时操作导致ANR
- bat执行java程序的脚本解析
- 每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询
- JavaScript中undefined 和not defined
- java同步和互斥【用具体程序说明】
- 表table
- JS经典题目解析
- 星型数据仓库olap工具kylin介绍和简单使用示例
- 使用jQuery+huandlebars防止编码注入攻击
- 【Java】 剑指offer(8) 用两个栈实现队列
- c++设计一个无法被继承的类
- quartz延迟执行一次
热门文章
- java 开发常用IDE
- 使用combineReducers注意事项
- SP34096 【DIVCNTK - Counting Divisors (general)】
- nrf52840蓝牙BLE5.0空中速率测试(nordic对nordic)
- [19/03/31-星期日] IO技术_四大抽象类_字符流( 字符输入流 Reader、 字符输出流 Writer )(含字符缓冲类)
- HTML5——前端预处理技术(Less、Sass、CoffeeScript)
- chromium之ScopedNSAutoreleasePool浅析
- 哈希查找解决地址冲突的两种最常见方法(线性探测再散列,链地址法)C++实现
- hdu_5698_瞬间移动
- linux系统基础之六--系统引导(基于centos7.4 1708)