| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App\Module\User;
- use App\Module\App\SessionApp;
- use App\Module\User\Enums\ActionStatus;
- use App\Module\User\Enums\ActionType;
- use App\Module\User\Enums\Status2;
- use App\Module\User\Model\UserAction;
- use App\Module\User\Unit\UserPublic;
- use Dcore\Db\Arr;
- use Dcore\Db\Helper;
- use Dcore\Helper\Logger;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Support\Facades\DB;
- /**
- * 用户操作
- *
- */
- class Action
- {
- /**
- * 封禁账号
- * @param $admin_id
- * @param $user_id
- * @param $desc
- * @param $re_id
- * @param $re_type
- * @param $exp
- * @return UserAction
- */
- static public function ban($admin_id, $user_id, $desc, $re_id, $re_type,$exp = 360000)
- {
- return self::call_type($admin_id, $user_id, $desc, $re_id, $re_type,ActionType::Ban,$exp);
- }
- /**
- * 封禁用户
- * @param $admin_id
- * @param $desc
- * @param $re_id
- * @param $re_type
- * @return UserAction
- */
- static public function call_type($admin_id, $user_id, $desc, $re_id, $re_type,ActionType $type,$exp = 360000)
- {
- Helper::check_tr();
- // 增加封禁记录
- $model = new UserAction();
- $model->admin_id = $admin_id;
- $model->desc = $desc;
- $model->user_id = $user_id;
- $model->re_id = $re_id;
- $model->re_type = $re_type;
- $model->type =$type;
- if($exp){
- $model->exp_time = time()+$exp;
- $model->status = ActionStatus::Ing;
- }else{
- $model->exp_time = 0;
- $model->status = ActionStatus::Ok;
- }
- if(! $model->save()){
- throw new \LogicException("user - Action save type error. ");
- }
- $list = [
- ActionType::Restrict->value() => Status2::Restrict->value(),
- ActionType::Normal->value() => Status2::Normal->value(),
- ActionType::Delete->value() => Status2::Deleteing->value(),
- ActionType::Ban->value() => Status2::Ban->value(),
- ];
- $status = $list[$type->value()]??null;
- if(is_null($status)){
- throw new \LogicException("user - Action call_type type error. ");
- }
- // 处理账号
- $info = User::info($user_id,true);
- $info->status2 = $status;
- if( ! $info->save()){
- throw new \LogicException("user - Action save error. ");
- }
- if($type === ActionType::Normal){
- // 正常化处理,需要解除进行中的惩罚
- // 没有过期时间的
- UserAction::query()
- ->where('user_id',$user_id)
- ->where('status',ActionStatus::Ok)
- ->whereNotIn('type',[ActionType::Normal])
- ->update([
- 'status'=>ActionStatus::End->value
- ]);
- // 生效中
- UserAction::query()
- ->where('user_id',$user_id)
- ->where('status',ActionStatus::Ing)
- ->whereNotIn('type',[ActionType::Normal])
- ->update([
- 'status'=>ActionStatus::End->value
- ]);
- }
- Logger::info('type',[$type->value()]);
- if($type === ActionType::Ban){
- // 禁止
- Logger::info('ban');
- SessionApp::removeUKeys($user_id);
- }
- if($type === ActionType::Restrict){
- // 限制登陆
- Logger::info('Restrict');
- SessionApp::removeUKeys($user_id);
- }
- return $model;
- }
- /**
- * 过期时间处理
- *
- * @return void
- */
- static public function outtime()
- {
- $time = time();
- /**
- * @var UserAction[] $list
- */
- $list = UserAction::query()
- ->where('status', ActionStatus::Ing)
- ->where('exp_time','>',0)
- ->where('exp_time','<',$time)
- ->limit(10)
- ->get();
- // dump($list);
- foreach ($list as $item){
- try {
- DB::beginTransaction();
- $item->status = ActionStatus::OutTime;
- $item->save();
- self::callItem($item);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- Logger::exception("user-Action-outtime",$exception);
- }
- }
- }
- /**
- * 处理过去
- * @param UserAction $action
- * @return void
- */
- static private function callItem(UserAction $action)
- {
- $type = $action->type->value();
- // 判断是否存在其他生效的禁令
- $lsit = UserAction::query()
- ->where('status', '=', ActionStatus::Ing)
- ->where('user_id','=',$action->user_id)
- ->where('type','=', $action->type)
- ->where(function ( \Illuminate\Database\Eloquent\Builder $builder) {
- $builder->orWhere('exp_time', '=', 0)
- ->orWhere('exp_time', '>', time());
- })->get();
- // ->get();
- // dd($lsit->toSql());
- if($lsit->count()){
- // 有其他禁令生效
- Logger::info("callItem $action->id $type have other. ");
- return;
- }
- $info = User::infoinfo($action->user_id,true);
- if($action->type === ActionType::Ban){
- if($info->status2 === Status2::Ban){
- // 接触 封禁
- self::call_type(\App\Module\Sys\Admin::AUTO_BOT,$action->user_id,"到期自动解除-封禁",$action->id,'ActionAuto',ActionType::Normal,0);
- Logger::info("callItem $action->id $type ban ->Normal ");
- }else{
- Logger::info("callItem $action->id $type now no ban ");
- }
- }
- if($action->type === ActionType::Restrict){
- if($info->status2 === Status2::Restrict){
- // 接触禁止登录
- self::call_type(\App\Module\Sys\Admin::AUTO_BOT,$action->user_id,"到期自动解除-限制登录",$action->id,'ActionAuto',ActionType::Normal);
- Logger::info("callItem $action->id $type Restrict ->Normal ");
- }else{
- Logger::info("callItem $action->id $type now no Restrict ");
- }
- }
- }
- }
|