表情处理
This commit is contained in:
@@ -98,6 +98,7 @@ class MessageController extends ApiController
|
||||
}else {
|
||||
$v['is_shop'] = 2; // 不是商家
|
||||
}
|
||||
$v['content'] = decodeEmojiFromDb($v['content']);
|
||||
}
|
||||
|
||||
$mute = Db::name('chat_group_mute')->where(['group_id' => $group_id, 'user_id' => 0])->find();
|
||||
|
||||
@@ -157,6 +157,54 @@ function param_sort($chatList, $param)
|
||||
return $chatList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 判断字符串是否包含Emoji表情(精准正则)
|
||||
* @param string $str 待检测字符串
|
||||
* @return bool
|
||||
*/
|
||||
function hasEmoji($str) {
|
||||
if (empty($str)) return false;
|
||||
$emojiRegex = '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{1F1E0}-\x{1F1FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}\x{1F900}-\x{1F9FF}]/u';
|
||||
return preg_match($emojiRegex, $str) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 2. 存储前处理:含Emoji则Base64编码,否则原样返回
|
||||
* @param string $str 待入库字符串
|
||||
* @return string 处理后的值(Base64编码/原字符串)
|
||||
*/
|
||||
function encodeEmojiForDb($str) {
|
||||
// 空值直接返回
|
||||
if (empty($str)) return $str;
|
||||
// 含表情则Base64编码,否则返回原字符串
|
||||
return hasEmoji($str) ? base64_encode($str) : $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 3. 提取后处理:识别Base64编码的Emoji内容,解码还原
|
||||
* @param string $str 从数据库取出的字符串
|
||||
* @return string 还原后的值(解码后/原字符串)
|
||||
*/
|
||||
function decodeEmojiFromDb($str) {
|
||||
if (empty($str)) return $str;
|
||||
|
||||
// 步骤1:判断是否为有效Base64(strict模式验证)
|
||||
$decoded = base64_decode($str, true);
|
||||
if ($decoded === false) {
|
||||
// 不是有效Base64,返回原字符串
|
||||
return $str;
|
||||
}
|
||||
|
||||
// 步骤2:解码后验证是否含Emoji(避免普通Base64字符串误解码)
|
||||
if (hasEmoji($decoded)) {
|
||||
// 是含表情的Base64,返回解码结果
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
// 是有效Base64但不含表情,返回原字符串(避免误解码普通Base64内容)
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -209,6 +209,7 @@ namespace extend\chat\model;
|
||||
}
|
||||
$time = d();
|
||||
$user = Db::name('chat_user')->where(['user_id' => self::$user_info['uid']])->find();
|
||||
$insert_content = encodeEmojiForDb($message['content']);
|
||||
// 通知数据
|
||||
$notice_data = [
|
||||
'from_id' => self::$user_info['uid'],
|
||||
@@ -218,7 +219,7 @@ namespace extend\chat\model;
|
||||
'chat_coupon_id' => !empty($message['chat_coupon_id'])?$message['chat_coupon_id']:null,
|
||||
'chat_type' => $message['chat_type'],
|
||||
'msg_type' => $message['msg_type'],
|
||||
'content' => !empty($message['content'])?$message['content']:null,
|
||||
'content' => !empty($insert_content)?$insert_content:null,
|
||||
'image_url' => !empty($message['image_url'])?$message['image_url']:null,
|
||||
'order_id' => !empty($message['order_id'])?$message['order_id']:null,
|
||||
'coupon' => !empty($message['coupon'])?json_encode($message['coupon']):null,
|
||||
@@ -226,6 +227,7 @@ namespace extend\chat\model;
|
||||
'session_id' => $session_id,
|
||||
];
|
||||
$res = Db::name('chat_message')->insert($notice_data);
|
||||
$notice_data['content'] = $message['content'];
|
||||
$notice_data['operate_type'] = $message['operate_type'];
|
||||
if(!$res) {
|
||||
// 插入失败
|
||||
|
||||
Reference in New Issue
Block a user