Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
kilvn committed May 21, 2021
1 parent 6bad4d3 commit 6d062f2
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 30 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 文件中。

Expand All @@ -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
Expand Down
115 changes: 91 additions & 24 deletions apps/api/Fcm.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
<?php

use Zttp\Zttp;

use Helpers\Fcm as FcmUtil;
use Helpers\Util;
use Predis\Client as Predis;

/**
* 网络游戏防沉迷实名认证系统 后端接口
* document uri: https://wlc.nppa.gov.cn/2021/02/25/16e2520acd9f4404897ed1a5b8fd1240.pdf
*
* document uri: https://wlc.nppa.gov.cn/fcm_company/index.html
* Class Fcm
*/
class Fcm extends Base
{
protected static self $instance;
protected static ?Predis $redis = null;
protected string $env = 'dev';
protected string $test_key = '';
protected array $header_data = [];
protected string $app_secret = '';
protected array $header_data = [];

public function __construct()
{
parent::__construct();

$this->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'];
}
}
Expand All @@ -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));
}

/**
* 实名认证提交
*/
Expand All @@ -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);
Expand All @@ -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']);

// {
Expand All @@ -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);
Expand All @@ -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']);

// {
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
16 changes: 12 additions & 4 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
# 实名认证查询 测试码
Expand Down

0 comments on commit 6d062f2

Please sign in to comment.