* @version : HUOSDK-9.0 */ namespace huoIdentify\identifyDriver\driver; use GuzzleHttp\Client; use huoIdentify\identifyDriver\Driver; use huoIdentify\model\IdentifyPlatformLogModel; use huolib\constant\IdentifyConst; use huolib\status\CommonStatus; use huolib\status\IdentifyStatus; use huolib\tool\Http; use huolib\tool\StrUtils; use think\Config; use think\Log; class Moxing extends Driver { const STATUS_SUCCESS = 3;//认证成功 const STATUS_IN_PROGRESS = 2;//认证中 const STATUS_FAIL = 1;//认证失败 /** * @var array */ protected $headers; protected $public_params; /** * @var int $timeout */ protected $timeout = 5; /** * @var Client */ protected $http; /** * @var string */ protected $app_id; protected $secret_key; protected $app_key; protected $channel_id; protected $require_url; /* 认证URL */ /** * @param array $config */ public function __construct($config = []) { if (empty($config)) { $config = (array)Config::get('identify_conf.fcm_game'); } parent::__construct($config); // 设置属性 $this->require_url = get_val($config, 'require_url', ''); $this->app_key = get_val($config, 'app_key', ''); $this->channel_id = get_val($config, 'channel_id', ''); $this->secret_key = get_val($config, 'secret_key', ''); $this->app_id = get_val($config, 'app_id', 0); if (!class_exists('\\GuzzleHttp\\Client')) { // 使用已有的GuzzleHttp require_once 'wxpay/TCloudAutoLoader.php'; } $this->http = new Client(); $this->headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset:utf-8']; $this->public_params = [ 'timestamp' => $this->getMillisecond(), 'appKey' => $this->app_key, 'channelId' => $this->channel_id, ]; } /** * 实名认证 * https://www.yuque.com/ys_sdk/eusdkv2/ppu4mb */ public function identify() { /* 提交认证之前获取当前玩家是否有认证中的数据,有则使用查询接口 */ $_query_rs = $this->checkCertificationData(); if (CommonStatus::DATA_NOT_FOUND_EXCEPTION != $_query_rs['code']) { /* 只要返回的不是数据未找到则认为无需再次提交认证信息 */ return $this->huoReturn($_query_rs); } $_results = $this->check(); $_results_arr = json_decode($_results, true); if (empty($_results_arr) || !isset($_results_arr['state']) || '1' != $_results_arr['state']) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name) ).'&rs='.$_results.'[防沉迷实名认证api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, $_results_arr['msg']); } /* 认证失败 */ if ($_results_arr['data']['status'] == self::STATUS_FAIL) { $_result = $_results_arr; $_step = 0; $_other = '默兴防沉迷实名认证API,认证失败'; \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other); $_code = IdentifyStatus::IDENTITY_FAIL; return $this->huoError($_code, $_other); } /** * 认证成功需要添加记录 */ $_ipl_model = new IdentifyPlatformLogModel(); $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_MOXING); $_log_data = [ 'mem_id' => $this->mem_id, 'mg_mem_id' => $this->mg_mem_id, 'app_id' => $this->app_id, 'identify_from' => IdentifyConst::DRIVER_KEY_MOXING, 'real_name' => $this->real_name, 'id_card' => $this->id_card, 'identify_pi' => $_results_arr['data']['thirdAuthentication'], 'status' => $_results_arr['data']['status'], ]; if (empty($_ipl_info)) { $_ipl_model->addData($_log_data); } else { $_ipl_model->updateData($_log_data, $_ipl_info['id']); } /* 认证中 */ if ($_results_arr['data']['status'] == self::STATUS_IN_PROGRESS) { $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } $_code = CommonStatus::NO_ERROR; $_results_arr['data']['pi'] = $_results_arr['data']['thirdAuthentication']; return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']); } /* 查询是否有认证中的数据 */ private function checkCertificationData() { $_ipl_model = new IdentifyPlatformLogModel(); $_last_order_data = $_ipl_model->getInfoByMgFrom( $this->mg_mem_id, IdentifyConst::DRIVER_KEY_MOXING ); if (empty($_last_order_data)) { /* 数据不存在,需重新认证 */ $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION; return $this->huoError($_code, CommonStatus::getMsg($_code)); } if ($_last_order_data['status'] == self::STATUS_SUCCESS) { $_code = CommonStatus::NO_ERROR; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } if ($_last_order_data['status'] != self::STATUS_IN_PROGRESS) { /* 状态不是认证中的/玩家证件信息对不上的认为数据不存在,需重新认证 */ $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION; return $this->huoError($_code, CommonStatus::getMsg($_code)); } $_query_rs = $this->query(); $_results_arr = json_decode($_query_rs, true); if (empty($_results_arr) || !isset($_results_arr['state']) || '1' != $_results_arr['state']) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode($_last_order_data) .'&rs='.$_query_rs.'[默兴防沉迷实名认证api,查询信息]', Log::ERROR ); $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION; return $this->huoError($_code, CommonStatus::getMsg($_code)); } /* 认证失败 */ $_result_data = get_val($_results_arr, 'data', []); $_auth_status = get_val($_result_data, 'status', 0); /* 认证中/认证失败 */ if ($_auth_status != self::STATUS_SUCCESS) { $_code = CommonStatus::INVALID_PARAMS; return $this->huoError($_code, CommonStatus::getMsg($_code)); } /** * 认证成功需要添加记录 */ $_log_data = [ 'mem_id' => $this->mem_id, 'mg_mem_id' => $this->mg_mem_id, 'app_id' => $this->app_id, 'identify_from' => IdentifyConst::DRIVER_KEY_MOXING, 'real_name' => $this->real_name, 'id_card' => $this->id_card, 'identify_pi' => $_results_arr['data']['thirdAuthentication'], 'status' => $_results_arr['data']['status'], ]; $_ipl_model->updateData($_log_data, $_last_order_data['id']); $_code = CommonStatus::NO_ERROR; return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']); } public function loginBehavior($identify_pi) { if (empty($identify_pi)) { $_code = CommonStatus::INVALID_PARAMS; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } $_data = [ [ 'behaviorType' => 1, 'userType' => 0, 'sessionId' => $this->user_token, 'behaviorTimestamp' => time(), 'thirdAuthentication' => $identify_pi, ] ]; $_results = $this->loginOrOut($_data); $_results_arr = json_decode($_results, true); if ('1' != $_results_arr['state']) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name, $identify_pi) ).'&rs='.$_results.'[防沉迷行为数据上报api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, $_results_arr['msg']); } $_code = CommonStatus::NO_ERROR; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } public function logoutBehavior($identify_pi) { if (empty($identify_pi)) { $_code = CommonStatus::INVALID_PARAMS; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } $_data = [ [ 'behaviorType' => 0, 'userType' => 0, 'sessionId' => $this->user_token, 'behaviorTimestamp' => time(), 'thirdAuthentication' => $identify_pi, ] ]; $_results = $this->loginOrOut($_data); $_results_arr = json_decode($_results, true); if ('1' != $_results_arr['state']) { Log::write( 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode( array($this->config, $this->id_card, $this->real_name, $identify_pi) ).'&rs='.$_results.'[防沉迷行为数据上报api]', Log::ERROR ); $_code = CommonStatus::INNER_ERROR; return $this->huoError($_code, $_results_arr['msg']); } $_code = CommonStatus::NO_ERROR; return $this->huoSuccess($_code, CommonStatus::getMsg($_code)); } /** * check the name and idNum * * @return string */ public function check() { $_uri = $this->require_url.'/game/authentication/check/v1'; $_headers = $this->headers; $_body = $this->public_params; $_body['userId'] = $this->mg_mem_id; $_body['realName'] = $this->real_name; $_body['idCard'] = $this->id_card; return $this->doRequest($_body, $_uri, 'POST', $_headers); } /** * query the ai * * @return string */ public function query() { $_uri = $this->require_url.'//game/authentication/query/v1'; $_headers = $this->headers; $_body = $this->public_params; $_body['userId'] = $this->mg_mem_id; return $this->doRequest($_body, $_uri, 'POST', $_headers); } /** * @param mixed $data * * @return string */ public function loginOrOut($data) { $collections = $this->public_params; foreach ($data as $i => $d) { $tmp = []; $tmp['no'] = $i + 1; $tmp['thirdAuthentication'] = $d['thirdAuthentication']; $tmp['sessionId'] = isset($d['sessionId']) ? $d['sessionId'] : md5($d['thirdAuthentication']); $tmp['userType'] = $d['userType']; $tmp['behaviorType'] = $d['behaviorType']; $tmp['behaviorTimestamp'] = $d['behaviorTimestamp']; $collections['behavior'][] = $tmp; } $collections['behavior'] = json_encode($collections['behavior']); $_uri = $this->require_url.'/game/authentication/sync/v1'; $_headers = $this->headers; return $this->doRequest($collections, $_uri, 'POST', $_headers); } /** * make the sign * * @param $body * * @return string */ private function makeSign($body) { $_params = StrUtils::argSort($body); $_str = ''; while (list($_key, $_val) = StrUtils::funcNewEach($_params)) { if (is_array($_val)) { $_val = json_encode($_val); } $_str .= $_key."=".$_val."&"; } // 去掉最后一个&字符 $_str = substr($_str, 0, strlen($_str) - 1); $_str = $_str.'&key='.$this->secret_key; return strtolower(md5($_str)); } /** * do request * * @param $params * @param string $url * @param string $method * @param array $headers * * @return mixed */ private function doRequest($params, $url, $method, $headers = []) { $params['sign'] = $this->makeSign($params); $_rs = Http::post($url, $params, ['header' => $headers, 'timeout' => $this->timeout]); return $_rs; } /** * 获取毫秒级别的时间戳 */ private static function getMillisecond() { //获取毫秒的时间戳 $time = explode(" ", microtime()); $time = $time[1].($time[0] * 1000); $time2 = explode(".", $time); $time = $time2[0]; return $time; } }