隐私号码保护

云信平台
<?php
/**
* 手机号绑定
*/
class PhoneBind extends Controller
{
//APP接入地址+接口访问URI
protected $realUrl = 'https://101.37.133.245:11008/voice/1.0.0/middleNumberAXB'; //绑定
protected $unUrl = 'https://101.37.133.245:11008/voice/1.0.0/middleNumberUnbind'; //解绑
protected $appid = '663###';
protected $token = '031dee664########';


/** AXB模式绑定示例代码 */
public function bind()
{
$phoneA = input('phone_a'); //A号码
$phoneB = input('phone_b'); //B号码
//判断号码是否正确
if(!Checking::phoneRule($phoneA)){
return json(['code'=>0,'msg'=>'请输入正确的手机号']);
}
if(!Checking::phoneRule($phoneB)){
return json(['code'=>0,'msg'=>'请输入正确的手机号']);
}
//查询是否已经绑定过了
$phoneX = $this->is_bind($phoneA,$phoneB);
if(!empty($phoneX)){ //绑定过的虚拟号码
return json(['code'=>1,'msg'=>'查询成功','data'=>$phoneX['phone_x']]);
}
//选择要绑定的虚拟号
$phoneX = $this->choose_x($phoneA,$phoneB);
if(empty($phoneX)){ //没有虚拟号可以分配了,返回真实号码
return json(['code'=>1,'msg'=>'司机号码','data'=>$phoneB]);
}
//参数
$axbBody = [];
$axbBody['middleNumber'] = $phoneX; //中间号
$axbBody['bindNumberA'] = $phoneA; //A号码
$axbBody['bindNumberB'] = $phoneB; //B号码
$axbBody['maxBindingTime'] = 3600 * 4; //4小时自动解绑
Db::startTrans();
try {
//绑定号码
$url = $this->realUrl;
$result = $this->sendRequest($url,json_encode($axbBody));
$voice_result = json_decode($result,true);
if($voice_result['result'] != '000000'){
throw new Exception($voice_result['message']);
}
//存表
$binding = [
'phone_a' => $phoneA,
'phone_b' => $phoneB,
'phone_x' => $phoneX,
'bind_id' => $voice_result['bindId'],
'createtime'=> date('Y-m-d H:i:s'),
];
$res = db('binding')->insert($binding);
if(!$res){
throw new Exception('存绑定关系失败');
}
//$bindphone = substr($result['relationNum'],3,11); //手机号
Db::commit();
} catch (Exception $e) {
Db::rollback();
return json(['code'=>0,'msg'=>$e->getMessage()]);
}
return json(['code'=>1,'msg'=>'绑定成功','data'=>$phoneX]);
}

/**
* 判断是否绑定
*/
public function is_bind($phoneA,$phoneB){
$phone_x = db('binding')->where(['phone_a'=>$phoneA,'phone_b'=>$phoneB])->field('phone_x,bind_id')->find();
if(empty($phone_x)){
$phone_x = db('binding')->where(['phone_b'=>$phoneA,'phone_a'=>$phoneB])->field('phone_x,bind_id')->find();
if(!empty($phone_x)){
return $phone_x;
}
}else{
return $phone_x;
}
}
/**
* 选择要绑定的X号码
*/
public function choose_x($phoneA,$phoneB){
//查询绑定过得虚拟手机号
$phone_x = db('binding')->where(['phone_a|phone_b'=>$phoneA,'phone_a|phone_b'=>$phoneB])->column('phone_x');
//查询可以选择的虚拟号
$data = db('phone_protect')->whereNotIn('phone',$phone_x)->column('phone');
if(!empty($data)){
$num = count($data) - 1;
$key = rand(0,$num);
$phone = $data[$key]; //随机选择的虚拟号
return $phone;
}

}

//毫秒数时间戳
function timetemps()
{
$time = explode (" ", microtime () );
$time = $time[1] . "000";
return $time;
}
//生成Authorization 用于包头信息验证
function getAuthorizationHeader($timeStamp)
{
$str = $this->appid.":".$timeStamp;
$header = base64_encode($str);
return $header;
}
//生成sig参数
function getSig($timeStamp)
{
return md5("$this->appid$this->token$timeStamp");
}

/**
* @param $url 请求地址
* @param $body 请求参数
* @return bool|string
*/
function sendRequest($url,$body)
{
$timeStamp = $this->timetemps(); //毫秒数时间戳
$sig = $this->getSig($timeStamp); //sig参数
//拼接请求地址
$post_url = "$url/$this->appid/$sig";
$auth_header = $this->getAuthorizationHeader($timeStamp); //Authorization 用于包头信息验证
$header = array("Accept:application/json","Content-Type:application/json;charset=utf-8","Authorization:$auth_header");
$result = $this->curl_post($post_url,$body,$header);

return $result;
}

/**
* 发起HTTPS请求
* @param $url
* @param $data
* @param $header
* @param int $post
* @return bool|string
*/
function curl_post($url,$data,$header,$post=1)
{
//初始化curl
$ch = curl_init();
//参数设置
$res= curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, $post);
if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
$result = curl_exec ($ch);
//连接失败
if($result == FALSE){
$result = "{\"result\":\"172001\",\"message\":\"网络错误\"}";
}
curl_close($ch);
return $result;
}


