Guopan.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Guopan.php UTF-8
  4. * 果盘实名认证
  5. *
  6. * @date : 2021-03-10 10:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK-IDENTITY 1.0
  11. */
  12. namespace huoIdentify\identifyDriver\driver;
  13. use huoIdentify\identifyDriver\Driver;
  14. use huoIdentify\model\IdentifyMemModel;
  15. use huoIdentify\model\IdentifyPlatformLogModel;
  16. use huolib\constant\IdentifyConst;
  17. use huolib\status\CommonStatus;
  18. use huolib\status\IdentifyStatus;
  19. use huolib\tool\RateLimit;
  20. use think\Config;
  21. class Guopan extends Driver {
  22. const STATUS_IN_PROGRESS = 1;//认证中
  23. const STATUS_SUCCESS = 2;//认证成功
  24. const STATUS_FAIL = 3;//认证失败
  25. protected $app_id; /* 当前游戏id,系统的游戏id 本系统 */
  26. protected $channel_code; /* 果盘分配给该游戏对应的应用编号 果盘提供 */
  27. protected $server_key; /* 签名key 果盘提供*/
  28. protected $require_url; /* 认证URL 果盘提供 */
  29. /**
  30. * @param array $config
  31. */
  32. public function __construct($config = []) {
  33. $_config = $config;
  34. if (empty($_config)) {
  35. $_config = (array)Config::get('identify_conf.guopan');
  36. }
  37. parent::__construct($_config);
  38. // 设置属性
  39. $this->app_id = get_val($_config, 'app_id', 0);
  40. $this->channel_code = get_val($_config, 'channel_code', '');
  41. $this->server_key = get_val($_config, 'server_key', '');
  42. $this->require_url = get_val($_config, 'require_url', '');
  43. }
  44. /**
  45. * 实名认证
  46. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  47. */
  48. public function identify() {
  49. $_results = $this->check();
  50. $_results_arr = json_decode($_results, true);
  51. if (empty($_results_arr) || !isset($_results_arr['code']) || '0' != $_results_arr['code']) {
  52. $_result = [$_results, $_results_arr];
  53. $_step = 0;
  54. $_other = '果盘防沉迷实名认证API,认证异常';
  55. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  56. $_code = CommonStatus::INNER_ERROR;
  57. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  58. return $this->huoError($_code, $_results_arr['msg']);
  59. }
  60. /* 认证失败 */
  61. if ($_results_arr['verify_status'] == self::STATUS_FAIL) {
  62. $_result = $_results_arr;
  63. $_step = 0;
  64. $_other = '果盘防沉迷实名认证API,认证失败';
  65. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  66. $_code = IdentifyStatus::IDENTITY_FAIL;
  67. return $this->huoError($_code, $_other);
  68. }
  69. /**
  70. * 认证成功需要添加记录
  71. */
  72. $_ipl_model = new IdentifyPlatformLogModel();
  73. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_GUOPAN);
  74. $_log_data = [
  75. 'mem_id' => $this->mem_id,
  76. 'mg_mem_id' => $this->mg_mem_id,
  77. 'app_id' => $this->app_id,
  78. 'identify_from' => IdentifyConst::DRIVER_KEY_GUOPAN,
  79. 'real_name' => $this->real_name,
  80. 'id_card' => $this->id_card,
  81. 'identify_pi' => $_results_arr['pi'],
  82. 'status' => $_results_arr['verify_status'],
  83. ];
  84. if (empty($_ipl_info)) {
  85. $_ipl_model->addData($_log_data);
  86. } else {
  87. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  88. }
  89. /* 认证中 */
  90. if ($_results_arr['verify_status'] == self::STATUS_IN_PROGRESS) {
  91. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  92. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  93. }
  94. $_code = CommonStatus::NO_ERROR;
  95. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr);
  96. }
  97. /**
  98. * 查询是否已认证
  99. *
  100. * @return bool
  101. */
  102. private function checkIsVerified() {
  103. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_GUOPAN);
  104. return empty($_pi) ? false : true;
  105. }
  106. public function loginBehavior($identify_pi) {
  107. $_check_rs = $this->checkIsVerified();
  108. if (true === $_check_rs) {
  109. /* 已认证无需处理 */
  110. $_code = CommonStatus::NO_ERROR;
  111. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  112. }
  113. /* 限制速率 */
  114. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  115. if (false == $_rs) {
  116. return false;
  117. }
  118. $_identify_rs = $this->identify();
  119. if (CommonStatus::NO_ERROR == $_identify_rs['code']) {
  120. $_im_model = new IdentifyMemModel();
  121. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  122. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  123. if (empty($_old_id_data)) {
  124. $_data = [
  125. 'identify_from' => IdentifyConst::DRIVER_KEY_GUOPAN,
  126. 'identify_pi' => $_identify_pi,
  127. 'mem_id' => $this->mem_id,
  128. 'real_name' => $this->real_name,
  129. 'id_card' => $this->id_card
  130. ];
  131. $_im_model->addData($_data);
  132. } elseif (empty($_old_id_data['identify_pi'])) {
  133. $_update_data = [
  134. 'identify_pi' => $_identify_pi
  135. ];
  136. $_im_model->updateData($_update_data, $_old_id_data['id']);
  137. }
  138. }
  139. $_code = CommonStatus::NO_ERROR;
  140. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  141. }
  142. /**
  143. * check the name and idNum
  144. *
  145. * @return string
  146. */
  147. public function check() {
  148. $_uri = $this->require_url;
  149. $_headers = ['Content-Type' => 'application/json; charset=utf-8'];
  150. if (!empty($this->headers)) {
  151. $_headers = array_merge($_headers, $this->headers);
  152. }
  153. $_time = time();
  154. $_params = array(
  155. 'appid' => $this->channel_code,
  156. 'real_name' => $this->real_name,
  157. 'id_card' => $this->id_card,
  158. 't' => $_time,
  159. 'game_uin' => $this->mg_mem_id,
  160. );
  161. $_params['sign'] = md5(
  162. $this->channel_code.$this->real_name.$this->id_card.$_time.$this->mg_mem_id.$this->server_key
  163. );
  164. return $this->doRequest($_params, $_uri, 'GET', $_headers);
  165. }
  166. /**
  167. * do request
  168. *
  169. * @param $params
  170. * @param string $url
  171. * @param string $method
  172. * @param array $headers
  173. *
  174. * @return mixed
  175. */
  176. private function doRequest($params, $url, $method, $headers = []) {
  177. if (count($params) > 0) {
  178. $url .= '?'.http_build_query($params);
  179. }
  180. $ch = curl_init();
  181. curl_setopt($ch, CURLOPT_URL, $url);
  182. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  183. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  184. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  185. curl_setopt(
  186. $ch, CURLOPT_HTTPHEADER, array_map(
  187. function ($v, $k) {
  188. return $k.': '.$v;
  189. }, array_values($headers), array_keys($headers)
  190. )
  191. );
  192. if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
  193. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  194. }
  195. $_rs_json = curl_exec($ch);
  196. curl_close($ch);
  197. return $_rs_json;
  198. }
  199. }