Juefeng.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Juefeng.php UTF-8
  4. * 绝峰实名认证pi上报
  5. *
  6. * @date : 2021/4/30 17:33
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK-9.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. class JUEFENG 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 = 3;
  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. if (!empty($this->pi)) {
  61. return $this->reportPi();
  62. }
  63. $_results = $this->check();
  64. $_results_arr = json_decode($_results, true);
  65. if (empty($_results_arr) || !isset($_results_arr['code'])
  66. || ('1' != $_results_arr['code']
  67. && '1001' != $_results_arr['code'])) {
  68. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  69. $_step = 0;
  70. $_other = '绝峰防沉迷实名认证API,认证异常realName';
  71. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  72. $_code = CommonStatus::INNER_ERROR;
  73. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  74. return $this->huoError($_code, $_results_arr['msg']);
  75. }
  76. /* 1001是用户不存在,默认为认证中 */
  77. if ('1001' == $_results_arr['code']) {
  78. $_results_arr['data'] = ['status' => self::STATUS_IN_PROGRESS, 'pi' => ''];
  79. }
  80. /* 认证失败 */
  81. if ($_results_arr['data']['status'] == self::STATUS_FAIL) {
  82. $_result = $_results_arr;
  83. $_step = 0;
  84. $_other = '绝峰防沉迷实名认证API,认证失败realName';
  85. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  86. $_code = IdentifyStatus::IDENTITY_FAIL;
  87. return $this->huoError($_code, $_other);
  88. }
  89. /**
  90. * 认证成功需要添加记录
  91. */
  92. $_ipl_model = new IdentifyPlatformLogModel();
  93. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_JUEFENG);
  94. $_log_data = [
  95. 'mem_id' => $this->mem_id,
  96. 'mg_mem_id' => $this->mg_mem_id,
  97. 'app_id' => $this->app_id,
  98. 'identify_from' => IdentifyConst::DRIVER_KEY_JUEFENG,
  99. 'real_name' => $this->real_name,
  100. 'id_card' => $this->id_card,
  101. 'identify_pi' => $_results_arr['data']['pi'],
  102. 'status' => $_results_arr['data']['status'],
  103. ];
  104. if (empty($_ipl_info)) {
  105. $_ipl_model->addData($_log_data);
  106. } else {
  107. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  108. }
  109. /* 认证中 */
  110. if ($_results_arr['data']['status'] == self::STATUS_IN_PROGRESS) {
  111. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  112. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  113. }
  114. $_code = CommonStatus::NO_ERROR;
  115. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  116. }
  117. /**
  118. * PI值上报
  119. *
  120. * @return array
  121. */
  122. public function reportPi() {
  123. $_uri = str_replace("realName", "reprotPi", $this->require_url);
  124. $_uri = $_uri.'/'.$this->channel_code;
  125. $_headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
  126. if (!empty($this->headers)) {
  127. $_headers = array_merge($_headers, $this->headers);
  128. }
  129. $_params = array(
  130. 'pi' => $this->pi,
  131. 'uid' => $this->mg_mem_id,
  132. 'gameId' => $this->app_id,
  133. 'timestamp' => date('Y-m-d H:i:s'),
  134. );
  135. $_str = $this->channel_code.$this->app_id.$this->mg_mem_id.$this->pi.$_params['timestamp'];
  136. $_params['sign'] = md5($_str);
  137. $_results = $this->doRequest($_params, $_uri, $_headers);
  138. $_results_arr = json_decode($_results, true);
  139. if (empty($_results_arr) || !isset($_results_arr['code']) || ('1' != $_results_arr['code']
  140. && '1001' != $_results_arr['code'])) {
  141. $_result = [$_results, $_results_arr, $this->real_name, $this->id_card];
  142. $_step = 0;
  143. $_other = '绝峰防沉迷实名认证API,认证异常reprotPi';
  144. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  145. $_code = CommonStatus::INNER_ERROR;
  146. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  147. return $this->huoError($_code, $_results_arr['msg']);
  148. }
  149. /* 1001是用户不存在,默认为认证中 */
  150. if ('1001' == $_results_arr['code']) {
  151. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  152. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  153. }
  154. /**
  155. * 认证成功需要添加记录
  156. */
  157. $_ipl_model = new IdentifyPlatformLogModel();
  158. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_JUEFENG);
  159. $_log_data = [
  160. 'mem_id' => $this->mem_id,
  161. 'mg_mem_id' => $this->mg_mem_id,
  162. 'app_id' => $this->app_id,
  163. 'identify_from' => IdentifyConst::DRIVER_KEY_JUEFENG,
  164. 'real_name' => $this->real_name,
  165. 'id_card' => $this->id_card,
  166. 'identify_pi' => $this->pi,
  167. 'status' => self::STATUS_SUCCESS,
  168. ];
  169. if (empty($_ipl_info)) {
  170. $_ipl_model->addData($_log_data);
  171. } else {
  172. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  173. }
  174. $_code = CommonStatus::NO_ERROR;
  175. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  176. }
  177. /**
  178. * 查询是否已认证
  179. *
  180. * @return bool
  181. */
  182. private function checkIsVerified() {
  183. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_JUEFENG);
  184. return empty($_pi) ? false : true;
  185. }
  186. public function loginBehavior($identify_pi) {
  187. $_check_rs = $this->checkIsVerified();
  188. if (true === $_check_rs) {
  189. /* 已认证无需处理 */
  190. $_code = CommonStatus::NO_ERROR;
  191. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  192. }
  193. $_identify_rs = $this->identify();
  194. if (CommonStatus::NO_ERROR != $_identify_rs['code']) {
  195. $_code = CommonStatus::NO_ERROR;
  196. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  197. }
  198. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  199. $_im_model = new IdentifyMemModel();
  200. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  201. if (empty($_old_id_data)) {
  202. $_data = [
  203. 'identify_from' => IdentifyConst::DRIVER_KEY_JUEFENG,
  204. 'identify_pi' => $_identify_pi,
  205. 'mem_id' => $this->mem_id,
  206. 'real_name' => $this->real_name,
  207. 'id_card' => $this->id_card
  208. ];
  209. $_im_model->addData($_data);
  210. } elseif (empty($_old_id_data['identify_pi'])) {
  211. $_update_data = [
  212. 'identify_pi' => $_identify_pi
  213. ];
  214. $_im_model->updateData($_update_data, $_old_id_data['id']);
  215. }
  216. $_code = CommonStatus::NO_ERROR;
  217. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  218. }
  219. /**
  220. * check the name and idNum
  221. *
  222. * @return string
  223. */
  224. public function check() {
  225. $_uri = str_replace("reprotPi", "realName", $this->require_url);
  226. $_uri = $_uri.'/'.$this->channel_code;
  227. $_headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
  228. if (!empty($this->headers)) {
  229. $_headers = array_merge($_headers, $this->headers);
  230. }
  231. $_params = array(
  232. 'cardId' => $this->id_card,
  233. 'realName' => base64_encode($this->real_name),
  234. 'uid' => $this->mg_mem_id,
  235. 'gameId' => $this->app_id,
  236. 'timestamp' => date('Y-m-d H:i:s'),
  237. );
  238. $_str = $this->channel_code.$this->app_id.$this->mg_mem_id.$this->real_name.$this->id_card
  239. .$_params['timestamp'];
  240. $_params['sign'] = md5($_str);
  241. return $this->doRequest($_params, $_uri, $_headers);
  242. }
  243. /**
  244. * do request
  245. *
  246. * @param $params
  247. * @param string $url
  248. * @param array $headers
  249. *
  250. * @return mixed
  251. */
  252. private function doRequest($params, $url, $headers = []) {
  253. $_rs = Http::post($url, $params, ['header' => $headers, 'timeout' => $this->timeout]);
  254. return $_rs;
  255. }
  256. }