Fcmgame.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. * FcmGame.php UTF-8
  4. *
  5. *
  6. * @date : 2021-03-10 10:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 9.0
  11. */
  12. namespace huoIdentify\identifyDriver\driver;
  13. use GuzzleHttp\Client;
  14. use GuzzleHttp\Exception\ConnectException;
  15. use huoIdentify\controller\AES;
  16. use huoIdentify\identifyDriver\Driver;
  17. use huoIdentify\model\IdentifyOrderModel;
  18. use huolib\status\CommonStatus;
  19. use huolib\status\IdentifyStatus;
  20. use think\Config;
  21. use think\Log;
  22. class Fcmgame extends Driver {
  23. const STATUS_SUCCESS = 0;//认证成功
  24. const STATUS_IN_PROGRESS = 1;//认证中
  25. const STATUS_FAIL = 2;//认证失败
  26. /**
  27. * @var string
  28. */
  29. protected $cipher = "aes-128-gcm";
  30. /**
  31. * @var AES
  32. */
  33. protected $aes;
  34. /**
  35. * @var array
  36. */
  37. protected $headers;
  38. /**
  39. * @var string
  40. */
  41. protected $key;
  42. /**
  43. * @var int $timeout
  44. */
  45. protected $timeout = 5;
  46. /**
  47. * @var Client
  48. */
  49. protected $http;
  50. /* ai标识 */
  51. protected $ai = '';
  52. protected $biz_id = '';
  53. /**
  54. * @param int $mem_id
  55. */
  56. public function setMemId($mem_id) {
  57. $_prefix = Config::get('identify_conf.fcmgame_ai_prefix');
  58. $_prefix .= time();
  59. $_pad_length = 32 - strlen($_prefix);
  60. $this->ai = $_prefix.str_pad($mem_id, $_pad_length, '0', STR_PAD_LEFT);
  61. $this->mem_id = $mem_id;
  62. }
  63. /**
  64. * @param array $config
  65. */
  66. public function __construct($config = []) {
  67. if (empty($config)) {
  68. $config = (array)Config::get('identify_conf.fcm_game');
  69. }
  70. parent::__construct($config);
  71. // 设置属性
  72. $appId = get_val($config, 'appId', '');
  73. $bizId = get_val($config, 'bizId', '');
  74. $this->biz_id = $bizId;
  75. $key = get_val($config, 'key', '');
  76. $this->aes = new AES($key, $this->cipher);
  77. $this->key = $key;
  78. if (!class_exists('\\GuzzleHttp\\Client')) {
  79. // 使用已有的GuzzleHttp
  80. require_once 'wxpay/TCloudAutoLoader.php';
  81. }
  82. $this->http = new Client(['timeout' => 5]);
  83. $this->setHeaders($appId, $bizId);
  84. }
  85. /**
  86. * 实名认证
  87. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  88. */
  89. public function identify() {
  90. /* 提交认证之前获取当前玩家是否有认证中的数据,有则使用查询接口 */
  91. $_query_rs = $this->checkCertificationData();
  92. if (CommonStatus::DATA_NOT_FOUND_EXCEPTION != $_query_rs['code']) {
  93. /* 只要返回的不是数据未找到则认为无需再次提交认证信息 */
  94. return $this->huoReturn($_query_rs);
  95. }
  96. $_results = $this->check($this->ai, $this->real_name, $this->id_card);
  97. $_results_arr = json_decode($_results, true);
  98. if ('0' != $_results_arr['errcode']) {
  99. Log::write(
  100. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  101. array($this->config, $this->id_card, $this->real_name)
  102. ).'&rs='.$_results.'[防沉迷实名认证api]', Log::ERROR
  103. );
  104. $_code = CommonStatus::INNER_ERROR;
  105. return $this->huoError($_code, $_results_arr['errmsg']);
  106. }
  107. /* 认证中 */
  108. if ($_results_arr['data']['result']['status'] == self::STATUS_IN_PROGRESS) {
  109. /* 记录上报信息,用于下次查询 */
  110. $_add_data = [
  111. 'ai' => $this->ai,
  112. 'mem_id' => $this->mem_id,
  113. 'biz_id' => $this->biz_id,
  114. 'real_name' => $this->real_name,
  115. 'id_card' => $this->id_card,
  116. 'status' => $_results_arr['data']['result']['status'],
  117. ];
  118. (new IdentifyOrderModel())->addData($_add_data);
  119. $_code = CommonStatus::INVALID_PARAMS;
  120. return $this->huoError($_code, $_results_arr['errmsg']);
  121. }
  122. /* 认证失败 */
  123. if ($_results_arr['data']['result']['status'] == self::STATUS_FAIL) {
  124. $_code = IdentifyStatus::IDENTITY_FAIL;
  125. return $this->huoError($_code, $_results_arr['errmsg']);
  126. }
  127. $_code = CommonStatus::NO_ERROR;
  128. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']['result']);
  129. }
  130. /* 查询是否有认证中的数据 */
  131. private function checkCertificationData() {
  132. $_last_order_data = (new IdentifyOrderModel())->getLastInfoByBizIdMem($this->biz_id, $this->mem_id);
  133. if (empty($_last_order_data)) {
  134. /* 数据不存在,需重新认证 */
  135. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  136. return $this->huoError($_code, CommonStatus::getMsg($_code));
  137. }
  138. if ($_last_order_data['status'] != self::STATUS_IN_PROGRESS
  139. || $this->real_name != $_last_order_data['real_name']
  140. || $this->id_card != $_last_order_data['id_card']) {
  141. /* 状态不是认证中的/玩家证件信息对不上的认为数据不存在,需重新认证 */
  142. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  143. return $this->huoError($_code, CommonStatus::getMsg($_code));
  144. }
  145. $_query_rs = $this->query($_last_order_data['ai']);
  146. $_results_arr = json_decode($_query_rs, true);
  147. if ('0' != $_results_arr['errcode']) {
  148. Log::write(
  149. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode($_last_order_data)
  150. .'&rs='.$_query_rs.'[防沉迷实名认证api,查询信息]', Log::ERROR
  151. );
  152. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  153. return $this->huoError($_code, $_results_arr['errmsg']);
  154. }
  155. /* 认证中/认证失败 */
  156. if ($_results_arr['data']['result']['status'] != self::STATUS_SUCCESS) {
  157. $_update_data = ['status' => $_results_arr['data']['result']['status']];
  158. (new IdentifyOrderModel())->updateData($_update_data, $_last_order_data['id']);
  159. $_code = CommonStatus::INVALID_PARAMS;
  160. return $this->huoError($_code, $_results_arr['errmsg']);
  161. }
  162. $_code = CommonStatus::NO_ERROR;
  163. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']['result']);
  164. }
  165. public function loginBehavior($identify_pi) {
  166. if (empty($identify_pi)) {
  167. $_code = CommonStatus::INVALID_PARAMS;
  168. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  169. }
  170. $_data = [
  171. ['bt' => 1, 'ct' => 0, 'pi' => $identify_pi]
  172. ];
  173. $_results = $this->loginOrOut($_data);
  174. $_results_arr = json_decode($_results, true);
  175. if ('0' != $_results_arr['errcode']) {
  176. Log::write(
  177. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  178. array($this->config, $this->id_card, $this->real_name, $identify_pi)
  179. ).'&rs='.$_results.'[防沉迷行为数据上报api]', Log::ERROR
  180. );
  181. $_code = CommonStatus::INNER_ERROR;
  182. return $this->huoError($_code, $_results_arr['errmsg']);
  183. }
  184. $_code = CommonStatus::NO_ERROR;
  185. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  186. }
  187. public function logoutBehavior($identify_pi) {
  188. if (empty($identify_pi)) {
  189. $_code = CommonStatus::INVALID_PARAMS;
  190. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  191. }
  192. $_data = [
  193. ['bt' => 0, 'ct' => 0, 'pi' => $identify_pi]
  194. ];
  195. $_results = $this->loginOrOut($_data);
  196. $_results_arr = json_decode($_results, true);
  197. if ('0' != $_results_arr['errcode']) {
  198. Log::write(
  199. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  200. array($this->config, $this->id_card, $this->real_name, $identify_pi)
  201. ).'&rs='.$_results.'[防沉迷行为数据上报api]', Log::ERROR
  202. );
  203. $_code = CommonStatus::INNER_ERROR;
  204. return $this->huoError($_code, $_results_arr['errmsg']);
  205. }
  206. $_code = CommonStatus::NO_ERROR;
  207. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  208. }
  209. /**
  210. * check the name and idNum
  211. *
  212. * @param string $ai
  213. * @param string $name
  214. * @param string $idNum
  215. * @param string $uri
  216. *
  217. * @return string
  218. */
  219. public function check($ai, $name, $idNum, $uri = '') {
  220. $uri = $uri ?: 'https://api.wlc.nppa.gov.cn/idcard/authentication/check';
  221. $headers = ['Content-Type' => 'application/json; charset=utf-8'];
  222. $headers = array_merge($headers, $this->headers);
  223. return $this->doRequest('POST', $uri, $headers, ['ai' => $ai, 'name' => $name, 'idNum' => $idNum]);
  224. }
  225. /**
  226. * check for test
  227. *
  228. * @param string $ai
  229. * @param string $name
  230. * @param string $idNum
  231. * @param string $testCode
  232. *
  233. * @return string
  234. */
  235. public function testCheck($ai, $name, $idNum, $testCode) {
  236. $uri = 'https://wlc.nppa.gov.cn/test/authentication/check/'.$testCode;
  237. return $this->check($ai, $name, $idNum, $uri);
  238. }
  239. /**
  240. * query the ai
  241. *
  242. * @param string $ai
  243. * @param string $uri
  244. *
  245. * @return string
  246. */
  247. public function query($ai, $uri = '') {
  248. $uri = $uri ?: 'http://api2.wlc.nppa.gov.cn/idcard/authentication/query';
  249. $query = ['ai' => $ai];
  250. return $this->doRequest('GET', $uri, $this->headers, $query, ['query' => $query]);
  251. }
  252. /**
  253. * query for test
  254. *
  255. * @param string $ai
  256. * @param $testCode
  257. *
  258. * @return string
  259. */
  260. public function testQuery($ai, $testCode) {
  261. $uri = 'https://wlc.nppa.gov.cn/test/authentication/query/'.$testCode;
  262. return $this->query($ai, $uri);
  263. }
  264. /**
  265. * @param $uri
  266. * @param mixed $data
  267. *
  268. * @return string
  269. * @example $data = [['bt'=>0, 'ct'=>0, 'pi'=>'1fffbjzos82bs9cnyj1dna7d6d29zg4esnh99u']]
  270. */
  271. public function loginOrOut(array $data, $uri = '') {
  272. $this->timeout = 3;
  273. $collections = [];
  274. foreach ($data as $i => $d) {
  275. $tmp = [];
  276. $tmp['no'] = $i + 1;
  277. $tmp['si'] = isset($d['si']) ? $d['si'] : md5($d['pi']);
  278. $tmp['bt'] = $d['bt'];
  279. $tmp['ot'] = isset($d['ot']) ? $d['ot'] : time();
  280. $tmp['ct'] = $d['ct'];
  281. $tmp['di'] = isset($d['di']) ? $d['di'] : "";
  282. $tmp['pi'] = isset($d['pi']) ? $d['pi'] : "";
  283. $collections['collections'][] = $tmp;
  284. }
  285. $uri = $uri ?: 'http://api2.wlc.nppa.gov.cn/behavior/collection/loginout';
  286. $headers = ['Content-Type' => 'application/json; charset=utf-8'];
  287. $headers = array_merge($this->headers, $headers);
  288. return $this->doRequest('POST', $uri, $headers, $collections);
  289. }
  290. /**
  291. * @param array $data
  292. * @param $testCode
  293. *
  294. * @return string
  295. */
  296. public function testLoginOrOut($data, $testCode) {
  297. $uri = 'https://wlc.nppa.gov.cn/test/collection/loginout/'.$testCode;
  298. return $this->loginOrOut($data, $uri);
  299. }
  300. /**
  301. * make the sign
  302. *
  303. * @param $body
  304. * @param array $query
  305. *
  306. * @return string
  307. */
  308. private function makeSign($body, $query = []) {
  309. $request = array_merge($this->headers, $query);
  310. ksort($request);
  311. $ret = '';
  312. foreach ($request as $r => $v) {
  313. $ret .= $r.$v;
  314. }
  315. // sha256
  316. $raw = $this->key.$ret.$body;
  317. return hash("sha256", $raw);
  318. }
  319. /**
  320. * common request headers
  321. *
  322. * @param string $appId
  323. * @param string $bizId
  324. */
  325. private function setHeaders($appId, $bizId) {
  326. $this->headers = [
  327. 'appId' => $appId,
  328. 'bizId' => $bizId,
  329. 'timestamps' => (int)(microtime(true) * 1000),
  330. ];
  331. }
  332. /**
  333. * do request
  334. *
  335. * @param string $method
  336. * @param string $uri
  337. * @param array $headers
  338. * @param array $body
  339. * @param array $options
  340. *
  341. * @return string
  342. */
  343. private function doRequest($method, $uri, array $headers = [], array $body = [], array $options = []) {
  344. $raw = json_encode($body, JSON_UNESCAPED_UNICODE);
  345. $query = isset($options['query']) ? $options['query'] : [];
  346. $body = '{"data":"'.$this->aes->encrypt($raw).'"}';
  347. $headers['sign'] = $this->makeSign($body, $query);
  348. $options = array_merge(['headers' => $headers, 'body' => $body, 'timeout' => $this->timeout], $options);
  349. try {
  350. $response = $this->http->request($method, $uri, $options);
  351. return $response->getBody()->getContents();
  352. } catch (ConnectException $e) {
  353. $_result = $e->getMessage();
  354. $_step = 0;
  355. $_other = 0;
  356. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  357. $_array = [
  358. 'errcode' => '-1',
  359. 'errmsg' => '链接超时'
  360. ];
  361. return json_encode($_array);
  362. }
  363. }
  364. ////////////////////////////////////////////////////// 测试用例 /////////////////////////////////////////////////////
  365. /**
  366. * check
  367. *
  368. * @param string $code1
  369. * @param string $code2
  370. * @param string $code3
  371. */
  372. public function testCaseCheck($code1, $code2, $code3) {
  373. // 认证成功
  374. $_result1 = $this->testCheck('100000000000000001', '某一一', '110000190101010001', $code1);
  375. var_dump($_result1);
  376. // 认证中
  377. $_result2 = $this->testCheck('200000000000000001', '某二一', '110000190201010009', $code2);
  378. var_dump($_result2);
  379. // 认证失败
  380. $_result3 = $this->testCheck('300000000000000001', '某三一', '110000190201010009', $code3);
  381. var_dump($_result3);
  382. }
  383. /**
  384. * query
  385. *
  386. * @param string $code1
  387. * @param string $code2
  388. * @param string $code3
  389. */
  390. public function testCaseQuery($code1, $code2, $code3) {
  391. // 认证成功
  392. $_result1 = $this->testQuery('100000000000000001', $code1);
  393. var_dump($_result1);
  394. // 认证中
  395. $_result2 = $this->testQuery('200000000000000001', $code2);
  396. var_dump($_result2);
  397. // 认证失败
  398. $_result3 = $this->testQuery('300000000000000001', $code3);
  399. var_dump($_result3);
  400. }
  401. /**
  402. * login or logout
  403. *
  404. * @param $code1
  405. * @param $code2
  406. */
  407. public function testCaseLoginOrOut($code1, $code2) {
  408. // 游客模式
  409. $_result1 = $this->testLoginOrOut(
  410. [['bt' => 0, 'ct' => 2, 'si' => uniqid(), 'di' => md5('device')]], $code1
  411. );
  412. var_dump($_result1);
  413. // 认证模式
  414. $_result2 = $this->testLoginOrOut(
  415. [['bt' => 1, 'ct' => 0, 'pi' => '1fffbjzos82bs9cnyj1dna7d6d29zg4esnh99u']], $code2
  416. );
  417. var_dump($_result2);
  418. }
  419. }