* @version : HUOSDK-IDENTITY 1.0 */ namespace huoIdentify\identifyDriver\driver; use huoIdentify\identifyDriver\Driver; use huoIdentify\model\IdentifyMemModel; use huoIdentify\model\IdentifyPlatformLogModel; use huolib\constant\IdentifyConst; use huolib\status\IdentifyStatus; use huolib\tool\Http; use huolib\tool\RateLimit; use think\Config; class Huanjuyou extends Driver { const STATUS_IN_PROGRESS = 0;//认证中 const STATUS_SUCCESS = 1;//认证成功 const STATUS_FAIL = 3;//认证失败 protected $app_id; /* 当前游戏id,系统的游戏id 本系统 */ protected $channel_code; /* 欢聚游分配给该游戏对应的应用编号 欢聚游提供 */ protected $server_key; /* 签名key 欢聚游提供*/ protected $require_url; /* 认证URL 欢聚游提供 */ /** * @param array $config */ public function __construct($config = []) { $_config = $config; if (empty($_config)) { $_config = (array)Config::get('identify_conf.huanjuyou'); } parent::__construct($_config); // 设置属性 $this->app_id = get_val($_config, 'app_id', 0); $this->channel_code = get_val($_config, 'channel_code', ''); $this->server_key = get_val($_config, 'server_key', ''); $this->require_url = get_val($_config, 'require_url', ''); } /** * 实名认证 */ public function identify() { $_results = $this->check(); $_results_arr = json_decode($_results, true); if (empty($_results_arr) || !isset($_results_arr['status']) || '1' != $_results_arr['status']) { $_result = [$_results, $_results_arr]; $_step = 0; $_other = '欢聚游防沉迷实名认证API,认证异常'; \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other); $_code = IdentifyStatus::INNER_ERROR; $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg']; return $this->huoError($_code, $_results_arr['msg']); } /** * 认证成功需要添加记录 */ $_ipl_model = new IdentifyPlatformLogModel(); $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_HUANJUYOU); $_log_data = [ 'mem_id' => $this->mem_id, 'mg_mem_id' => $this->mg_mem_id, 'app_id' => $this->app_id, 'identify_from' => IdentifyConst::DRIVER_KEY_HUANJUYOU, 'real_name' => $this->real_name, 'id_card' => $this->id_card, 'identify_pi' => get_val($_results_arr, 'pi', ''), 'status' => $_results_arr['status'], ]; if (empty($_ipl_info)) { $_ipl_model->addData($_log_data); } else { $_ipl_model->updateData($_log_data, $_ipl_info['id']); } $_code = IdentifyStatus::NO_ERROR; return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code), $_results_arr); } /** * 查询是否已认证 * * @return bool */ private function checkIsVerified() { $_info = (new IdentifyPlatformLogModel())->getInfoByMgFrom( $this->mg_mem_id, IdentifyConst::DRIVER_KEY_HUANJUYOU ); return empty($_info) ? false : true; } public function loginBehavior($identify_pi) { $_check_rs = $this->checkIsVerified(); if (true === $_check_rs) { /* 已认证无需处理 */ $_code = IdentifyStatus::NO_ERROR; return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code)); } /* 限制速率 */ $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3); if (false == $_rs) { return false; } $_identify_rs = $this->identify(); if (IdentifyStatus::NO_ERROR == $_identify_rs['code']) { $_im_model = new IdentifyMemModel(); $_old_id_data = $_im_model->getInfoByMemId($this->mem_id); $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : ''; if (empty($_old_id_data)) { $_data = [ 'identify_from' => IdentifyConst::DRIVER_KEY_HUANJUYOU, 'identify_pi' => $_identify_pi, 'mem_id' => $this->mem_id, 'real_name' => $this->real_name, 'id_card' => $this->id_card ]; $_im_model->addData($_data); } elseif (empty($_old_id_data['identify_pi']) && !empty($_identify_pi)) { $_update_data = [ 'identify_pi' => $_identify_pi ]; $_im_model->updateData($_update_data, $_old_id_data['id']); } } $_code = IdentifyStatus::NO_ERROR; return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code)); } /** * check the name and idNum * * @return string */ public function check() { $_uri = $this->require_url; $_headers = ['Content-Type' => 'application/json; charset=utf-8']; if (!empty($this->headers)) { $_headers = array_merge($_headers, $this->headers); } $_params = array( 'uid' => $this->mg_mem_id, 'realName' => $this->real_name, 'identify' => $this->id_card, 'channelCode' => $this->channel_code ); $_str = 'uid='.$this->mg_mem_id.'&realName='.$this->real_name.'&identify='.$this->id_card.'&channelCode=' .$this->channel_code.'&key='.$this->server_key; $_params['sign'] = md5($_str); return $this->doRequest($_params, $_uri, $_headers); } /** * do request * * @param $params * @param string $url * @param array $headers * * @return mixed */ private function doRequest($params, $url, $headers = []) { $_rs = Http::post($url, $params, ['header' => $headers]); return $_rs; } }