Action.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Module\User;
  3. use App\Module\App\SessionApp;
  4. use App\Module\User\Enums\ActionStatus;
  5. use App\Module\User\Enums\ActionType;
  6. use App\Module\User\Enums\Status2;
  7. use App\Module\User\Model\UserAction;
  8. use App\Module\User\Unit\UserPublic;
  9. use Dcore\Db\Arr;
  10. use Dcore\Db\Helper;
  11. use Dcore\Helper\Logger;
  12. use Illuminate\Database\Query\Builder;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 用户操作
  16. *
  17. */
  18. class Action
  19. {
  20. /**
  21. * 封禁账号
  22. * @param $admin_id
  23. * @param $user_id
  24. * @param $desc
  25. * @param $re_id
  26. * @param $re_type
  27. * @param $exp
  28. * @return UserAction
  29. */
  30. static public function ban($admin_id, $user_id, $desc, $re_id, $re_type,$exp = 360000)
  31. {
  32. return self::call_type($admin_id, $user_id, $desc, $re_id, $re_type,ActionType::Ban,$exp);
  33. }
  34. /**
  35. * 封禁用户
  36. * @param $admin_id
  37. * @param $desc
  38. * @param $re_id
  39. * @param $re_type
  40. * @return UserAction
  41. */
  42. static public function call_type($admin_id, $user_id, $desc, $re_id, $re_type,ActionType $type,$exp = 360000)
  43. {
  44. Helper::check_tr();
  45. // 增加封禁记录
  46. $model = new UserAction();
  47. $model->admin_id = $admin_id;
  48. $model->desc = $desc;
  49. $model->user_id = $user_id;
  50. $model->re_id = $re_id;
  51. $model->re_type = $re_type;
  52. $model->type =$type;
  53. if($exp){
  54. $model->exp_time = time()+$exp;
  55. $model->status = ActionStatus::Ing;
  56. }else{
  57. $model->exp_time = 0;
  58. $model->status = ActionStatus::Ok;
  59. }
  60. if(! $model->save()){
  61. throw new \LogicException("user - Action save type error. ");
  62. }
  63. $list = [
  64. ActionType::Restrict->value() => Status2::Restrict->value(),
  65. ActionType::Normal->value() => Status2::Normal->value(),
  66. ActionType::Delete->value() => Status2::Deleteing->value(),
  67. ActionType::Ban->value() => Status2::Ban->value(),
  68. ];
  69. $status = $list[$type->value()]??null;
  70. if(is_null($status)){
  71. throw new \LogicException("user - Action call_type type error. ");
  72. }
  73. // 处理账号
  74. $info = User::info($user_id,true);
  75. $info->status2 = $status;
  76. if( ! $info->save()){
  77. throw new \LogicException("user - Action save error. ");
  78. }
  79. if($type === ActionType::Normal){
  80. // 正常化处理,需要解除进行中的惩罚
  81. // 没有过期时间的
  82. UserAction::query()
  83. ->where('user_id',$user_id)
  84. ->where('status',ActionStatus::Ok)
  85. ->whereNotIn('type',[ActionType::Normal])
  86. ->update([
  87. 'status'=>ActionStatus::End->value
  88. ]);
  89. // 生效中
  90. UserAction::query()
  91. ->where('user_id',$user_id)
  92. ->where('status',ActionStatus::Ing)
  93. ->whereNotIn('type',[ActionType::Normal])
  94. ->update([
  95. 'status'=>ActionStatus::End->value
  96. ]);
  97. }
  98. Logger::info('type',[$type->value()]);
  99. if($type === ActionType::Ban){
  100. // 禁止
  101. Logger::info('ban');
  102. SessionApp::removeUKeys($user_id);
  103. }
  104. if($type === ActionType::Restrict){
  105. // 限制登陆
  106. Logger::info('Restrict');
  107. SessionApp::removeUKeys($user_id);
  108. }
  109. return $model;
  110. }
  111. /**
  112. * 过期时间处理
  113. *
  114. * @return void
  115. */
  116. static public function outtime()
  117. {
  118. $time = time();
  119. /**
  120. * @var UserAction[] $list
  121. */
  122. $list = UserAction::query()
  123. ->where('status', ActionStatus::Ing)
  124. ->where('exp_time','>',0)
  125. ->where('exp_time','<',$time)
  126. ->limit(10)
  127. ->get();
  128. // dump($list);
  129. foreach ($list as $item){
  130. try {
  131. DB::beginTransaction();
  132. $item->status = ActionStatus::OutTime;
  133. $item->save();
  134. self::callItem($item);
  135. DB::commit();
  136. }catch (\Exception $exception){
  137. DB::rollBack();
  138. Logger::exception("user-Action-outtime",$exception);
  139. }
  140. }
  141. }
  142. /**
  143. * 处理过去
  144. * @param UserAction $action
  145. * @return void
  146. */
  147. static private function callItem(UserAction $action)
  148. {
  149. $type = $action->type->value();
  150. // 判断是否存在其他生效的禁令
  151. $lsit = UserAction::query()
  152. ->where('status', '=', ActionStatus::Ing)
  153. ->where('user_id','=',$action->user_id)
  154. ->where('type','=', $action->type)
  155. ->where(function ( \Illuminate\Database\Eloquent\Builder $builder) {
  156. $builder->orWhere('exp_time', '=', 0)
  157. ->orWhere('exp_time', '>', time());
  158. })->get();
  159. // ->get();
  160. // dd($lsit->toSql());
  161. if($lsit->count()){
  162. // 有其他禁令生效
  163. Logger::info("callItem $action->id $type have other. ");
  164. return;
  165. }
  166. $info = User::infoinfo($action->user_id,true);
  167. if($action->type === ActionType::Ban){
  168. if($info->status2 === Status2::Ban){
  169. // 接触 封禁
  170. self::call_type(\App\Module\Sys\Admin::AUTO_BOT,$action->user_id,"到期自动解除-封禁",$action->id,'ActionAuto',ActionType::Normal,0);
  171. Logger::info("callItem $action->id $type ban ->Normal ");
  172. }else{
  173. Logger::info("callItem $action->id $type now no ban ");
  174. }
  175. }
  176. if($action->type === ActionType::Restrict){
  177. if($info->status2 === Status2::Restrict){
  178. // 接触禁止登录
  179. self::call_type(\App\Module\Sys\Admin::AUTO_BOT,$action->user_id,"到期自动解除-限制登录",$action->id,'ActionAuto',ActionType::Normal);
  180. Logger::info("callItem $action->id $type Restrict ->Normal ");
  181. }else{
  182. Logger::info("callItem $action->id $type now no Restrict ");
  183. }
  184. }
  185. }
  186. }