// +---------------------------------------------------------------------- // app/controller/Unread.php namespace addon\wxchat\controller\api; use think\Request; use think\facade\Cache; use GatewayClient\Gateway; use ywxapp\controller\FrontendBase as AddonController; /** * Unread 类 * * @author ywxapp */ class Unread extends AddonController { /** * Summary of noNeedLogin * @var array */ protected $noNeedLogin = ['*']; /** * Summary of noNeedVerify * @var array */ protected $noNeedVerify = ['*']; /** * 控制器初始化 initialize * @return void */ protected function initialize() {} // 获取未读计数 public function count(Request $request) { $userId = $request->user->id; // 从Redis读取 $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $unreadData = $redis->hGetAll("unread:user:{$userId}"); // 格式化数据 $result = []; foreach ($unreadData as $key => $count) { list($type, $id) = explode(':', $key); $result[] = [ 'contact_type' => $type, 'contact_id' => $id, 'unread_count' => $count ]; } return json(['code' => 200, 'data' => $result]); } // 标记已读 public function read(Request $request) { $userId = $request->user->id; $contactType = $request->post('contact_type'); $contactId = $request->post('contact_id'); // 通知GatewayWorker Gateway::$registerAddress = '127.0.0.1:1238'; Gateway::sendToUid($userId, json_encode([ 'type' => 'read', 'contact_type' => $contactType, 'contact_id' => $contactId ])); return json(['code' => 200, 'msg' => '已标记已读']); } }