diff --git a/app/common/task/UserClearMessage.php b/app/common/task/UserClearMessage.php new file mode 100644 index 0000000..79c0f7a --- /dev/null +++ b/app/common/task/UserClearMessage.php @@ -0,0 +1,139 @@ +path = root_path() . 'crontab.txt'; + + $content = '重置中!'; + if (!file_exists($this->path)) { + fopen($this->path, 'w'); + } + if (date('d') != 10) { + $content = file_get_contents($this->path); + } + file_put_contents($this->path, $content . date('Y-m-d H:i:s') . ':' . $text . PHP_EOL); + } + + public function configure() + { + //设置每天2点执行 +// $this->dailyAt('02:00'); + $this->everyFiveMinutes(); + } + + /** + * 执行任务 + * @return mixed + */ + protected function execute() + { + if(date('H:i')!='02:00'){ + return false; + } + try { + $config=Config::getSystemInfo(); + $status=$config['chatInfo']['userMsgClear'] ?? false; + if($status){ + // 个人记录删除 + $whereFriend = [ + ['is_blacklist','=' , 0], + ['status','=' , 1], + ['delete_time','=' , 0], + ['clear_msg_day','>' , 0] + ]; + $friend = Friend::where($whereFriend)->field('create_user,friend_user_id,clear_msg_day')->select(); + foreach ($friend as $item) { + $days = $item['clear_msg_day']; + $to_user = $item['friend_user_id']; + $form_user = $item['create_user']; + $time = time() - ($days * $this->daytime); + $chat_identify = chat_identify($form_user, $to_user); + + $whereMsg = [ + ['create_time', '<', $time], + ['from_user', '=', $form_user], + ['to_user', '=', $to_user], + ['chat_identify', '=', $chat_identify] + ]; + $msgRes = Message::where($whereMsg)->select(); + foreach ($msgRes as $msg) { + if ($msg['del_user']) { + $msg->delete_time = time(); + } else { + $msg->del_user = $form_user; + } + $msg->save(); + } + } + + // 群聊个人记录删除 + $whereGroupUser = [ + ['status','=' , 1], + ['delete_time','=' , 0], + ['clear_msg_day','>' , 0] + ]; + + $groupUser = GroupUser::where($whereGroupUser)->field('user_id,group_id,clear_msg_day')->select(); + foreach ($groupUser as $item) { + $days = $item['clear_msg_day']; + $group_id = $item['group_id']; + $form_user = $item['user_id']; + $time = time() - ($days * $this->daytime); + + $chat_identify = "group-{$group_id}"; + // 查询群成员总数 + $countWhere = [ + ['status','=' , 1], + ['delete_time','=' , 0], + ['group_id','=' , 0] + ]; + $group_user_count = GroupUser::where($countWhere)->count(); + + $whereMsg = [ + ['create_time', '<', $time], + ['from_user', '=', $form_user], + ['to_user', '=', $group_id], + ['chat_identify', '=', $chat_identify] + ]; + $msgRes = Message::where($whereMsg)->select(); + foreach ($msgRes as $msg) { + if ($msg['del_user']) { + $msg->del_user .= ',' . $form_user; + } else { + $msg->del_user = $form_user; + } + // 如果群的删除用户数量等于群员数量则删除这条信息 + $del_user_count = count(explode(',',$msg->del_user)); + if ($del_user_count == $group_user_count) { + $msg->delete_time = time(); + } + $msg->save(); + } + } + } + print "****************消息清理成功******************\n"; + } catch (Exception $e) { + print '消息清理失败:'.$e->getMessage()."\n"; + } + } +} \ No newline at end of file diff --git a/config/cron.php b/config/cron.php index 6f4bea6..518870e 100644 --- a/config/cron.php +++ b/config/cron.php @@ -4,5 +4,6 @@ return [ 'tasks' => [ \app\common\task\ClearMessage::class, //定时清理消息 \app\common\task\SetAtRead::class, //定时清理@消息 + \app\common\task\UserClearMessage::class, //用户定时清理消息 ] ]; \ No newline at end of file