第一步,在小程序后台打开获取手机号功能(需审核,提前申请)
提交资料时,建议严格按照示例样式申请;因为只能提交一张图片,所以要把好几张示意图P到一张图片上(无美工者可以用画图3D的贴纸功能)。
另外,其中一张图明确要求展示获取手机号时的弹窗,而不开通功能弹窗又不能显示,可以选择把示例的弹窗P上去~~,就是这个弹窗:
第二步,前端代码(务必先完成第一步,否则getphonenumber会报fail auth? deny)
Html:
<button open-type="getPhoneNumber" @getphonenumber="getphonenumber">手机号快捷登录</button>
Js:
let session_key = null;
let bd_applet_openid = null; //可选,以后推送消息时可能会用,建议登录时带上
//request改成自己的请求函数,没有就用uni.request
//百度小程序特有的uni.getLoginCode
uni.getLoginCode({
success: res => {
request({
url: `get_access_token.php`,
data: {
code: res.code
}
}).then(v => {
session_key = v.session_key;
bd_applet_openid = v.open_id;//可选,以后推送消息时可能会用,建议登录时带上
})
}
})
function getphonenumber(e) {
uni.checkSession({
success:res=>{
console.log(res)
}
}) //可以把下方代码都放到checkSession里,这里仅作示例
// #ifdef MP-BAIDU
const url = `load.php`;
const data = {
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
bd_applet_openid: bd_applet_openid,
session_key: session_key
}
// #endif
if (e.detail.errMsg === 'getPhoneNumber:ok') {
request({
url: url,
data: data,
method: 'POST',
needLoading: true
}).then(v => {
loadOk(v)
})
}
}
function loadOk(){
console.log('登录成功')
}
get_access_token.php:
<?php
date_default_timezone_set("Asia/Shanghai");
//包含app_id、app_key、app_secret
include 'applet_info.php';
$ch = curl_init();
$url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=$app_key&client_secret=$app_secret&scope=smartapp_snsapi_base";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$response = json_decode($response,true);
$access_token = $response['access_token'];
//获取手机号码
$code = $_GET['code'];
$url = "https://openapi.baidu.com/rest/2.0/smartapp/getsessionkey?access_token=$access_token&code=$code";
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$getuserphonenumber_json = json_decode($response,true);
if(!$getuserphonenumber_json['data']){
$json = json_encode(array(
"resultCode"=>901,
"message"=>'获取手机号失败901'
),256);
exit($json);
}
$open_id = $getuserphonenumber_json['data']['open_id'];
$session_key = $getuserphonenumber_json['data']['session_key'];
if(!$session_key){
$json = json_encode(array(
"resultCode"=>902,
"message"=>'获取手机号失败902',
),256);
exit($json);
}
echo json_encode(array(
"resultCode"=>200,
"message"=>'ok',
"open_id" => $open_id,
"session_key" => $session_key,
));
load.php
<?php
$encryptedData = $_POST['encryptedData'];
$iv = $_POST['iv'];
$session_key = $_POST['session_key'];
$bd_applet_openid = $_POST['bd_applet_openid'];
if(!$encryptedData ||!$iv ||!$session_key ||!$bd_applet_openid){
$array = array(
"resultCode"=>800,
"resultMsg"=>"参数错误"
);
echo json_encode($array);
exit();
}
//包含app_id、app_key、app_secret
include 'applet_info.php';
/**
* @Author: smartprogram_rd@baidu.com
* Copyright 2018 The BAIDU. All rights reserved.
*
* 百度小程序用户信息加解密示例代码(面向过程版)
* 示例代码未做异常判断,请勿用于生产环境
*/
function test($app_secret, $session_key, $iv, $encryptedData) {
// var_dump(array(
// "app_secret" => $app_secret,
// "session_key" => $session_key,
// "iv" => $iv,
// "encryptedData" => $encryptedData
// ));
$plaintext = decrypt($encryptedData, $iv, $app_secret, $session_key);
// 解密结果应该是 '{"openid":"open_id","nickname":"baidu_user","headimgurl":"url of image","sex":1}' //错误的,解密出来只有mobile属性
$obj = json_decode($plaintext, true);
$mobile = $obj['mobile'];
//之后进行登录操作
}
test($app_secret, $session_key, $iv, $encryptedData);
/**
* 数据解密:低版本使用mcrypt库(PHP < 5.3.0),高版本使用openssl库(PHP >= 5.3.0)。
*
* @param string $ciphertext 待解密数据,返回的内容中的data字段
* @param string $iv 加密向量,返回的内容中的iv字段
* @param string $app_key 创建小程序时生成的app_key
* @param string $session_key 登录的code换得的
* @return string | false
*/
function decrypt($ciphertext, $iv, $app_key, $session_key) {
// var_dump(array(
// "app_key" => $app_key,
// "ciphertext" => $ciphertext,
// "iv" => $iv,
// "session_key" => $session_key
// ));
$session_key = base64_decode($session_key);
$iv = base64_decode($iv);
$ciphertext = base64_decode($ciphertext);
$plaintext = false;
if (function_exists("openssl_decrypt")) {
$plaintext = openssl_decrypt($ciphertext, "AES-192-CBC", $session_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
} else {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, null, MCRYPT_MODE_CBC, null);
mcrypt_generic_init($td, $session_key, $iv);
$plaintext = mdecrypt_generic($td, $ciphertext);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if ($plaintext == false) {
return false;
}
// trim pkcs#7 padding
$pad = ord(substr($plaintext, -1));
$pad = ($pad < 1 || $pad > 32) ? 0 : $pad;
$plaintext = substr($plaintext, 0, strlen($plaintext) - $pad);
// trim header
$plaintext = substr($plaintext, 16);
// get content length
$unpack = unpack("Nlen/", substr($plaintext, 0, 4));
// get content
$content = substr($plaintext, 4, $unpack['len']);
// get app_key
$app_key_decode = substr($plaintext, $unpack['len'] + 4);
// if($app_key == $app_key_decode){
// echo '1';
// }else{
// echo '0';
// }
return $content;
// return $app_key == $app_key_decode ? $content : false;
}
?>