DyGameOpen.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace app\api\controller\v2;
  3. use app\api\controller\Api;
  4. use app\common\library\DyGameOpen as DyGameOpenLib;
  5. use app\common\library\Sms;
  6. use think\Db;
  7. use think\Request;
  8. // 抖音游戏开放平台
  9. class DyGameOpen
  10. {
  11. public function __construct()
  12. {
  13. $input = input();
  14. $url = request()->url();
  15. log_message('DyGameOpen.Input: '. $url.' : '.json_encode($input), 'log', LOG_PATH . 'DyGameOpen/');
  16. }
  17. public function getCommon(){
  18. return json(['err_no' => 0, 'err_msg' => ""]);
  19. // return $this->success();
  20. }
  21. private function success($data = "", $code = 0, $msg = ""){
  22. $result = ['err_no' => $code, 'err_msg' => $msg];
  23. if($data){
  24. $result['data'] = $data;
  25. }
  26. log_message('DyGameOpen.result.success: '. json_encode($result), 'log', LOG_PATH . 'DyGameOpen/');
  27. return json($result);
  28. }
  29. private function fail($code, $msg){
  30. $result = ['err_no' => $code, 'err_msg' => $msg];
  31. log_message('DyGameOpen.result.fail: '. json_encode($result), 'log', LOG_PATH . 'DyGameOpen/');
  32. return json($result);
  33. }
  34. //
  35. private function getGameRole($string, $type=1){
  36. // $type=1; $string=open_id; 授权登录,获取游戏的账户下所有角色
  37. // $type=1; $string=phone_num; 手机验证码登录,获取游戏的账户下最新的角色
  38. $game_id = '10'; // TODO: 待完善
  39. $roleList = Db::table('cy_members')->alias('cm')
  40. ->join('nw_member_game_server nmgs', 'cm.id = nmgs.member_id', 'left')
  41. ->where(['cm.dy_uid' => $string, 'nmgs.game_id' => $game_id])->field('nmgs.mgs_id, nmgs.rolename, nmgs.game_id, cm.mobile')->select();
  42. // dump($roleList);
  43. if(!$roleList){
  44. return $this->fail(4014540, '用户游戏账号不存在!');
  45. }
  46. // $this->getHashMobile($open_id); // 新注册用户 获取 加密手机号
  47. $newList = [];
  48. foreach($roleList as $v){
  49. $newList[] = [
  50. 'game_user_id' => $v['mgs_id'],
  51. 'game_user_name' => $v['rolename'],
  52. 'mask_account_number' => mobileObfuscation($v['mobile']),
  53. ];
  54. }
  55. $data = [
  56. 'game_users' => $newList
  57. ];
  58. }
  59. /**
  60. * 3.4.1 抖音授权登录
  61. *
  62. * @return \think\response\Json
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public function getAuthLogin(){
  68. $app_id = input('app_id', '');
  69. $auth_code = input('auth_code', '');
  70. if(empty($app_id) || empty($auth_code)){
  71. return $this->fail(4001015, '请求参数有误!');
  72. }
  73. $dy = new DyGameOpenLib($app_id, 'app_id');
  74. $dy_config = $dy->dy_config;
  75. $dyResult = $dy->getAccessToken($auth_code);
  76. if($dyResult['code'] != 100){
  77. return $this->fail(-100, $dyResult['msg']);
  78. }
  79. $memberInfo = Db::table('cy_members')->where(['dy_uid' => $dyResult['data']['open_id'], 'gameid' => $dy_config['game_id']])->field('id, username')->find();
  80. if(!$memberInfo){
  81. return $this->fail(4014540, '用户游戏账号不存在!');
  82. }
  83. $data = [
  84. 'game_users' => [
  85. [
  86. 'game_user_id' => $memberInfo['username'],
  87. // 'game_user_name' => $memberInfo['rolename'],
  88. // 'mask_account_number' => mobileObfuscation($memberInfo['mobile']),
  89. ]
  90. ]
  91. ];
  92. // $this->getHashMobile($open_id); // 新注册用户 获取 加密手机号
  93. return $this->success($data);
  94. }
  95. /**
  96. * 3.4.2 抖音授权登录根据 code 获取加密手机号
  97. * - 用于抖音新注册用户。
  98. * - 目前没有什么用处,暂不获取,不然还需要进行存储。
  99. *
  100. * @param $open_id
  101. *
  102. * @return \think\response\Json|void
  103. */
  104. private function getHashMobile($open_id, $app_id){
  105. $dy = new DyGameOpenLib($app_id, 'app_id');
  106. $result = $dy->getHashMobile($open_id);
  107. if($result['code'] != 100){
  108. return $this->fail(-100, $result['msg']);
  109. }
  110. $hash_mobile = $result['data'];
  111. // TODO: 存储加密手机号
  112. }
  113. /**
  114. * 3.4.3 输入手机号发送验证码
  115. *
  116. * @return \think\response\Json
  117. */
  118. public function getMobileCode(){
  119. $app_id = input('app_id', '');
  120. $phone_num = input('phone_num', '');
  121. if(empty($app_id) || empty($phone_num)){
  122. return $this->fail(4014456, '请求参数有误!');
  123. }
  124. if(preg_match('/^1[3-9]\d{9}$/', $phone_num) !== 1){
  125. return $this->fail(4014456, '手机号格式有误!');
  126. }
  127. $sms = new Sms;
  128. $sms_result = $sms->sendCode($phone_num);
  129. if(!$sms_result['status']){
  130. return $this->fail(4014458, $sms_result['msg']);
  131. }
  132. Db::table('cy_mail_log')->insert([
  133. 'content' => $sms->_validate_content,
  134. 'target' => $phone_num,
  135. 'create_time' => time(),
  136. 'client_ip' => request()->ip(),
  137. 'type' => 'sms'
  138. ]);
  139. return $this->success();
  140. }
  141. /**
  142. * 3.4.4 输入手机号和验证码进行校验(登录|获取绑定过的角色信息)
  143. *
  144. * @return \think\response\Json
  145. */
  146. public function getMobileCheck(){
  147. $app_id = input('app_id', '');
  148. $phone_num = input('phone_num', '');
  149. $captcha = input('captcha', '');
  150. if(empty($app_id) || empty($phone_num) || empty($captcha)){
  151. return $this->fail(4014457, '请求参数有误!');
  152. }
  153. $sms_result = (new Sms)->checkCode($phone_num, $captcha);
  154. if(!$sms_result['status']){
  155. return $this->fail(4014457, $sms_result['msg']);
  156. }
  157. $dy = new DyGameOpenLib($app_id, 'app_id');
  158. $dy_config = $dy->dy_config;
  159. $memberInfo = Db::table('cy_members')->where(['mobile' => $phone_num, 'gameid' => $dy_config['game_id']])->field('id,username')->order('id desc')->find();
  160. if(!$memberInfo){
  161. return $this->fail(4014540, '用户游戏账号不存在!');
  162. }
  163. $info = [
  164. 'game_user_id' => $memberInfo['username'],
  165. // 'game_user_name' => $roleInfo['rolename'],
  166. // 'mask_account_number' => mobileObfuscation($phone_num),
  167. ];
  168. return $this->success($info);
  169. }
  170. /**
  171. * 3.4.8 获取角色列表
  172. *
  173. * - 请求范例:{"app_id":721109,"users":null,"game_user_ids":["c6zk0ix18lb"], "open_id":"_0003zW-CmybIK-fFQr7LgYR8DLJXsQgatI0"}
  174. *
  175. * @return \think\response\Json
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. * @throws \think\exception\DbException
  179. */
  180. public function getRoleList(){
  181. $app_id = input('app_id', '');
  182. $game_user_ids = input('game_user_ids/a', []); // 游戏账号 ID 列表
  183. $open_id = input('open_id', '');
  184. if(empty($app_id) || empty($open_id)){
  185. return $this->fail(-100, '请求参数有误!');
  186. }
  187. $dy = new DyGameOpenLib($app_id, 'app_id');
  188. $dy_config = $dy->dy_config;
  189. $roleInfo = Db::table('cy_members')->alias('cm')
  190. ->join('nw_member_game_server nmgs', 'cm.id=nmgs.member_id', 'left')
  191. ->where(['cm.dy_uid' => $open_id, 'nmgs.game_id' => $dy_config['game_id']])
  192. ->field('cm.username, nmgs.roleid, nmgs.rolename, nmgs.rolelevel, nmgs.serverid, nmgs.servername')
  193. ->select();
  194. $head_url = Db::table('cy_gameinfo')->where(['game_id' => $dy_config['game_id']])->value('mobileicon');
  195. $newList = [];
  196. foreach($roleInfo as $v){
  197. $newList[] = [
  198. "game_user_id" => $v['username'],
  199. "role_id" => $v['roleid'],
  200. "role_name" => $v['rolename'],
  201. "level" => $v['rolelevel'],
  202. "region_id" => $v['serverid'],
  203. "region_name" => $v['servername'],
  204. "avatar_url" => STATIC_DOMAIN.$head_url,
  205. ];
  206. }
  207. return $this->success([
  208. 'roles' => $newList
  209. ]);
  210. }
  211. /**
  212. * 3.4.9 抖音游戏绑定角色/解绑角色/绑定游戏账号通知
  213. * - 用户游戏绑定抖音:{"app_id":721109,"game_user_id":"c6zk0ix18lb","open_id":"_0003zW-CmybIK-fFQr7LgYR8DLJXsQgatI0","role_id":"5","allied_id":"","bind":1,"tm":1748050777,"bind_type":0}
  214. *
  215. * @return void
  216. */
  217. public function handleEventNotice()
  218. {
  219. // $input = Request::instance()->only(['app_id', 'game_user_id', 'role_id', 'open_id', 'allied_id', 'bind', 'bind_type', 'tm', 'changes']);
  220. return $this->success();
  221. }
  222. }