FastAdmin框架下阿里云隐私号集成方案
本方案基于FastAdmin封装框架进行开发。
核心业务功能涵盖AXN隐私号的绑定与解绑操作。
应用场景为为店铺手机号开通虚拟号,当用户联系店铺时,展示的是虚拟号码而非真实手机号。
相关技术参考官方开放文档:https://help.aliyun.com/document_detail/59655.html?spm=a2c4g.11174283.6.661.b8365d50CKM5Ma
最终运行效果如下所示:

参照官网开放文档指引,下载对应的SDK压缩包。
解压后获取 aliyun-dypls-php-sdk 目录,将其中的 api_sdk 文件夹以及 api_demo 文件夹内的 PlsDemo.php 文件,复制至项目 vendor 目录下,此处示例存放于 yinsi 子文件夹中。

接着编辑 PlsDemo.php 文件,定位到 getAcsClient 方法,填入正确的 AccessKeyId 和 AccessKeySecret 凭证信息。

继续在 PlsDemo.php 文件中,调整绑定环节所使用的号码池配置。

同样在 PlsDemo.php 文件中,配置解绑环节所关联的号码池参数。

最后,在用户列表相关的控制器代码中添加以下逻辑:
/**
* 编辑
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
// print_r($params);exit;
//开通虚拟号
if ($params['privacydata'] == '2'){
vendor("yinsi.PlsDemo");
$response = \PlsDemo::bindAxn($params['mobile']) ;
if ($response->Code == 'OK'){
$params['privacy_tel'] = $response->SecretBindDTO->SecretNo;
$params['subsid'] = $response->SecretBindDTO->SubsId;
}else{
$this->error($response->Message);
}
}else{
vendor("yinsi.PlsDemo");
$response = \PlsDemo::unbindSubscription($params['subsid'],$params['privacy_tel']) ;
if ($response->Code == 'OK'){
$params['privacy_tel'] = '';
$params['subsid'] = '';
}
};
$params = $this->preExcludeFields($params);
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException(true)->validate($validate);
}
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("row", $row);
return $this->view->fetch();
}
该实现与原始修改方法的差异主要体现在接口描述层面:

对于无需后台管理界面的需求,可直接采用以下代码方案:
此方式实现了通过接口简单快捷地完成虚拟号的绑定与解绑流程。
//隐私号码绑定
public function yinsi(){
$type = $this->request->request('type');//1绑定2解绑
$mobile = $this->request->request('mobile');//需要绑定的手机号
vendor("yinsi.PlsDemo");
$response = \PlsDemo::bindAxn($mobile) ;
print_r($response);exit;//打印接口返回参数
}
//隐私号码解绑
public function Unbundling(){
$subsid = $this->request->request('subsid');//绑定接口中返回的subsId;
$secretNo = $this->request->request('secretNo');//绑定接口中返回的secretNo;
vendor("yinsi.PlsDemo");
$response = \PlsDemo::unbindSubscription($subsid,$secretNo) ;
print_r($response);exit;//打印接口返回参数
}
至此,关于PHP对接阿里云虚拟号以实现号码隐私保护的介绍已完成,更多相关内容可搜索本站历史文章或继续浏览后续内容,感谢支持。