From 7a42b01aed8f1d42814c3bdcfabd9deb69e0b7b5 Mon Sep 17 00:00:00 2001 From: liqi Date: Sat, 18 Jan 2025 09:44:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/core/BaseActiveRecord.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/common/core/BaseActiveRecord.php b/common/core/BaseActiveRecord.php index cff35fe..92f3183 100644 --- a/common/core/BaseActiveRecord.php +++ b/common/core/BaseActiveRecord.php @@ -27,11 +27,11 @@ class BaseActiveRecord extends ActiveRecord { if (parent::beforeSave($insert)) { // 加密ID卡信息之前检查是否已经加密过 - if (!empty($this->idcard) && !is_encrypted($this->idcard)) { + if (!empty($this->idcard) && !$this->is_encrypted($this->idcard)) { $this->idcard = (new DataEncryptor(\Yii::$app->params['ase_key']))->encrypt($this->idcard); } // 加密ID卡信息之前检查是否已经加密过 - if (!empty($this->id_card) && !is_encrypted($this->id_card)) { + if (!empty($this->id_card) && !$this->is_encrypted($this->id_card)) { $this->id_card = (new DataEncryptor(\Yii::$app->params['ase_key']))->encrypt($this->id_card); } return true; @@ -43,11 +43,20 @@ class BaseActiveRecord extends ActiveRecord { parent::afterFind(); // 解密ID卡信息之前检查是否已经是明文 - if (!empty($this->id_card) && is_encrypted($this->id_card)) { + if (!empty($this->id_card) && $this->is_encrypted($this->id_card)) { $this->id_card = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($this->id_card); } - if (!empty($this->idcard) && is_encrypted($this->idcard)) { + if (!empty($this->idcard) && $this->is_encrypted($this->idcard)) { $this->idcard = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($this->idcard); } } + + private function is_encrypted($data): bool + { + if (!is_string($data)) { + return false; + } + // 这里应该有逻辑来判断$data是否已经被加密。未加密是true + return strpos($data, 'xk_ase_256_') === 0; + } }