AccountLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\v1\logic\customer;
  3. use plugin\saiadmin\basic\BaseLogic;
  4. use plugin\saiadmin\exception\ApiException;
  5. use plugin\saiadmin\utils\Helper;
  6. use app\v1\model\customer\Account;
  7. use support\Redis;
  8. use support\think\Db;
  9. /**
  10. * 账号信息逻辑层
  11. */
  12. class AccountLogic extends BaseLogic
  13. {
  14. /**
  15. * 构造函数
  16. */
  17. public function __construct()
  18. {
  19. $this->model = new Account();
  20. }
  21. /**
  22. * 获取IP地理位置
  23. */
  24. public function getIpLocation($ip): string
  25. {
  26. $ip2region = new \Ip2Region();
  27. try {
  28. $region = $ip2region->memorySearch($ip);
  29. } catch (\Exception $e) {
  30. return '未知';
  31. }
  32. list($country, $number, $province, $city, $network) = explode('|', $region['region']);
  33. if ($network === '内网IP') {
  34. return $network;
  35. }
  36. if ($country == '中国') {
  37. return $province.'-'.$city.':'.$network;
  38. } else if ($country == '0') {
  39. return '未知';
  40. } else {
  41. return $country;
  42. }
  43. }
  44. /**
  45. * 列表
  46. */
  47. public function list($query)
  48. {
  49. if($query['type'] == '1'){
  50. $uid = Db::connect('db_origin')->table('users')->where('user_name', $query['val'])->value('uid');
  51. $data = Db::connect('db_origin')->table('user_'.$uid%10)->where('uid', $uid)->select()->toArray();
  52. }else{
  53. $data = Db::connect('db_origin')->table('user_'.$query['val']%10)->where('uid', $query['val'])->select()->toArray();
  54. }
  55. $data = array_map(function($item){
  56. $item['login_ip'] = $item['login_ip'].$this->getIpLocation($item['login_ip']);
  57. $item['reg_ip'] = $item['reg_ip'].$this->getIpLocation($item['reg_ip']);
  58. $item['reg_time'] = date('Y-m-d H:i:s', $item['reg_time']);
  59. $item['login_time'] = date('Y-m-d H:i:s', $item['login_time']);
  60. unset($item['user_pwd']);
  61. return $item;
  62. }, $data);
  63. return $data;
  64. }
  65. /**
  66. * 修改密码
  67. */
  68. public function updatePwd($data)
  69. {
  70. $user_pwd = password_hash($data['user_pwd'], PASSWORD_DEFAULT);
  71. return Db::connect('db_origin')->table('user_'.$data['uid']%10)->where('uid', $data['uid'])->update(['user_pwd' => $user_pwd]);
  72. }
  73. /**
  74. * 修改手机
  75. */
  76. public function updateMobile($data)
  77. {
  78. return Db::connect('db_origin')->table('user_'.$data['uid']%10)->where('uid', $data['uid'])->update(['mobile' => $data['mobile']]);
  79. }
  80. /**
  81. * 修改真实名
  82. */
  83. public function updateReal($data)
  84. {
  85. return Db::connect('db_origin')->table('user_'.$data['uid']%10)->where('uid', $data['uid'])->update(['real_name' => $data['real_name'], 'id_card' => $data['id_card']]);
  86. }
  87. /**
  88. * 封禁列表
  89. */
  90. public function getBanList($query)
  91. {
  92. $saiType = request()->input('saiType', 'list');
  93. $page = request()->input('page', 1);
  94. $limit = request()->input('limit', 10);
  95. $orderBy = request()->input('orderBy', '');
  96. $orderType = request()->input('orderType', $this->orderType);
  97. if(empty($orderBy)) {
  98. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  99. }
  100. if ($saiType === 'all') {
  101. $data = Db::connect('db_game_log')->table('ban_log')->where($query)->order($orderBy, $orderType)->select()->toArray();
  102. }else{
  103. $data = Db::connect('db_game_log')->table('ban_log')->where($query)->order($orderBy, $orderType)->paginate($limit)->toArray();
  104. }
  105. $data['data'] = array_map(function($item) {
  106. $item['action'] = $item['action'] == 1 ? '封禁' : '解封';
  107. $item['type'] = $item['type'] == 1 ? 'UID' : ($item['type'] == 2 ? 'IP' : '设备');
  108. return $item;
  109. }, $data['data']);
  110. $data['data'] = $this->trandformListColumn($data['data'],['auth']);
  111. return $data;
  112. }
  113. /**
  114. * 添加/解除封禁
  115. */
  116. public function saveBan($data)
  117. {
  118. $type = $data['type']; // 封禁类型
  119. $value = $data['value']; // 封禁值
  120. $action = $data['action']; // 操作
  121. $reason = $data['reason']; // 理由
  122. $auth_id = $data['auth_id']; // 操作人
  123. $data = Db::connect('db_game_log')->table('ban_log')->insert([
  124. 'type' => $type,
  125. 'value' => $value,
  126. 'action' => $action,
  127. 'reason' => $reason,
  128. 'auth_id' => $auth_id,
  129. ]);
  130. // 如果成功插入,则更新redis
  131. if($data){
  132. $key = $type==1 ? 'ban_uid' : ($type==2 ? 'ban_ip' : ($type==3 ? 'ban_device' : ''));
  133. $value = explode(',', $value);
  134. $action == 1 ? Redis::sAdd($key, ...$value) : Redis::sRem($key, ...$value);
  135. }
  136. return $data;
  137. }
  138. }