services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic'); if(!isset($config['status']) || !$config['status']){ throw new Exception('尚未开启实名认证功能'); } // 云市场分配的密钥Id , 云市场分配的密钥Key if(empty($config['secret_id']) || empty($config['secret_key'])){ throw new Exception('实名认证配置错误'); } if ($check_pass_mode == 1) { $auth_user = RealAuthUser::getUserByInfo($mall_id,$user_id, $real_name, $id_no); } else { $auth_user = RealAuthUser::getUser($mall_id,$user_id); } if($auth_user && $auth_user->auth && $auth_user->auth_type == $params['auth_type']){ throw new Exception('您已通过实名认证'); } //企业验证设置 if(!empty($config['setting_status']) && $auth_user && $auth_user->auth_type != $params['auth_type']){ throw new Exception('不能同时认证企业和个人账户'); } if($config['id_no_limit'] && $params['auth_type'] == RealAuthUser::USER){ //1个证件号码只能用于1个会员 $flag = true; $id_no_exist = RealAuthUser::find()->where(['mall_id' => $mall_id,'id_no' => $id_no, 'auth' => 1,'auth_type'=>$params['auth_type']])->select('id')->column(); if(!empty($id_no_exist)){ $flag = false; } if(!$flag){ throw new Exception('该身份证号码已存在认证'); } } if($config['upgrade_status'] && $params['auth_type'] == RealAuthUser::BUSINESS){ //1个营业执照号只能用于1个会员 $flag = true; $id_no_exist = RealAuthUser::find() ->where(['mall_id' => $mall_id,'business_code' => $business_code]) ->andWhere(['!=','user_id',$user_id]) ->select('id')->column(); if(!empty($id_no_exist)){ $flag = false; } if(!$flag){ throw new Exception('该营业执照号码已存在认证'); } } $result = $this->sendAuthRequest($config, $real_name, $id_no); if(!isset($result['error_code']) || $result['error_code'] !== self::CODE_SUCCESS){ throw new Exception('实名认证失败'); } if(empty($result['result']['isok'])){ return false; } // $data = '{"error_code":0,"reason":"成功","result":{"realname":"甘**","idcard":"450422************","isok":true,"IdCardInfor":{"province":"广西壮族自治区","city":"梧州市","district":"藤县","area":"广西壮族自治区梧州市藤县","sex":"男","birthday":"1996-8-2"}},"sn":"0303171859282651960407737232"}'; // $result = json_decode($data,true); //认证通过才存表, isok = true , 认证通过 //企业认证 需要后台审核实名状态才会更改为认证通过 if(!empty($result['result']['isok'])){ $t = \Yii::$app->db->beginTransaction(); try{ $auth_data = [ 'id' => $auth_user && empty($auth_user->auth) ? $auth_user->id : 0, 'mall_id' => $mall_id, 'user_id' => $user_id, 'real_name' => $real_name, 'id_no' => $id_no, 'id_type' => 1, 'auth_result' => json_encode($result), 'auth' => $params['auth_type'] == RealAuthUser::BUSINESS ? 0 : 1, 'auth_type' => $params['auth_type'], 'business_code' => (string)$business_code, ]; $real_auth_user_id = RealAuthUser::setData($auth_data); if(!$real_auth_user_id){ throw new Exception('实名认证失败'); } $detail_arr = [ 'mall_id' => $mall_id, 'user_id' => $user_id, 'auth_type' => $params['auth_type'], 'auth_id' => $real_auth_user_id, 'detail' => $params['detail'] ?? [], 'check_status' => 0, ]; $real_auth_detail = RealAuthDetail::setData($detail_arr); if(!$real_auth_detail){ throw new Exception('实名认证失败'); } $t->commit(); }catch(\Exception $e){ $t->rollBack(); throw new \Exception($e->getMessage()); } } //实名认证成功触发事务 $event = new RealAuthEvent($mall_id); $data = [ 'mall_id' => $mall_id, 'user_id' => $user_id, ]; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); // return $result['result']['isok']; if($params['auth_type'] == RealAuthUser::BUSINESS){ $msg = '企业信息已提交,工作人员将于'.$config['business_verify_time'].'小时内完成审核'; }else{ $msg = '实名认证成功'; } return $msg; } /** * 实名认证请求 * @param $config * @param $real_name * @param $id_no * @return mixed * @throws Exception */ protected function sendAuthRequest($config, $real_name, $id_no) { $source = 'market'; $datetime = gmdate('D, d M Y H:i:s T'); $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source); $sign = base64_encode(hash_hmac('sha1', $signStr, $config['secret_key'], true)); $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $config['secret_id'], $sign); $headers = [ 'X-Source' => $source, 'X-Date' => $datetime, 'Authorization' => $auth, ]; $queryParams = ['cardNo' => $id_no, 'realName' => $real_name]; $url = $this->host . '?' . http_build_query($queryParams); if ($config['version'] == 1) { $signStr = sprintf("x-date: %s", $datetime); $sign = base64_encode(hash_hmac('sha1', $signStr, $config['secret_key'], true)); $auth = sprintf('{"id": "%s", "x-date": "%s" , "signature": "%s"}', $config['secret_id'], $datetime, $sign); $headers = ['Authorization' => $auth]; $url = $this->newHost . '?' . http_build_query($queryParams); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) { return $k . ': ' . $v; }, array_values($headers), array_keys($headers))); $data = curl_exec($ch); if (curl_errno($ch)) { Yii::error('实名认证接口Error:' . curl_error($ch) . ' | URL: ' . $url); throw new Exception('实名认证系统异常'); } curl_close($ch); return json_decode($data, true); } /** * 个人认证升级企业认证 * @param $params * @return bool|void */ public function authUpgrade($params){ $t = \Yii::$app->db->beginTransaction(); try{ $user_id = $params['user_id']; $mall_id = $params['mall_id']; $real_name = $params['real_name']; $id_no = $params['id_no']; $business_code = isset($params['detail']['business_code']) ? $params['detail']['business_code'] : ''; if(empty($real_name)){ throw new Exception('请输入姓名'); } if(empty($id_no)){ throw new Exception('请输入身份证号码'); } if(!is_array($params['detail'])){ $params['detail'] = json_decode($params['detail'],true); } $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic'); if(!isset($config['status']) || !$config['status']){ throw new Exception('尚未开启实名认证功能'); } // 云市场分配的密钥Id , 云市场分配的密钥Key if(empty($config['secret_id']) || empty($config['secret_key'])){ throw new Exception('实名认证配置错误'); } $auth = RealAuthUser::getUser($mall_id,$user_id); if(!$auth){ throw new Exception('您未通过实名认证,无法申请升级为企业认证'); } if($auth->auth && $auth->auth_type == RealAuthUser::BUSINESS){ throw new Exception('您已通过企业实名认证'); } if($config['upgrade_status']){ //1个营业执照号只能用于1个会员 $flag = true; $id_no_exist = RealAuthUser::find() ->where(['mall_id' => $mall_id,'business_code' => $business_code]) ->andWhere(['!=','user_id',$user_id]) ->select('id')->column(); if(!empty($id_no_exist)){ $flag = false; } if(!$flag){ throw new Exception('该营业执照号码已存在认证'); } } //一个身份证可有多个企业实名 // $id_no_exist = RealAuthUser::find() // ->where(['mall_id' => $mall_id,'id_no' => $id_no, 'auth' => 1,'auth_type'=>RealAuthUser::BUSINESS])->one(); // if($id_no_exist){ // throw new Exception('该身份证号已存在企业认证'); // } $detail_arr = [ 'mall_id' => $mall_id, 'user_id' => $user_id, 'auth_type' => RealAuthUser::BUSINESS, 'auth_id' => $auth['id'], 'detail' => $params['detail'] ?? [], 'check_status' => 0, ]; $real_auth_detail = RealAuthDetail::setData($detail_arr); if(!$real_auth_detail){ throw new Exception('认证升级失败'); } $auth = RealAuthUser::findOne(['id'=>$auth['id']]); $auth->business_code = (string)$business_code; if(!$auth->save()){ throw new Exception('认证升级失败'); } $t->commit(); return true; }catch(\Exception $e){ $t->rollBack(); throw new \Exception($e->getMessage()); } } /** * 检测当前应用场景是否需要实名认证 * @param $params * @return bool|void */ public function checkNeedAuth($params) { if (MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_REAL_AUTH) == 0) return false; $config = Yii::$app->services->setting->addons->get($params['mall_id'], AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic'); if(!isset($config['status']) || !$config['status']){ return false; } $config['scene'] = $config['scene'] ?? ''; if(!$config['scene'] || strpos($config['scene'],$params['type']) === false){ return false; } //查看应用场景 $scene = explode(',',$config['scene']); if($params['type'] == 'order' && !in_array('order',$scene)){ //下单 if( in_array('part_order',$scene) && isset($params['goods_ids'])){ if(!$config['goods_ids']) return false; //部分商品下单 $config_goods_ids = json_decode($config['goods_ids']); //下单的商品不在后台设置范围 if(!array_intersect($params['goods_ids'],$config_goods_ids)) return false; } } $auth = RealAuthUser::getUser($params['mall_id'],$params['user_id']); if($auth && $auth->auth){ return false; } return true; } /** * 检测当前应用场景是否需要实名认证(企业) * @param $params * @return bool|void */ public function checkNeedAuthBusiness($params) { if (MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_REAL_AUTH) == 0) return false; $config = Yii::$app->services->setting->addons->get($params['mall_id'], AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic'); if(!isset($config['setting_status']) || !$config['setting_status']){ return false; } if(!isset($config['business_status']) || !$config['business_status']){ return false; } //查看应用场景 $scene = explode(',',$config['business_scene']); if(!$scene || !in_array($params['type'],$scene)){ return false; } $auth = RealAuthUser::getUser($params['mall_id'],$params['user_id']); if($auth && $auth->auth && $auth->auth_type == RealAuthUser::BUSINESS){ return false; } return true; } /** * 检测是否开启认证设置 * @param $params * @return bool|void */ public function getSetting($mall_id,$user_id = 0) { $result['setting_status'] = 0; $result['auth'] = 0; $result['auth_type'] = 0; if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH) == 0){ return $result; } $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic'); if(isset($config['setting_status'])){ $result['setting_status'] = $config['setting_status']; } $auth = RealAuthUser::getUser($mall_id,$user_id); if($auth){ $result['auth'] = $auth['auth']; $result['auth_type'] = $auth['auth_type']; } return $result; } /** * 企业认证审核 * @param $params * @return bool|void */ public function apply($params) { $t = \Yii::$app->db->beginTransaction(); try{ $real_auth_detail = RealAuthDetail::findOne(['id'=>$params['id']]); if(empty($real_auth_detail)){ throw new \Exception('数据不存在'); } $real_auth_detail->check_status = $params['status']; $real_auth_detail->check_at = time(); $real_auth_detail->remark = $params['remark']; if(!$real_auth_detail->save()){ throw new \Exception('数据更新异常'); } if($params['status'] == 1){ $auth = RealAuthUser::findOne(['id'=>$real_auth_detail->auth_id]); $auth->auth = 1; $auth->auth_type = RealAuthUser::BUSINESS; if(!$auth->save()){ throw new \Exception('数据更新异常'); } } $t->commit(); if($params['status'] == 1){ //实名认证成功触发事务 $event = new RealAuthEvent($auth['mall_id']); $data = [ 'mall_id' => $auth['mall_id'], 'user_id' => $auth['user_id'], ]; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); } }catch(\Exception $e){ $t->rollBack(); throw new \Exception($e->getMessage()); } return true; } /** * 认证详情 * @param $params * @return bool|void */ public function getAuthDetail($mall_id,$user_id = 0) { $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic'); $result['business_no_limit'] = 0; $result['auth'] = 0; $result['auth_type'] = 0; //是否申请审核 $result['is_apply'] = 0; //审核状态 $result['check_status'] = 0; $result['remark'] = ''; //是否申请实名升级 $result['upgrade_status'] = 0; $result['info'] = []; $result['business_info'] = []; if(!empty($config['setting_status']) && !empty($config['business_no_limit'])){ $result['business_no_limit'] = $config['business_no_limit']; } //是否显示 个人实名认证 $result['person_show'] = $config['status'] ?? 0; $result['business_show'] = $config['business_status'] ?? 0; $auth = RealAuthUser::find()->select('id,auth,auth_type,real_name,id_no')->where(['mall_id'=>$mall_id,'user_id'=>$user_id])->asArray()->one(); if($auth){ $result['is_apply'] = 1; $result['auth'] = $auth['auth']; $result['auth_type'] = $auth['auth_type']; $real_auth_detail = RealAuthDetail::findOne(['auth_id'=>$auth['id'],'auth_type'=>$auth['auth_type'],'status'=>StatusEnum::ENABLED]); unset($auth['auth'],$auth['auth_type']); if($real_auth_detail){ $detail['detail'] = $real_auth_detail['detail']; if(!is_array($detail['detail'])){ $detail['detail'] = json_decode($detail['detail'],true); } $result['check_status'] = $real_auth_detail['check_status']; $result['remark'] = $real_auth_detail['remark']; $result['info'] = array_merge($auth,$detail); } if($result['business_no_limit'] == 1){ $real_auth_detail_business = RealAuthDetail::find()->where(['auth_id'=>$auth['id'],'auth_type'=>RealAuthUser::BUSINESS,'status'=>StatusEnum::ENABLED])->select('detail,check_status,remark')->asArray()->one(); if($real_auth_detail_business){ $result['check_status'] = $real_auth_detail_business['check_status']; $result['remark'] = $real_auth_detail_business['remark']; if(!is_array($real_auth_detail_business['detail'])){ $real_auth_detail_business['detail'] = json_decode($real_auth_detail_business['detail'],true); } $result['business_info'] = array_merge($auth,$real_auth_detail_business); if($auth['auth_type'] == RealAuthUser::USER){ $result['upgrade_status'] = 1; } } } } return $result; } }