GlobalUserInfo.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Game;
  3. use App\Game\Services\BetbyService;
  4. use App\Http\helper\NumConfig;
  5. use App\IpLocation;
  6. use App\Jobs\GameTask;
  7. use App\Models\AccountsInfo;
  8. use App\Models\Treasure\GameScoreInfo;
  9. use App\Util;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Support\Facades\Crypt;
  12. use Illuminate\Support\Facades\Redis;
  13. class GlobalUserInfo extends Model
  14. {
  15. /**
  16. * @var GlobalUserInfo
  17. */
  18. public static $me=null;
  19. public static function getLocale()
  20. {
  21. if(isset(self::$me)&&!empty(self::$me)&&!empty(self::$me->DefaultLanguage)){
  22. return substr(self::$me->DefaultLanguage,0,2);
  23. }else{
  24. return substr( @$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
  25. }
  26. }
  27. public static function getLocaleByUserID($UserID,$default='en')
  28. {
  29. if(isset(self::$me)&&!empty(self::$me)&&!empty(self::$me->DefaultLanguage)&&self::$me->UserID==$UserID){
  30. return substr(self::$me->DefaultLanguage,0,2);
  31. }else{
  32. $user=self::getGameUserInfo('UserID',$UserID);
  33. $locale=$user->DefaultLanguage??substr( @$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
  34. if(!$locale||empty($locale)){
  35. $locale=env('DEFAULT_LOCALE','en');
  36. }
  37. return strstr(env('VALID_LOCALE','en,es,pt,ru'),$locale)?$locale:$default;
  38. }
  39. }
  40. public static function UpdateLoginDate($request,$forceLogin=false)
  41. {
  42. if(self::$me) {
  43. $UserID=self::$me->UserID;
  44. $LastLogonDate = date('Y-m-d H:i:s');
  45. $LastLogonIP=IpLocation::getRealIp();
  46. if(!Redis::exists('UpdateLoginCheck_'.$UserID)||$forceLogin) {
  47. Redis::setex('UpdateLoginCheck_'.$UserID,600,$UserID);
  48. $FPID = $request->input("bfp", "");
  49. GameTask::dispatch(['UpdateLogin', [$UserID, $LastLogonDate, $LastLogonIP,$FPID]]);
  50. Util::WriteLog('24680dispatch', ['UpdateLogin', [$UserID, $LastLogonDate, $LastLogonIP]]);
  51. }
  52. }
  53. }
  54. protected $primaryKey = 'GlobalUID'; // Set the primary key
  55. public $incrementing = false; // Disable auto-increment
  56. public $timestamps = false; // If the table does not have 'created_at' and 'updated_at' columns
  57. protected $connection = 'mysql';
  58. // 指定表名,如果表名与类名的复数相同则不需要
  59. protected $table = 'webgame.GlobalUserInfo';
  60. // Fillable attributes for mass assignment
  61. protected $fillable = [
  62. 'GlobalUID', 'UserID', 'GameID','FPID','LastFPID','ShortHashID', 'Accounts', 'Email', 'Phone', 'NickName', 'FaceID', 'LogonPass',
  63. 'InsurePass', 'Gender', 'RegisterDate', 'RegisterIP',
  64. 'RegisterLocation', 'DefaultLanguage', 'ServerRegion',
  65. 'ThemeColor', 'Level', 'Exp', 'UserRight',
  66. 'SpreaderID', 'LastLogonIP', 'LastLogonDate', 'ReferrType','Channel','GpsAdid','FavoriteGames','PwaInstalled'
  67. ];
  68. // Attributes that should be cast to native types
  69. protected $casts = [
  70. 'Gender' => 'integer',
  71. 'Level' => 'integer',
  72. 'Exp' => 'integer',
  73. 'UserRight' => 'integer',
  74. 'ThemeColor' => 'string', // Casting enum as string
  75. ];
  76. // Custom date attributes
  77. protected $dates = [
  78. 'RegisterDate',
  79. 'LastLogonDate'
  80. ];
  81. public static function faceidToAvatar($faceID)
  82. {
  83. // return "https://cdn.moeda777.com/24680/assets/avatar/2/avatar_$faceID.png";
  84. return "https://24680.imgix.net/24680/assets/avatar/2/avatar_$faceID.png?auto=format,compress&cs=srgb";
  85. }
  86. /**
  87. * @param $key 'UserID', 'GlobalUID', 'Email', 'Phone'
  88. * @param $value
  89. * @return GlobalUserInfo
  90. */
  91. public static function getGameUserInfo($key, $value)
  92. {
  93. return self::query()->where($key, $value)->first();
  94. }
  95. /**
  96. * @param $key 'UserID', 'GlobalUID', 'Email', 'Phone'
  97. * @param $value
  98. * @return array|false
  99. */
  100. public static function getGameUserInfoToWeb($key, $value)
  101. {
  102. return self::toWebData(self::getGameUserInfo($key, $value));
  103. }
  104. /**
  105. * @param GlobalUserInfo $user
  106. * @return array|false
  107. */
  108. public static function toWebData(GlobalUserInfo $user,$makeBB=false)
  109. {
  110. $data = false;
  111. if ($user) {
  112. $u = $user->toArray();
  113. $existKey = ['UserID', 'GameID', 'GlobalUID','Email', 'Phone','DefaultLanguage', 'NickName', 'FaceID', 'Gender', 'RegisterDate', 'RegisterLocation', 'InsurePass', 'Level', 'Exp', 'UserRight','Channel'];
  114. $data = [];
  115. foreach ($existKey as $key) {
  116. $data[$key] = $u[$key];
  117. }
  118. if (!empty($data['InsurePass'])) $data['InsurePass'] = 1;
  119. $data['sign'] = self::genGuuidSign($user);
  120. $data['img'] = self::faceidToAvatar($data['FaceID']);
  121. if (!intval($data['GameID'])) {
  122. $data['GameID'] = AccountsInfo::query()->select(['GameID'])->where('UserID', $data['UserID'])->first()->GameID;
  123. $user->GameID = $data['GameID'];
  124. $user->update(['GameID' => $data['GameID']]);
  125. }
  126. $data['Score'] = self::getScoreByUserID($data['UserID']);
  127. if($makeBB){
  128. $data['bb']=['token'=>(new BetbyService())->getDefaultJWT($u)];
  129. }
  130. //intval(GameScoreInfo::query()->select(['Score'])->where('UserID', $data['UserID'])->first()->Score) / 100;
  131. }
  132. return $data;
  133. }
  134. private static $userToScores=[];
  135. public static function getScoreByUserID($userID)
  136. {
  137. if(!isset(self::$userToScores[$userID])) {
  138. $scoreObj=GameScoreInfo::query()->select(['Score'])->where('UserID', $userID)->first();
  139. if($scoreObj)self::$userToScores[$userID] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  140. else self::$userToScores[$userID]=0;
  141. }
  142. return self::$userToScores[$userID];
  143. }
  144. public static function GlobalToUserID($GlobalUID)
  145. {
  146. return intval(explode('-', $GlobalUID)[3]);
  147. }
  148. public static function genGuuidSign($user)
  149. {
  150. return Crypt::encryptString($user->GlobalUID . '|' . (time() + 86400 * 365) . '|' . substr(md5($user->LogonPass), 0, 6));
  151. }
  152. public function addFavoGame($gameid)
  153. {
  154. $gids=explode(',',$this->FavoriteGames);
  155. array_unshift($gids,$gameid);
  156. if(count($gids)>20){
  157. array_pop($gids);
  158. }
  159. $this->update(['FavoriteGames'=>implode(',',$gids)]);
  160. }
  161. public function removeFavoGame($gameid)
  162. {
  163. $gids=explode(',',$this->FavoriteGames);
  164. foreach($gids as $key=>$gid){
  165. if($gid==$gameid){
  166. array_splice($gids,$key,1);
  167. }
  168. }
  169. $this->update(['FavoriteGames'=>implode(',',$gids)]);
  170. }
  171. }