Files
p_ysk/extend/chat/model/Business.php
2025-12-10 10:01:02 +08:00

49 lines
1.5 KiB
PHP

<?php
namespace extend\chat\model;
use ba\Random;
use EasyWeChat\Factory;
use support\Redis;
class Business
{
// 存消息 未读消息保存30天 30天以后自动删除
public static function setRedisMessage($uid, $user_type, $data, $open_id = '')
{
$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::setEx($set_str, 30 * 86400, json_encode($sent));
}else {
Redis::setEx($set_str, 30 * 86400, json_encode([$set_info_str]));
}
Redis::setEx($set_info_str, 30 * 86400, 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) {
$msg = Redis::get($v);
if($msg) {
$data[] = json_decode(Redis::get($v), true);
}
Redis::del($v);
}
Redis::del($set_str);
}
return $data;
}
}