DyGameOpen.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\GameBindDouyinOpen;
  4. use DouYinGameOpen\Init;
  5. use DouYinGameOpen\User;
  6. use think\Cache;
  7. use think\Env;
  8. /**
  9. * 抖音广告队列类
  10. * Class MakeReport
  11. */
  12. class DyGameOpen
  13. {
  14. private $_redis = null;
  15. public $dy_config = null;
  16. private $init_status = [];
  17. public function __construct(string $key, string $type)
  18. {
  19. $this->_redis = Cache::init()->handler();
  20. $this->init($key, $type);
  21. }
  22. /**
  23. * 根据类型初始化配置
  24. *
  25. * @param $key string 参数值
  26. * @param $type string 参数类型=game_id|app_id
  27. *
  28. * @return array
  29. * @throws \think\Exception
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. * @throws \think\exception\DbException
  33. */
  34. public function init(string $key, string $type)
  35. {
  36. if($type == 'game_id'){
  37. $config = GameBindDouyinOpen::getInfoByGameId($key);
  38. }else{
  39. $config = GameBindDouyinOpen::getInfoByAppId($key);
  40. }
  41. if(!$config){
  42. $this->init_status = $this->handleResponse([], -100, '当前游戏未进行抖音平台绑定!');
  43. }
  44. $this->dy_config = $config;
  45. }
  46. private function handleResponse($data = [], $code = 100, $msg = 'success'): array
  47. {
  48. if($code == -100){
  49. $msg = 'error: '.$msg;
  50. }
  51. return ['data' => $data, 'code' => $code, 'msg' => $msg];
  52. }
  53. // -- 获取公共 --
  54. /**
  55. * 获取平台的 client_token
  56. * @return mixed
  57. */
  58. public function getClientToken(){
  59. if($this->init_status){
  60. return $this->init_status;
  61. }
  62. $cache_key = 'dy_game_open:client_token:'.$this->dy_config['app_id'];
  63. $client_token = $this->_redis->get($cache_key);
  64. if(!$client_token){
  65. $client_info = (new Init($this->dy_config))->client_token();
  66. $this->_redis->set($cache_key, $client_info['data']['access_token'], $client_info['data']['expires_in']-60);
  67. $client_token = $client_info['data']['access_token'];
  68. }
  69. return $this->handleResponse($client_token);
  70. }
  71. /**
  72. * 获取用户的 access_token
  73. * @param $code
  74. *
  75. * @return array
  76. */
  77. public function getAccessToken($code){
  78. if($this->init_status){
  79. return $this->init_status;
  80. }
  81. $access_info = (new Init($this->dy_config))->access_token($code);
  82. if($access_info['message'] == 'error' || !empty($dyResult['data']['description'])){
  83. return $this->handleResponse([], -100, $access_info['data']['description'].'-'.$access_info['data']['error_code']);
  84. }
  85. $result = [
  86. 'access_token' => $access_info['data']['access_token'],
  87. 'open_id' => $access_info['data']['open_id'],
  88. ];
  89. return $this->handleResponse($result);
  90. }
  91. /**
  92. * 刷新用户的 access_token
  93. * @param $access_token
  94. *
  95. * @return array
  96. */
  97. public function refreshToken($access_token){
  98. if($this->init_status){
  99. return $this->init_status;
  100. }
  101. // 请求刷新access_token
  102. $access_info = (new Init($this->dy_config))->refresh_token($access_token);
  103. if ($access_info['message'] == 'error' || !empty($access_info['data']['description'])) {
  104. return $this->handleResponse([], -100, '@刷新token失败:' . $access_info['data']['description'] . '-' . $access_info['data']['error_code']);
  105. }
  106. // 更新缓存
  107. $token_data = [
  108. 'access_token' => $access_info['data']['access_token'],
  109. 'refresh_token' => $access_info['data']['refresh_token'],
  110. 'expires_in' => $access_info['data']['expires_in'],
  111. 'refresh_expires_in' => $access_info['data']['refresh_expires_in'],
  112. 'open_id' => $access_info['data']['open_id']
  113. ];
  114. return $this->handleResponse($token_data);
  115. }
  116. // -- 游戏绑定抖音账户 --
  117. /**
  118. * 根据 open_id 获取用户绑定意愿
  119. * @param $open_id
  120. *
  121. * @return array
  122. */
  123. public function getAuthInfoOpen($open_id){
  124. if($this->init_status){
  125. return $this->init_status;
  126. }
  127. $client_token = $this->getClientToken();
  128. if($client_token['code'] != 100){
  129. return $this->handleResponse([], -100, $client_token['msg']);
  130. }
  131. $result = (new User($this->dy_config))->get_auth_info_open($client_token['data'], $open_id);
  132. if($result['err_no'] > 0){
  133. return $this->handleResponse([], -100, $result['err_msg']);
  134. }
  135. // 用户是否同意进行账号绑定 = true/false
  136. return $this->handleResponse($result['data']);
  137. }
  138. // -- 抖音绑定游戏账户 --
  139. /**
  140. * 3.4.2 抖音授权登录根据 code 获取加密手机号
  141. *
  142. * @param $open_id
  143. *
  144. * @return array
  145. */
  146. public function getHashMobile($open_id){
  147. if($this->init_status){
  148. return $this->init_status;
  149. }
  150. $client_token = $this->getClientToken();
  151. if($client_token['code'] != 100){
  152. return $this->handleResponse([], -100, 'error: '.$client_token['msg']);
  153. }
  154. $result = (new User($this->dy_config))->get_user_hash_mobile($client_token['data'], $open_id);
  155. if($result['err_no'] > 0){
  156. return $this->handleResponse([], -100, $result['err_msg']);
  157. }
  158. return $this->handleResponse($result['data']['hash_mobile']);
  159. }
  160. /**
  161. * 3.4.7 游戏内授权后回调抖音游戏
  162. *
  163. * @param $is_agree
  164. * @param $uid
  165. * @param $scheme
  166. *
  167. * @return array
  168. */
  169. public function getJumpAuthCallback($is_agree, $uid, $scheme){
  170. if($this->init_status){
  171. return $this->init_status;
  172. }
  173. $client_token = $this->getClientToken();
  174. if($client_token['code'] != 100){
  175. return $this->handleResponse([], -100, $client_token['msg']);
  176. }
  177. $result = (new Init($this->dy_config))->jump_game_auth_callback($client_token['data'], $is_agree, $uid, $scheme);
  178. if($result['err_no'] > 0){
  179. return $this->handleResponse([], -100, $result['err_msg']);
  180. }
  181. // 用户是否同意进行账号绑定 = true/false
  182. return $this->handleResponse();
  183. }
  184. }