GlobalUserInfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace App\Game;
  3. use App\Facade\TableName;
  4. use App\Game\Services\BetbyService;
  5. use App\Http\helper\NumConfig;
  6. use App\IpLocation;
  7. use App\Jobs\GameTask;
  8. use App\Models\AccountsInfo;
  9. use App\Models\Treasure\GameScoreInfo;
  10. use App\Services\VipService;
  11. use App\Util;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Support\Facades\Crypt;
  14. use Illuminate\Support\Facades\Redis;
  15. use Illuminate\Support\Facades\DB;
  16. class GlobalUserInfo extends Model
  17. {
  18. /**
  19. * @var GlobalUserInfo
  20. */
  21. public static $me=null;
  22. public static function getLocale()
  23. {
  24. if(isset(self::$me)&&!empty(self::$me)&&!empty(self::$me->DefaultLanguage)){
  25. return substr(self::$me->DefaultLanguage,0,2);
  26. }else{
  27. return substr( @$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
  28. }
  29. }
  30. public static function getLocaleByUserID($UserID,$default='en')
  31. {
  32. if(isset(self::$me)&&!empty(self::$me)&&!empty(self::$me->DefaultLanguage)&&self::$me->UserID==$UserID){
  33. return substr(self::$me->DefaultLanguage,0,2);
  34. }else{
  35. $user=self::getGameUserInfo('UserID',$UserID);
  36. $locale=$user->DefaultLanguage??substr( @$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
  37. if(!$locale||empty($locale)){
  38. $locale=env('DEFAULT_LOCALE','en');
  39. }
  40. return strstr(env('VALID_LOCALE','en,es,pt,ru'),$locale)?$locale:$default;
  41. }
  42. }
  43. /**
  44. * @param $request
  45. * @return GlobalUserInfo|null
  46. */
  47. public static function GetRecentLogin($request){
  48. $FF = $request->input("ff", "");
  49. $LastLogonIP=IpLocation::getRealIp();
  50. $ua=$request->userAgent();
  51. $data1=Redis::get('UpdateLoginCheck_'.$FF);
  52. $data2=Redis::get('UpdateLoginCheck_'.$LastLogonIP);
  53. if($data1){
  54. $data=json_decode($data1,true);
  55. if($data['LastLogonIP']==$LastLogonIP){
  56. return self::getGameUserInfo('UserID',$data['UserID']);
  57. }
  58. }
  59. if($data2){
  60. $data=json_decode($data2,true);
  61. if($data['FF']==$FF){
  62. return self::getGameUserInfo('UserID',$data['UserID']);
  63. }else if($data['ua']==$ua){
  64. return self::getGameUserInfo('UserID',$data['UserID']);
  65. }
  66. }
  67. $user=GlobalUserInfo::query()->where('LastLogonIP',$LastLogonIP)->where('FF',$FF)->first();
  68. return $user;
  69. }
  70. public static function UpdateLoginDate($request,$forceLogin=false)
  71. {
  72. if(self::$me) {
  73. $UserID=self::$me->UserID;
  74. $LastLogonDate = date('Y-m-d H:i:s');
  75. $LastLogonIP=IpLocation::getRealIp();
  76. $FPID = $request->input("bfp", "");
  77. $FF = $request->input("ff", "");
  78. if(!Redis::exists('UpdateLoginCheck_'.$UserID)||$forceLogin) {
  79. GameTask::dispatch(['UpdateLogin', [$UserID, $LastLogonDate, $LastLogonIP,$FPID,$FF]]);
  80. Util::WriteLog('24680dispatch', ['UpdateLogin', [$UserID, $LastLogonDate, $LastLogonIP,$FPID,$FF]]);
  81. }
  82. //无论是否通知后端更新,都要记录最新的数据,方便及时取回
  83. $ua=$request->userAgent();
  84. $saved=json_encode(compact('UserID','FPID','FF','LastLogonDate','LastLogonIP','ua'));
  85. Redis::setex('UpdateLoginCheck_'.$UserID,300,$saved);
  86. Redis::setex('UpdateLoginCheck_'.$FF,1200,$saved);
  87. Redis::setex('UpdateLoginCheck_'.$LastLogonIP,1200,$saved);
  88. }
  89. }
  90. protected $primaryKey = 'GlobalUID'; // Set the primary key
  91. public $incrementing = false; // Disable auto-increment
  92. public $timestamps = false; // If the table does not have 'created_at' and 'updated_at' columns
  93. protected $connection = 'mysql';
  94. // 指定表名,如果表名与类名的复数相同则不需要
  95. protected $table = 'webgame.GlobalUserInfo';
  96. // Fillable attributes for mass assignment
  97. protected $fillable = [
  98. 'GlobalUID', 'UserID', 'GameID','FPID','LastFPID','ShortHashID', 'Accounts', 'Email', 'Phone', 'NickName', 'FaceID', 'LogonPass',
  99. 'InsurePass', 'Gender', 'RegisterDate', 'RegisterIP',
  100. 'RegisterLocation', 'DefaultLanguage', 'ServerRegion',
  101. 'ThemeColor', 'Level', 'Exp', 'UserRight',
  102. 'SpreaderID', 'LastLogonIP', 'LastLogonDate', 'ReferrType','Channel','GpsAdid','FavoriteGames','PwaInstalled','Registed'
  103. ];
  104. // Attributes that should be cast to native types
  105. protected $casts = [
  106. 'Gender' => 'integer',
  107. 'Level' => 'integer',
  108. 'Exp' => 'integer',
  109. 'UserRight' => 'integer',
  110. 'ThemeColor' => 'string', // Casting enum as string
  111. ];
  112. // Custom date attributes
  113. protected $dates = [
  114. 'RegisterDate',
  115. 'LastLogonDate'
  116. ];
  117. public static function faceidToAvatar($faceID)
  118. {
  119. return "https://cdn.moeda777.com/24680/assets/avatar/2/avatar_$faceID.png";
  120. // return "https://24680.imgix.net/24680/assets/avatar/5/avatar_$faceID.webp?auto=format,compress&cs=srgb";
  121. }
  122. /**
  123. * @param $key 'UserID', 'GlobalUID', 'Email', 'Phone'
  124. * @param $value
  125. * @return GlobalUserInfo
  126. */
  127. public static function getGameUserInfo($key, $value)
  128. {
  129. if(self::$useCache) {
  130. $cacheKey = "GlobalUserInfo:{$key}:{$value}";
  131. // 尝试从缓存获取
  132. $cached = Redis::get($cacheKey);
  133. if ($cached !== null && $cached !== false) {
  134. $data = json_decode($cached, true);
  135. if ($data) {
  136. $instance = new self();
  137. $instance->exists = true;
  138. $instance->setRawAttributes($data, true);
  139. return $instance;
  140. }
  141. }
  142. }
  143. // 从数据库查询
  144. $user = self::query()->where($key, $value)->first();
  145. if(self::$useCache) {
  146. // 缓存结果(300秒 = 5分钟)
  147. if ($user) {
  148. Redis::setex($cacheKey, 300, json_encode($user->getAttributes()));
  149. }
  150. }
  151. return $user;
  152. }
  153. /**
  154. * @param $key 'UserID', 'GlobalUID', 'Email', 'Phone'
  155. * @param $value
  156. * @return array|false
  157. */
  158. public static function getGameUserInfoToWeb($key, $value)
  159. {
  160. return self::toWebData(self::getGameUserInfo($key, $value));
  161. }
  162. /**
  163. * @param GlobalUserInfo $user
  164. * @return array|false
  165. */
  166. public static function toWebData(GlobalUserInfo $user,$makeBB=false)
  167. {
  168. if(is_array($user)){
  169. if(isset($user['Score'])) return $user;
  170. if(isset($user['UserID'])){
  171. $user = self::getGameUserInfo('UserID', $user['UserID']);
  172. }
  173. }
  174. $data = false;
  175. if ($user) {
  176. $u = $user->toArray();
  177. $existKey = ['UserID', 'GameID', 'GlobalUID','Email', 'Phone','DefaultLanguage', 'NickName', 'FaceID', 'Gender', 'RegisterDate', 'RegisterLocation', 'InsurePass', 'Level', 'Exp', 'UserRight','Channel','PwaInstalled'];
  178. $data = [];
  179. foreach ($existKey as $key) {
  180. if(isset($u[$key]))$data[$key] = $u[$key];
  181. }
  182. if (!empty($data['InsurePass'])) $data['InsurePass'] = 1;
  183. $data['sign'] = self::genGuuidSign($user);
  184. $data['img'] = self::faceidToAvatar($data['FaceID']);
  185. if (!intval($data['GameID'])) {
  186. $data['GameID'] = AccountsInfo::query()->select(['GameID'])->where('UserID', $data['UserID'])->first()->GameID;
  187. $user->GameID = $data['GameID'];
  188. $user->update(['GameID' => $data['GameID']]);
  189. }
  190. $scoreData = self::getScoreDataByUserID($data['UserID']);
  191. $data['Score'] = $scoreData['Score'];
  192. $data['InsureScore'] = $scoreData['InsureScore'];
  193. // if($makeBB){
  194. // $data['bb']=['token'=>(new BetbyService())->getDefaultJWT($u)];
  195. // }
  196. $data['vip'] = VipService::calculateVipLevel($data['UserID'] ?? 0);
  197. //intval(GameScoreInfo::query()->select(['Score'])->where('UserID', $data['UserID'])->first()->Score) / 100;
  198. }
  199. return $data;
  200. }
  201. private static $userToScores=[];
  202. private static $userToScoresData=[];
  203. public static function getScoreByUserID($userID)
  204. {
  205. if(!isset(self::$userToScores[$userID])) {
  206. $scoreObj=GameScoreInfo::query()->select(['Score'])->where('UserID', $userID)->first();
  207. if($scoreObj)self::$userToScores[$userID] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  208. else self::$userToScores[$userID]=0;
  209. }
  210. return self::$userToScores[$userID];
  211. }
  212. public static function getScoreDataByUserID($userID)
  213. {
  214. if(!isset(self::$userToScoresData[$userID])) {
  215. $scoreObj=GameScoreInfo::query()->select(['Score','InsureScore'])->where('UserID', $userID)->first();
  216. if($scoreObj){
  217. self::$userToScoresData[$userID]['Score'] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  218. self::$userToScoresData[$userID]['InsureScore'] = intval($scoreObj->InsureScore) / NumConfig::NUM_VALUE;
  219. $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  220. ->where('UserID', $userID)
  221. ->value('Recharge') ?: 0;
  222. self::$userToScoresData[$userID]['Recharge'] = $user_recharge;
  223. if(!$user_recharge){
  224. self::$userToScoresData[$userID]['Score'] = 0;
  225. self::$userToScoresData[$userID]['InsureScore'] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  226. }
  227. }else{
  228. self::$userToScoresData[$userID]['Score'] = 0;
  229. self::$userToScoresData[$userID]['Recharge'] = 0;
  230. self::$userToScoresData[$userID]['InsureScore'] = 0;
  231. }
  232. }
  233. return self::$userToScoresData[$userID];
  234. }
  235. public static function GlobalToUserID($GlobalUID)
  236. {
  237. return intval(explode('-', $GlobalUID)[3]);
  238. }
  239. public static function genGuuidSign($user)
  240. {
  241. return Crypt::encryptString($user->GlobalUID . '|' . (time() + 86400 * 365) . '|' . substr(md5($user->LogonPass), 0, 6));
  242. }
  243. public function addFavoGame($gameid)
  244. {
  245. $gids=explode(',',$this->FavoriteGames);
  246. array_unshift($gids,$gameid);
  247. if(count($gids)>20){
  248. array_pop($gids);
  249. }
  250. $this->update(['FavoriteGames'=>implode(',',$gids)]);
  251. }
  252. public function removeFavoGame($gameid)
  253. {
  254. $gids=explode(',',$this->FavoriteGames);
  255. foreach($gids as $key=>$gid){
  256. if($gid==$gameid){
  257. array_splice($gids,$key,1);
  258. }
  259. }
  260. $this->update(['FavoriteGames'=>implode(',',$gids)]);
  261. }
  262. /**
  263. * 模型启动方法,注册事件监听
  264. */
  265. protected static function boot()
  266. {
  267. parent::boot();
  268. if(!self::$useCache)return;
  269. // 监听 saved 事件(包括 created 和 updated)
  270. static::saved(function ($user) {
  271. $user->clearUserCache();
  272. });
  273. // 监听 deleted 事件
  274. static::deleted(function ($user) {
  275. $user->clearUserCache();
  276. });
  277. }
  278. public static $useCache=false;
  279. /**
  280. * 清除用户缓存
  281. */
  282. private function clearUserCache()
  283. {
  284. if(!self::$useCache)return;
  285. $keys = ['UserID', 'GlobalUID', 'Email', 'Phone','Accounts','FPID'];
  286. foreach ($keys as $key) {
  287. if (isset($this->attributes[$key]) && !empty($this->attributes[$key])) {
  288. $cacheKey = "GlobalUserInfo:{$key}:{$this->attributes[$key]}";
  289. Redis::del($cacheKey);
  290. }
  291. }
  292. }
  293. }