Haiquyou.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Haiquyou.php UTF-8
  4. * 嗨趣游实名认证
  5. *
  6. * @date : 2021-03-10 10:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@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 Haiquyou extends Driver {
  22. const STATUS_UNKNOWN = 0;//未知
  23. const STATUS_IN_PROGRESS = 2;//认证中
  24. const STATUS_SUCCESS = 1;//认证成功
  25. const STATUS_FAIL = 3;//认证失败
  26. protected $app_id; /* 当前游戏id,系统的游戏id 本系统 */
  27. protected $channel_code; /* 嗨趣游分配给该游戏对应的应用编号 嗨趣游提供 */
  28. protected $game_id; /* CP游戏id 嗨趣游提供*/
  29. protected $require_url; /* 认证URL 嗨趣游提供 */
  30. /**
  31. * @param array $config
  32. */
  33. public function __construct($config = []) {
  34. $_config = $config;
  35. if (empty($_config)) {
  36. $_config = (array)Config::get('identify_conf.haiquyou');
  37. }
  38. parent::__construct($_config);
  39. // 设置属性
  40. $this->app_id = get_val($_config, 'app_id', 0);
  41. $this->channel_code = get_val($_config, 'channel_code', '');
  42. $this->game_id = get_val($_config, 'game_id', 0);
  43. $this->require_url = get_val($_config, 'require_url', '');
  44. }
  45. /**
  46. * 实名认证
  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']) || '1' != $_results_arr['code']) {
  52. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  53. $_step = 0;
  54. $_other = '嗨趣游防沉迷实名认证API,认证异常';
  55. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  56. $_code = IdentifyStatus::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['piStatus'] == 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. if (self::STATUS_UNKNOWN == $_results_arr['piStatus']) {
  70. $_results_arr['piStatus'] = self::STATUS_IN_PROGRESS;
  71. \think\Log::error(['嗨趣游未知认证状态', $_results, $_results_arr, $this->real_name, $this->id_card]);
  72. }
  73. /**
  74. * 认证成功需要添加记录
  75. */
  76. $_ipl_model = new IdentifyPlatformLogModel();
  77. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_HAIQUYOU);
  78. $_log_data = [
  79. 'mem_id' => $this->mem_id,
  80. 'mg_mem_id' => $this->mg_mem_id,
  81. 'app_id' => $this->app_id,
  82. 'identify_from' => IdentifyConst::DRIVER_KEY_HAIQUYOU,
  83. 'real_name' => $this->real_name,
  84. 'id_card' => $this->id_card,
  85. 'identify_pi' => $_results_arr['pi'],
  86. 'status' => $_results_arr['piStatus'],
  87. ];
  88. if (empty($_ipl_info)) {
  89. $_ipl_model->addData($_log_data);
  90. } else {
  91. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  92. }
  93. /* 认证中 */
  94. if ($_results_arr['piStatus'] == self::STATUS_IN_PROGRESS) {
  95. $_code = IdentifyStatus::MEM_IDENTIFY_IN_PROGRESS;
  96. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code));
  97. }
  98. $_code = IdentifyStatus::NO_ERROR;
  99. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code), $_results_arr['data']);
  100. }
  101. /**
  102. * 查询是否已认证
  103. *
  104. * @return bool
  105. */
  106. private function checkIsVerified() {
  107. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_HAIQUYOU);
  108. return empty($_pi) ? false : true;
  109. }
  110. public function loginBehavior($identify_pi) {
  111. $_check_rs = $this->checkIsVerified();
  112. if (true === $_check_rs) {
  113. /* 已认证无需处理 */
  114. $_code = IdentifyStatus::NO_ERROR;
  115. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code));
  116. }
  117. /* 限制速率 */
  118. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  119. if (false == $_rs) {
  120. return false;
  121. }
  122. $_identify_rs = $this->identify();
  123. if (IdentifyStatus::NO_ERROR == $_identify_rs['code']) {
  124. $_im_model = new IdentifyMemModel();
  125. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  126. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  127. if (empty($_old_id_data)) {
  128. $_data = [
  129. 'identify_from' => IdentifyConst::DRIVER_KEY_HAIQUYOU,
  130. 'identify_pi' => $_identify_pi,
  131. 'mem_id' => $this->mem_id,
  132. 'real_name' => $this->real_name,
  133. 'id_card' => $this->id_card
  134. ];
  135. $_im_model->addData($_data);
  136. } elseif (empty($_old_id_data['identify_pi']) && !empty($_identify_pi)) {
  137. $_update_data = [
  138. 'identify_pi' => $_identify_pi
  139. ];
  140. $_im_model->updateData($_update_data, $_old_id_data['id']);
  141. }
  142. }
  143. $_code = IdentifyStatus::NO_ERROR;
  144. return $this->huoSuccess($_code, IdentifyStatus::getMsg($_code));
  145. }
  146. /**
  147. * check the name and idNum
  148. *
  149. * @return string
  150. */
  151. public function check() {
  152. $_uri = $this->require_url.'/plat/get_user_pi/channel/'.$this->channel_code.'/brappid/'.$this->game_id.'.html';
  153. $_time = time();
  154. $_params = array(
  155. 'userId' => urlencode($this->mg_mem_id),
  156. 'real_name' => urlencode($this->real_name),
  157. 'id_number' => urlencode($this->id_card),
  158. 'time' => $_time
  159. );
  160. $_str = $this->real_name.$this->id_card.$_time.$this->mg_mem_id;
  161. $_params['sign'] = md5($_str);
  162. return $this->doRequest($_params, $_uri);
  163. }
  164. /**
  165. * do request
  166. *
  167. * @param $params
  168. * @param string $url
  169. *
  170. * @return mixed
  171. */
  172. private function doRequest($params, $url) {
  173. $_rs = Http::post($url, $params);
  174. return $_rs;
  175. }
  176. }