123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- /**
- * Yixin.php UTF-8
- * 易信实名认证接口
- *
- * @date : 2021/4/29 18:24
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @version : HUOSDK-IDENTITY 1.0
- */
- namespace huoIdentify\identifyDriver\driver;
- use GuzzleHttp\Client;
- use huoIdentify\identifyDriver\Driver;
- use huoIdentify\model\IdentifyMemModel;
- use huoIdentify\model\IdentifyPlatformLogModel;
- use huolib\constant\IdentifyConst;
- use huolib\status\CommonStatus;
- use huolib\status\IdentifyStatus;
- use huolib\tool\RateLimit;
- class Yixin extends Driver {
- const STATUS_SUCCESS = 1;//认证成功
- const STATUS_IN_PROGRESS = 2;//认证中
- const STATUS_FAIL = 3;//认证失败
- /**
- * @var array
- */
- protected $headers;
- /**
- * @var string
- */
- protected $key;
- /**
- * @var int $timeout
- */
- protected $timeout = 10;
- /**
- * @var Client
- */
- protected $http;
- protected $app_id; /* 当前游戏id */
- protected $channel_code; /* 渠道code 易信提供 */
- protected $require_url; /* 认证URL 易信提供 */
- /**
- * @param array $config
- */
- public function __construct($config = []) {
- $_config = $config;
- parent::__construct($_config);
- // 设置属性
- $this->app_id = get_val($_config, 'app_id', 0);
- $this->channel_code = get_val($_config, 'channel_code', '');
- $this->require_url = get_val($_config, 'require_url', '');
- }
- /**
- * 实名认证
- * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
- */
- public function identify() {
- $_results = $this->check();
- $_results_arr = json_decode($_results, true);
- if (empty($_results_arr) || !isset($_results_arr['code']) || '200' != $_results_arr['code']) {
- $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
- $_step = 0;
- $_other = '易信防沉迷实名认证API,认证异常';
- \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
- $_code = CommonStatus::INNER_ERROR;
- $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
- return $this->huoError($_code, $_results_arr['msg']);
- }
- /* 认证失败 */
- if ($_results_arr['result']['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_YIXIN);
- $_log_data = [
- 'mem_id' => $this->mem_id,
- 'mg_mem_id' => $this->mg_mem_id,
- 'app_id' => $this->app_id,
- 'identify_from' => IdentifyConst::DRIVER_KEY_YIXIN,
- 'real_name' => $this->real_name,
- 'id_card' => $this->id_card,
- 'identify_pi' => $_results_arr['result']['pi'],
- 'status' => $_results_arr['result']['status'],
- ];
- if (empty($_ipl_info)) {
- $_ipl_model->addData($_log_data);
- } else {
- $_ipl_model->updateData($_log_data, $_ipl_info['id']);
- }
- /* 认证中 */
- if ($_results_arr['result']['status'] == self::STATUS_IN_PROGRESS) {
- $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
- }
- $_code = CommonStatus::NO_ERROR;
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['result']);
- }
- /**
- * 查询是否已认证
- *
- * @return bool
- */
- private function checkIsVerified() {
- $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_YIXIN);
- return empty($_pi) ? false : true;
- }
- public function loginBehavior($identify_pi) {
- $_check_rs = $this->checkIsVerified();
- if (true === $_check_rs) {
- /* 已认证无需处理 */
- $_code = CommonStatus::NO_ERROR;
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
- }
- /* 限制请求 */
- $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
- if (false == $_rs) {
- $_code = CommonStatus::NO_ERROR;
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
- }
- $_identify_rs = $this->identify();
- if (CommonStatus::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_YIXIN,
- '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'])) {
- $_update_data = [
- 'identify_pi' => $_identify_pi
- ];
- $_im_model->updateData($_update_data, $_old_id_data['id']);
- }
- }
- $_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;
- $_headers = ['Content-Type' => 'application/json; charset=utf-8'];
- if (!empty($this->headers)) {
- $_headers = array_merge($_headers, $this->headers);
- }
- $_params = array(
- 'uid' => md5($this->mg_mem_id),
- 'channelname' => $this->channel_code,
- 'gameid' => $this->app_id,
- 'realname' => $this->real_name,
- 'cardinfo' => $this->id_card,
- 'timestamp' => self::getMillisecond(),
- );
- $_params['sign'] = md5(
- $_params['uid'].$_params['channelname'].$_params['gameid'].$_params['timestamp']
- );
- return $this->doRequest($_params, $_uri, 'POST', $_headers);
- }
- /**
- * 获取毫秒级别的时间戳
- */
- private static function getMillisecond() {
- //获取毫秒的时间戳
- $time = explode(" ", microtime());
- $time = $time[1].($time[0] * 1000);
- $time2 = explode(".", $time);
- $time = $time2[0];
- return $time;
- }
- /**
- * do request
- *
- * @param $params
- * @param string $url
- * @param string $method
- * @param array $headers
- *
- * @return mixed
- */
- private function doRequest($params, $url, $method, $headers = []) {
- if (count($params) > 0) {
- $url .= '?'.http_build_query($params);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt(
- $ch, CURLOPT_HTTPHEADER, array_map(
- function ($v, $k) {
- return $k.': '.$v;
- }, array_values($headers), array_keys($headers)
- )
- );
- if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
- }
- $_rs_json = curl_exec($ch);
- curl_close($ch);
- return $_rs_json;
- }
- }
|