Identify_20210908.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. /**
  3. * Identify.php UTF-8
  4. * 玩家实名认证
  5. *
  6. * @date : 2020/6/20 11:53
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOUNION 8.5
  11. */
  12. namespace huoIdentify\controller;
  13. use huo\controller\common\Base;
  14. use huo\controller\common\CommonFunc;
  15. use huo\controller\integral\MemIa;
  16. use huo\controller\member\MemCache;
  17. use huo\model\game\GameModel;
  18. use huo\model\member\MemberModel;
  19. use huo\model\member\MemGameModel;
  20. use huoIdentify\model\IdentifyGameModel;
  21. use huoIdentify\model\IdentifyMemModel;
  22. use huoIdentify\model\IdentifyOrderModel;
  23. use huoIdentify\model\IdentifyPiModel;
  24. use huoIdentify\model\IdentifyPlatformLogModel;
  25. use huolib\constant\CommonConst;
  26. use huolib\constant\DeviceTypeConst;
  27. use huolib\constant\GameConst;
  28. use huolib\constant\IaConst;
  29. use huolib\constant\IdentifyConst;
  30. use huolib\constant\MemConst;
  31. use huolib\status\CommonStatus;
  32. use huolib\status\IdentifyStatus;
  33. use huolib\status\MemberStatus;
  34. use huolib\tool\RateLimit;
  35. use huolib\tool\StrUtils;
  36. use huolib\utils\IdentifyUtils;
  37. use think\Config;
  38. use think\Exception;
  39. class Identify_20210908 extends Base {
  40. protected $app_id = 0;
  41. public function __construct() {
  42. $this->__initConfig();
  43. }
  44. private function __initConfig() {
  45. $_app_id = (int)request()->param('game_id/d', 0);
  46. if (empty($_app_id)) {
  47. $_app_id = (int)request()->param('app_id/d', 0);
  48. }
  49. $this->app_id = $_app_id;
  50. if (!empty($_app_id)) {
  51. $_identify_game_model = new IdentifyGameModel();
  52. $_identify_game_info = (array)$_identify_game_model->getInfoByAppId($_app_id);
  53. if (!empty($_identify_game_info['driver_key'])) {
  54. Config::set('identify_conf.default', $_identify_game_info['driver_key']);
  55. }
  56. }
  57. }
  58. /**
  59. * 获取会员认证信息
  60. *
  61. * @param int $mem_id 玩家id
  62. * @param bool $encrypt 是否加密
  63. * @param int $app_id 当前游戏id
  64. *
  65. * @return mixed : array
  66. */
  67. public function getIdentifyByMemId($mem_id, $encrypt = false, $app_id = 0) {
  68. $_mem_data = (new IdentifyMemModel())->getInfoByMemId($mem_id);
  69. if (empty($_mem_data) && !empty($app_id)) {
  70. /* 未获取到实名信息,则查看是否有认证中的数据 */
  71. $this->checkMemIdentify($mem_id, $app_id);
  72. $_mem_data = (new IdentifyMemModel())->getInfoByMemId($mem_id);
  73. }
  74. $_data['id_card'] = get_val($_mem_data, 'id_card');
  75. $_data['real_name'] = get_val($_mem_data, 'real_name');
  76. $_data['pi'] = get_val($_mem_data, 'identify_pi');
  77. $_data['birthday'] = IdentifyUtils::getBirthday($_data['id_card']);
  78. $_data['age'] = IdentifyUtils::getAge($_data['birthday']);
  79. $_data['is_auth'] = IdentifyConst::IS_AUTH_NO;
  80. if (!empty($_data['id_card'])) {
  81. $_data['is_auth'] = IdentifyConst::IS_AUTH_YES;
  82. }
  83. if (true == $encrypt) {
  84. if (!empty($_data['real_name'])) {
  85. $_data['real_name'] = StrUtils::encryptName($_data['real_name']);
  86. }
  87. if (!empty($_data['id_card'])) {
  88. $_data['id_card'] = StrUtils::encryptName($_data['id_card']);
  89. }
  90. }
  91. return $_data;
  92. }
  93. /**
  94. * 根据app_id获取游戏实名认证状态
  95. *
  96. * @param int $app_id
  97. *
  98. * @return:int
  99. */
  100. public function getGameAuthByAppId($app_id) {
  101. $_is_auth = (new GameModel())->getIsAuthById($app_id);
  102. return $_is_auth;
  103. }
  104. /**
  105. * 获取用户实名信息状况
  106. *
  107. * @param int $app_id 游戏id
  108. * @param int $mem_id 会员id
  109. * @param string $device_id 设备号 用于未实名玩家时长统计
  110. *
  111. * @return array
  112. */
  113. public function getMemIdentifyInfo($app_id, $mem_id, $device_id = '') {
  114. /*定义返回数组*/
  115. $_rdata = [
  116. 'auth_info' => [
  117. 'id_card' => '',
  118. 'real_name' => '',
  119. 'birthday' => '',
  120. 'age' => '',
  121. 'is_auth' => IdentifyConst::IS_AUTH_NO
  122. ],
  123. 'url' => '',
  124. ];
  125. $_app_ids = CommonFunc::getAppAppId();
  126. $_game_auth = $this->getGameAuthByAppId($app_id);
  127. if (!in_array($app_id, $_app_ids)) {
  128. /* 不需要实名不做任何处理 */
  129. if (GameConst::GAME_IDENTIFY_IS_NO == $_game_auth) {
  130. return $_rdata;
  131. }
  132. }
  133. $_param = [
  134. 'app_id' => $app_id,
  135. 'device_id' => $device_id,
  136. 'token' => session_id()
  137. ];
  138. $_identify_url = IdentifyConst::getIdentifyUrl($_param);
  139. $_mem_auth_info = $this->getIdentifyByMemId($mem_id, false, $app_id);
  140. $_mem_auth_info['is_auth'] = $_game_auth;
  141. $_rdata['auth_info'] = $_mem_auth_info;
  142. /* 获取玩家是否已绑定手机 */
  143. $_mem_model = new MemberModel();
  144. $_mobile = $_mem_model->getMobileById($mem_id);
  145. $_status = $_mem_model->getStatus($mem_id);
  146. if (empty($_mobile) && MemConst::STATUS_TRY == $_status) {
  147. $_rdata['url'] = IdentifyConst::getBindMobileUrl($_param);
  148. return $_rdata;
  149. }
  150. $_rdata['url'] = $_identify_url;
  151. /* 玩家未实名返回实名认证地址 */
  152. if (empty($_mem_auth_info['id_card'])) {
  153. return $_rdata;
  154. }
  155. /* 玩家已实名校验未成年限制 */
  156. $_rdata['url'] = (new Online($app_id, $mem_id, $device_id))->checkLimit();
  157. return $_rdata;
  158. }
  159. /**
  160. * 更新实名认证信息
  161. *
  162. * @param $mem_id
  163. * @param int $type
  164. * @param $real_name
  165. * @param $id_card
  166. * @param int $app_id
  167. *
  168. * @return mixed
  169. */
  170. public function updateIdentify($mem_id, $type = 1, $real_name, $id_card, $app_id = 0) {
  171. $id_card = strtoupper($id_card);
  172. $_mem_cache = MemCache::ins();
  173. $_mem_data = $_mem_cache->getInfoById($mem_id);
  174. if (empty($_mem_data)) {
  175. $_code = MemberStatus::LOGIN_IS_OUT;
  176. return $this->huoSuccess($_code, MemberStatus::getMsg(MemberStatus::LOGIN_IS_OUT));
  177. }
  178. $_chk_rs = IdentifyUtils::checkIdentify($type, $real_name, $id_card);
  179. if (IdentifyStatus::NO_ERROR != $_chk_rs) {
  180. return $this->huoError($_chk_rs, IdentifyStatus::getMsg($_chk_rs));
  181. }
  182. $_im_model = new IdentifyMemModel();
  183. $_pi_info = $this->getPiInfoByIdCard($id_card);
  184. $_pi = get_val($_pi_info, 'identify_pi', '');
  185. $_cnt = $_im_model->getCntByIdCard($id_card);
  186. $_id_bind_limit = (new IdentifyConf())->getIdCardBindCnt();
  187. $_is_other_api_check = (new IdentifyConf())->getIsOtherApiCheck();
  188. $_identify_from = Config::get('identify_conf.default'); //实名认证api
  189. /* 需要pi的类型 */
  190. $_need_pi_type = IdentifyConst::getNeedPiTypes();
  191. if ($_cnt >= $_id_bind_limit) {
  192. $_code = IdentifyStatus::IDCARD_BIND_LIMIT;
  193. return $this->huoError($_code, IdentifyStatus::getMsg($_code));
  194. } elseif ((empty($_cnt) && CommonConst::STATUS_YES == $_is_other_api_check)
  195. || (empty($_pi) && in_array($_identify_from, $_need_pi_type))
  196. ) {
  197. /* 当前证件号没有绑定过玩家并且开启了第三方api校验才需要验证 */
  198. $_app_id = $app_id;
  199. if (empty($_app_id)) {
  200. $_app_id = $this->app_id;
  201. }
  202. if (empty($_app_id)) {
  203. $_app_id = $_mem_data['app_id'];
  204. }
  205. try {
  206. $_identify_conf = \huoIdentify\controller\IdentifyDriverConf::getConfig($_identify_from, $_app_id);
  207. $_identify_class = \huoIdentify\identifyDriver\Identify::ins()->get($_identify_from, $_identify_conf);
  208. $_identify_class->setRealName($real_name);
  209. $_identify_class->setIdCard($id_card);
  210. $_identify_class->setMemId($mem_id);
  211. $_identify_class->setIdentifyFrom($_identify_from);
  212. $_mg_mem_id = (new MemGameModel())->getDefaultMgIdByMemAppId($mem_id, $_app_id);
  213. $_identify_class->setMgMemId($_mg_mem_id);
  214. $_classify = (new \huoIdentify\model\GameModel())->getClassifyById($_app_id);
  215. $_pf = GameConst::GAME_IOS == $_classify ? 2 : 1; /* 1 安卓 2 ios*/
  216. $_identify_class->setPf($_pf);
  217. $_identify_rs = $_identify_class->identify();
  218. /* 玩家实名认证中可放行 */
  219. if (CommonStatus::MEM_IDENTIFY_IN_PROGRESS == $_identify_rs['code']) {
  220. return $this->retSucMsg(IdentifyStatus::NO_ERROR);
  221. }
  222. if (CommonStatus::NO_ERROR != $_identify_rs['code']) {
  223. $_identify_rs['msg'] = '认证失败,请确认身份信息是否正确';
  224. return $this->huoReturn($_identify_rs);
  225. }
  226. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  227. } catch (Exception $e) {
  228. return $this->huoError($e->getCode(), $e->getMessage());
  229. };
  230. } else {
  231. $_identify_from = get_val($_pi_info, 'identify_from', '');
  232. $_identify_pi = get_val($_pi_info, 'identify_pi', '');
  233. }
  234. $_old_id_data = $_im_model->getInfoByMemId($mem_id);
  235. $_old_id_card = get_val($_old_id_data, 'id_card', '');
  236. $_data = [
  237. 'identify_from' => $_identify_from,
  238. 'identify_pi' => $_identify_pi,
  239. 'mem_id' => $mem_id,
  240. 'real_name' => $real_name,
  241. 'id_card' => $id_card,
  242. 'identify_type' => $type,
  243. ];
  244. if (empty($_old_id_data)) {
  245. $_rs = $_im_model->addData($_data);
  246. } else {
  247. $_rs = $_im_model->updateData($_data, $_old_id_data['id']);
  248. }
  249. if (false === $_rs) {
  250. return $this->huoSuccess(MemberStatus::INNER_ERROR, MemberStatus::getMsg(MemberStatus::INNER_ERROR));
  251. }
  252. $this->updatePiInfo($id_card, $real_name, $_identify_pi, $_identify_from);
  253. $_mem_data = [
  254. 'status' => MemConst::STATUS_NORMAL
  255. ];
  256. $_mem_data['real_name'] = $real_name;
  257. $_mem_data['id_card'] = $id_card;
  258. $_mem_data['identify_type'] = $type;
  259. $_mem_cache->updateMem($mem_id, $_mem_data);
  260. /* 实名认证成功获取积分 */
  261. if (empty($_old_id_card)) {
  262. (new MemIa($mem_id))->doItgAct(IaConst::IA_IDENTIFY);
  263. }
  264. $_data = $_im_model->getInfoByMemId($mem_id);
  265. return $this->retSucMsg(IdentifyStatus::NO_ERROR, $_data);
  266. }
  267. /**
  268. * 获取游戏实名认证信息
  269. *
  270. * @param int $app_id 游戏id
  271. * @param int $mem_id 会员id
  272. * @param string $device_type 设备类型
  273. *
  274. * @return array
  275. */
  276. public function getAuthInfo($app_id, $mem_id, $device_type = DeviceTypeConst::DEVICE_TYPE_MP) {
  277. /*定义返回数组*/
  278. $_data = [];
  279. $_is_auth = $this->getGameAuthByAppId($app_id);
  280. if (GameConst::GAME_IDENTIFY_IS_NO == $_is_auth) {
  281. return $_data;
  282. }
  283. $_data['auth_info'] = $this->getIdentifyByMemId($mem_id);
  284. $_data['url'] = '';
  285. if (IdentifyConst::IS_AUTH_NO == $_data['auth_info']['is_auth']) {
  286. $_param = [
  287. 'token' => session_id(),
  288. 'app_id' => $app_id,
  289. 'device_type' => $device_type,
  290. ];
  291. $_data['url'] = MemConst::getIdentifyUrl($_param);
  292. }
  293. return $_data;
  294. }
  295. /**
  296. * 上报登录行为
  297. *
  298. * @param $app_id
  299. * @param $mem_id
  300. *
  301. * @return array|bool
  302. */
  303. public function reportLoginBehavior($app_id, $mem_id) {
  304. return $this->reportCommon($app_id, $mem_id, CommonConst::STATUS_YES);
  305. }
  306. /**
  307. * 上报退出登录行为
  308. *
  309. * @param $app_id
  310. * @param $mem_id
  311. *
  312. * @return array|bool
  313. */
  314. public function reportLogoutBehavior($app_id, $mem_id) {
  315. return $this->reportCommon($app_id, $mem_id, CommonConst::STATUS_NO);
  316. }
  317. protected function reportCommon($app_id, $mem_id, $is_login = CommonConst::STATUS_YES) {
  318. $_identify_mem_info = (new IdentifyMemModel())->getInfoByMemId($mem_id);
  319. if (!empty($_identify_mem_info['id_card'])) {
  320. $_identify_from = Config::get('identify_conf.default');
  321. if (!empty($app_id)) {
  322. $_identify_game_model = new IdentifyGameModel();
  323. $_identify_game_info = (array)$_identify_game_model->getInfoByAppId($app_id);
  324. if (!empty($_identify_game_info['driver_key'])) {
  325. $_identify_from = $_identify_game_info['driver_key'];
  326. Config::set('identify_conf.default', $_identify_game_info['driver_key']);
  327. }
  328. }
  329. /* 需要pi的类型 */
  330. $_need_pi_type = IdentifyConst::getNeedPiTypes();
  331. try {
  332. $_identify_conf = \huoIdentify\controller\IdentifyDriverConf::getConfig($_identify_from, $app_id);
  333. $_id_card = isset($_identify_mem_info['id_card']) ? $_identify_mem_info['id_card'] : '';
  334. $_real_name = isset($_identify_mem_info['real_name']) ? $_identify_mem_info['real_name'] : '';
  335. $_identify_pi = isset($_identify_mem_info['identify_pi']) ? $_identify_mem_info['identify_pi'] : '';
  336. if (empty($_identify_pi) && !empty($_id_card) && !empty($_real_name)
  337. && in_array($_identify_from, $_need_pi_type)
  338. ) {
  339. /* 游戏开启国家防沉迷认证但是玩家没有在国家防沉迷系统认证的需要前往认证 */
  340. $_rs = $this->updateIdentify($mem_id, 1, $_real_name, $_id_card, $app_id);
  341. if (IdentifyStatus::NO_ERROR == $_rs['code']) {
  342. $_identify_pi = get_val($_rs['data'], 'identify_pi', '');
  343. }
  344. }
  345. $_identify_class = \huoIdentify\identifyDriver\Identify::ins()->get($_identify_from, $_identify_conf);
  346. $_identify_class->setRealName($_real_name);
  347. $_identify_class->setIdCard($_id_card);
  348. $_identify_class->setMemId($mem_id);
  349. $_identify_class->setIdentifyFrom($_identify_from);
  350. $_mg_mem_id = (new MemGameModel())->getDefaultMgIdByMemAppId($mem_id, $app_id);
  351. $_identify_class->setMgMemId($_mg_mem_id);
  352. $_classify = (new \huoIdentify\model\GameModel())->getClassifyById($app_id);
  353. $_pf = GameConst::GAME_IOS == $_classify ? 2 : 1; /* 1 安卓 2 ios*/
  354. $_identify_class->setPf($_pf);
  355. $_identify_class->setPi($_identify_pi);
  356. if ($is_login == CommonConst::STATUS_YES) {
  357. $_identify_rs = $_identify_class->loginBehavior($_identify_pi);
  358. } else {
  359. $_identify_rs = $_identify_class->logoutBehavior($_identify_pi);
  360. }
  361. if (CommonStatus::NO_ERROR != $_identify_rs['code']) {
  362. $_identify_rs['msg'] = '上报失败';
  363. return $this->huoReturn($_identify_rs);
  364. }
  365. } catch (exception $e) {
  366. return $this->huoError($e->getCode(), $e->getMessage());
  367. }
  368. }
  369. $_code = IdentifyStatus::NO_ERROR;
  370. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  371. }
  372. /**
  373. * 查询是否有认证中数据
  374. *
  375. * @param $mem_id
  376. * @param $app_id
  377. */
  378. protected function checkMemIdentify($mem_id, $app_id) {
  379. /* 限制速率 */
  380. $_rs = (new RateLimit())->rateLimit($mem_id, true, true, 1, 3);
  381. if (false == $_rs) {
  382. return false;
  383. }
  384. $_identify_from = Config::get('identify_conf.default');
  385. switch ($_identify_from) {
  386. case IdentifyConst::DRIVER_KEY_ALIPAY:
  387. case IdentifyConst::DRIVER_KEY_WXPAY:
  388. return false;
  389. case IdentifyConst::DRIVER_KEY_FCMGAME:
  390. try {
  391. $_identify_conf = \huoIdentify\controller\IdentifyDriverConf::getConfig($_identify_from, $app_id);
  392. } catch (exception $e) {
  393. return false;
  394. }
  395. $_in_progress_data = $this->getFcmGameInProgressData($mem_id, $_identify_conf['bizId']);
  396. break;
  397. default:
  398. $_in_progress_data = [];
  399. $_class_name = '\\huoIdentify\\identifyDriver\\driver\\'.ucfirst(strtolower($_identify_from));
  400. if (class_exists($_class_name)) {
  401. $_status_in_progress = $_class_name::STATUS_IN_PROGRESS; /* 获取各实名驱动类的认证中状态值 */
  402. $_in_progress_data = $this->getInProgressDataByMemAppFrom(
  403. $mem_id, $app_id, $_identify_from, $_status_in_progress
  404. );
  405. }
  406. }
  407. if (empty($_in_progress_data)) {
  408. return false;
  409. }
  410. $_real_name = get_val($_in_progress_data, 'real_name', '');
  411. $_id_card = get_val($_in_progress_data, 'id_card', '');
  412. $_rs = $this->updateIdentify($mem_id, 1, $_real_name, $_id_card, $app_id);
  413. if (IdentifyStatus::NO_ERROR == $_rs['code']) {
  414. return true;
  415. }
  416. return false;
  417. }
  418. /**
  419. * 获取国家防沉迷系统认证中数据
  420. *
  421. * @param $mem_id
  422. * @param $biz_id
  423. *
  424. * @return array
  425. */
  426. public function getFcmGameInProgressData($mem_id, $biz_id) {
  427. $_io_data = (new IdentifyOrderModel())->getLastInfoByBizIdMem($biz_id, $mem_id);
  428. if (empty($_io_data)) {
  429. return [];
  430. }
  431. $_status = get_val($_io_data, 'status', 2);
  432. if (\huoIdentify\identifyDriver\driver\Fcmgame::STATUS_IN_PROGRESS != $_status) {
  433. /* 非认证中不处理 */
  434. return [];
  435. }
  436. return $_io_data;
  437. }
  438. /**
  439. * 获取实名认证中的数据
  440. *
  441. * @param int $mem_id 玩家id
  442. * @param int $app_id 游戏id
  443. * @param string $identify_from 认证平台
  444. * @param int $status_in_progress 认证中状态
  445. *
  446. * @return array
  447. */
  448. private function getInProgressDataByMemAppFrom($mem_id, $app_id, $identify_from, $status_in_progress) {
  449. $_mg_mem_id = (new MemGameModel())->getDefaultMgIdByMemAppId($mem_id, $app_id);
  450. if (empty($_mg_mem_id)) {
  451. return [];
  452. }
  453. $_ipl_data = (new IdentifyPlatformLogModel())->getInfoByMgFrom($_mg_mem_id, $identify_from);
  454. if (empty($_ipl_data)) {
  455. return [];
  456. }
  457. $_status = get_val($_ipl_data, 'status', 0);
  458. if ($status_in_progress != $_status) {
  459. /* 非认证中不处理 */
  460. return [];
  461. }
  462. return $_ipl_data;
  463. }
  464. /**
  465. * 根据身份证号获取pi信息
  466. *
  467. * @param string $id_card 身份证号
  468. *
  469. * @return array
  470. */
  471. public function getPiInfoByIdCard($id_card) {
  472. $_pi_info = (new IdentifyPiModel())->getInfoByIdCard($id_card);
  473. // if (empty($_pi_info)) {
  474. // /* 此处兼容旧数据查找的一种方案可注释,也可将旧数据查找后写入信数据表中后注释 */
  475. // $_pi_info = (new IdentifyMemModel())->getInfoByIdCard($id_card);
  476. // }
  477. return $_pi_info;
  478. }
  479. /**
  480. * 更新PI信息
  481. *
  482. * @param string $id_card 身份证号
  483. * @param string $real_name 真实姓名
  484. * @param string $identify_pi 中宣部PI
  485. * @param string $identify_from 认证来源
  486. *
  487. * @return bool|false|int
  488. */
  489. public function updatePiInfo($id_card, $real_name, $identify_pi, $identify_from) {
  490. if (empty($id_card) || empty($real_name) || empty($identify_pi)) {
  491. return false;
  492. }
  493. $_ip_model = new IdentifyPiModel();
  494. $_pi_info = $_ip_model->getInfoByIdCard($id_card);
  495. $_rs = true;
  496. if (empty($_pi_info)) {
  497. $_data = [
  498. 'id_card' => $id_card,
  499. 'real_name' => $real_name,
  500. 'identify_pi' => $identify_pi,
  501. 'identify_from' => $identify_from,
  502. ];
  503. $_rs = $_ip_model->addData($_data);
  504. }
  505. return $_rs;
  506. }
  507. }