[更新]增加Emoji表情处理

This commit is contained in:
Anyon 2017-05-18 18:16:21 +08:00
parent 9d704084c8
commit 3915fecb22
3 changed files with 35 additions and 7 deletions

View File

@ -16,6 +16,7 @@ namespace app\wechat\controller;
use controller\BasicAdmin; use controller\BasicAdmin;
use service\LogService; use service\LogService;
use service\ToolsService;
use service\WechatService; use service\WechatService;
use think\Db; use think\Db;
@ -61,6 +62,7 @@ class Fans extends BasicAdmin {
protected function _data_filter(&$list) { protected function _data_filter(&$list) {
$tags = Db::name('WechatFansTags')->column('id,name'); $tags = Db::name('WechatFansTags')->column('id,name');
foreach ($list as &$vo) { foreach ($list as &$vo) {
$vo['nickname'] = ToolsService::emojiDecode($vo['nickname']);
$vo['tags_list'] = []; $vo['tags_list'] = [];
foreach (explode(',', $vo['tagid_list']) as $tag) { foreach (explode(',', $vo['tagid_list']) as $tag) {
if ($tag !== '' && isset($tags[$tag])) { if ($tag !== '' && isset($tags[$tag])) {

View File

@ -22,6 +22,7 @@ namespace service;
* @date 2016/10/25 14:49 * @date 2016/10/25 14:49
*/ */
class ToolsService { class ToolsService {
/** /**
* Cors Options 授权处理 * 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 数据列表 * @param array $list 数据列表
@ -100,7 +123,7 @@ class ToolsService {
$tree[] = $_tree; $tree[] = $_tree;
if (!empty($sub)) { if (!empty($sub)) {
$sub_array = self::arr2table($sub, $id, $pid, $path, $_tree[$path]); $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; return $tree;

View File

@ -156,6 +156,7 @@ class WechatService {
$userInfo['tagid_list'] = join(',', $userInfo['tagid_list']); $userInfo['tagid_list'] = join(',', $userInfo['tagid_list']);
} }
$userInfo['appid'] = $appid; $userInfo['appid'] = $appid;
$userInfo['nickname'] = ToolsService::emojiEncode($userInfo['nickname']);
return DataService::save('WechatFans', $userInfo, 'openid'); return DataService::save('WechatFans', $userInfo, 'openid');
} }
@ -168,7 +169,10 @@ class WechatService {
public static function getFansInfo($openid, $appid = null) { public static function getFansInfo($openid, $appid = null) {
$map = ['openid' => $openid]; $map = ['openid' => $openid];
is_string($appid) && $map['appid'] = $appid; 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 = '') { public static function syncAllFans($next_openid = '') {
$wechat = &load_wechat('User'); $wechat = &load_wechat('User');
$result = $wechat->getUserList($next_openid); if (false === ($result = $wechat->getUserList($next_openid)) || empty($result['data']['openid'])) {
if ($result === false || empty($result['data']['openid'])) { Log::error("获取粉丝列表失败, {$wechat->errMsg} [{$wechat->errCode}]");
Log::error("获取粉丝列表失败,{$wechat->errMsg} [{$wechat->errCode}]");
return false; return false;
} }
foreach (array_chunk($result['data']['openid'], 100) as $openids) { foreach (array_chunk($result['data']['openid'], 100) as $openids) {
if (false === ($info = $wechat->getUserBatchInfo($openids)) || !is_array($info)) { if (false === ($info = $wechat->getUserBatchInfo($openids)) || !is_array($info)) {
Log::error("获取用户信息失败$wechat->errMsg"); Log::error("获取用户信息失败, {$wechat->errMsg} [{$wechat->errCode}]");
return false; return false;
} }
foreach ($info as $userInfo) { foreach ($info as $userInfo) {
if (false === self::setFansInfo($userInfo, $wechat->appid)) { if (false === self::setFansInfo($userInfo, $wechat->appid)) {
Log::error('更新粉丝信息更新失败'); Log::error('更新粉丝信息更新失败!');
return false; return false;
} }
if ($result['next_openid'] === $userInfo['openid']) { if ($result['next_openid'] === $userInfo['openid']) {