ComplexAuthentication.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * 聚合渠道 玩家实名认证管理
  4. */
  5. namespace app\api\controller\v2;
  6. use app\api\controller\Api;
  7. use app\common\library\RsaSign;
  8. use app\common\model\ComplexLoginlog;
  9. use app\common\model\ComplexChannelModel;
  10. use app\common\model\ComplexMembers;
  11. use app\common\logic\Complex;
  12. use Authentication;
  13. use think\Db;
  14. use think\Cache;
  15. use think\Exception;
  16. use think\Config;
  17. use app\common\model\PolychannelGame;
  18. use think\Env;
  19. use think\exception\HttpResponseException;
  20. use think\Request;
  21. use think\Validate;
  22. use app\common\controller\Base;
  23. class ComplexAuthentication extends Api
  24. {
  25. public function _initialize()
  26. {
  27. // parent::_initialize();
  28. }
  29. private function initCheck()
  30. {
  31. }
  32. // 实名认证
  33. public function check()
  34. {
  35. $validate = new Validate([
  36. 'uid' => 'require', // CP渠道用户ID
  37. 'game_id' => 'require', // 游戏ID
  38. 'channel_mark' => 'require', // 渠道标识
  39. 'real_name' => 'require|chsAlpha', // 真实姓名
  40. 'id_card' => 'require|alphaNum', // 身份证号
  41. 'sign' => 'require'
  42. ], [
  43. 'uid.require' => '缺少参数: uid',
  44. 'game_id.require' => '缺少参数: game_id',
  45. 'channel_mark.require' => '缺少参数: channel_mark',
  46. 'real_name.require' => '缺少参数: real_name',
  47. 'real_name.chsAlpha' => '真实姓名 格式有误!',
  48. 'id_card.require' => '缺少参数: id_card',
  49. 'id_card.alphaNum' => '身份证号 格式有误!',
  50. 'id_card' => '身份证号 格式有误!',
  51. 'sign.require' => '缺少参数: sign',
  52. ]);
  53. $data = Request::instance()->only(['uid', 'game_id', 'channel_mark', 'real_name', 'id_card', 'sign']);
  54. if (!$validate->check($data)) {
  55. return resultJson(-100, $validate->getError());
  56. }
  57. $gameInfo = model('Common/Game')->alias('g')
  58. ->join('cy_app a', 'a.gameid = g.id', 'left')
  59. ->where('g.id', $data['game_id'])->field('a.appkey,g.game_bizid,g.game_auth_type,g.id')->find();
  60. if (empty($gameInfo['id'])) return resultJson(-101, '当前游戏不存在!');
  61. if (empty($gameInfo['appkey'])) return resultJson(-102, '当前游戏缺少实名配置项!(请联系商务)');
  62. if (empty($gameInfo['game_bizid'])) return resultJson(-103, '当前游戏未配置中宣!(请联系商务)');
  63. if (empty($gameInfo['game_auth_type'])) return resultJson(-104, '当前游戏未开启中宣认证!(请联系商务)');
  64. $sign = $this->checkSign($data, $gameInfo['appkey']);
  65. if ($data['sign'] != $sign) return resultJson([], -120, '签名有误!');
  66. vendor('authSdk.Authentication', '.class.php');
  67. $auth = new Authentication();
  68. $result = $auth->check(trim($gameInfo['game_bizid']), $gameInfo['id'] . '-' . $data['uid'], $data['real_name'], $data['id_card']);
  69. if ($result['errcode'] <> 0) {
  70. if ($result['errcode'] > 1000 && $result['errcode'] < 2000) {
  71. // return resultJson(-130, '中宣 系统错误!:' . $result['errmsg']);
  72. return resultJson(-130, '中宣 系统错误!');
  73. }
  74. switch ($result['errcode']) {
  75. case 2001:
  76. $msg = '身份证号格式校验失败!';
  77. break;
  78. case 2002:
  79. $msg = '实名认证条目已达上限!';
  80. break;
  81. case 2003:
  82. $msg = '无该编码提交的实名认证记录!';
  83. break;
  84. case 2004:
  85. // $msg = '编码已经被占用!';
  86. $msg = '调用频繁,请稍后重试!';
  87. break;
  88. case 2005:
  89. $msg = '姓名合法性校验错误!';
  90. break;
  91. case 2006:
  92. $msg = 'PlayerId生成错误!';
  93. }
  94. return resultJson(-131, $msg ?? $result['errmsg']);
  95. }
  96. $resultData = $result['data']['result'];
  97. $authData = [
  98. 'complex_id' => $data['uid'],
  99. 'real_name' => trim($data['real_name']),
  100. 'idcard_num' => trim($data['id_card']),
  101. 'last_auth_time' => time(),
  102. 'update_time' => time(),
  103. ];
  104. $complexAuthModel = model('Common/ComplexMembersAuthentication');
  105. $caId = $complexAuthModel->where(['auth_pi' => $resultData['pi']])->value('id');
  106. if ($resultData['status'] == 0) {
  107. $authData['auth_status'] = 3; // 认证状态: 0=未认证, 1=认证中, 2=认证不通过, 3=认证通过
  108. } else if ($resultData['status'] == 1) {
  109. $authData['auth_status'] = 1;
  110. } else {
  111. $authData['auth_status'] = 2;
  112. }
  113. if($caId){
  114. $updResult = $complexAuthModel->where(['id' => $caId])->update($authData);
  115. }else{
  116. $authData['auth_type'] = 1;
  117. $authData['auth_pi'] = $resultData['pi'];
  118. $authData['createtime'] = time();
  119. $updResult = $complexAuthModel->insert($authData);
  120. }
  121. return resultJson(200, 'success', ['pi' => $resultData['pi'], 'auth_status' => $authData['auth_status']]);
  122. }
  123. // 实名认证结果查询
  124. public function query()
  125. {
  126. $validate = new Validate([
  127. 'uid' => 'require', // 渠道用户ID
  128. 'channel_mark' => 'require', // 渠道标识
  129. 'sign' => 'require'
  130. ], [
  131. 'uid.require' => '缺少参数: uid',
  132. 'channel_mark.require' => '缺少参数: channel_mark',
  133. 'sign.require' => '缺少参数: sign',
  134. ]);
  135. $data = Request::instance()->only(['uid', 'channel_mark', 'sign']);
  136. if (!$validate->check($data)) {
  137. $this->result([], -110, $validate->getError(), 'json');
  138. }
  139. $info = model('Common/ComplexMembers')->alias('cc')
  140. ->join('cy_app a', 'cc.gameid = a.gameid', 'left')
  141. ->join('cy_game g', 'cc.gameid = g.id', 'left')
  142. ->where('complex_id', $data['uid'])->field('cc.id,cc.complex_id,cc.gameid,cc.channel_id,a.appkey,g.game_bizid,g.game_auth_type')->find();
  143. // dump($info->toArray());
  144. if (!$info) $this->result([], -111, '渠道用户不存在!', 'json');
  145. if (!isset($info['appkey'])) $this->result([], -112, '当前游戏缺少实名配置项!(请联系商务)', 'json');
  146. if (!isset($info['game_bizid'])) $this->result([], -113, '当前游戏未配置中宣!(请联系商务)', 'json');
  147. if (!isset($info['game_auth_type'])) $this->result([], -114, '当前游戏未开启中宣认证!(请联系商务)', 'json');
  148. $sign = $this->checkSign($data, $info['appkey']);
  149. if ($data['sign'] != $sign) $this->result([], -120, '签名有误!', 'json');
  150. $complexModel = model('Common/ComplexMembersAuthentication');
  151. $info = $complexModel->where(['members_id' => $info['id']])->find();
  152. if (!empty($info)) $this->result([], -140, '当前用户不存在!', 'json');
  153. if ($info['auth_status'] === 0) $this->result([], -141, '当前用户未认证!', 'json');
  154. vendor('authSdk.Authentication', '.class.php');
  155. $auth = new Authentication();
  156. $result = $auth->query(trim($info['game_bizid']), $info['id'] . '-' . $data['uid']);
  157. if ($result['errcode'] <> 0) {
  158. if ($result['errcode'] == 2004) {
  159. $this->result([], -130, '调用频繁,请稍后重试!', 'json');
  160. } else {
  161. $this->result([], -131, '中宣接口异常:' . $result['errmsg'], 'json');
  162. }
  163. }
  164. $resultData = $result['data']['result'];
  165. $authData = [
  166. 'id' => $info['id'],
  167. 'last_query_time' => time(),
  168. 'update_time' => time(),
  169. ];
  170. if ($resultData['status'] == 0) {
  171. $authData['auth_status'] = 3;
  172. $updResult = $complexModel->save($authData);
  173. if (!$updResult) {
  174. $this->jsonResult([], -41, '实名认证结果保存异常');
  175. }
  176. } else if ($resultData['status'] == 1) {
  177. $authData['auth_status'] = 1;
  178. $complexModel->save($authData);
  179. $this->result([], -42, '当前用户认证中审核中...', 'json');
  180. } else {
  181. $authData['auth_status'] = 2;
  182. $complexModel->save($authData);
  183. $this->result([], -43, '当前用户认证不通过!', 'json');
  184. }
  185. $this->jsonResult([
  186. 'real_name' => $info['real_name'],
  187. 'idcard_num' => $info['idcard_num'],
  188. 'auth_status' => $info['auth_status'],
  189. ], 200, 'success');
  190. }
  191. // 中宣部游戏用户行为数据上报
  192. public function sync()
  193. {
  194. }
  195. // 签名校验
  196. private function checkSign($data, $app_key)
  197. {
  198. unset($data['sign']); // 去除 sign 参数
  199. ksort($data); // 按照键名排序
  200. $url_params = http_build_query($data); // 将数组转换为 URL 参数字符串
  201. $url_params = urldecode($url_params);
  202. return md5($url_params . $app_key); // MD5(字符串+app_key)
  203. }
  204. }