Moxing.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * Moxing.php UTF-8
  4. *
  5. * @date : 2021/5/28 9:34
  6. *
  7. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  8. * @author : chenbingling <cbl@huosdk.com>
  9. * @version : HUOSDK-9.0
  10. */
  11. namespace huoIdentify\identifyDriver\driver;
  12. use GuzzleHttp\Client;
  13. use huoIdentify\identifyDriver\Driver;
  14. use huoIdentify\model\IdentifyPlatformLogModel;
  15. use huolib\constant\IdentifyConst;
  16. use huolib\status\CommonStatus;
  17. use huolib\status\IdentifyStatus;
  18. use huolib\tool\Http;
  19. use huolib\tool\StrUtils;
  20. use think\Config;
  21. use think\Log;
  22. class Moxing extends Driver {
  23. const STATUS_SUCCESS = 3;//认证成功
  24. const STATUS_IN_PROGRESS = 2;//认证中
  25. const STATUS_FAIL = 1;//认证失败
  26. /**
  27. * @var array
  28. */
  29. protected $headers;
  30. protected $public_params;
  31. /**
  32. * @var int $timeout
  33. */
  34. protected $timeout = 5;
  35. /**
  36. * @var Client
  37. */
  38. protected $http;
  39. /**
  40. * @var string
  41. */
  42. protected $app_id;
  43. protected $secret_key;
  44. protected $app_key;
  45. protected $channel_id;
  46. protected $require_url; /* 认证URL */
  47. /**
  48. * @param array $config
  49. */
  50. public function __construct($config = []) {
  51. if (empty($config)) {
  52. $config = (array)Config::get('identify_conf.fcm_game');
  53. }
  54. parent::__construct($config);
  55. // 设置属性
  56. $this->require_url = get_val($config, 'require_url', '');
  57. $this->app_key = get_val($config, 'app_key', '');
  58. $this->channel_id = get_val($config, 'channel_id', '');
  59. $this->secret_key = get_val($config, 'secret_key', '');
  60. $this->app_id = get_val($config, 'app_id', 0);
  61. if (!class_exists('\\GuzzleHttp\\Client')) {
  62. // 使用已有的GuzzleHttp
  63. require_once 'wxpay/TCloudAutoLoader.php';
  64. }
  65. $this->http = new Client();
  66. $this->headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset:utf-8'];
  67. $this->public_params = [
  68. 'timestamp' => $this->getMillisecond(),
  69. 'appKey' => $this->app_key,
  70. 'channelId' => $this->channel_id,
  71. ];
  72. }
  73. /**
  74. * 实名认证
  75. * https://www.yuque.com/ys_sdk/eusdkv2/ppu4mb
  76. */
  77. public function identify() {
  78. /* 提交认证之前获取当前玩家是否有认证中的数据,有则使用查询接口 */
  79. $_query_rs = $this->checkCertificationData();
  80. if (CommonStatus::DATA_NOT_FOUND_EXCEPTION != $_query_rs['code']) {
  81. /* 只要返回的不是数据未找到则认为无需再次提交认证信息 */
  82. return $this->huoReturn($_query_rs);
  83. }
  84. $_results = $this->check();
  85. $_results_arr = json_decode($_results, true);
  86. if (empty($_results_arr) || !isset($_results_arr['state']) || '1' != $_results_arr['state']) {
  87. Log::write(
  88. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  89. array($this->config, $this->id_card, $this->real_name)
  90. ).'&rs='.$_results.'[防沉迷实名认证api]', Log::ERROR
  91. );
  92. $_code = CommonStatus::INNER_ERROR;
  93. return $this->huoError($_code, $_results_arr['msg']);
  94. }
  95. /* 认证失败 */
  96. if ($_results_arr['data']['status'] == self::STATUS_FAIL) {
  97. $_result = $_results_arr;
  98. $_step = 0;
  99. $_other = '默兴防沉迷实名认证API,认证失败';
  100. \huolib\tool\Log::outErrorLog(debug_backtrace(false, 1)[0], $_result, $_step, $_other);
  101. $_code = IdentifyStatus::IDENTITY_FAIL;
  102. return $this->huoError($_code, $_other);
  103. }
  104. /**
  105. * 认证成功需要添加记录
  106. */
  107. $_ipl_model = new IdentifyPlatformLogModel();
  108. $_ipl_info = $_ipl_model->getInfoByMgFrom($this->mg_mem_id, IdentifyConst::DRIVER_KEY_MOXING);
  109. $_log_data = [
  110. 'mem_id' => $this->mem_id,
  111. 'mg_mem_id' => $this->mg_mem_id,
  112. 'app_id' => $this->app_id,
  113. 'identify_from' => IdentifyConst::DRIVER_KEY_MOXING,
  114. 'real_name' => $this->real_name,
  115. 'id_card' => $this->id_card,
  116. 'identify_pi' => $_results_arr['data']['thirdAuthentication'],
  117. 'status' => $_results_arr['data']['status'],
  118. ];
  119. if (empty($_ipl_info)) {
  120. $_ipl_model->addData($_log_data);
  121. } else {
  122. $_ipl_model->updateData($_log_data, $_ipl_info['id']);
  123. }
  124. /* 认证中 */
  125. if ($_results_arr['data']['status'] == self::STATUS_IN_PROGRESS) {
  126. $_code = CommonStatus::MEM_IDENTIFY_IN_PROGRESS;
  127. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  128. }
  129. $_code = CommonStatus::NO_ERROR;
  130. $_results_arr['data']['pi'] = $_results_arr['data']['thirdAuthentication'];
  131. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  132. }
  133. /* 查询是否有认证中的数据 */
  134. private function checkCertificationData() {
  135. $_ipl_model = new IdentifyPlatformLogModel();
  136. $_last_order_data = $_ipl_model->getInfoByMgFrom(
  137. $this->mg_mem_id, IdentifyConst::DRIVER_KEY_MOXING
  138. );
  139. if (empty($_last_order_data)) {
  140. /* 数据不存在,需重新认证 */
  141. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  142. return $this->huoError($_code, CommonStatus::getMsg($_code));
  143. }
  144. if ($_last_order_data['status'] == self::STATUS_SUCCESS) {
  145. $_code = CommonStatus::NO_ERROR;
  146. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  147. }
  148. if ($_last_order_data['status'] != self::STATUS_IN_PROGRESS) {
  149. /* 状态不是认证中的/玩家证件信息对不上的认为数据不存在,需重新认证 */
  150. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  151. return $this->huoError($_code, CommonStatus::getMsg($_code));
  152. }
  153. $_query_rs = $this->query();
  154. $_results_arr = json_decode($_query_rs, true);
  155. if (empty($_results_arr) || !isset($_results_arr['state']) || '1' != $_results_arr['state']) {
  156. Log::write(
  157. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode($_last_order_data)
  158. .'&rs='.$_query_rs.'[默兴防沉迷实名认证api,查询信息]', Log::ERROR
  159. );
  160. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  161. return $this->huoError($_code, CommonStatus::getMsg($_code));
  162. }
  163. /* 认证失败 */
  164. $_result_data = get_val($_results_arr, 'data', []);
  165. $_auth_status = get_val($_result_data, 'status', 0);
  166. /* 认证中/认证失败 */
  167. if ($_auth_status != self::STATUS_SUCCESS) {
  168. $_code = CommonStatus::INVALID_PARAMS;
  169. return $this->huoError($_code, CommonStatus::getMsg($_code));
  170. }
  171. /**
  172. * 认证成功需要添加记录
  173. */
  174. $_log_data = [
  175. 'mem_id' => $this->mem_id,
  176. 'mg_mem_id' => $this->mg_mem_id,
  177. 'app_id' => $this->app_id,
  178. 'identify_from' => IdentifyConst::DRIVER_KEY_MOXING,
  179. 'real_name' => $this->real_name,
  180. 'id_card' => $this->id_card,
  181. 'identify_pi' => $_results_arr['data']['thirdAuthentication'],
  182. 'status' => $_results_arr['data']['status'],
  183. ];
  184. $_ipl_model->updateData($_log_data, $_last_order_data['id']);
  185. $_code = CommonStatus::NO_ERROR;
  186. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_results_arr['data']);
  187. }
  188. public function loginBehavior($identify_pi) {
  189. if (empty($identify_pi)) {
  190. $_code = CommonStatus::INVALID_PARAMS;
  191. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  192. }
  193. $_data = [
  194. [
  195. 'behaviorType' => 1,
  196. 'userType' => 0,
  197. 'sessionId' => $this->user_token,
  198. 'behaviorTimestamp' => time(),
  199. 'thirdAuthentication' => $identify_pi,
  200. ]
  201. ];
  202. $_results = $this->loginOrOut($_data);
  203. $_results_arr = json_decode($_results, true);
  204. if ('1' != $_results_arr['state']) {
  205. Log::write(
  206. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  207. array($this->config, $this->id_card, $this->real_name, $identify_pi)
  208. ).'&rs='.$_results.'[防沉迷行为数据上报api]', Log::ERROR
  209. );
  210. $_code = CommonStatus::INNER_ERROR;
  211. return $this->huoError($_code, $_results_arr['msg']);
  212. }
  213. $_code = CommonStatus::NO_ERROR;
  214. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  215. }
  216. public function logoutBehavior($identify_pi) {
  217. if (empty($identify_pi)) {
  218. $_code = CommonStatus::INVALID_PARAMS;
  219. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  220. }
  221. $_data = [
  222. [
  223. 'behaviorType' => 0,
  224. 'userType' => 0,
  225. 'sessionId' => $this->user_token,
  226. 'behaviorTimestamp' => time(),
  227. 'thirdAuthentication' => $identify_pi,
  228. ]
  229. ];
  230. $_results = $this->loginOrOut($_data);
  231. $_results_arr = json_decode($_results, true);
  232. if ('1' != $_results_arr['state']) {
  233. Log::write(
  234. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  235. array($this->config, $this->id_card, $this->real_name, $identify_pi)
  236. ).'&rs='.$_results.'[防沉迷行为数据上报api]', Log::ERROR
  237. );
  238. $_code = CommonStatus::INNER_ERROR;
  239. return $this->huoError($_code, $_results_arr['msg']);
  240. }
  241. $_code = CommonStatus::NO_ERROR;
  242. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  243. }
  244. /**
  245. * check the name and idNum
  246. *
  247. * @return string
  248. */
  249. public function check() {
  250. $_uri = $this->require_url.'/game/authentication/check/v1';
  251. $_headers = $this->headers;
  252. $_body = $this->public_params;
  253. $_body['userId'] = $this->mg_mem_id;
  254. $_body['realName'] = $this->real_name;
  255. $_body['idCard'] = $this->id_card;
  256. return $this->doRequest($_body, $_uri, 'POST', $_headers);
  257. }
  258. /**
  259. * query the ai
  260. *
  261. * @return string
  262. */
  263. public function query() {
  264. $_uri = $this->require_url.'//game/authentication/query/v1';
  265. $_headers = $this->headers;
  266. $_body = $this->public_params;
  267. $_body['userId'] = $this->mg_mem_id;
  268. return $this->doRequest($_body, $_uri, 'POST', $_headers);
  269. }
  270. /**
  271. * @param mixed $data
  272. *
  273. * @return string
  274. */
  275. public function loginOrOut($data) {
  276. $collections = $this->public_params;
  277. foreach ($data as $i => $d) {
  278. $tmp = [];
  279. $tmp['no'] = $i + 1;
  280. $tmp['thirdAuthentication'] = $d['thirdAuthentication'];
  281. $tmp['sessionId'] = isset($d['sessionId']) ? $d['sessionId'] : md5($d['thirdAuthentication']);
  282. $tmp['userType'] = $d['userType'];
  283. $tmp['behaviorType'] = $d['behaviorType'];
  284. $tmp['behaviorTimestamp'] = $d['behaviorTimestamp'];
  285. $collections['behavior'][] = $tmp;
  286. }
  287. $collections['behavior'] = json_encode($collections['behavior']);
  288. $_uri = $this->require_url.'/game/authentication/sync/v1';
  289. $_headers = $this->headers;
  290. return $this->doRequest($collections, $_uri, 'POST', $_headers);
  291. }
  292. /**
  293. * make the sign
  294. *
  295. * @param $body
  296. *
  297. * @return string
  298. */
  299. private function makeSign($body) {
  300. $_params = StrUtils::argSort($body);
  301. $_str = '';
  302. while (list($_key, $_val) = StrUtils::funcNewEach($_params)) {
  303. if (is_array($_val)) {
  304. $_val = json_encode($_val);
  305. }
  306. $_str .= $_key."=".$_val."&";
  307. }
  308. // 去掉最后一个&字符
  309. $_str = substr($_str, 0, strlen($_str) - 1);
  310. $_str = $_str.'&key='.$this->secret_key;
  311. return strtolower(md5($_str));
  312. }
  313. /**
  314. * do request
  315. *
  316. * @param $params
  317. * @param string $url
  318. * @param string $method
  319. * @param array $headers
  320. *
  321. * @return mixed
  322. */
  323. private function doRequest($params, $url, $method, $headers = []) {
  324. $params['sign'] = $this->makeSign($params);
  325. $_rs = Http::post($url, $params, ['header' => $headers, 'timeout' => $this->timeout]);
  326. return $_rs;
  327. }
  328. /**
  329. * 获取毫秒级别的时间戳
  330. */
  331. private static function getMillisecond() {
  332. //获取毫秒的时间戳
  333. $time = explode(" ", microtime());
  334. $time = $time[1].($time[0] * 1000);
  335. $time2 = explode(".", $time);
  336. $time = $time2[0];
  337. return $time;
  338. }
  339. }