/** AXB模式解绑示例代码 */
public function untying($phoneA, $phoneB, $phoneX,$bind_id)
{
// $phoneA = input('phone_a'); //A号码
// $phoneB = input('phone_b'); //B号码
//查询绑定过的ID
$unbindBody = [];
$unbindBody['middleNumber']= $phoneX;
$unbindBody['bindNumberA'] = $phoneA;
$unbindBody['bindNumberB'] = $phoneB;
$url = $this->unUrl;
$result = $this->sendRequest($url,json_encode($unbindBody));

$print = $result.'--phone_a:'.$phoneA.'--phone_b:'.$phoneB.'--phone_x:'.$phoneX.'--'.date('Y-m-d H:i:s');
file_put_contents('info.text',json_encode($print,JSON_FORCE_OBJECT).PHP_EOL,FILE_APPEND);

$voice_result = json_decode($result,true);
if($voice_result['result'] == '400092'){
$res = db('binding')->where(['bind_id'=>$bind_id])->delete();
return 0;
}
if ($voice_result['result'] == '000000') { //解绑成功
//删除绑定表的关系
$res = db('binding')->where(['bind_id'=>$bind_id])->delete();
if(!$res){
return '解绑失败';
}
}else{
return $voice_result['message'];
}

// return '解绑成功';
}
}

