diff --git a/admin/controllers/LoginController.php b/admin/controllers/LoginController.php index 09013fd..d72461f 100644 --- a/admin/controllers/LoginController.php +++ b/admin/controllers/LoginController.php @@ -9,8 +9,15 @@ use common\core\BaseAdminController; use common\enums\UserRoleEnum; use common\helpers\ArrayHelper; use common\models\oldDb\AdminAccessToken; +use common\models\oldDb\DoctorInfo; +use common\models\oldDb\DoctorPatient; +use common\models\oldDb\LeadInfo; use common\models\oldDb\Role; +use common\models\oldDb\ServInfo; use common\models\oldDb\Store; +use common\models\oldDb\User; +use common\models\oldDb\UserPatient; +use common\modelsgii\oldDb\PharmacistInfo; use common\services\SmsService; use Yii; use yii\base\Exception; @@ -20,10 +27,100 @@ class LoginController extends BaseAdminController public $modelClass = Admin::class; public $enableCsrfValidation = false; public $defaultAction = 'register'; - public $optional = ['register', 'logout', 'login', 'send-code']; + public $optional = ['register', 'logout', 'login', 'send-code', 'test']; public $mallId; + public function actionTest() + { + return UserPatient::find()->all(); + $this->updateDoctor(); + $this->updateUser(); + $this->updateServInfo(); + $this->updatePharmacistInfo(); + $this->updateLeadInfo(); + $this->updateUserPatient(); + $this->updateDoctorPatient(); + + return ['执行完成']; + } + + private function updateDoctor() + { + + // 用到了身份证的模型:DoctorInfo + $info = DoctorInfo::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->idcard)) { + $v->updated_at = time(); + } + $v->save(); + } + } + + private function updateUser() + { + $info = User::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->idcard)) { + $v->updated_at = time(); + } + $v->save(); + } + } + + private function updateServInfo() + { + $info = ServInfo::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->idcard)) { + $v->updated_at = time(); + } + $v->save(); + } + } + + private function updatePharmacistInfo() + { + $info = PharmacistInfo::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->idcard)) { + $v->updated_at = time(); + } + $v->save(); + } + } + private function updateLeadInfo() + { + $info = LeadInfo::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->idcard)) { + $v->updated_at = time(); + } + $v->save(); + } + } + private function updateUserPatient() + { + $info = UserPatient::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->id_card)) { + $v->updated_at = time(); + } + $v->save(); + } + } + private function updateDoctorPatient() + { + $info = DoctorPatient::find()->all(); + foreach ($info as $v) { + if (!is_encrypted($v->id_card)) { + $v->updated_at = time(); + } + $v->save(); + } + } + public function actionLogin() { $post = Yii::$app->request->post(); diff --git a/admin/foundation/JsonResponseFormatter.php b/admin/foundation/JsonResponseFormatter.php index dcfdcd5..f95d38e 100644 --- a/admin/foundation/JsonResponseFormatter.php +++ b/admin/foundation/JsonResponseFormatter.php @@ -8,6 +8,7 @@ namespace admin\foundation; use common\jobs\LogJob; +use common\services\DataEncryptor; use Yii; use yii\base\UserException; use yii\helpers\Json; @@ -49,6 +50,10 @@ class JsonResponseFormatter extends \yii\web\JsonResponseFormatter if (is_array($value)) { $value = self::aseEncode($value); // 递归处理子数组 } else { + // 数据库中加密的方法需要解密 + if (in_array($key, \Yii::$app->params['database_ase_list']) && is_encrypted($value)) { + $value = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($value); + } if (in_array($key, \Yii::$app->params['aseList'])) { if (is_string($key)) { $value = ase_encode($value); // 加密指定键值 diff --git a/common/config/main.php b/common/config/main.php index d5046ed..775d34f 100644 --- a/common/config/main.php +++ b/common/config/main.php @@ -34,7 +34,7 @@ return [ 'class' => 'yii\redis\Connection', 'hostname' => env('REDIS_HOST'), 'port' => env('REDIS_PORT'), - 'password' => env('REDIS_PASSWORD'), +// 'password' => env('REDIS_PASSWORD'), 'database' => 1, ], // 'cache' => [ diff --git a/common/config/params-local.php b/common/config/params-local.php index 69c6c00..702be66 100644 --- a/common/config/params-local.php +++ b/common/config/params-local.php @@ -144,6 +144,11 @@ JJvBfLfudYNUjUCyW3gAcOIJ # 身份证 'id_card', 'idcard', + ], + 'ase_key' => '3a8f9b2d7c1e4f8a9b2d7c1e4f8a9b2d7c1e4f8a9b2d7c1e4f8a9b2d7c1e4f8a', + 'database_ase_list' => [ + 'id_card', + 'idcard', ] ]; diff --git a/common/core/BaseActiveRecord.php b/common/core/BaseActiveRecord.php index a0cc596..cff35fe 100644 --- a/common/core/BaseActiveRecord.php +++ b/common/core/BaseActiveRecord.php @@ -1,6 +1,8 @@ idcard) && !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)) { + $this->id_card = (new DataEncryptor(\Yii::$app->params['ase_key']))->encrypt($this->id_card); + } + return true; + } + return false; + } + + public function afterFind() + { + parent::afterFind(); + // 解密ID卡信息之前检查是否已经是明文 + if (!empty($this->id_card) && 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)) { + $this->idcard = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($this->idcard); + } + } } diff --git a/common/foundation/JsonResponseFormatter.php b/common/foundation/JsonResponseFormatter.php index e5fb2c3..3ad9dcf 100644 --- a/common/foundation/JsonResponseFormatter.php +++ b/common/foundation/JsonResponseFormatter.php @@ -6,6 +6,7 @@ namespace common\foundation; +use common\services\DataEncryptor; use Yii; use yii\helpers\Json; @@ -132,6 +133,10 @@ class JsonResponseFormatter extends \yii\web\JsonResponseFormatter if (is_array($value)) { $value = self::aseEncode($value); // 递归处理子数组 } else { + // 数据库中加密的方法需要解密 + if (in_array($key, \Yii::$app->params['database_ase_list']) && is_encrypted($value)) { + $value = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($value); + } if (in_array($key, \Yii::$app->params['aseList'])) { if (is_string($key)) { $value = ase_encode($value); // 加密指定键值 diff --git a/common/models/oldDb/User.php b/common/models/oldDb/User.php index e7cac5d..dca6d38 100644 --- a/common/models/oldDb/User.php +++ b/common/models/oldDb/User.php @@ -1,6 +1,7 @@ secretKey = hash('sha256', $key, true); // 使用SHA-256哈希生成32字节的密钥 + $this->ivLength = openssl_cipher_iv_length($this->cipherMethod); + } + + /** + * 加密数据 + * @param $data + * @return string + * @throws Exception + */ + public function encrypt($data): string + { + $iv = openssl_random_pseudo_bytes($this->ivLength); + $encryptedData = openssl_encrypt($data, $this->cipherMethod, $this->secretKey, OPENSSL_RAW_DATA, $iv); + if ($encryptedData === false) { + throw new Exception("加密失败" . openssl_error_string()); + } + return 'xk_ase_256_'. base64_encode($iv . $encryptedData); // 将初始化向量和加密数据一起返回 + } + + /** + * 解密数据 + * @param $base64EncodedEncryptedData + * @return string + * @throws Exception + */ + public function decrypt($base64EncodedEncryptedData): string + { + if (!is_string($base64EncodedEncryptedData)) return $base64EncodedEncryptedData; + // 替换xk_ase_256_ + $base64EncodedEncryptedData = str_replace('xk_ase_256_', '', $base64EncodedEncryptedData); + $decodedData = base64_decode($base64EncodedEncryptedData); + if ($decodedData === false) { + throw new Exception("Base64 decoding failed"); + } + $iv = substr($decodedData, 0, $this->ivLength); + $encryptedData = substr($decodedData, $this->ivLength); + $decryptedData = openssl_decrypt($encryptedData, $this->cipherMethod, $this->secretKey, OPENSSL_RAW_DATA, $iv); + if ($decryptedData === false) { + throw new Exception("解密失败" . openssl_error_string()); + } +// $pattern = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\xFF]|[\x{E000}-\x{F8FF}]/u'; +// if (!preg_match($pattern, $str)) return $str; +// ds($decryptedData); + return $decryptedData; + } +} \ No newline at end of file diff --git a/common/services/UtilsService.php b/common/services/UtilsService.php new file mode 100644 index 0000000..35b81e7 --- /dev/null +++ b/common/services/UtilsService.php @@ -0,0 +1,50 @@ + &$value) { + if (in_array($key, $this->urlList)) { + $value = str_replace('http://xiaokang88.oss-cn-hangzhou.aliyuncs.com/', '', $value); + $value = str_replace('https://xiaokang88.oss-cn-hangzhou.aliyuncs.com/', '', $value); + $value = explode('?', $value)[0]; + } + } + return $data; + } +} \ No newline at end of file diff --git a/helpers/helpers.php b/helpers/helpers.php index 692de16..9bd47ef 100644 --- a/helpers/helpers.php +++ b/helpers/helpers.php @@ -13,6 +13,16 @@ if (!function_exists('ds')) { )); } } +if (!function_exists('is_encrypted')) { + function is_encrypted($data): bool + { + if (!is_string($data)) { + return false; + } + // 这里应该有逻辑来判断$data是否已经被加密。 + return strpos($data, 'xk_ase_256_') === 0; + } +} if (!function_exists('ase_encode')) { function ase_encode($data) { diff --git a/service/config/main.php b/service/config/main.php index aab8ed8..c9f4212 100644 --- a/service/config/main.php +++ b/service/config/main.php @@ -9,7 +9,7 @@ $params = array_merge( return [ 'id' => 'app-service', 'basePath' => dirname(__DIR__), - 'bootstrap' => ['log'], +// 'bootstrap' => ['log'], 'controllerNamespace' => 'service\controllers', 'modules' => [ 'v1' => [ diff --git a/service/modules/v1/controllers/DoctorWorkController.php b/service/modules/v1/controllers/DoctorWorkController.php index fc5e1a8..4333594 100644 --- a/service/modules/v1/controllers/DoctorWorkController.php +++ b/service/modules/v1/controllers/DoctorWorkController.php @@ -40,105 +40,116 @@ class DoctorWorkController extends BaseAppController */ public function actionInfo() { - $post= \Yii::$app->request->post(); - $store = Store::findOne(\Yii::$app->store); - if(!$store)throw new Exception('门店不存在'); + try { + $post= \Yii::$app->request->post(); + $store = Store::findOne(\Yii::$app->store); + if(!$store)throw new Exception('门店不存在'); - $DoctorInfo= DoctorInfo::find() - ->where(['su_id' => \Yii::$app->user->identity->id]) - ->with(['title', 'hospital','depart','user']) - ->one(); - $DoctorIdentity= DoctorIdentity::find() - ->where(['su_id' => \Yii::$app->user->identity->id]) - ->one(); - $DoctorPracticing= DoctorPracticing::find() - ->where(['su_id' => \Yii::$app->user->identity->id]) - ->one(); - if(!$DoctorInfo || !$DoctorIdentity || !$DoctorPracticing){ - throw new Exception('医生信息错误'); - } - $storeDoctor = StoreDoctor::find()->where([ - 'su_id' => \Yii::$app->user->identity->getId(), - 'store_id' => \Yii::$app->store - ])->one(); - if(!$storeDoctor)throw new Exception('非该门店医生'); + $DoctorInfo= DoctorInfo::find() + ->where(['su_id' => \Yii::$app->user->identity->id]) + ->with(['title', 'hospital','depart','user']) + ->one(); + $DoctorIdentity= DoctorIdentity::find() + ->where(['su_id' => \Yii::$app->user->identity->id]) + ->one(); + $DoctorPracticing= DoctorPracticing::find() + ->where(['su_id' => \Yii::$app->user->identity->id]) + ->one(); + if(!$DoctorInfo || !$DoctorIdentity || !$DoctorPracticing){ + throw new Exception('医生信息错误'); + } + $storeDoctor = StoreDoctor::find()->where([ + 'su_id' => \Yii::$app->user->identity->getId(), + 'store_id' => \Yii::$app->store + ])->one(); + if(!$storeDoctor)throw new Exception('非该门店医生'); - //门店医生小程序码 - if(!$storeDoctor->qr_code){ - $response = WechatService::getInstance()->app->app_code->getUnlimit('su_id='.\Yii::$app->user->identity->getId().'&store_id='.\Yii::$app->store, [ - 'page' => 'subPackages/doctor/doctor-detail', - 'check_path' => false, - ]); - if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { - $path = 'uploads/doctor_code/' . date('Ymd'); - $filename = $response->save('uploads/doctor_code/' . date('Ymd'), 'doctor_code_' . $DoctorInfo->su_id.\Yii::$app->store); - $realpath = FuncHelper::getRealFilepath('service', $path . '/' . $filename); - $uploadService = new UploadService(); - $url = $uploadService->saveFile($realpath); - $storeDoctor->qr_code = $url; + //门店医生小程序码 + if(!$storeDoctor->qr_code){ + $response = WechatService::getInstance()->app->app_code->getUnlimit('su_id='.\Yii::$app->user->identity->getId().'&store_id='.\Yii::$app->store, [ + 'page' => 'subPackages/doctor/doctor-detail', + 'check_path' => false, + ]); + if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { + $path = 'uploads/doctor_code/' . date('Ymd'); + $filename = $response->save('uploads/doctor_code/' . date('Ymd'), 'doctor_code_' . $DoctorInfo->su_id.\Yii::$app->store); + $realpath = FuncHelper::getRealFilepath('service', $path . '/' . $filename); + $uploadService = new UploadService(); + $url = $uploadService->saveFile($realpath); + $storeDoctor->qr_code = $url; + $storeDoctor->save(); + } + } + //门店医生在线状态 + if($storeDoctor->is_online != 1){ + StoreDoctor::updateAll(['is_online' => 0],['su_id' => \Yii::$app->user->identity->getId(), 'is_online' => 1]); + $storeDoctor->is_online = 1; + $storeDoctor->last_login_time = time(); $storeDoctor->save(); } - } - //门店医生在线状态 - if($storeDoctor->is_online != 1){ - StoreDoctor::updateAll(['is_online' => 0],['su_id' => \Yii::$app->user->identity->getId(), 'is_online' => 1]); - $storeDoctor->is_online = 1; - $storeDoctor->last_login_time = time(); - $storeDoctor->save(); - } - - $relates = $DoctorInfo->getRelatedRecords(); - $DoctorInfo = $DoctorInfo->toArray(); - $DoctorInfo = array_merge($DoctorInfo, $relates); - $wait_accept=Register::find()->where([ - 'service_user_id'=> \Yii::$app->user->identity->id, - 'store_id'=> $post['store_id'], - 'status'=>RegisterEnum::WAIT, - 'is_pay'=>1, - 'is_cancel'=>0, - 'is_delete'=>0 - ])->count(); + $relates = $DoctorInfo->getRelatedRecords(); + $DoctorInfo = $DoctorInfo->toArray(); + $DoctorInfo = array_merge($DoctorInfo, $relates); - $accepting = Register::find()->where([ - 'service_user_id'=> \Yii::$app->user->identity->id, - 'store_id'=> $post['store_id'], - 'status'=>RegisterEnum::ACCEPTING, - 'is_pay'=>1, - 'is_cancel'=>0, - 'is_delete'=>0 - ])->count(); + $wait_accept=Register::find()->where([ + 'service_user_id'=> \Yii::$app->user->identity->id, + 'store_id'=> $post['store_id'], + 'status'=>RegisterEnum::WAIT, + 'is_pay'=>1, + 'is_cancel'=>0, + 'is_delete'=>0 + ])->count(); - $idcard= DoctorInfo::find()->select(['idcard'])->where(['su_id'=>\Yii::$app->user->id])->one(); - $number = substr($idcard['idcard'], strlen($idcard['idcard']) - 2, 1); - $sex=$number % 2 == 0?'女':'男'; + $accepting = Register::find()->where([ + 'service_user_id'=> \Yii::$app->user->identity->id, + 'store_id'=> $post['store_id'], + 'status'=>RegisterEnum::ACCEPTING, + 'is_pay'=>1, + 'is_cancel'=>0, + 'is_delete'=>0 + ])->count(); - #医生开通的服务 - $doctor_service = DoctorService::find()->where(['su_id' => \Yii::$app->user->identity->id])->asArray()->one(); - $DoctorInfo['qr_code'] = $storeDoctor->qr_code; + // TODO 这里 + $idcard= DoctorInfo::find()->select(['idcard'])->where(['su_id'=>\Yii::$app->user->id])->one(); + $number = substr($idcard['idcard'], strlen($idcard['idcard']) - 2, 1); + $sex=$number % 2 == 0?'女':'男'; - $ServiceUser=ServiceUser::find()->select(['reason','status'])->where(['id'=>\Yii::$app->user->identity->id])->one(); + #医生开通的服务 + $doctor_service = DoctorService::find()->where(['su_id' => \Yii::$app->user->identity->id])->asArray()->one(); + $DoctorInfo['qr_code'] = $storeDoctor->qr_code; - return [ - 'store'=>[ - 'store_id' => $store->id, - 'store' => [ - 'id' => $store->id, - 'name' => $store->name, - 'see_rate'=>$store->see_rate, + $ServiceUser=ServiceUser::find()->select(['reason','status'])->where(['id'=>\Yii::$app->user->identity->id])->one(); + + return [ + 'store'=>[ + 'store_id' => $store->id, + 'store' => [ + 'id' => $store->id, + 'name' => $store->name, + 'see_rate'=>$store->see_rate, + ] + + ], + 'DoctorInfo'=>$DoctorInfo, + 'doctor_service'=>$doctor_service, + 'DoctorIdentity'=>$DoctorIdentity, + 'DoctorPracticing'=>$DoctorPracticing, + 'wait_accept'=>$wait_accept, + 'accepting'=>$accepting, + 'sex'=>$sex, + 'ServiceUser'=>$ServiceUser + ]; + } catch (\Exception $e) { + return [ + 'code' => 1, + 'msg' => $e->getMessage(), + 'data' => [ + 'line' => $e->getLine(), ] - - ], - 'DoctorInfo'=>$DoctorInfo, - 'doctor_service'=>$doctor_service, - 'DoctorIdentity'=>$DoctorIdentity, - 'DoctorPracticing'=>$DoctorPracticing, - 'wait_accept'=>$wait_accept, - 'accepting'=>$accepting, - 'sex'=>$sex, - 'ServiceUser'=>$ServiceUser - ]; + ]; + } } /**