GlobalUserInfo.php 8.3 KB

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