From 3915fecb22e8976d0fd2aa53ea3709d1b0610210 Mon Sep 17 00:00:00 2001 From: Anyon Date: Thu, 18 May 2017 18:16:21 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0Emoji?= =?UTF-8?q?=E8=A1=A8=E6=83=85=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wechat/controller/Fans.php | 2 ++ extend/service/ToolsService.php | 25 ++++++++++++++++++++++++- extend/service/WechatService.php | 15 +++++++++------ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/application/wechat/controller/Fans.php b/application/wechat/controller/Fans.php index 96e5f80f9..81bdf0d4d 100644 --- a/application/wechat/controller/Fans.php +++ b/application/wechat/controller/Fans.php @@ -16,6 +16,7 @@ namespace app\wechat\controller; use controller\BasicAdmin; use service\LogService; +use service\ToolsService; use service\WechatService; use think\Db; @@ -61,6 +62,7 @@ class Fans extends BasicAdmin { protected function _data_filter(&$list) { $tags = Db::name('WechatFansTags')->column('id,name'); foreach ($list as &$vo) { + $vo['nickname'] = ToolsService::emojiDecode($vo['nickname']); $vo['tags_list'] = []; foreach (explode(',', $vo['tagid_list']) as $tag) { if ($tag !== '' && isset($tags[$tag])) { diff --git a/extend/service/ToolsService.php b/extend/service/ToolsService.php index b1bc14e5e..137a973f0 100644 --- a/extend/service/ToolsService.php +++ b/extend/service/ToolsService.php @@ -22,6 +22,7 @@ namespace service; * @date 2016/10/25 14:49 */ class ToolsService { + /** * Cors Options 授权处理 */ @@ -54,6 +55,28 @@ class ToolsService { ]; } + /** + * Emoji原形转换为String + * @param string $content + * @return string + */ + public static function emojiEncode($content) { + return json_decode(preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i", function($str) { + return addslashes($str[0]); + }, json_encode($content))); + } + + /** + * Emoji字符串转换为原形 + * @param string $content + * @return string + */ + public static function emojiDecode($content) { + return json_decode(preg_replace_callback('/\\\\\\\\/i', function() { + return '\\'; + }, json_encode($content))); + } + /** * 一维数据数组生成数据树 * @param array $list 数据列表 @@ -100,7 +123,7 @@ class ToolsService { $tree[] = $_tree; if (!empty($sub)) { $sub_array = self::arr2table($sub, $id, $pid, $path, $_tree[$path]); - $tree = array_merge($tree, (Array)$sub_array); + $tree = array_merge($tree, (Array) $sub_array); } } return $tree; diff --git a/extend/service/WechatService.php b/extend/service/WechatService.php index 95feab13f..35a91555a 100644 --- a/extend/service/WechatService.php +++ b/extend/service/WechatService.php @@ -156,6 +156,7 @@ class WechatService { $userInfo['tagid_list'] = join(',', $userInfo['tagid_list']); } $userInfo['appid'] = $appid; + $userInfo['nickname'] = ToolsService::emojiEncode($userInfo['nickname']); return DataService::save('WechatFans', $userInfo, 'openid'); } @@ -168,7 +169,10 @@ class WechatService { public static function getFansInfo($openid, $appid = null) { $map = ['openid' => $openid]; is_string($appid) && $map['appid'] = $appid; - return Db::name('WechatFans')->where($map)->find(); + if (($fans = Db::name('WechatFans')->where($map)->find()) && isset($fans['nickname'])) { + $fans['nickname'] = ToolsService::emojiDecode($fans['nickname']); + } + return $fans; } /** @@ -178,19 +182,18 @@ class WechatService { */ public static function syncAllFans($next_openid = '') { $wechat = &load_wechat('User'); - $result = $wechat->getUserList($next_openid); - if ($result === false || empty($result['data']['openid'])) { - Log::error("获取粉丝列表失败,{$wechat->errMsg} [{$wechat->errCode}]"); + if (false === ($result = $wechat->getUserList($next_openid)) || empty($result['data']['openid'])) { + Log::error("获取粉丝列表失败, {$wechat->errMsg} [{$wechat->errCode}]"); return false; } foreach (array_chunk($result['data']['openid'], 100) as $openids) { if (false === ($info = $wechat->getUserBatchInfo($openids)) || !is_array($info)) { - Log::error("获取用户信息失败,$wechat->errMsg"); + Log::error("获取用户信息失败, {$wechat->errMsg} [{$wechat->errCode}]"); return false; } foreach ($info as $userInfo) { if (false === self::setFansInfo($userInfo, $wechat->appid)) { - Log::error('更新粉丝信息更新失败!'); + Log::error('更新粉丝信息更新失败!'); return false; } if ($result['next_openid'] === $userInfo['openid']) {