GlobalUserInfo.php 8.1 KB

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