Browse Source

用户自动清除消息脚本

master
wanghongjun 4 months ago
parent
commit
aee095db60
  1. 139
      app/common/task/UserClearMessage.php
  2. 1
      config/cron.php

139
app/common/task/UserClearMessage.php

@ -0,0 +1,139 @@
<?php
namespace app\common\task;
use app\enterprise\model\Friend;
use app\enterprise\model\GroupUser;
use app\enterprise\model\Message;
use app\manage\model\Config;
use yunwuxin\cron\Task;
class UserClearMessage extends Task
{
// 定时任务日志内容
protected $content = '';
protected $path = '';
protected $daytime = 60;//86400;
/**
* 自动写入定时任务日志
* @return \think\response\Json
*/
protected function writeLog($text)
{
$this->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";
}
}
}

1
config/cron.php

@ -4,5 +4,6 @@ return [
'tasks' => [
\app\common\task\ClearMessage::class, //定时清理消息
\app\common\task\SetAtRead::class, //定时清理@消息
\app\common\task\UserClearMessage::class, //用户定时清理消息
]
];
Loading…
Cancel
Save