Kuchang.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Kuchang.php UTF-8
  4. * 酷畅实名认证api
  5. *
  6. * @date : 2021/5/7 11:27
  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\Http;
  21. use huolib\tool\RateLimit;
  22. class Kuchang extends Driver {
  23. const STATUS_SUCCESS = 0;//认证成功
  24. const STATUS_IN_PROGRESS = 1;//认证中
  25. const STATUS_FAIL = 2;//认证失败
  26. /**
  27. * @var array
  28. */
  29. protected $headers;
  30. /**
  31. * @var string
  32. */
  33. protected $key;
  34. /**
  35. * @var int $timeout
  36. */
  37. protected $timeout = 3;
  38. /**
  39. * @var Client
  40. */
  41. protected $http;
  42. protected $app_id; /* 当前游戏id */
  43. protected $channel_code; /* 渠道code 酷畅提供 */
  44. protected $require_url; /* 认证URL 酷畅提供 */
  45. /**
  46. * @param array $config
  47. */
  48. public function __construct($config = []) {
  49. $_config = $config;
  50. parent::__construct($_config);
  51. // 设置属性
  52. $this->app_id = get_val($_config, 'app_id', 0);
  53. $this->channel_code = get_val($_config, 'channel_code', '');
  54. $this->require_url = get_val($_config, 'require_url', '');
  55. }
  56. /**
  57. * 实名认证
  58. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  59. */
  60. public function identify() {
  61. $_results = $this->check();
  62. $_results_arr = json_decode($_results, true);
  63. if (empty($_results_arr) || !isset($_results_arr['resultCode'])
  64. || ('100' != $_results_arr['resultCode']
  65. && '1005' != $_results_arr['resultCode'])) {
  66. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  67. $_step = 0;
  68. $_other = '酷畅防沉迷实名认证API,认证异常';
  69. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  70. $_code = CommonStatus::INNER_ERROR;
  71. $_results_arr['resultDesc'] = empty($_results_arr['resultDesc']) ? $_other : $_results_arr['resultDesc'];
  72. return $this->huoError($_code, $_results_arr['resultDesc']);
  73. }
  74. /* 1005是用户不存在,默认为认证中 */
  75. if ('1005' == $_results_arr['resultCode']) {
  76. $_results_arr['data'] = ['status' => self::STATUS_IN_PROGRESS, 'pi' => ''];
  77. }
  78. /* 认证失败 */
  79. if ($_results_arr['data']['status'] == self::STATUS_FAIL) {
  80. $_result = $_results_arr;
  81. $_step = 0;
  82. $_other = '酷畅防沉迷实名认证API,认证失败';
  83. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  84. $_code = IdentifyStatus::IDENTITY_FAIL;
  85. return $this->huoError($_code, $_other);
  86. }
  87. /**
  88. * 认证成功需要添加记录
  89. */
  90. $_ipl_model = new IdentifyPlatformLogModel();
  91. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_KUCHANG);
  92. $_log_data = [
  93. 'mem_id' => $this->mem_id,
  94. 'mg_mem_id' => $this->mg_mem_id,
  95. 'app_id' => $this->app_id,
  96. 'identify_from' => IdentifyConst::DRIVER_KEY_KUCHANG,
  97. 'real_name' => $this->real_name,
  98. 'id_card' => $this->id_card,
  99. 'identify_pi' => $_results_arr['data']['pi'],
  100. 'status' => $_results_arr['data']['status'],
  101. ];
  102. if (empty($_ipl_info)) {
  103. $_ipl_model->addData($_log_data);
  104. } else {
  105. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  106. }
  107. /* 认证中 */
  108. if ($_results_arr['data']['status'] == self::STATUS_IN_PROGRESS) {
  109. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  110. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  111. }
  112. $_code = CommonStatus::NO_ERROR;
  113. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  114. }
  115. /**
  116. * 查询是否已认证
  117. *
  118. * @return bool
  119. */
  120. private function checkIsVerified() {
  121. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_KUCHANG);
  122. return empty($_pi) ? false : true;
  123. }
  124. public function loginBehavior($identify_pi) {
  125. $_check_rs = $this->checkIsVerified();
  126. if (true === $_check_rs) {
  127. /* 已认证无需处理 */
  128. $_code = CommonStatus::NO_ERROR;
  129. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  130. }
  131. /* 限制请求 */
  132. $_rs = (new RateLimit())->rateLimit($this->mem_id, true, true, 1, 3);
  133. if (false == $_rs) {
  134. $_code = CommonStatus::NO_ERROR;
  135. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  136. }
  137. $_identify_rs = $this->identify();
  138. if (CommonStatus::NO_ERROR == $_identify_rs['code']) {
  139. $_im_model = new IdentifyMemModel();
  140. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  141. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  142. if (empty($_old_id_data)) {
  143. $_data = [
  144. 'identify_from' => IdentifyConst::DRIVER_KEY_KUCHANG,
  145. 'identify_pi' => $_identify_pi,
  146. 'mem_id' => $this->mem_id,
  147. 'real_name' => $this->real_name,
  148. 'id_card' => $this->id_card
  149. ];
  150. $_im_model->addData($_data);
  151. } elseif (empty($_old_id_data['identify_pi'])) {
  152. $_update_data = [
  153. 'identify_pi' => $_identify_pi
  154. ];
  155. $_im_model->updateData($_update_data, $_old_id_data['id']);
  156. }
  157. }
  158. $_code = CommonStatus::NO_ERROR;
  159. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  160. }
  161. /**
  162. * check the name and idNum
  163. *
  164. * @return string
  165. */
  166. public function check() {
  167. $_uri = $this->require_url.'/'.$this->channel_code;
  168. $_headers = ['Content-Type' => 'application/json; charset=utf-8'];
  169. if (!empty($this->headers)) {
  170. $_headers = array_merge($_headers, $this->headers);
  171. }
  172. $_params = array(
  173. 'user_id' => $this->mg_mem_id,
  174. 'id_name' => $this->real_name,
  175. 'id_no' => $this->id_card
  176. );
  177. $_params['sign'] = md5(
  178. 'app_no='.$this->channel_code.'&id_name='.$this->real_name.'&id_no='.$this->id_card.'&user_id='
  179. .$this->mg_mem_id
  180. );
  181. return $this->doRequest($_params, $_uri, $_headers);
  182. }
  183. /**
  184. * do request
  185. *
  186. * @param $params
  187. * @param string $url
  188. * @param array $headers
  189. *
  190. * @return mixed
  191. */
  192. private function doRequest($params, $url, $headers = []) {
  193. $_rs = Http::post($url, $params, ['header' => $headers, 'timeout' => $this->timeout]);
  194. return $_rs;
  195. }
  196. }