Huanjuyou.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Huanjuyou.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\IdentifyStatus;
  18. use huolib\tool\Http;
  19. use huolib\tool\RateLimit;
  20. use think\Config;
  21. class Huanjuyou extends Driver {
  22. const STATUS_IN_PROGRESS = 0;//认证中
  23. const STATUS_SUCCESS = 1;//认证成功
  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.huanjuyou');
  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. */
  47. public function identify() {
  48. $_results = $this->check();
  49. $_results_arr = json_decode($_results, true);
  50. if (empty($_results_arr) || !isset($_results_arr['status']) || '1' != $_results_arr['status']) {
  51. $_result = [$_results, $_results_arr];
  52. $_step = 0;
  53. $_other = '欢聚游防沉迷实名认证API,认证异常';
  54. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  55. $_code = IdentifyStatus::INNER_ERROR;
  56. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  57. return $this->huoError($_code, $_results_arr['msg']);
  58. }
  59. /**
  60. * 认证成功需要添加记录
  61. */
  62. $_ipl_model = new IdentifyPlatformLogModel();
  63. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_HUANJUYOU);
  64. $_log_data = [
  65. 'mem_id' => $this->mem_id,
  66. 'mg_mem_id' => $this->mg_mem_id,
  67. 'app_id' => $this->app_id,
  68. 'identify_from' => IdentifyConst::DRIVER_KEY_HUANJUYOU,
  69. 'real_name' => $this->real_name,
  70. 'id_card' => $this->id_card,
  71. 'identify_pi' => get_val($_results_arr, 'pi', ''),
  72. 'status' => $_results_arr['status'],
  73. ];
  74. if (empty($_ipl_info)) {
  75. $_ipl_model->addData($_log_data);
  76. } else {
  77. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  78. }
  79. $_code = IdentifyStatus::NO_ERROR;
  80. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code), $_results_arr);
  81. }
  82. /**
  83. * 查询是否已认证
  84. *
  85. * @return bool
  86. */
  87. private function checkIsVerified() {
  88. $_info = (new IdentifyPlatformLogModel())->getInfoByMgFrom(
  89. $this->mg_mem_id, IdentifyConst::DRIVER_KEY_HUANJUYOU
  90. );
  91. return empty($_info) ? false : true;
  92. }
  93. public function loginBehavior($identify_pi) {
  94. $_check_rs = $this->checkIsVerified();
  95. if (true === $_check_rs) {
  96. /* 已认证无需处理 */
  97. $_code = IdentifyStatus::NO_ERROR;
  98. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code));
  99. }
  100. /* 限制速率 */
  101. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  102. if (false == $_rs) {
  103. return false;
  104. }
  105. $_identify_rs = $this->identify();
  106. if (IdentifyStatus::NO_ERROR == $_identify_rs['code']) {
  107. $_im_model = new IdentifyMemModel();
  108. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  109. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  110. if (empty($_old_id_data)) {
  111. $_data = [
  112. 'identify_from' => IdentifyConst::DRIVER_KEY_HUANJUYOU,
  113. 'identify_pi' => $_identify_pi,
  114. 'mem_id' => $this->mem_id,
  115. 'real_name' => $this->real_name,
  116. 'id_card' => $this->id_card
  117. ];
  118. $_im_model->addData($_data);
  119. } elseif (empty($_old_id_data['identify_pi']) && !empty($_identify_pi)) {
  120. $_update_data = [
  121. 'identify_pi' => $_identify_pi
  122. ];
  123. $_im_model->updateData($_update_data, $_old_id_data['id']);
  124. }
  125. }
  126. $_code = IdentifyStatus::NO_ERROR;
  127. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code));
  128. }
  129. /**
  130. * check the name and idNum
  131. *
  132. * @return string
  133. */
  134. public function check() {
  135. $_uri = $this->require_url;
  136. $_headers = ['Content-Type' => 'application/json; charset=utf-8'];
  137. if (!empty($this->headers)) {
  138. $_headers = array_merge($_headers, $this->headers);
  139. }
  140. $_params = array(
  141. 'uid' => $this->mg_mem_id,
  142. 'realName' => $this->real_name,
  143. 'identify' => $this->id_card,
  144. 'channelCode' => $this->channel_code
  145. );
  146. $_str = 'uid='.$this->mg_mem_id.'&realName='.$this->real_name.'&identify='.$this->id_card.'&channelCode='
  147. .$this->channel_code.'&key='.$this->server_key;
  148. $_params['sign'] = md5($_str);
  149. return $this->doRequest($_params, $_uri, $_headers);
  150. }
  151. /**
  152. * do request
  153. *
  154. * @param $params
  155. * @param string $url
  156. * @param array $headers
  157. *
  158. * @return mixed
  159. */
  160. private function doRequest($params, $url, $headers = []) {
  161. $_rs = Http::post($url, $params, ['header' => $headers]);
  162. return $_rs;
  163. }
  164. }