微信昵称emoji表情,特殊表情导致列表不显示,导出EXCEL报错等问题解决!

最近做的项目,上线后一切正常,过段时间管理员反馈用户导出EXCEL报错,前台获取用户列表不显示,查找问题找到是微信昵称、emoji表情导致报错,

技术分享图片

emoji表情介绍

由于微信接口中对于emoji表情使用的是UTF-8的二进制字符串,并没有解码,表现就是当收到微信端用户发来的emoji表情时,显示为一个方块型「」或是无法显示的字符,这时就需要对其进行转码。

每个emoji表情其实都有相应的unicode编码,在解析用户向公众号发送的文字中的emoji表情字符时,我们可以根据unicode码来匹配或存储信息中的emoji表情;同理在向用户发送包含emoji表情的文字消息时,则将表情字符根据unicode编码进行二进制转码后再发送。

找网上各种,全是PHP和JAVA拿来试验一下,没解决问题,坑~~~,继续寻找,然后改造和请教群友,解决此问题

 

我使用的简单粗暴的方法,直接过滤到了emoji编码,暂时没有发现误伤:

 1 #region 去掉表情符号 2 /// <summary> 3 /// 去掉表情符号 4 /// </summary> 5 /// <param name="codePoint"></param> 6 /// <returns></returns> 7 public static bool isEmojiCharacter(char codePoint) 8  { 9 return (codePoint >= 0x2600 && codePoint <= 0x27BF) // 杂项符号与符号字体 10 || codePoint == 0x303D 11 || codePoint == 0x2049 12 || codePoint == 0x203C 13 || (codePoint >= 0x2000 && codePoint <= 0x200F) // 14 || (codePoint >= 0x2028 && codePoint <= 0x202F) // 15 || codePoint == 0x205F // 16 || (codePoint >= 0x2065 && codePoint <= 0x206F) // 17 /* 标点符号占用区域 */ 18 || (codePoint >= 0x2100 && codePoint <= 0x214F) // 字母符号 19 || (codePoint >= 0x2300 && codePoint <= 0x23FF) // 各种技术符号 20 || (codePoint >= 0x2B00 && codePoint <= 0x2BFF) // 箭头A 21 || (codePoint >= 0x2900 && codePoint <= 0x297F) // 箭头B 22 || (codePoint >= 0x3200 && codePoint <= 0x32FF) // 中文符号 23 || (codePoint >= 0xD800 && codePoint <= 0xDFFF) // 高低位替代符保留区域 24 || (codePoint >= 0xE000 && codePoint <= 0xF8FF) // 私有保留区域 25 || (codePoint >= 0xFE00 && codePoint <= 0xFE0F) // 变异选择器 26 // || (codePoint >= U + 2600 && codePoint <= 0xFE0F) 27 || codePoint >= 0x10000; // Plane在第二平面以上的,char都不可以存,全部都转 28  29  } 30 /// <summary> 31 /// 检测是否有emoji字符 32 /// </summary> 33 /// <param name="source"></param> 34 /// <returns></returns> 35 public static bool containsEmoji(String source) 36  { 37 if (string.IsNullOrEmpty(source)) 38  { 39 return false; 40  } 41  42 int len = source.Length; 43  44 for (int i = 0; i < len; i++) 45  { 46 char codePoint = source[i]; 47  48 if (isEmojiCharacter(codePoint)) 49  { 50 //do nothing,判断到了这里表明,确认有表情字符 51 return true; 52  } 53  } 54  55 return false; 56  } 57 /// <summary> 58 /// 过滤emoji 或者 其他非文字类型的字符 59 /// </summary> 60 /// <param name="source">param source</param> 61 /// <returns></returns> 62 public static String filterEmoji(String source) 63  { 64 if(string.IsNullOrWhiteSpace(source)) 65  { 66 return ""; 67  } 68 source = source.Replace("[^\\u0000-\\uFFFF]", "").Replace("??", ""); 69 if (!containsEmoji(source)) 70  { 71 return source; //如果不包含,直接返回 72  } 73 //到这里铁定包含 74 StringBuilder buf = null; 75  76 int len = source.Length; 77  78 for (int i = 0; i < len; i++) 79  { 80 char codePoint = source[i]; 81  82 if (!isEmojiCharacter(codePoint)) 83  { 84 if (buf == null) 85  { 86 buf = new StringBuilder(source.Length); 87  } 88  89  buf.Append(codePoint); 90  } 91 else 92  { 93  } 94  } 95  96 if (buf == null) 97  { 98 return source; //如果没有找到 emoji表情,则返回源字符串 99  }100 else101  {102 if (buf.Length == len)103  {104 //这里的意义在于尽可能少的toString,因为会重新生成字符串105 buf = null;106 return source;107  }108 else109  {110 return buf.ToString();111  }112  }113 114  }115 #endregion

前台

技术分享图片

 

成功……

技术分享图片

到此解决了微信昵称emoji表情,特殊表情导致列表不显示,导出EXCEL报错等问题解决!

代码虽然不是最完美的,也有优化的空间,非常感谢群友“燃冰”。

 

相关文章