1. 程式人生 > 其它 >接入移動手機號一鍵登入類的封裝,app應用,php服務端類的封裝與呼叫

接入移動手機號一鍵登入類的封裝,app應用,php服務端類的封裝與呼叫

需求:實現手機號一鍵登入,由於官方只有java的demo和jar包,沒有php的sdk及demo
<?php

/*
* 手機號一鍵登入加解密
*/


class Autophone
{
const A_APPID='xxxx';//Android appid
const IOS_APPID='xxxxxxxxx';//IOS appid
const PRIVATEKEY='/upload/file/rsa_private_key.pem';//私鑰地址
const PUBLICKEY='/upload/file/rsa_pub_key.pem';//公鑰地址
public $privateKey;//私鑰資源
public $publicKey;//公鑰資源
const API_URL='https://onekey2.cmpassport.com/unisdk/rsapi/loginTokenValidate';//請求地址
const VERSION='2.0';//引數,文件讓寫死
const STRICTCHECK="0";//引數,文件讓寫死
const ENCRTYTYPE='RSA';//引數,文件讓寫死

public $appid='';
public function __construct($type=1)
{
$this->appid=$this->getAppid($type);
$this->_getPrivateKey(getcwd().self::PRIVATEKEY);
$this->_getPublicKey(getcwd().self::PUBLICKEY);

}

/**
*獲取公鑰
*
* @param $file
*
*/
private function _getPublicKey($file)
{
$key_content = $this->_readFile($file);
if ($key_content) {
$this->publicKey = openssl_get_publickey($key_content);
}
}

/**
*獲取私鑰
*
* @param $file
*
*/
private function _getPrivateKey($file)
{
$key_content = $this->_readFile($file);
if ($key_content) {
$this->privateKey = openssl_get_privatekey($key_content);
}
}

/**
*讀檔案
*
* @param $file
*
* @return bool|string
* @throws Exception
*/
private function _readFile($file)
{
$ret = false;
if (!file_exists($file)) {
throw new Exception('pem file not found');
} else {
$ret = file_get_contents($file);
}
return $ret;
}



/**
*$data = $appId . $traceId . $timestamp . $token . $version;
*$appId 應用id
*$traceId 請求id
*$timestamp 時間戳,精確到毫秒
*$token 前端引數
*$version 版本號
*@param $data
*
* @return string
*/
public function sign($token)
{
$appid=$this->appid;
$data=$appid.$token;
$signature = '';
openssl_sign($data, $signature, $this->privateKey, 7);
return bin2hex($signature);
}

/**
*安卓和ios appid 不同
*
* @param $type
*
* @return string
*/
public function getAppid($type){
return $type==1 ? self::A_APPID: self::IOS_APPID ;
}

/**
*轉成hex編碼
*
* @param bool $hex
*
* @return bool|string
*/
public function _hex2bin($hex = false)
{
$ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) ? pack("H*", $hex) : false;
return $ret;
}

/**
*編碼方式
*
* @param $data
* @param $code
*
* @return bool|string
*/
public function _decode($data, $code)
{
switch (strtolower($code)) {
case 'base64':
$data = base64_decode($data);
break;
case 'hex':
$data = $this->_hex2bin($data);
break;
case 'bin':
default:
}
return $data;
}

/**
*解密手機號
*
* @param $data
*
* @return string
* @throws Exception
*/
public function decrypt($data){
$data = $this->_decode($data, 'hex');
$decrypted='';
$return_de = openssl_private_decrypt($data, $decrypted, $this->privateKey);
if (!$return_de) {
throw new Exception("解密失敗");
}
return $decrypted;
}


/**
*構建請求引數
*
* @param $token
* @param $sign
*
* @return false|string
*/
public function buildParam($token){
$sign=$this->sign($token);
return json_encode([
'version'=>self::VERSION,
'msgid'=>substr(getLoggerId(),0,36),
'systemtime'=>get_millisecondNew(),
'strictcheck'=>self::STRICTCHECK,
'appid'=>$this->appid,
'token'=>$token,
'sign'=>$sign,
'encryptionalgorithm'=>'RSA'
]);
}

}

//呼叫,token由前端傳入

$class= new Autophone($type);//type 1=安卓,2=ios
$json=$class->buildParam($token);//組裝引數

$data=$http::curlPost($class::API_URL,$json,15,'json');//傳送請求
if($data['resultCode'] !=103000 || empty($data['msisdn'])){

throw new Exception($data['resultCode'] .'-'. $data['desc']);
}

$phone=$class->decrypt($data['msisdn']);//解密手機號