|
|
<?php
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
use think\Db;
|
|
|
|
|
|
/**
|
|
|
* @ApiInternal
|
|
|
* 隐私保护通话接口
|
|
|
*/
|
|
|
class PrivateNumber extends Api
|
|
|
{
|
|
|
|
|
|
//如果$noNeedLogin为空表示所有接口都需要登录才能请求
|
|
|
//如果$noNeedRight为空表示所有接口都需要验证权限才能请求
|
|
|
//如果接口已经设置无需登录,那也就无需鉴权了
|
|
|
//
|
|
|
// 无需登录的接口,*表示全部
|
|
|
protected $noNeedLogin = ['bind', 'search', 'unbind'];
|
|
|
// 无需鉴权的接口,*表示全部
|
|
|
protected $noNeedRight = ['*'];
|
|
|
|
|
|
/**
|
|
|
* 测试方法
|
|
|
*
|
|
|
* @ApiTitle (测试名称)
|
|
|
* @ApiSummary (测试描述信息)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/demo/test/id/{id}/name/{name})
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
* @ApiParams (name="id", type="integer", required=true, description="会员ID")
|
|
|
* @ApiParams (name="name", type="string", required=true, description="用户名")
|
|
|
* @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
|
|
|
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
|
|
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
|
|
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
|
|
|
* @ApiReturn ({
|
|
|
'code':'1',
|
|
|
'msg':'返回成功'
|
|
|
})
|
|
|
*/
|
|
|
public function bind($mobile_a = '15133120361',$mobile_b = '15620696576',$order_id = 0)
|
|
|
{
|
|
|
// 必填,请参考"开发准备"获取如下数据,替换为实际值
|
|
|
$realUrl = config('privatenumber.real_url'); // APP接入地址+接口访问URI
|
|
|
$APP_KEY = config('privatenumber.app_key'); // APP_Key
|
|
|
$APP_SECRET = config('privatenumber.app_secret'); // APP_Secret
|
|
|
// $relationNum = '+8617010000001'; // X号码(隐私号码)
|
|
|
$callerNum = '+86'.$mobile_a; // A号码
|
|
|
$calleeNum = '+86'.$mobile_b; // B号码
|
|
|
|
|
|
/*
|
|
|
* 选填,各参数要求请参考"AXB模式绑定接口"
|
|
|
*/
|
|
|
$areaCode = '021'; // 需要绑定的X号码对应的城市码
|
|
|
// $callDirection = 0; // 允许呼叫的方向
|
|
|
// $duration = 86400; // 绑定关系保持时间,单位为秒。到期后会被系统自动解除绑定关系
|
|
|
// $recordFlag = 'false'; // 是否需要针对该绑定关系产生的所有通话录音
|
|
|
// $recordHintTone = 'recordHintTone.wav'; // 设置录音提示音
|
|
|
// $maxDuration = 60; // 设置允许单次通话进行的最长时间,单位为分钟。通话时间从接通被叫的时刻开始计算
|
|
|
// $lastMinVoice = 'lastMinVoice.wav'; // 设置通话剩余最后一分钟时的提示音
|
|
|
// $privateSms = 'true'; // 设置该绑定关系是否支持短信功能
|
|
|
|
|
|
// $callerHintTone = 'callerHintTone.wav'; // 设置A拨打X号码时的通话前等待音
|
|
|
// $calleeHintTone = 'calleeHintTone.wav'; // 设置B拨打X号码时的通话前等待音
|
|
|
// $preVoice = [
|
|
|
// 'callerHintTone' => $callerHintTone,
|
|
|
// 'calleeHintTone' => $calleeHintTone
|
|
|
// ];
|
|
|
|
|
|
// 请求Headers
|
|
|
$headers = [
|
|
|
'Accept: application/json',
|
|
|
'Content-Type: application/json;charset=UTF-8',
|
|
|
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
|
|
'X-WSSE: ' . build_wsse_header($APP_KEY, $APP_SECRET)
|
|
|
];
|
|
|
// 请求Body,可按需删除选填参数
|
|
|
$data = json_encode([
|
|
|
// 'relationNum' => $relationNum,
|
|
|
'areaCode' => $areaCode,
|
|
|
'callerNum' => $callerNum,
|
|
|
'calleeNum' => $calleeNum,
|
|
|
// 'callDirection' => $callDirection,
|
|
|
// 'duration' => $duration,
|
|
|
// 'recordFlag' => $recordFlag,
|
|
|
// 'recordHintTone' => $recordHintTone,
|
|
|
// 'maxDuration' => $maxDuration,
|
|
|
// 'lastMinVoice' => $lastMinVoice,
|
|
|
// 'privateSms' => $privateSms,
|
|
|
// 'preVoice' => $preVoice
|
|
|
]);
|
|
|
|
|
|
$context_options = [
|
|
|
'http' => [
|
|
|
'method' => 'POST', // 请求方法为POST
|
|
|
'header' => $headers,
|
|
|
'content' => $data,
|
|
|
'ignore_errors' => true // 获取错误码,方便调测
|
|
|
],
|
|
|
'ssl' => [
|
|
|
'verify_peer' => false,
|
|
|
'verify_peer_name' => false
|
|
|
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
|
|
|
];
|
|
|
|
|
|
try {
|
|
|
$file=fopen('bind_data.txt', 'a'); //打开文件
|
|
|
// print_r($data . PHP_EOL); // 打印请求数据
|
|
|
fwrite($file, '绑定请求数据:' . $data . PHP_EOL); //绑定请求参数记录到本地文件,方便定位问题
|
|
|
$response = file_get_contents($realUrl, false, stream_context_create($context_options)); // 发送请求
|
|
|
// print_r($response . PHP_EOL); // 打印响应结果
|
|
|
fwrite($file, '绑定结果:' . $response . PHP_EOL); //绑定ID很重要,请记录到本地文件,方便后续修改绑定关系及解绑
|
|
|
$res = json_decode($response,true);
|
|
|
if($res['resultcode'] != '0'){
|
|
|
$this->error($res['resultdesc']);
|
|
|
}
|
|
|
Db::name('private_number')->insert([
|
|
|
'order_id' => $order_id,
|
|
|
'mobile_a' => $callerNum,
|
|
|
'mobile_b' => $calleeNum,
|
|
|
'mobile_x' => $res['relationNum'],
|
|
|
'subscription_id'=> $res['subscriptionId'],
|
|
|
]);
|
|
|
echo $res['subscriptionId'];
|
|
|
} catch (Exception $e) {
|
|
|
$this->error($e->getMessage());
|
|
|
} finally {
|
|
|
fclose($file); //关闭文件
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询接口
|
|
|
*/
|
|
|
public function search($subscriptionId = '',$relationNum = '')
|
|
|
{
|
|
|
// 必填,请参考"开发准备"获取如下数据,替换为实际值
|
|
|
$realUrl = config('privatenumber.real_url'); // APP接入地址+接口访问URI
|
|
|
$APP_KEY = config('privatenumber.app_key'); // APP_Key
|
|
|
$APP_SECRET = config('privatenumber.app_secret'); // APP_Secret
|
|
|
|
|
|
/*
|
|
|
* 选填,各参数要求请参考"AXB模式绑定信息查询接口"
|
|
|
* subscriptionId和relationNum为二选一关系,两者都携带时以subscriptionId为准
|
|
|
*/
|
|
|
// $subscriptionId = '0167ecc9-bfb6-4eec-b671-a7dab2ba78cf'; // 指定"AXB模式绑定接口"返回的绑定ID进行查询
|
|
|
// $relationNum = '+8617010000001'; // 指定X号码(隐私号码)进行查询
|
|
|
// $pageIndex = 1; //查询的分页索引,从1开始编号
|
|
|
// $pageSize = 20; //查询的分页大小,即每次查询返回多少条数据
|
|
|
|
|
|
// 请求Headers
|
|
|
$headers = [
|
|
|
'Accept: application/json',
|
|
|
'Content-Type: application/json;charset=UTF-8',
|
|
|
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
|
|
'X-WSSE: ' . build_wsse_header($APP_KEY, $APP_SECRET)
|
|
|
];
|
|
|
// 请求URL参数
|
|
|
$data = http_build_query([
|
|
|
'subscriptionId' => $subscriptionId,
|
|
|
'relationNum' => $relationNum
|
|
|
// 'pageIndex' => $pageIndex,
|
|
|
// 'pageSize' => $pageSize
|
|
|
]);
|
|
|
// 完整请求地址
|
|
|
$fullUrl = $realUrl . '?' . $data;
|
|
|
|
|
|
$context_options = [
|
|
|
'http' => [
|
|
|
'method' => 'GET', // 请求方法为GET
|
|
|
'header' => $headers,
|
|
|
'ignore_errors' => true // 获取错误码,方便调测
|
|
|
],
|
|
|
'ssl' => [
|
|
|
'verify_peer' => false,
|
|
|
'verify_peer_name' => false
|
|
|
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
|
|
|
];
|
|
|
|
|
|
try {
|
|
|
$file=fopen('bind_data.txt', 'a'); //打开文件
|
|
|
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求
|
|
|
// print_r($response . PHP_EOL); // 打印响应结果
|
|
|
fwrite($file, '绑定查询结果:' . $response . PHP_EOL); //查询结果,记录到本地文件
|
|
|
$res = json_decode($response,true);
|
|
|
if($res['resultcode'] != '0'){
|
|
|
if($res['resultcode'] == '1012007'){
|
|
|
Db::name('private_number')->where('subscription_id',$subscriptionId)->delete();
|
|
|
return false;
|
|
|
}
|
|
|
$this->error($res['resultdesc']);
|
|
|
}
|
|
|
return true;
|
|
|
} catch (Exception $e) {
|
|
|
$this->error($e->getMessage());
|
|
|
} finally {
|
|
|
fclose($file); //关闭文件
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解绑接口
|
|
|
*/
|
|
|
public function unbind($subscriptionId = '',$relationNum = '')
|
|
|
{
|
|
|
// 必填,请参考"开发准备"获取如下数据,替换为实际值
|
|
|
$realUrl = config('privatenumber.real_url'); // APP接入地址+接口访问URI
|
|
|
$APP_KEY = config('privatenumber.app_key'); // APP_Key
|
|
|
$APP_SECRET = config('privatenumber.app_secret'); // APP_Secret
|
|
|
|
|
|
/*
|
|
|
* 选填,各参数要求请参考"AXB模式解绑接口"
|
|
|
* subscriptionId和relationNum为二选一关系,两者都携带时以subscriptionId为准
|
|
|
*/
|
|
|
// $subscriptionId = '0167ecc9-bfb6-4eec-b671-a7dab2ba78cf';
|
|
|
// $relationNum = '+8617010000001';
|
|
|
|
|
|
// 请求Headers
|
|
|
$headers = [
|
|
|
'Accept: application/json',
|
|
|
'Content-Type: application/json;charset=UTF-8',
|
|
|
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
|
|
'X-WSSE: ' . build_wsse_header($APP_KEY, $APP_SECRET)
|
|
|
];
|
|
|
// 请求URL参数
|
|
|
$data = http_build_query([
|
|
|
'subscriptionId' => $subscriptionId,
|
|
|
'relationNum' => $relationNum
|
|
|
]);
|
|
|
// 完整请求地址
|
|
|
$fullUrl = $realUrl . '?' . $data;
|
|
|
|
|
|
$context_options = [
|
|
|
'http' => [
|
|
|
'method' => 'DELETE', // 请求方法为DELETE
|
|
|
'header' => $headers,
|
|
|
'ignore_errors' => true // 获取错误码,方便调测
|
|
|
],
|
|
|
'ssl' => [
|
|
|
'verify_peer' => false,
|
|
|
'verify_peer_name' => false
|
|
|
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
|
|
|
];
|
|
|
|
|
|
try {
|
|
|
// print_r($data . PHP_EOL); // 打印请求数据
|
|
|
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求
|
|
|
// print_r($response . PHP_EOL); // 打印响应结果
|
|
|
$res = json_decode($response,true);
|
|
|
if($res['resultcode'] != '0') {
|
|
|
$this->error($res['resultdesc']);
|
|
|
}
|
|
|
Db::name('private_number')->where('subscription_id',$subscriptionId)->delete();
|
|
|
} catch (Exception $e) {
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
$this->success('解绑成功');
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|