فهرست منبع

[更新]增加Emoji表情处理

Anyon 8 سال پیش
والد
کامیت
3915fecb22
3فایلهای تغییر یافته به همراه35 افزوده شده و 7 حذف شده
  1. 2 0
      application/wechat/controller/Fans.php
  2. 24 1
      extend/service/ToolsService.php
  3. 9 6
      extend/service/WechatService.php

+ 2 - 0
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])) {

+ 24 - 1
extend/service/ToolsService.php

@@ -22,6 +22,7 @@ namespace service;
  * @date 2016/10/25 14:49
  */
 class ToolsService {
+
     /**
      * Cors Options 授权处理
      */
@@ -55,6 +56,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 string $id 父ID Key
@@ -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;

+ 9 - 6
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']) {