500], ['mobile',MobileValidator::class], ['id_card', 'match', 'pattern' => '/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/i'], // ['id_card', 'checkCard'], [['avatar'],'string'], [['name','avatar'],'string'] ]; } // public function checkCard($attribute, $params) // { // $response = (new Client(['http_errors' => false]))->post("https://eid.shumaidata.com/eid/check", [ // 'headers' => ['Authorization' =>"APPCODE f98bba3251714c759f5bd29d00e1c46e"], // 'query' => [ // 'idcard' => $this->id_card, // 'name' => $this->name // ], // ]); // $result = json_decode($response->getBody(),true); // if(!($result['code']==0 && $result['result']['res']==1)){ // $this->addError($attribute, '身份证名字不匹配'); // } // } public function save() { if (!$this->validate()) { throw new Exception($this->getErrorMsg()); } $UserPatient = UserPatient::findOne([ 'user_id' => $this->user_id, 'id_card' => $this->id_card, 'is_delete' => 0 ]); if($UserPatient->id){ //更新 $UserPatientHealthInquiry = UserPatientHealthInquiry::findOne(['user_patient_id' => $UserPatient->id]); if(!$UserPatientHealthInquiry){ throw new Exception('就诊人健康信息不存在'); } }else{ //新增 $UserPatient = new UserPatient(); $UserPatientHealthInquiry = new UserPatientHealthInquiry(); } $Transaction = \Yii::$app->db->beginTransaction(); try { if($this->is_default){ UserPatient::updateAll(['is_default'=>0],['user_id'=>$this->user_id]); } //就诊人 $UserPatient->attributes = $this->attributes; $UserPatient->saveOrFail(); $attributes = $this->attributes; $attributes['user_patient_id'] = $UserPatient->id; //就诊人健康信息 $UserPatientHealthInquiry->attributes = $attributes; $UserPatientHealthInquiry->saveOrFail(); $Transaction->commit(); }catch (Exception $exception){ $Transaction->rollback(); throw $exception; } return ['同步成功']; } }