用户端敏感字段加密

This commit is contained in:
2024-12-24 17:44:05 +08:00
parent db323fddb5
commit 7abfde2649
4 changed files with 74 additions and 3 deletions

View File

@@ -23,7 +23,6 @@ class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
public static function formatData($data, $errno = 0, $msg = '')
{
// TODO 加密后续再完善
if (empty($data)) {
$data = [];
} else {

View File

@@ -151,7 +151,10 @@ JJvBfLfudYNUjUCyW3gAcOIJ
'accept_tel',
'patient',
'accept_name',
'patient_mobile'
'patient_mobile',
# 身份证
'id_card',
'idcard',
]
];

View File

@@ -93,12 +93,34 @@ class Cors extends ActionFilter
'Access-Control-Expose-Headers' => [],
];
/**
* @var mixed
*/
private static $param;
/**
* {@inheritdoc}
*/
public function beforeAction($action)
{
$this->request = $this->request ?: Yii::$app->getRequest();
// 解密传参
$method = Yii::$app->request->getMethod();
self::$param = $method === 'GET' ? Yii::$app->request->get() : Yii::$app->request->post();
if (empty(self::$param)) {
self::$param = [];
} else {
self::$param = self::aseDecode(self::$param);
}
if ($method === 'GET') {
Yii::$app->request->setQueryParams(self::$param);
} else {
Yii::$app->request->setBodyParams(self::$param);
}
$this->request = $this->request ?: Yii::$app->getRequest();
$this->response = $this->response ?: Yii::$app->getResponse();
@@ -276,4 +298,21 @@ class Cors extends ActionFilter
{
return 'HTTP_' . strtoupper(str_replace([' ', '-'], ['_', '_'], $string));
}
private static function aseDecode($data)
{
// return $data;
if (is_array($data)) {
foreach ($data as $key => &$value) {
if (is_array($value)) {
$value = self::aseDecode($value); // 递归处理子数组
} else {
if (in_array($key, \Yii::$app->params['aseList'])) {
$value = ase_decode($value); // 加密指定键值
}
}
}
}
return $data;
}
}

View File

@@ -14,7 +14,16 @@ class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
private $defaultErrorCode = -1;
public static function formatData($data,$errno=0,$msg=''){
// if(empty($data)) $data = new \stdClass();
if(empty($data)) $data = [];
if (empty($data)) {
$data = [];
} else {
// if (array_key_exists('list', $data)) {
// $data['list'] = self::aseEncode($data['list']);
// } else {
// $data = self::aseEncode($data);
// }
}
return ['data'=>$data,'errcode'=>$errno,'msg'=>$msg];
}
@@ -75,6 +84,7 @@ class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
}
}
/**
* Formats response data in JSONP format.
* @param \yii\web\Response $response
@@ -112,4 +122,24 @@ class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
Yii::warning("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.", __METHOD__);
}
}
private static function aseEncode($data)
{
// return $data;
if (is_array($data)) {
foreach ($data as $key => &$value) {
if (is_array($value)) {
$value = self::aseEncode($value); // 递归处理子数组
} else {
if (in_array($key, \Yii::$app->params['aseList'])) {
if (is_string($key)) {
$value = ase_encode($value); // 加密指定键值
}
}
}
}
}
return $data;
}
}