华为云平台
/**
* 手机号绑定
*/
class PhoneBind extends Controller
{
//APP接入地址+接口访问URI
protected $realUrl = 'https://rtcapi.cn-north-1.myhuaweicloud.com:12543/rest/caas/relationnumber/partners/v1.0';
protected $app_key = 'pTZt0t6#########fq30';
protected $app_secret = 'IQ18###########50r7P';

/**
* @return string|\think\response\Json
* 绑定虚拟号
*/
public function bind(){
$phoneA = input('phone_a'); //A号码
$phoneB = input('phone_b'); //B号码
//判断号码是否正确
if(!Checking::phoneRule($phoneA)){
return json(['code'=>0,'msg'=>'请输入正确的手机号']);
}
if(!Checking::phoneRule($phoneB)){
return json(['code'=>0,'msg'=>'请输入正确的手机号']);
}
//查询是否已经绑定过了
$phoneX = $this->is_bind($phoneA,$phoneB);
if(!empty($phoneX)){ //绑定过的虚拟号码
return json(['code'=>1,'msg'=>'查询成功','data'=>$phoneX['phone_x']]);
}
//继续绑定
$callerNum = '+86'.$phoneA; // A号码
$calleeNum = '+86'.$phoneB; // B号码
//选择要绑定的虚拟号
$phoneX = $this->choose_x($phoneA,$phoneB);
if(empty($phoneX)){ //没有虚拟号可以分配了
return json(['code'=>1,'msg'=>'司机号码','data'=>$phoneB]);
}
$relationNum = '+86'.$phoneX; //X号码
// $areaCode = '0755'; // 需要绑定的X号码对应的城市码
// 请求Headers
$headers = [
'Accept: application/json',
'Content-Type: application/json;charset=UTF-8',
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
'X-WSSE: ' .$this->buildWsseHeader($this->app_key, $this->app_secret)
];
// 请求Body,可按需删除选填参数
$data = json_encode([
'relationNum' => $relationNum,
// 'areaCode' => $areaCode,
'callerNum' => $callerNum,
'calleeNum' => $calleeNum,
]);

$context_options = [
'http' => [
'method' => 'POST', // 请求方法为POST
'header' => $headers,
'content' => $data,
'ignore_errors' => true // 获取错误码,方便调测
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];
Db::startTrans();
try {
//绑定号码
$response = file_get_contents($this->realUrl, false, stream_context_create($context_options)); // 发送请求
$result = json_decode($response,true);
if($result['resultcode'] != 0){
throw new Exception('绑定失败');
}
//存表
$binding = [
'phone_a' => $phoneA,
'phone_b' => $phoneB,
'phone_x' => $phoneX,
'bind_id' => $result['subscriptionId'],
'createtime'=> date('Y-m-d H:i:s'),
];
$res = db('binding')->insert($binding);
if(!$res){
throw new Exception('存绑定关系失败');
}
$bindphone = substr($result['relationNum'],3,11); //手机号
Db::commit();
} catch (Exception $e) {
Db::rollback();
return json(['code'=>0,'msg'=>$e->getMessage(),'data'=>$result]);
}

return json(['code'=>1,'msg'=>'绑定成功','data'=>$bindphone]);
}

/**
* 构建X-WSSE值
* @param string $appKey
* @param string $appSecret
* @return string
*/
function buildWsseHeader($appKey, $appSecret)
{
date_default_timezone_set("UTC");
$Created = date('Y-m-d\TH:i:s\Z'); //Created
$nonce = uniqid(); //Nonce
$base64 = base64_encode(hash('sha256', ($nonce . $Created . $appSecret), TRUE)); //PasswordDigest

return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"", $appKey, $base64, $nonce, $Created);
}
/**
* 判断是否绑定
*/
public function is_bind($phoneA,$phoneB){
$phone_x = db('binding')->where(['phone_a'=>$phoneA,'phone_b'=>$phoneB])->field('phone_x,bind_id')->find();
if(empty($phone_x)){
$phone_x = db('binding')->where(['phone_b'=>$phoneA,'phone_a'=>$phoneB])->field('phone_x,bind_id')->find();
if(!empty($phone_x)){
return $phone_x;
}
}else{
return $phone_x;
}
}
/**
* 选择要绑定的X号码
*/
public function choose_x($phoneA,$phoneB){
//查询绑定过得虚拟手机号
$phone_x = db('binding')->where(['phone_a|phone_b'=>$phoneA,'phone_a|phone_b'=>$phoneB])->column('phone_x');
//查询可以选择的虚拟号
$data = db('phone_protect')->whereNotIn('phone',$phone_x)->column('phone');
if(!empty($data)){
$num = count($data) - 1;
$key = rand(0,$num);
$phone = $data[$key]; //随机选择的虚拟号
return $phone;
}

}

/**
* 解除手机号绑定
*/
public function untying($phoneA, $phoneB)
{
//查询绑定过的ID
$phoneX = $this->is_bind($phoneA, $phoneB);
if (!empty($phoneX)) { //绑定过的虚拟号码

$subscriptionId = $phoneX['bind_id']; //绑定id
// 请求Headers
$headers = [
'Accept: application/json',
'Content-Type: application/json;charset=UTF-8',
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
'X-WSSE: ' . $this->buildWsseHeader($this->app_key, $this->app_secret)
];
// 请求URL参数
$data = http_build_query([
'subscriptionId' => $subscriptionId,
]);
// 完整请求地址
$fullUrl = $this->realUrl . '?' . $data;
$context_options = [
'http' => [
'method' => 'DELETE', // 请求方法为DELETE
'header' => $headers,
'ignore_errors' => true // 获取错误码,方便调测
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];

try {
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求
$result = json_decode($response, true);
if ($result['resultcode'] != 0) {
throw new Exception('解绑失败');
}
//删除绑定表的关系
$res = db('binding')->where(['bind_id'=>$subscriptionId])->delete();
if(!$res){
throw new Exception('删除绑定关系失败');
}
} catch (Exception $e) {
return ['status'=>201,'msg'=>$e->getMessage(),'data'=>$result];
}
}
return ['status' => 200, 'msg' => '解绑成功'];
}
}


猜你喜欢

转载自www.cnblogs.com/summer-g/p/12082857.html