157 lines
4.7 KiB
PHP
157 lines
4.7 KiB
PHP
<?php
|
|
namespace server\components;
|
|
|
|
use server\lib\Gateway;
|
|
use server\components\Utility;
|
|
|
|
class Base {
|
|
|
|
public static $_redis;
|
|
public static $_db;
|
|
protected static $pushNum = 0;
|
|
|
|
/**
|
|
* 初始化数据.
|
|
*/
|
|
public function __construct(){
|
|
self::$_redis = Redis::instance();
|
|
self::$_db = Db::instance();
|
|
}
|
|
|
|
/**
|
|
* 发送消息.
|
|
*
|
|
* @param int $code
|
|
* @param string $errorMsg
|
|
* @param array $data
|
|
*/
|
|
public static function sendMessage($code = 200, $errorMsg = 'ok', $data = []){
|
|
|
|
if(!empty($data)){
|
|
foreach($data as $item){
|
|
// $item['data']['server_msg_id'] = !isset($item['data']['server_msg_id']) ? Utility::makeUuid() : $item['data']['server_msg_id'];
|
|
// $item['data']['server_msg_id'] ?? Utility::makeUuid();
|
|
$result = [
|
|
'code'=>$code,
|
|
'msg'=>$errorMsg,
|
|
'message_type'=>$item['msg_type'],
|
|
'data'=>$item['data'],
|
|
];
|
|
$logData = $result;
|
|
$logData['time'] = date('Y-m-d H:i:s');
|
|
self::writeLog($logData);
|
|
$sendMessage = json_encode($result,JSON_UNESCAPED_UNICODE);
|
|
if(!empty($item['is_resend'])){
|
|
self::resend($item['data']['msg_id'], $item['target'], $sendMessage);
|
|
}
|
|
switch($item['type']){
|
|
case 'single':
|
|
Gateway::sendToClient($item['target'], $sendMessage);
|
|
break;
|
|
case 'all':
|
|
Gateway::sendToAll($sendMessage);
|
|
break;
|
|
case 'group':
|
|
Gateway::sendToGroup($item['target'], $sendMessage);
|
|
break;
|
|
}
|
|
}
|
|
}elseif($code != 200){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* base64转图片
|
|
*/
|
|
public function base64ToImg($base64){
|
|
preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64,$res);
|
|
|
|
if(empty($res)){
|
|
$imgType = "jpg";
|
|
}else{
|
|
$imgType = $res[2];
|
|
$base64 = str_replace($res[1],'',$base64);
|
|
}
|
|
|
|
$imgname = md5(uniqid(rand())) .'.'. $imgType;
|
|
$date_m = date('Y-m');
|
|
$date_d = date('Y-m-d');
|
|
$dir_m = __DIR__ . "/../../public/uploads/chatimg/user/$date_m/"; //月
|
|
$dir_d = __DIR__ . "/../../public/uploads/chatimg/user/$date_m/$date_d/"; //以当前日期为文件夹名创建文件夹
|
|
if(!file_exists(__DIR__ . "/../../public/uploads/chatimg/user/")){
|
|
mkdir(__DIR__ . "/../../public/uploads/chatimg/user/",0755,true);
|
|
}
|
|
if(!file_exists($dir_m)){
|
|
mkdir($dir_m);
|
|
}
|
|
if(!file_exists($dir_d)){
|
|
mkdir($dir_d);
|
|
}
|
|
$path=$dir_d.$imgname;
|
|
|
|
file_put_contents($path, base64_decode($base64)); // 将图片保存到相应位置
|
|
global $configFile;
|
|
$url = $configFile['site_url']."/uploads/chatimg/user/$date_m/$date_d/".$imgname;
|
|
return $url;
|
|
}
|
|
|
|
public static function writeLog($data){
|
|
$date_m = date('Y-m');
|
|
$date_d = date('Y-m-d');
|
|
global $configFile;
|
|
$dir_m = $configFile['chat_msg_dir'].$date_m;
|
|
|
|
if(!file_exists($dir_m)){
|
|
mkdir($dir_m,0755,true);
|
|
}
|
|
|
|
file_put_contents(
|
|
$dir_m.'/'.$date_d.'.log',
|
|
json_encode($data,JSON_UNESCAPED_UNICODE)."\n",
|
|
FILE_APPEND
|
|
);
|
|
}
|
|
|
|
/**
|
|
* emoji表情过滤
|
|
*/
|
|
public function filterEmoji($str)
|
|
{
|
|
$str = preg_replace_callback(
|
|
'/./u',
|
|
function (array $match) {
|
|
return strlen($match[0]) >= 4 ? '' : $match[0];
|
|
},
|
|
$str);
|
|
|
|
return $str;
|
|
}
|
|
|
|
/**
|
|
* 获取表名
|
|
*/
|
|
public function getTableName($table, $uid)
|
|
{
|
|
global $configFile;
|
|
return $table.'_'.($uid % $configFile['chat_table_num']);
|
|
}
|
|
|
|
/**
|
|
* 超时重发
|
|
*/
|
|
private static function resend($msgId, $clientId, $sendMessage){
|
|
swoole_timer_tick(3000, function ($timerId)use($msgId,$clientId,$sendMessage){
|
|
self::$pushNum ++;
|
|
if(self::$pushNum >= 3 || !self::$_redis->get($msgId)){
|
|
self::$pushNum = 0;
|
|
swoole_timer_clear($timerId);
|
|
return false;
|
|
}
|
|
echo date('Y-m-d H:i:s')." 重发".$msgId.'---'.self::$pushNum."\n";
|
|
Gateway::sendToClient($clientId, $sendMessage);
|
|
});
|
|
}
|
|
|
|
} |