288 lines
13 KiB
PHP
288 lines
13 KiB
PHP
<?php
|
||
|
||
namespace app\cron\controller;
|
||
|
||
use app\common\controller\Push;
|
||
use think\Cache;
|
||
use think\Db;
|
||
|
||
|
||
/*
|
||
* 定时任务,每秒执行的方法
|
||
*/
|
||
class RoomPan
|
||
{
|
||
/*
|
||
* 运行函数
|
||
*/
|
||
function index()
|
||
{
|
||
echo "巡乐会礼物发放开始:\n";
|
||
$this->xlh_gift_send();//拍卖房结束提醒
|
||
echo "礼物发放结束 \n";
|
||
|
||
echo "盲盒转盘礼物补发:\n";
|
||
$this->blind_box_turntable_gift_send();//盲盒转盘礼物补发
|
||
echo "盲盒转盘礼物补发结束 \n";
|
||
|
||
echo "查询用户是否在房间内保持心跳:\n";
|
||
$this->keep_room_heartbeat();
|
||
echo "\n";
|
||
}
|
||
|
||
|
||
/*
|
||
* 查询用户是否在房间内保持心跳
|
||
*/
|
||
public function keep_room_heartbeat(){
|
||
$user_list = db::name('vs_room_heartbeat')->select();
|
||
if($user_list){
|
||
foreach ($user_list as $value){
|
||
$is_online = db::name('vs_room_visitor')
|
||
->where(['user_id' => $value['user_id'],'room_id' => $value['room_id']])
|
||
->order('id desc')
|
||
->field('id,is_online,room_id')
|
||
->find();
|
||
if($is_online){//现在 在房间内
|
||
if(time() - $value['updatetime'] >= 180){//三分钟没有心跳
|
||
if($is_online['is_online'] == 2){//之前就是离开
|
||
continue;
|
||
}else{//之前就是在线则改为离开
|
||
db::name('vs_room_visitor')->where('id',$is_online['id'])->update(['is_online'=>2]);
|
||
$text['text'] = '离开';
|
||
$text['user_id'] = $value['user_id'];
|
||
$text['type'] = 2;
|
||
model('api/Chat')->sendMsg(1058,$is_online['room_id'],$text);
|
||
}
|
||
}else{
|
||
if($is_online['is_online'] == 1){//之前就是在线
|
||
continue;
|
||
}else{//之前就是离开则改为在线
|
||
db::name('vs_room_visitor')->where('id',$is_online['id'])->update(['is_online'=>1]);
|
||
$text['text'] = '回到房间';
|
||
$text['user_id'] = $value['user_id'];
|
||
$text['type'] = 1;
|
||
model('api/Chat')->sendMsg(1058,$is_online['room_id'],$text);
|
||
}
|
||
}
|
||
|
||
}else{
|
||
//删掉
|
||
db::name('vs_room_heartbeat')->where('id',$value['id'])->delete();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
* 盲盒转盘礼物推送补发
|
||
*/
|
||
public function blind_box_turntable_gift_send(){
|
||
$blind_box_turntable = db('vs_blind_box_turntable_log')->where(['is_sued'=>0,'createtime'=>['>=',time()-60*30]])->limit(1000)->select();
|
||
if(empty($blind_box_turntable)){
|
||
echo "没有需要发放的礼物 \n";
|
||
}
|
||
echo "开始发放".count($blind_box_turntable)." \n";
|
||
foreach ($blind_box_turntable as $k => $v) {
|
||
$blind_box_turntable_results_log = db('vs_blind_box_turntable_results_log')->where('tid',$v['id'])->select();
|
||
if(empty($blind_box_turntable_results_log)){
|
||
echo $v['id']." 没有需要发放的礼物 \n";
|
||
continue;
|
||
}
|
||
$room_id = $v['room_id'];
|
||
$room_name = Db::name('vs_room')->where(['id' => $room_id, 'apply_status' => 2])->value('room_name');
|
||
$FromUserInfo = Db::name('user')->where(['id'=>$v['user_id']])->find();
|
||
$FromUserInfo['user_id'] = $FromUserInfo['id'];
|
||
$FromUserInfo['icon'][0] = model('api/UserData')->user_wealth_icon($v['user_id']);//财富图标
|
||
$FromUserInfo['icon'][1] = model('api/UserData')->user_charm_icon($v['user_id']);//魅力图标
|
||
$FromUserInfo['chat_bubble'] = model('api/Decorate')->user_decorate_detail($v['user_id'],9);//聊天气泡
|
||
$user_nickname = $FromUserInfo['nickname'];
|
||
$userGiftMap = [];
|
||
foreach ($blind_box_turntable_results_log as $key => $value) {
|
||
$ToUserInfo = Db::name('user')->where(['id' => $value['gift_user_id']])->field('id as user_id,nickname,avatar,sex')->find();
|
||
$draw_gift = Db::name('vs_gift')->where(['gid'=>$value['gift_id']])->find();
|
||
$text_message = $user_nickname . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n";
|
||
if($draw_gift['is_public_server'] == 1) {
|
||
$text_list_new[] = [
|
||
'text' => $text_message,
|
||
'gift_picture' => $draw_gift['base_image'],
|
||
'room_id' => $room_id,
|
||
'fromUserName' => $FromUserInfo['nickname'],
|
||
'toUserName' => $ToUserInfo['nickname'],
|
||
'giftName' => $draw_gift['gift_name'],
|
||
'roomId' => $room_id,
|
||
'number' => $value['count'],
|
||
];
|
||
}
|
||
|
||
$userId = $value['gift_user_id'];
|
||
if (!isset($userGiftMap[$userId])) {
|
||
$userGiftMap[$userId] = [
|
||
'userInfo' => $ToUserInfo,
|
||
'gifts' => []
|
||
];
|
||
}
|
||
$userGiftMap[$userId]['gifts'][] = [
|
||
'gift_id' => $draw_gift['gid'],
|
||
'gift_name' => $draw_gift['gift_name'],
|
||
'count' => $value['count'],
|
||
'play_image' => $draw_gift['play_image'],
|
||
'base_image' => $draw_gift['base_image']
|
||
];
|
||
}
|
||
|
||
// 为每个用户单独推送消息
|
||
foreach($userGiftMap as $userId => $userData) {
|
||
$userInfo = $userData['userInfo'];
|
||
$gifts = $userData['gifts'];
|
||
|
||
// 构建用户专属文本消息,接收者名字只出现一次
|
||
$userTextMessage = $user_nickname . ' 送给 ' . $userInfo['nickname'];
|
||
// 添加该用户收到的所有礼物
|
||
$giftCount = [];
|
||
foreach ($gifts as $gift) {
|
||
$userTextMessage .= ' 盲盒转盘礼物 ' . $gift['gift_name'].' x ' .$gift['count'];
|
||
// 统计相同礼物的数量
|
||
$giftCount[] = [
|
||
'gift_info' => $gift,
|
||
'count' => $gift['count']
|
||
];
|
||
|
||
}
|
||
|
||
$userInfo['icon'][0] = model('api/UserData')->user_wealth_icon($userInfo['user_id']);
|
||
$userInfo['icon'][1] = model('api/UserData')->user_charm_icon($userInfo['user_id']);
|
||
$userInfo['charm'] = db::name('vs_room_user_charm')->where(['user_id' => $userInfo['user_id'],'room_id' => $room_id])->value('charm');
|
||
|
||
$userText = [
|
||
'FromUserInfo' => $FromUserInfo,
|
||
'ToUserInfo' => $userInfo,
|
||
'GiftInfos' => array_values($gifts),
|
||
'text' => $userTextMessage
|
||
];
|
||
|
||
// 聊天室推送系统消息给每个用户
|
||
model('api/Chat')->sendMsg(1038,$room_id,$userText);
|
||
|
||
foreach ($giftCount as $giftData) {
|
||
$gift_inf = $giftData['gift_info'];
|
||
$count = $giftData['count'];
|
||
|
||
// 推送礼物特效消息
|
||
$effectData = [
|
||
'FromUserInfo' => $FromUserInfo,
|
||
'ToUserInfo' => $userInfo,
|
||
'GiftInfo' => $gift_inf,
|
||
'gift_num' => $count,
|
||
'text' => null
|
||
];
|
||
|
||
// 聊天室推送礼物特效消息
|
||
model('api/Chat')->sendMsg(1005,$room_id,$effectData);
|
||
}
|
||
}
|
||
$roomtype = Db::name('vs_room')->where(['id' => $room_id])->value('type_id');
|
||
if($roomtype == 6){
|
||
//推送消息
|
||
$hot_value = db::name('vs_give_gift')->where('from_id', $room_id)->where('from',6)
|
||
->sum('total_price');
|
||
$text1 = [
|
||
'room_id' => $room_id,
|
||
'hot_value' => $hot_value * 10,
|
||
'text' => '房间心动值变化'
|
||
];
|
||
//聊天室推送系统消息
|
||
model('api/Chat')->sendMsg(1028,$room_id,$text1);
|
||
}else{
|
||
if(!empty($text_list_new)){
|
||
//推送礼物横幅
|
||
$push = new Push($v['user_id'], $room_id);
|
||
$push->giftBanner($text_list_new);
|
||
}
|
||
}
|
||
db::name('vs_blind_box_turntable_log')->where('id', $v['id'])->update(['is_sued' => 1, 'updatetime' => time()]);
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 巡乐会结束 礼物发放 【定时脚本】
|
||
*/
|
||
public function xlh_gift_send(){
|
||
$xlh_list = db::name('vs_room_pan_xlh')->where(['send_time'=>0,'end_time'=>['<=',time()]])->select();
|
||
if(empty($xlh_list)){
|
||
echo "没有需要发放的礼物 \n";
|
||
}
|
||
foreach ($xlh_list as $key=>$value){
|
||
try{
|
||
if($value['user_id'] == 0){
|
||
echo "第.".$value['periods']." 巡乐会结束 没有中奖用户 \n";
|
||
$res = db::name('vs_room_pan_xlh')->where('id',$value['id'])->update([
|
||
'send_time' => time()
|
||
]);
|
||
// db::name("vs_room")->where('id',$value['room_id'])->update([
|
||
// 'xlh_periods_num' => 0
|
||
// ]);
|
||
Cache::set("xlh_periods_num", 0, 0);
|
||
//推送礼物横幅
|
||
$text = "本轮巡乐会已结束,请大家重新开始下一轮巡乐会";
|
||
$push = new Push(0, $value['room_id']);
|
||
$text_list_new = [
|
||
'text' => $text,
|
||
'room_id' => $value['room_id'],
|
||
'from_type' => 104
|
||
];
|
||
$push->xunlehui($text_list_new);
|
||
continue;
|
||
}
|
||
//发放
|
||
//抽中礼物落包
|
||
$res = [];
|
||
$res = model('api/UserGiftPack')->change_user_gift_pack($value['user_id'],$value['gift_id'],$value['num'],model('api/UserGiftPack')::XLH_DRAW_GIFT_GET,"巡乐会中奖发放");
|
||
if ($res['code'] != 1) {
|
||
echo $res['msg']."\n";
|
||
continue;
|
||
}
|
||
echo "巡乐会中奖礼物发放成功 用户Id:".$value['user_id']."\n";
|
||
//房主礼物落包
|
||
$res = [];
|
||
//获取房主id
|
||
$user_id = db::name('vs_room')->where(['id'=>$value['room_id']])->value('user_id');
|
||
$res = model('api/UserGiftPack')->change_user_gift_pack($user_id,$value['homeowner_gift_id'],1,model('api/UserGiftPack')::XLH_DRAW_GIFT_GET,"巡乐会中奖后房主礼物发放");
|
||
if ($res['code'] != 1) {
|
||
echo $res['msg']."\n";
|
||
continue;
|
||
}
|
||
echo "巡乐会中奖后房主礼物发放成功 房主Id:".$user_id."\n";
|
||
//处理发放记录
|
||
$res = [];
|
||
$res = db::name('vs_room_pan_xlh')->where('id',$value['id'])->update([
|
||
'send_time' => time()
|
||
]);
|
||
$xlh_log = db::name('vs_room_pan_xlh_log')->where(['xlh_id'=>$value['id'],'user_id'=>$value['user_id']])->order('id desc')->find();
|
||
$res = db::name('vs_room_pan_xlh_log')->where('id',$xlh_log['id'])->update([
|
||
'is_send' => 1
|
||
]);
|
||
if ($res === false) {
|
||
echo "处理发放记录失败 \n";
|
||
continue;
|
||
}
|
||
Cache::set("xlh_periods_num", 0, 0);
|
||
//推送
|
||
$FromUserInfo = db::name('user')->field('nickname,avatar')->where(['id'=>$value['user_id']])->find();
|
||
$gift_name = db::name('vs_gift')->where(['gid'=>$value['gift_id']])->value('gift_name');
|
||
$text = $FromUserInfo['nickname'] . ' 用户在巡乐会中 ' .$gift_name.'礼物 x ' .$value['num']." 已收入背包";
|
||
//推送礼物横幅
|
||
$push = new Push(0, $value['room_id']);
|
||
$text_list_new = [
|
||
'text' => $text,
|
||
'room_id' => $value['room_id'],
|
||
'from_type' => 104
|
||
];
|
||
$push->xunlehui($text_list_new);
|
||
}catch (\Exception $e){
|
||
echo $e->getMessage()."\n";
|
||
}
|
||
}
|
||
}
|
||
} |