path = root_path() . 'crontab2.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(); $this->everyMinute(); } /** * 执行任务 * @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], ['is_group', '=', 0], ['chat_identify', '=', $chat_identify] ]; $msgRes = Message::where($whereMsg)->select(); foreach ($msgRes as $msg) { $msg->force()->delete(); File::syncDelFile($msg->file_id); } wsSendMsg([$form_user, $to_user], 'delMessageAll', ['form_user' => $form_user, 'to_user' => $to_user]); } // 群聊个人记录删除 $whereGroupUser = [ ['status','=' , 1], ['delete_time','=' , 0], ['clear_msg_day','>' , 0] ]; $group = GroupModel::where($whereGroupUser)->field('group_id,clear_msg_day')->select(); foreach ($group as $item) { $days = $item['clear_msg_day']; $group_id = $item['group_id']; $time = time() - ($days * $this->daytime); $chat_identify = "group-{$group_id}"; $whereMsg = [ ['create_time', '<', $time], ['is_group', '=', 1], ['to_user', '=', $group_id], ['chat_identify', '=', $chat_identify] ]; $msgRes = Message::where($whereMsg)->select(); foreach ($msgRes as $msg) { $msg->force()->delete(); File::syncDelFile($msg->file_id); } GroupUser::editGroupUser(['group_id'=>$group_id], ['unread' => 0]); wsSendMsg($group_id, 'delMessageAll', ['group_id' => $group_id], 1); } } print "****************消息清理成功******************\n"; } catch (Exception $e) { print '消息清理失败:'.$e->getMessage()."\n"; } } }