GlobalUserInfo.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. $cacheKey = "GlobalUserInfo:{$key}:{$value}";
  97. // 尝试从缓存获取
  98. $cached = Redis::get($cacheKey);
  99. if ($cached !== null && $cached !== false) {
  100. $data = json_decode($cached, true);
  101. if ($data) {
  102. $instance = new self();
  103. $instance->exists = true;
  104. $instance->setRawAttributes($data, true);
  105. return $instance;
  106. }
  107. }
  108. // 从数据库查询
  109. $user = self::query()->where($key, $value)->first();
  110. // 缓存结果(300秒 = 5分钟)
  111. if ($user) {
  112. Redis::setex($cacheKey, 300, json_encode($user->getAttributes()));
  113. }
  114. return $user;
  115. }
  116. /**
  117. * @param $key 'UserID', 'GlobalUID', 'Email', 'Phone'
  118. * @param $value
  119. * @return array|false
  120. */
  121. public static function getGameUserInfoToWeb($key, $value)
  122. {
  123. return self::toWebData(self::getGameUserInfo($key, $value));
  124. }
  125. /**
  126. * @param GlobalUserInfo $user
  127. * @return array|false
  128. */
  129. public static function toWebData(GlobalUserInfo $user,$makeBB=false)
  130. {
  131. $data = false;
  132. if ($user) {
  133. $u = $user->toArray();
  134. $existKey = ['UserID', 'GameID', 'GlobalUID','Email', 'Phone','DefaultLanguage', 'NickName', 'FaceID', 'Gender', 'RegisterDate', 'RegisterLocation', 'InsurePass', 'Level', 'Exp', 'UserRight','Channel'];
  135. $data = [];
  136. foreach ($existKey as $key) {
  137. $data[$key] = $u[$key];
  138. }
  139. if (!empty($data['InsurePass'])) $data['InsurePass'] = 1;
  140. $data['sign'] = self::genGuuidSign($user);
  141. $data['img'] = self::faceidToAvatar($data['FaceID']);
  142. if (!intval($data['GameID'])) {
  143. $data['GameID'] = AccountsInfo::query()->select(['GameID'])->where('UserID', $data['UserID'])->first()->GameID;
  144. $user->GameID = $data['GameID'];
  145. $user->update(['GameID' => $data['GameID']]);
  146. }
  147. $scoreData = self::getScoreDataByUserID($data['UserID']);
  148. $data['Score'] = $scoreData['Score'];
  149. $data['InsureScore'] = $scoreData['InsureScore'];
  150. if($makeBB){
  151. $data['bb']=['token'=>(new BetbyService())->getDefaultJWT($u)];
  152. }
  153. $data['vip'] = VipService::calculateVipLevel($data['UserID'] ?? 0);
  154. //intval(GameScoreInfo::query()->select(['Score'])->where('UserID', $data['UserID'])->first()->Score) / 100;
  155. }
  156. return $data;
  157. }
  158. private static $userToScores=[];
  159. private static $userToScoresData=[];
  160. public static function getScoreByUserID($userID)
  161. {
  162. if(!isset(self::$userToScores[$userID])) {
  163. $scoreObj=GameScoreInfo::query()->select(['Score'])->where('UserID', $userID)->first();
  164. if($scoreObj)self::$userToScores[$userID] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  165. else self::$userToScores[$userID]=0;
  166. }
  167. return self::$userToScores[$userID];
  168. }
  169. public static function getScoreDataByUserID($userID)
  170. {
  171. if(!isset(self::$userToScoresData[$userID])) {
  172. $scoreObj=GameScoreInfo::query()->select(['Score','InsureScore'])->where('UserID', $userID)->first();
  173. if($scoreObj){
  174. self::$userToScoresData[$userID]['Score'] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  175. self::$userToScoresData[$userID]['InsureScore'] = intval($scoreObj->InsureScore) / NumConfig::NUM_VALUE;
  176. $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  177. ->where('UserID', $userID)
  178. ->value('Recharge') ?: 0;
  179. self::$userToScoresData[$userID]['Recharge'] = $user_recharge;
  180. if(!$user_recharge){
  181. self::$userToScoresData[$userID]['Score'] = 0;
  182. self::$userToScoresData[$userID]['InsureScore'] = intval($scoreObj->Score) / NumConfig::NUM_VALUE;
  183. }
  184. }else{
  185. self::$userToScoresData[$userID]['Score'] = 0;
  186. self::$userToScoresData[$userID]['Recharge'] = 0;
  187. self::$userToScoresData[$userID]['InsureScore'] = 0;
  188. }
  189. }
  190. return self::$userToScoresData[$userID];
  191. }
  192. public static function GlobalToUserID($GlobalUID)
  193. {
  194. return intval(explode('-', $GlobalUID)[3]);
  195. }
  196. public static function genGuuidSign($user)
  197. {
  198. return Crypt::encryptString($user->GlobalUID . '|' . (time() + 86400 * 365) . '|' . substr(md5($user->LogonPass), 0, 6));
  199. }
  200. public function addFavoGame($gameid)
  201. {
  202. $gids=explode(',',$this->FavoriteGames);
  203. array_unshift($gids,$gameid);
  204. if(count($gids)>20){
  205. array_pop($gids);
  206. }
  207. $this->update(['FavoriteGames'=>implode(',',$gids)]);
  208. }
  209. public function removeFavoGame($gameid)
  210. {
  211. $gids=explode(',',$this->FavoriteGames);
  212. foreach($gids as $key=>$gid){
  213. if($gid==$gameid){
  214. array_splice($gids,$key,1);
  215. }
  216. }
  217. $this->update(['FavoriteGames'=>implode(',',$gids)]);
  218. }
  219. /**
  220. * 模型启动方法,注册事件监听
  221. */
  222. protected static function boot()
  223. {
  224. parent::boot();
  225. // 监听 saved 事件(包括 created 和 updated)
  226. static::saved(function ($user) {
  227. $user->clearUserCache();
  228. });
  229. // 监听 deleted 事件
  230. static::deleted(function ($user) {
  231. $user->clearUserCache();
  232. });
  233. }
  234. /**
  235. * 清除用户缓存
  236. */
  237. private function clearUserCache()
  238. {
  239. $keys = ['UserID', 'GlobalUID', 'Email', 'Phone','Accounts','FPID'];
  240. foreach ($keys as $key) {
  241. if (isset($this->attributes[$key]) && !empty($this->attributes[$key])) {
  242. $cacheKey = "GlobalUserInfo:{$key}:{$this->attributes[$key]}";
  243. Redis::del($cacheKey);
  244. }
  245. }
  246. }
  247. }