Files
p_ysk/extend/chat/model/Business.php
2025-11-24 11:25:10 +08:00

45 lines
1.2 KiB
PHP

<?php
namespace extend\chat\model;
use ba\Random;
use support\Redis;
class Business
{
// 存消息
public static function setRedisMessage($uid, $user_type, $data)
{
$str_uid = Base::getUid($uid, $user_type);
$rand_msg_str = $str_uid . Random::build();
$set_str = 'nomsg:message:' . $str_uid;
$set_info_str = 'nomsg:message:' . $rand_msg_str;
$sent = Redis::get($set_str);
if($sent) {
$sent = json_decode($sent, true);
$sent[count($sent)] = $set_info_str;
Redis::set($set_str, json_encode($sent));
}else {
Redis::set($set_str, json_encode([$set_info_str]));
}
Redis::set($set_info_str, json_encode($data));
}
// 拉消息
public static function getRedisMessage($uid, $user_type)
{
$str_uid = Base::getUid($uid, $user_type);
$set_str = 'nomsg:message:' . $str_uid;
$sent = Redis::get($set_str);
$data = [];
if($sent) {
$sent = json_decode($sent, true);
foreach ($sent as $k => $v) {
$data[] = json_decode(Redis::get($v), true);
Redis::del($v);
}
Redis::del($set_str);
}
return $data;
}
}