Qianxi.php 8.1 KB

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