對於微信小程序登錄的理解圖
阿新 • • 發佈:2017-05-04
function set gin sre 接口 psr 登錄 img als
有兩種獲取用戶信息的方案。
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);
}
};
對於微信小程序登錄的理解圖