Tianyu.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Tianyu.php UTF-8
  4. * 天宇实名认证接口
  5. *
  6. * @date : 2021/4/30 15:50
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK-IDENTITY 1.0
  11. */
  12. namespace huoIdentify\identifyDriver\driver;
  13. use GuzzleHttp\Client;
  14. use huoIdentify\identifyDriver\Driver;
  15. use huoIdentify\model\IdentifyMemModel;
  16. use huoIdentify\model\IdentifyPlatformLogModel;
  17. use huolib\constant\IdentifyConst;
  18. use huolib\status\CommonStatus;
  19. use huolib\status\IdentifyStatus;
  20. use huolib\tool\Http;
  21. use huolib\tool\RateLimit;
  22. use huolib\tool\StrUtils;
  23. class Tianyu extends Driver {
  24. const STATUS_SUCCESS = 0;//认证成功
  25. const STATUS_IN_PROGRESS = 1;//认证中
  26. const STATUS_FAIL = 2;//认证失败
  27. /**
  28. * @var array
  29. */
  30. protected $headers;
  31. /**
  32. * @var string
  33. */
  34. protected $key;
  35. /**
  36. * @var int $timeout
  37. */
  38. protected $timeout = 3;
  39. /**
  40. * @var Client
  41. */
  42. protected $http;
  43. protected $app_id; /* 当前游戏id */
  44. protected $channel_code; /* 渠道code 天宇提供 */
  45. protected $require_url; /* 认证URL 天宇提供 */
  46. /**
  47. * @param array $config
  48. */
  49. public function __construct($config = []) {
  50. $_config = $config;
  51. parent::__construct($_config);
  52. // 设置属性
  53. $this->app_id = get_val($_config, 'app_id', 0);
  54. $this->channel_code = get_val($_config, 'channel_code', '');
  55. $this->require_url = get_val($_config, 'require_url', '');
  56. $this->key = get_val($_config, 'app_key', '');
  57. }
  58. /**
  59. * 实名认证
  60. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  61. */
  62. public function identify() {
  63. $_results = $this->check();
  64. $_results_arr = json_decode($_results, true);
  65. if (empty($_results_arr) || !isset($_results_arr['code']) || '1' != $_results_arr['code']) {
  66. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  67. $_step = 0;
  68. $_other = '天宇防沉迷实名认证API,认证异常';
  69. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  70. $_code = CommonStatus::INNER_ERROR;
  71. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  72. return $this->huoError($_code, $_results_arr['msg']);
  73. }
  74. /* 认证失败 */
  75. if ($_results_arr['data']['status'] == self::STATUS_FAIL) {
  76. $_result = $_results_arr;
  77. $_step = 0;
  78. $_other = '天宇防沉迷实名认证API,认证失败';
  79. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  80. $_code = IdentifyStatus::IDENTITY_FAIL;
  81. return $this->huoError($_code, $_other);
  82. }
  83. /**
  84. * 认证成功需要添加记录
  85. */
  86. $_ipl_model = new IdentifyPlatformLogModel();
  87. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_TIANYU);
  88. $_log_data = [
  89. 'mem_id' => $this->mem_id,
  90. 'mg_mem_id' => $this->mg_mem_id,
  91. 'app_id' => $this->app_id,
  92. 'identify_from' => IdentifyConst::DRIVER_KEY_TIANYU,
  93. 'real_name' => $this->real_name,
  94. 'id_card' => $this->id_card,
  95. 'identify_pi' => $_results_arr['data']['pi'],
  96. 'status' => $_results_arr['data']['status'],
  97. ];
  98. if (empty($_ipl_info)) {
  99. $_ipl_model->addData($_log_data);
  100. } else {
  101. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  102. }
  103. /* 认证中 */
  104. if ($_results_arr['data']['status'] == self::STATUS_IN_PROGRESS) {
  105. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  106. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  107. }
  108. $_code = CommonStatus::NO_ERROR;
  109. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  110. }
  111. /**
  112. * 查询是否已认证
  113. *
  114. * @return bool
  115. */
  116. private function checkIsVerified() {
  117. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_TIANYU);
  118. return empty($_pi) ? false : true;
  119. }
  120. public function loginBehavior($identify_pi) {
  121. $_check_rs = $this->checkIsVerified();
  122. if (true === $_check_rs) {
  123. /* 已认证无需处理 */
  124. $_code = CommonStatus::NO_ERROR;
  125. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  126. }
  127. /* 限制请求 */
  128. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  129. if (false == $_rs) {
  130. $_code = CommonStatus::NO_ERROR;
  131. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  132. }
  133. if (!empty($this->pi)) {
  134. /* 有pi无需上报 */
  135. $_identify_rs['code'] = CommonStatus::NO_ERROR;
  136. $_identify_pi = $this->pi;
  137. } else {
  138. $_identify_rs = $this->identify();
  139. if (CommonStatus::NO_ERROR != $_identify_rs['code']) {
  140. $_code = CommonStatus::NO_ERROR;
  141. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  142. }
  143. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  144. }
  145. $_im_model = new IdentifyMemModel();
  146. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  147. if (empty($_old_id_data)) {
  148. $_data = [
  149. 'identify_from' => IdentifyConst::DRIVER_KEY_TIANYU,
  150. 'identify_pi' => $_identify_pi,
  151. 'mem_id' => $this->mem_id,
  152. 'real_name' => $this->real_name,
  153. 'id_card' => $this->id_card
  154. ];
  155. $_im_model->addData($_data);
  156. } elseif (empty($_old_id_data['identify_pi'])) {
  157. $_update_data = [
  158. 'identify_pi' => $_identify_pi
  159. ];
  160. $_im_model->updateData($_update_data, $_old_id_data['id']);
  161. }
  162. $_code = CommonStatus::NO_ERROR;
  163. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  164. }
  165. /**
  166. * check the name and idNum
  167. *
  168. * @return string
  169. */
  170. public function check() {
  171. $_uri = $this->require_url;
  172. $_headers = ['Content-Type' => 'application/json; charset=utf-8'];
  173. if (!empty($this->headers)) {
  174. $_headers = array_merge($_headers, $this->headers);
  175. }
  176. $_params = array(
  177. 'game_id' => $this->app_id,
  178. 'agent_id' => $this->channel_code,
  179. 'agent_user_id' => $this->mg_mem_id,
  180. 'real_name' => $this->real_name,
  181. 'id_card' => $this->id_card,
  182. 'time' => time(),
  183. );
  184. $_params = StrUtils::argSort($_params);
  185. $_str = '';
  186. while (list($_key, $_val) = StrUtils::funcNewEach($_params)) {
  187. $_str .= $_key."=".$_val."&";
  188. }
  189. // 去掉最后一个&字符
  190. $_str = substr($_str, 0, strlen($_str) - 1);
  191. $_str = $_str.'&key='.$this->key;
  192. $_params['sign'] = md5($_str);
  193. return $this->doRequest($_params, $_uri, $_headers);
  194. }
  195. /**
  196. * do request
  197. *
  198. * @param $params
  199. * @param string $url
  200. * @param array $headers
  201. *
  202. * @return mixed
  203. */
  204. private function doRequest($params, $url, $headers = []) {
  205. $_rs = Http::post($url, $params, ['header' => $headers, 'timeout' => $this->timeout]);
  206. return $_rs;
  207. }
  208. }