Huounion.php 7.8 KB

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