ActionService.php 6.2 KB

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