Yixin.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Yixin.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. class Yixin extends Driver {
  22. const STATUS_SUCCESS = 1;//认证成功
  23. const STATUS_IN_PROGRESS = 2;//认证中
  24. const STATUS_FAIL = 3;//认证失败
  25. /**
  26. * @var array
  27. */
  28. protected $headers;
  29. /**
  30. * @var string
  31. */
  32. protected $key;
  33. /**
  34. * @var int $timeout
  35. */
  36. protected $timeout = 10;
  37. /**
  38. * @var Client
  39. */
  40. protected $http;
  41. protected $app_id; /* 当前游戏id */
  42. protected $channel_code; /* 渠道code 易信提供 */
  43. protected $require_url; /* 认证URL 易信提供 */
  44. /**
  45. * @param array $config
  46. */
  47. public function __construct($config = []) {
  48. $_config = $config;
  49. parent::__construct($_config);
  50. // 设置属性
  51. $this->app_id = get_val($_config, 'app_id', 0);
  52. $this->channel_code = get_val($_config, 'channel_code', '');
  53. $this->require_url = get_val($_config, 'require_url', '');
  54. }
  55. /**
  56. * 实名认证
  57. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  58. */
  59. public function identify() {
  60. $_results = $this->check();
  61. $_results_arr = json_decode($_results, true);
  62. if (empty($_results_arr) || !isset($_results_arr['code']) || '200' != $_results_arr['code']) {
  63. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  64. $_step = 0;
  65. $_other = '易信防沉迷实名认证API,认证异常';
  66. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  67. $_code = CommonStatus::INNER_ERROR;
  68. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  69. return $this->huoError($_code, $_results_arr['msg']);
  70. }
  71. /* 认证失败 */
  72. if ($_results_arr['result']['status'] == self::STATUS_FAIL) {
  73. $_result = $_results_arr;
  74. $_step = 0;
  75. $_other = '易信防沉迷实名认证API,认证失败';
  76. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  77. $_code = IdentifyStatus::IDENTITY_FAIL;
  78. return $this->huoError($_code, $_other);
  79. }
  80. /**
  81. * 认证成功需要添加记录
  82. */
  83. $_ipl_model = new IdentifyPlatformLogModel();
  84. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_YIXIN);
  85. $_log_data = [
  86. 'mem_id' => $this->mem_id,
  87. 'mg_mem_id' => $this->mg_mem_id,
  88. 'app_id' => $this->app_id,
  89. 'identify_from' => IdentifyConst::DRIVER_KEY_YIXIN,
  90. 'real_name' => $this->real_name,
  91. 'id_card' => $this->id_card,
  92. 'identify_pi' => $_results_arr['result']['pi'],
  93. 'status' => $_results_arr['result']['status'],
  94. ];
  95. if (empty($_ipl_info)) {
  96. $_ipl_model->addData($_log_data);
  97. } else {
  98. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  99. }
  100. /* 认证中 */
  101. if ($_results_arr['result']['status'] == self::STATUS_IN_PROGRESS) {
  102. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  103. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  104. }
  105. $_code = CommonStatus::NO_ERROR;
  106. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['result']);
  107. }
  108. /**
  109. * 查询是否已认证
  110. *
  111. * @return bool
  112. */
  113. private function checkIsVerified() {
  114. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_YIXIN);
  115. return empty($_pi) ? false : true;
  116. }
  117. public function loginBehavior($identify_pi) {
  118. $_check_rs = $this->checkIsVerified();
  119. if (true === $_check_rs) {
  120. /* 已认证无需处理 */
  121. $_code = CommonStatus::NO_ERROR;
  122. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  123. }
  124. /* 限制请求 */
  125. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  126. if (false == $_rs) {
  127. $_code = CommonStatus::NO_ERROR;
  128. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  129. }
  130. $_identify_rs = $this->identify();
  131. if (CommonStatus::NO_ERROR == $_identify_rs['code']) {
  132. $_im_model = new IdentifyMemModel();
  133. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  134. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  135. if (empty($_old_id_data)) {
  136. $_data = [
  137. 'identify_from' => IdentifyConst::DRIVER_KEY_YIXIN,
  138. 'identify_pi' => $_identify_pi,
  139. 'mem_id' => $this->mem_id,
  140. 'real_name' => $this->real_name,
  141. 'id_card' => $this->id_card
  142. ];
  143. $_im_model->addData($_data);
  144. } elseif (empty($_old_id_data['identify_pi'])) {
  145. $_update_data = [
  146. 'identify_pi' => $_identify_pi
  147. ];
  148. $_im_model->updateData($_update_data, $_old_id_data['id']);
  149. }
  150. }
  151. $_code = CommonStatus::NO_ERROR;
  152. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  153. }
  154. /**
  155. * check the name and idNum
  156. *
  157. * @return string
  158. */
  159. public function check() {
  160. $_uri = $this->require_url;
  161. $_headers = ['Content-Type' => 'application/json; charset=utf-8'];
  162. if (!empty($this->headers)) {
  163. $_headers = array_merge($_headers, $this->headers);
  164. }
  165. $_params = array(
  166. 'uid' => md5($this->mg_mem_id),
  167. 'channelname' => $this->channel_code,
  168. 'gameid' => $this->app_id,
  169. 'realname' => $this->real_name,
  170. 'cardinfo' => $this->id_card,
  171. 'timestamp' => self::getMillisecond(),
  172. );
  173. $_params['sign'] = md5(
  174. $_params['uid'].$_params['channelname'].$_params['gameid'].$_params['timestamp']
  175. );
  176. return $this->doRequest($_params, $_uri, 'POST', $_headers);
  177. }
  178. /**
  179. * 获取毫秒级别的时间戳
  180. */
  181. private static function getMillisecond() {
  182. //获取毫秒的时间戳
  183. $time = explode(" ", microtime());
  184. $time = $time[1].($time[0] * 1000);
  185. $time2 = explode(".", $time);
  186. $time = $time2[0];
  187. return $time;
  188. }
  189. /**
  190. * do request
  191. *
  192. * @param $params
  193. * @param string $url
  194. * @param string $method
  195. * @param array $headers
  196. *
  197. * @return mixed
  198. */
  199. private function doRequest($params, $url, $method, $headers = []) {
  200. if (count($params) > 0) {
  201. $url .= '?'.http_build_query($params);
  202. }
  203. $ch = curl_init();
  204. curl_setopt($ch, CURLOPT_URL, $url);
  205. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  206. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  207. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  208. curl_setopt(
  209. $ch, CURLOPT_HTTPHEADER, array_map(
  210. function ($v, $k) {
  211. return $k.': '.$v;
  212. }, array_values($headers), array_keys($headers)
  213. )
  214. );
  215. if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
  216. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  217. }
  218. $_rs_json = curl_exec($ch);
  219. curl_close($ch);
  220. return $_rs_json;
  221. }
  222. }