Liebao.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Liebao.php UTF-8
  4. * 猎宝实名认证接口
  5. *
  6. * @date : 2021/4/30 15:11
  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\controller\AesEcb;
  15. use huoIdentify\identifyDriver\Driver;
  16. use huoIdentify\model\IdentifyMemModel;
  17. use huoIdentify\model\IdentifyPlatformLogModel;
  18. use huolib\constant\IdentifyConst;
  19. use huolib\status\CommonStatus;
  20. use huolib\status\IdentifyStatus;
  21. use huolib\tool\RateLimit;
  22. class Liebao extends Driver {
  23. const STATUS_SUCCESS = 1;//认证成功
  24. const STATUS_IN_PROGRESS = 2;//认证中
  25. const STATUS_FAIL = 3;//认证失败
  26. /**
  27. * @var string
  28. */
  29. protected $cipher = "aes-128-ecb";
  30. /**
  31. * @var AesEcb
  32. */
  33. protected $aes;
  34. /**
  35. * @var array
  36. */
  37. protected $headers;
  38. /**
  39. * @var string
  40. */
  41. protected $key;
  42. protected $game_id;
  43. /**
  44. * @var int $timeout
  45. */
  46. protected $timeout = 3;
  47. /**
  48. * @var Client
  49. */
  50. protected $http;
  51. protected $app_id; /* 当前游戏id */
  52. protected $channel_code; /* 渠道code 猎宝提供 */
  53. protected $require_url; /* 认证URL 猎宝提供 */
  54. /**
  55. * @param array $config
  56. */
  57. public function __construct($config = []) {
  58. $_config = $config;
  59. parent::__construct($_config);
  60. // 设置属性
  61. $this->app_id = get_val($_config, 'app_id', 0);
  62. $this->channel_code = get_val($_config, 'channel_code', '');
  63. $this->require_url = get_val($_config, 'require_url', '');
  64. $this->game_id = get_val($_config, 'game_id', 0);
  65. $_key = $this->channel_code;
  66. $this->aes = new AesEcb($_key, $this->cipher);
  67. if (!class_exists('\\GuzzleHttp\\Client')) {
  68. // 使用已有的GuzzleHttp
  69. require_once 'wxpay/TCloudAutoLoader.php';
  70. }
  71. $this->http = new Client();
  72. }
  73. /**
  74. * 实名认证
  75. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  76. */
  77. public function identify() {
  78. $_results = $this->check($this->real_name, $this->id_card);
  79. $_results_arr = json_decode($_results, true);
  80. if (empty($_results_arr) || !isset($_results_arr['code']) || '0' != $_results_arr['code']) {
  81. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  82. $_step = 0;
  83. $_other = '猎宝防沉迷实名认证API,认证异常';
  84. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  85. $_code = CommonStatus::INNER_ERROR;
  86. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  87. return $this->huoError($_code, $_results_arr['msg']);
  88. }
  89. /* 认证失败 */
  90. if ($_results_arr['data']['auth_state'] == self::STATUS_FAIL) {
  91. $_result = $_results_arr;
  92. $_step = 0;
  93. $_other = '猎宝防沉迷实名认证API,认证失败';
  94. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  95. $_code = IdentifyStatus::IDENTITY_FAIL;
  96. return $this->huoError($_code, $_other);
  97. }
  98. /**
  99. * 认证成功需要添加记录
  100. */
  101. $_ipl_model = new IdentifyPlatformLogModel();
  102. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_LIEBAO);
  103. $_log_data = [
  104. 'mem_id' => $this->mem_id,
  105. 'mg_mem_id' => $this->mg_mem_id,
  106. 'app_id' => $this->app_id,
  107. 'identify_from' => IdentifyConst::DRIVER_KEY_LIEBAO,
  108. 'real_name' => $this->real_name,
  109. 'id_card' => $this->id_card,
  110. 'identify_pi' => $_results_arr['data']['pi'],
  111. 'status' => $_results_arr['data']['auth_state'],
  112. ];
  113. if (empty($_ipl_info)) {
  114. $_ipl_model->addData($_log_data);
  115. } else {
  116. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  117. }
  118. /* 认证中 */
  119. if ($_results_arr['data']['auth_state'] == self::STATUS_IN_PROGRESS) {
  120. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  121. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  122. }
  123. $_code = CommonStatus::NO_ERROR;
  124. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  125. }
  126. /**
  127. * 查询是否已认证
  128. *
  129. * @return bool
  130. */
  131. private function checkIsVerified() {
  132. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_LIEBAO);
  133. return empty($_pi) ? false : true;
  134. }
  135. public function loginBehavior($identify_pi) {
  136. $_check_rs = $this->checkIsVerified();
  137. if (true === $_check_rs) {
  138. /* 已认证无需处理 */
  139. $_code = CommonStatus::NO_ERROR;
  140. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  141. }
  142. /* 限制请求 */
  143. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  144. if (false == $_rs) {
  145. $_code = CommonStatus::NO_ERROR;
  146. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  147. }
  148. $_identify_rs = $this->identify();
  149. if (CommonStatus::NO_ERROR == $_identify_rs['code']) {
  150. $_im_model = new IdentifyMemModel();
  151. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  152. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  153. if (empty($_old_id_data)) {
  154. $_data = [
  155. 'identify_from' => IdentifyConst::DRIVER_KEY_LIEBAO,
  156. 'identify_pi' => $_identify_pi,
  157. 'mem_id' => $this->mem_id,
  158. 'real_name' => $this->real_name,
  159. 'id_card' => $this->id_card
  160. ];
  161. $_im_model->addData($_data);
  162. } elseif (empty($_old_id_data['identify_pi'])) {
  163. $_update_data = [
  164. 'identify_pi' => $_identify_pi
  165. ];
  166. $_im_model->updateData($_update_data, $_old_id_data['id']);
  167. }
  168. }
  169. $_code = CommonStatus::NO_ERROR;
  170. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  171. }
  172. /**
  173. * check the name and idNum
  174. *
  175. * @param string $name
  176. * @param string $idNum
  177. *
  178. * @return string
  179. */
  180. public function check($name, $idNum) {
  181. $_uri = $this->require_url;
  182. $_headers = ['Content-Type' => 'application/json'];
  183. if (!empty($this->headers)) {
  184. $_headers = array_merge($_headers, $this->headers);
  185. }
  186. $_uid = $this->mg_mem_id;
  187. $_body = ['id_name' => $name, 'id_no' => $idNum, 'uid' => $_uid, 'platform' => $this->getPf(),
  188. 'gameId' => $this->game_id];
  189. return $this->doRequest('POST', $_uri, $_headers, $_body);
  190. }
  191. /**
  192. * do request
  193. *
  194. * @param string $method
  195. * @param string $uri
  196. * @param array $headers
  197. * @param array $body
  198. * @param array $options
  199. *
  200. * @return string
  201. */
  202. private function doRequest($method, $uri, array $headers = [], array $body = [], array $options = []) {
  203. $raw = json_encode($body, JSON_UNESCAPED_UNICODE);
  204. $encryptParameter = $this->aes->encrypt($raw);
  205. $body = ['encryptParameter' => $encryptParameter, 'timestamp' => time()];
  206. $options = array_merge(
  207. ['headers' => $headers, 'body' => json_encode($body), 'timeout' => $this->timeout], $options
  208. );
  209. $response = $this->http->request($method, $uri, $options);
  210. return $response->getBody()->getContents();
  211. }
  212. }