Yiwan.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * FcmGame.php UTF-8
  4. *
  5. *
  6. * @date : 2021-03-10 10:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 9.0
  11. */
  12. namespace huoIdentify\identifyDriver\driver;
  13. use GuzzleHttp\Client;
  14. use GuzzleHttp\Exception\ConnectException;
  15. use huoIdentify\controller\AesEcb;
  16. use huoIdentify\identifyDriver\Driver;
  17. use huoIdentify\model\IdentifyMemModel;
  18. use huoIdentify\model\IdentifyPlatformLogModel;
  19. use huolib\constant\IdentifyConst;
  20. use huolib\status\CommonStatus;
  21. use huolib\status\IdentifyStatus;
  22. use think\Config;
  23. class Yiwan extends Driver {
  24. const STATUS_SUCCESS = 1;//认证成功
  25. const STATUS_IN_PROGRESS = 2;//认证中
  26. const STATUS_FAIL = 3;//认证失败
  27. /**
  28. * @var string
  29. */
  30. protected $cipher = "aes-128-ecb";
  31. /**
  32. * @var AesEcb
  33. */
  34. protected $aes;
  35. /**
  36. * @var array
  37. */
  38. protected $headers;
  39. /**
  40. * @var string
  41. */
  42. protected $key;
  43. /**
  44. * @var int $timeout
  45. */
  46. protected $timeout = 3;
  47. /**
  48. * @var Client
  49. */
  50. protected $http;
  51. protected $app_id; /* 当前游戏id */
  52. protected $channel_code; /* 渠道code 益玩提供 */
  53. protected $require_url; /* 认证URL 益玩提供 */
  54. /**
  55. * @param array $config
  56. */
  57. public function __construct($config = []) {
  58. $_config = $config;
  59. if (empty($_config)) {
  60. $_config = (array)Config::get('identify_conf.yiwan');
  61. }
  62. parent::__construct($_config);
  63. // 设置属性
  64. $this->app_id = get_val($_config, 'app_id', 0);
  65. $this->channel_code = get_val($_config, 'channel_code', '');
  66. $this->require_url = get_val($_config, 'require_url', '');
  67. $this->aes = new AesEcb($this->channel_code, $this->cipher);
  68. if (!class_exists('\\GuzzleHttp\\Client')) {
  69. // 使用已有的GuzzleHttp
  70. require_once 'wxpay/TCloudAutoLoader.php';
  71. }
  72. $this->http = new Client();
  73. }
  74. /**
  75. * 实名认证
  76. * https://wlc.nppa.gov.cn/fcm_company/index.html#/login
  77. */
  78. public function identify() {
  79. $_results = $this->check($this->real_name, $this->id_card);
  80. $_results_arr = json_decode($_results, true);
  81. if (empty($_results_arr) || !isset($_results_arr['code']) || '0' != $_results_arr['code']) {
  82. $_result = [$_results, $_results_arr];
  83. $_step = 0;
  84. $_other = '益玩防沉迷实名认证API,认证异常';
  85. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  86. $_code = CommonStatus::INNER_ERROR;
  87. $_results_arr['msg'] = empty($_results_arr['msg']) ? $_other : $_results_arr['msg'];
  88. return $this->huoError($_code, $_results_arr['msg']);
  89. }
  90. /* 认证失败 */
  91. if ($_results_arr['data']['auth_state'] == self::STATUS_FAIL) {
  92. $_result = $_results_arr;
  93. $_step = 0;
  94. $_other = '益玩防沉迷实名认证API,认证失败';
  95. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  96. $_code = IdentifyStatus::IDENTITY_FAIL;
  97. return $this->huoError($_code, $_other);
  98. }
  99. /**
  100. * 认证成功需要添加记录
  101. */
  102. $_ipl_model = new IdentifyPlatformLogModel();
  103. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_YIWAN);
  104. $_log_data = [
  105. 'mem_id' => $this->mem_id,
  106. 'mg_mem_id' => $this->mg_mem_id,
  107. 'app_id' => $this->app_id,
  108. 'identify_from' => IdentifyConst::DRIVER_KEY_YIWAN,
  109. 'real_name' => $this->real_name,
  110. 'id_card' => $this->id_card,
  111. 'identify_pi' => $_results_arr['data']['pi'],
  112. 'status' => $_results_arr['data']['auth_state'],
  113. ];
  114. if (empty($_ipl_info)) {
  115. $_ipl_model->addData($_log_data);
  116. } else {
  117. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  118. }
  119. /* 认证中 */
  120. if ($_results_arr['data']['auth_state'] == self::STATUS_IN_PROGRESS) {
  121. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  122. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  123. }
  124. $_code = CommonStatus::NO_ERROR;
  125. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  126. }
  127. /**
  128. * 查询是否已认证
  129. *
  130. * @return bool
  131. */
  132. private function checkIsVerified() {
  133. $_pi = (new IdentifyPlatformLogModel())->getPiByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_YIWAN);
  134. return empty($_pi) ? false : true;
  135. }
  136. public function loginBehavior($identify_pi) {
  137. $_check_rs = $this->checkIsVerified();
  138. if (true === $_check_rs) {
  139. /* 已认证无需处理 */
  140. $_code = CommonStatus::NO_ERROR;
  141. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  142. }
  143. $_identify_rs = $this->identify();
  144. if (CommonStatus::NO_ERROR == $_identify_rs['code']) {
  145. $_im_model = new IdentifyMemModel();
  146. $_old_id_data = $_im_model->getInfoByMemId($this->mem_id);
  147. $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';
  148. if (empty($_old_id_data)) {
  149. $_data = [
  150. 'identify_from' => IdentifyConst::DRIVER_KEY_YIWAN,
  151. 'identify_pi' => $_identify_pi,
  152. 'mem_id' => $this->mem_id,
  153. 'real_name' => $this->real_name,
  154. 'id_card' => $this->id_card
  155. ];
  156. $_im_model->addData($_data);
  157. } elseif (empty($_old_id_data['identify_pi'])) {
  158. $_update_data = [
  159. 'identify_pi' => $_identify_pi
  160. ];
  161. $_im_model->updateData($_update_data, $_old_id_data['id']);
  162. }
  163. }
  164. $_code = CommonStatus::NO_ERROR;
  165. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  166. }
  167. /**
  168. * check the name and idNum
  169. *
  170. * @param string $name
  171. * @param string $idNum
  172. *
  173. * @return string
  174. */
  175. public function check($name, $idNum) {
  176. $_uri = $this->require_url;
  177. $_headers = ['Content-Type' => 'text/plain', 'pf' => $this->getPf()];
  178. if (!empty($this->headers)) {
  179. $_headers = array_merge($_headers, $this->headers);
  180. }
  181. $_uid = $this->mg_mem_id;
  182. $_body = ['id_no' => $idNum, 'id_name' => $name, 'uid' => $_uid, 'app_id' => $this->app_id];
  183. return $this->doRequest('POST', $_uri, $_headers, $_body);
  184. }
  185. /**
  186. * do request
  187. *
  188. * @param string $method
  189. * @param string $uri
  190. * @param array $headers
  191. * @param array $body
  192. * @param array $options
  193. *
  194. * @return string
  195. */
  196. private function doRequest($method, $uri, array $headers = [], array $body = [], array $options = []) {
  197. $raw = json_encode($body, JSON_UNESCAPED_UNICODE);
  198. $body = $this->aes->encrypt($raw);
  199. $options = array_merge(['headers' => $headers, 'body' => $body, 'timeout' => $this->timeout], $options);
  200. try {
  201. $response = $this->http->request($method, $uri, $options);
  202. return $response->getBody()->getContents();
  203. } catch (ConnectException $e) {
  204. $_result = $e->getMessage();
  205. $_step = 0;
  206. $_other = 0;
  207. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  208. throw new \think\Exception('实名认证系统异常');
  209. }
  210. }
  211. }