diff --git a/README.md b/README.md index 6c00940..242fc29 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ try_files $uri $uri/ /index.php$is_args$args; #### Use -防沉迷系统官方文档地址:https://wlc.nppa.gov.cn/2021/02/25/16e2520acd9f4404897ed1a5b8fd1240.pdf +防沉迷系统官方文档地址:https://wlc.nppa.gov.cn/fcm_company/index.html 防沉迷系统的配置在 `.env` 文件中。 @@ -43,6 +43,12 @@ try_files $uri $uri/ /index.php$is_args$args; | 401|第三方API远程请求失败| | 404|接口不存在| +0. 查询本地redis库,ai实名认证状态 + - POST + - http://xxx.com/api/fcm/get_auth_status + - 连接本地redis查询当前ai实名认证状态 + + 1. 认证提交 - POST - http://xxx.com/api/fcm/idcard_check diff --git a/apps/api/Fcm.php b/apps/api/Fcm.php index d9641a0..7469a72 100644 --- a/apps/api/Fcm.php +++ b/apps/api/Fcm.php @@ -1,37 +1,41 @@ timestamp *= 1000; - $this->env = $_ENV['APP_ENV']; + $this->env = strtolower($_ENV['APP_ENV']); + $this->app_secret = $this->env == 'production' ? $_ENV['FCM_APPSECRET'] : $_ENV['TEST_FCM_APPSECRET']; $this->header_data = [ 'Content-Type' => 'application/json; charset=utf-8', - 'appId' => $_ENV['FCM_APPID'], - 'bizId' => $_ENV['FCM_BIZID'], + 'appId' => $this->env == 'production' ? $_ENV['FCM_APPID'] : $_ENV['TEST_FCM_APPID'], + 'bizId' => $this->env == 'production' ? $_ENV['FCM_BIZID'] : $_ENV['TEST_FCM_APPID'], 'timestamps' => $this->timestamp, 'sign' => '', ]; - if ($this->env == 'dev' and isset($this->reqData['test_key'])) { + if ($this->env != 'production' and isset($this->reqData['test_key'])) { $this->test_key = $this->reqData['test_key']; } } @@ -49,6 +53,53 @@ public static function get() return self::$instance; } + /** + * Get the instance of redis. + * + * @return Predis + */ + public static function getRedis() + { + if (!self::$redis) { + self::$redis = new Predis(array( + 'scheme' => $_ENV['REDIS_SCHEME'], + 'host' => $_ENV['REDIS_HOST'], + 'port' => $_ENV['REDIS_PORT'], + 'password' => $_ENV['REDIS_PWD'], + 'database' => $_ENV['REDIS_DB'], + )); + } + return self::$redis; + } + + /** + * 检查用户实名认证状态 + */ + public function get_auth_status() + { + $ai = $this->reqData['ai'] ?? ''; + + if (empty($ai)) { + Util::jsonReturn(400, '参数 ai 不能为空'); + } + + self::getRedis(); + $redis = self::$redis; + $redis_key = 'FCM-CACHE:' . $ai; + + if (!$redis->exists($redis_key)) { + $redis_data = [ + 'pi' => null, + 'status' => 0, + ]; + Util::jsonReturn(200, MSG_CODE[200], $redis_data); + } + + $res = $redis->get($redis_key); + + Util::jsonReturn(200, MSG_CODE[200], json_decode($res, JSON_OBJECT_AS_ARRAY)); + } + /** * 实名认证提交 */ @@ -72,16 +123,15 @@ public function idcard_check() Util::jsonReturn(400, '参数 idNum 不能为空'); } - $api_key = $_ENV['FCM_IDCARD_CHECK_KEY']; - if ($this->env == 'dev' and strlen($this->test_key)) { - $api_key = $this->test_key; + if ($this->env != 'production' and !strlen($this->test_key)) { + $this->test_key = $_ENV['FCM_IDCARD_CHECK_KEY']; } $urls = [ 'production' => 'https://api.wlc.nppa.gov.cn/idcard/authentication/check', - 'dev' => 'https://wlc.nppa.gov.cn/test/authentication/check/' . $api_key, + 'dev' => 'https://wlc.nppa.gov.cn/test/authentication/check/' . $this->test_key, ]; - $body = FcmUtil::aesEncode($_ENV['FCM_APPSECRET'], $api_data); - $this->header_data['sign'] = FcmUtil::createSign($_ENV['FCM_APPSECRET'], $this->header_data, $body); + $body = FcmUtil::aesEncode($this->app_secret, $api_data); + $this->header_data['sign'] = FcmUtil::createSign($this->app_secret, $this->header_data, $body); // dump($this->header_data);exit; $response = Zttp::timeout(60)->withHeaders($this->header_data)->post($urls[$this->env], $body); @@ -99,6 +149,15 @@ public function idcard_check() Util::jsonReturn(400, MSG_CODE[400], $json); } + self::getRedis(); + $redis = self::$redis; + $redis_key = 'FCM-CACHE:' . $api_data['ai']; + $redis_data = json_encode([ + 'pi' => $json['data']['result']['pi'] ?? null, + 'status' => $json['data']['result']['status'] ?? 0, + ]); + $redis->setex($redis_key, 0, $redis_data); + Util::jsonReturn(200, MSG_CODE[200], $json['data']['result']); // { @@ -124,16 +183,15 @@ public function idcard_query() Util::jsonReturn(400, '参数 ai 不能为空'); } - $api_key = $_ENV['FCM_IDCARD_QUERY_KEY']; - if ($this->env == 'dev' and strlen($this->test_key)) { - $api_key = $this->test_key; + if ($this->env != 'production' and !strlen($this->test_key)) { + $this->test_key = $_ENV['FCM_IDCARD_QUERY_KEY']; } $urls = [ 'production' => 'http://api2.wlc.nppa.gov.cn/idcard/authentication/query', - 'dev' => 'https://wlc.nppa.gov.cn/test/authentication/query/' . $api_key, + 'dev' => 'https://wlc.nppa.gov.cn/test/authentication/query/' . $this->test_key, ]; unset($this->header_data['Content-Type']); - $this->header_data['sign'] = FcmUtil::createSign($_ENV['FCM_APPSECRET'], array_merge($this->header_data, $api_data)); + $this->header_data['sign'] = FcmUtil::createSign($this->app_secret, array_merge($this->header_data, $api_data)); $url = $urls[$this->env] . '?' . http_build_query($api_data); $response = Zttp::timeout(60)->withHeaders($this->header_data)->get($url); @@ -151,6 +209,16 @@ public function idcard_query() Util::jsonReturn(400, MSG_CODE[400], $json); } + self::getRedis(); + $redis = self::$redis; + $redis_key = 'FCM-CACHE:' . $api_data['ai']; + $redis_data = json_encode([ + 'pi' => $json['data']['result']['pi'] ?? null, + 'status' => $json['data']['result']['status'] ?? 0, + ]); + $redis->del($redis_key); + $redis->setex($redis_key, 0, $redis_data); + Util::jsonReturn(200, MSG_CODE[200], $json['data']['result']); // { @@ -182,16 +250,15 @@ public function behavior() ] ]; - $api_key = $_ENV['FCM_BEHAVIOR_KEY']; - if ($this->env == 'dev' and strlen($this->test_key)) { - $api_key = $this->test_key; + if ($this->env != 'production' and !strlen($this->test_key)) { + $this->test_key = $_ENV['FCM_BEHAVIOR_KEY']; } $urls = [ 'production' => 'http://api2.wlc.nppa.gov.cn/behavior/collection/loginout', - 'dev' => 'https://wlc.nppa.gov.cn/test/collection/loginout/' . $api_key, + 'dev' => 'https://wlc.nppa.gov.cn/test/collection/loginout/' . $this->test_key, ]; - $body = FcmUtil::aesEncode($_ENV['FCM_APPSECRET'], $api_data); - $this->header_data['sign'] = FcmUtil::createSign($_ENV['FCM_APPSECRET'], $this->header_data, $body); + $body = FcmUtil::aesEncode($this->app_secret, $api_data); + $this->header_data['sign'] = FcmUtil::createSign($this->app_secret, $this->header_data, $body); $response = Zttp::timeout(60)->withHeaders($this->header_data)->post($urls[$this->env], $body); diff --git a/composer.json b/composer.json index fd4d907..fd2b638 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,15 @@ { "name": "fcm-system/0.01", "description": "防沉迷系统API", + "license": "MIT", "require": { "kitetail/zttp": "^0.6.0", - "vlucas/phpdotenv": "^5.3" + "predis/predis": "^1.1", + "vlucas/phpdotenv": "^5.3", + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^8.0 || ~9.4.4" }, "autoload": { "psr-4": { diff --git a/env.example b/env.example index 3ec15a3..386a4af 100644 --- a/env.example +++ b/env.example @@ -2,15 +2,23 @@ APP_NAME=FCM # dev,testing,production APP_ENV=dev -LOG_PATH=/var/www/anti_addiction/logs +LOG_PATH=/www/wwwroot/anti_addiction/logs -# 应用标识 FCM_APPID= -# 用户密钥 FCM_APPSECRET= -# 游戏备案识别码 FCM_BIZID= +# redis设置 +# tcp/tls +REDIS_SCHEME=tcp +REDIS_HOST=redis +REDIS_PORT=6379 +REDIS_PWD= +REDIS_DB=2 + +TEST_FCM_APPID= +TEST_FCM_APPSECRET= +TEST_FCM_BIZID= # 实名认证上报 测试码 FCM_IDCARD_CHECK_KEY= # 实名认证查询 测试码