创蓝闪验php手机号一键登录
注意:对外接口需要两个必要参数,flash_token、type(安卓或ios)
flash_token从哪里来:
是安卓和ios端集成创蓝闪验sdk以后通过调用sdk相关接口获得,参数名为token。
例如:
===================oneKeyLoginListener: <CLCompleteResult: 0x282759130> { authPagePresented = 1; code = 1000; data = { token = "A2-TbeW2bEc8sWcqa9X2_R3GJK4rKulBIEQhyfRn0NWpi_MBoxslDC3c22RByVMAcJddO5X291FBpBshtwcXBp-giIDFXc-7uw-t7t36qhV6HUnWsanbQkVFYH1wFbelVU_uNKk3BwbTtmZ44z7qZIdQHZfYZfsqHTyQGLV7aCkUbGQk0ax_p_xtWzqRTyqpN3HglJmPnsaeATXH_8cn5iJYN8bx6U2-XrkCVw4xxv2seF8oJ82MKD70BHGG776F6vU" }; error = <nil>; innerCode = 0; innerData = <nil>; innerDesc = <nil>; innerError = <nil>; message = "SDK获取Token成功" } ==================
少废话,上代码
对外(安卓、ios)一键登录接口:
/**
* 闪验手机号一键登录接口
* @param $flash_token
* @param $type 1安卓、2ios
*/
public function flashLogin()
{
// 接值 $params
···
···
// 校验参数
if ( !isset($params['flash_token']) ){
throw new Exception(71018);
}
if ( !isset($params['type']) ){
throw new Exception(71018);
}
// 判断是安卓还是ios
$platform = $params['type'];
if ($platform == 'android'){
$type = 1;
}elseif($platform == 'ios'){
$type = 2;
}else{
throw new Exception(71018);
}
// 调用封装的公共方法 返回手机号
$phone = getFlashPhone( $params['flash_token'] , $type );
// 判断手机号
if (empty($phone)){
throw new Exception(71019);
}
// 写库登录操作
···
···
// 返回接口登录成功或失败
···
···
}
公共方法:
/**
* 创蓝闪验 一键登录取号
* @param $tonen
* @param $type 1安卓、2ios
* @param string $massage
* @return $return_phone 返回手机号,失败则返回false
* @throws Exception
*/
function getFlashPhone( $token ,$type = 1 )
{
$clapi = new \service\ChuanglanFlashSms();
// 获取手机号
$return_phone = $clapi->getFlashPhone( $token , $type );
return $return_phone;
}
创蓝闪验封装类:
<?php
namespace service;
/* *
* 创蓝闪验(手机号一键登录)
* smt
*/
class ChuanglanFlashSms
{
// 创蓝手机号一键登录 闪验地址
protected $flash_query_url;
// 安卓对应的appid
protected $ad_flash_appid;
// 安卓对应的appkey
protected $ad_flash_appkey;
// ios对应的appid
protected $ios_flash_appid;
// ios对应的appkey
protected $ios_flash_appkey;
// 应用私钥,可选,如用RSA解密,必须填写
protected $flash_private_key;
// AES 1 RSA , 默认0 AES
protected $flash_encrypt_type;
public function __construct()
{
$this->flash_query_url = config('cl_flash_config.flash_query_url');
$this->ad_flash_appid = config('cl_flash_config.ad_flash_appid');
$this->ad_flash_appkey = config('cl_flash_config.ad_flash_appkey');
$this->ios_flash_appid = config('cl_flash_config.ios_flash_appid');
$this->ios_flash_appkey = config('cl_flash_config.ios_flash_appkey');
$this->flash_private_key = config('cl_flash_config.flash_private_key');
$this->flash_encrypt_type = config('cl_flash_config.flash_encrypt_type');
}
/**
* 闪验短信取号校验
* @param $token
* @param $type 1安卓、2ios
*/
public function getFlashPhone($token, $type = 1)
{
if ($type == 1) {
// android
// 生成sign串
$content = 'appId' . $this->ad_flash_appid . 'token' . $token;
$sign = bin2hex(hash_hmac('sha256', $content, $this->ad_flash_appkey, true));
$params = [
'appId' => $this->ad_flash_appid,
'token' => $token,
'sign' => $sign //签名
];
} elseif ($type == 2) {
// ios
// 生成sign串
$content = 'appId' . $this->ios_flash_appid . 'token' . $token;
$sign = bin2hex(hash_hmac('sha256', $content, $this->ios_flash_appkey, true));
$params = [
'appId' => $this->ios_flash_appid,
'token' => $token,
'sign' => $sign //签名
];
}
$return_phone = $this->flashCurlPost($this->flash_query_url, $params, $type);
return $return_phone;
}
/**
* 闪验curl
* @param $url
* @param $data
* @param $type 1安卓、2ios
*/
private function flashCurlPost($url, $params, $type = 1)
{
// CURL 模拟 post 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$resultJson = curl_exec($ch);
$curlInfo = curl_getinfo($ch);
// CURL 错误码
$errNo = curl_errno($ch);
if ($errNo > 0) {
// 通信失败处理逻辑,请自己填充
// CURL 错误信息
$errMsg = curl_error($ch);
logs($errMsg, 'flash_login_error');
} elseif (!empty($curlInfo) && intval($curlInfo['http_code']) != 200) {
// http 状态码不是 200,请求失败处理逻辑
$httpCode = $curlInfo['http_code'];
logs($httpCode, 'flash_login_error');
} else {
// 拿到请求结果,使用返回结果逻辑
$requestData = json_decode($resultJson, true);
if ($requestData['code'] == 200000) {
$chargeStatus = $requestData['chargeStatus']; // 是否收费,枚举值:1 :收费 0:不收费
$mobile = $requestData['data']['mobileName']; // 手机号
if ('0' == $this->flash_encrypt_type) { //AES解密 ,默认方式
if ($type == 1) {
// android
$key = md5($this->ad_flash_appkey);
} elseif ($type == 2) {
// ios
$key = md5($this->ios_flash_appkey);
} else {
return false;
}
$mobile = openssl_decrypt(hex2bin($mobile), 'AES-128-CBC', substr($key, 0, 16), OPENSSL_RAW_DATA, substr($key, 16));
// 如解密失败 请检查$appKey是否正确
} elseif ('1' == $this->flash_encrypt_type) { //RSA解密
$pi_key = openssl_pkey_get_private($this->flash_private_key);
openssl_private_decrypt(hex2bin($mobile), $mobile, $pi_key);//私钥解密
// 如解密失败 请检查$private_key是否正确
}
// $tradeNo = $requestData['data']['tradeNo']; // 流水号
// 拿到返回数据继续处理逻辑
return $mobile;
} else {
// 响应异常处理逻辑
logs($resultJson, 'flash_login_error');
// return $resultJson;
return false;
}
}
}
}
?>
完 ···