getOphostModel()->getInfoByHost('dev.kpread.com'); $platformData = OpenPlatformService::instance()->getPlatformModel()->getInfo($ophost['platform_id']); Config::set('wechat.app_id', $platformData['appid']); Config::set('wechat.secret', $platformData['secret']); Config::set('wechat.token', $platformData['token']); Config::set('wechat.aes_key', $platformData['aes_key']); OpenPlatformService::instance()->getOpenPlatform(); OpenPlatformService::instance()->getOfficialAccount($appId, $token); } /** * 文本 * @throws Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function testEventText() { $message = ["ToUserName" => "gh_a663b5b13cc0", "FromUserName" => "oKWvT0zCJH40HMT4ie4HlF7MoARQ", "CreateTime" => "1544006375", "MsgType" => "text", "Content" => "特工狂妃", "MsgId" => "6631456885899741567"]; $appId = 'wx7610e3344bdea6f6'; $messageObj = (new EventObject())->bind($message); $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId); $adminConfigObject = (new AdminConfigObject())->bind($adminConfig); $text = OfficialAccountsEventService::instance()->eventText($messageObj, $adminConfigObject); if (is_string($text)) { $this->assertTrue($text == BookService::instance()->getEventTextNoResult($adminConfigObject->admin_id)); } else { $this->assertInstanceOf(Message::class, $text); } } /** * 关注 * @throws Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function testEventSubscribe() { $message = ["ToUserName" => "gh_a663b5b13cc0", "FromUserName" => "oKWvT0zCJH40HMT4ie4HlF7MoARQ", "CreateTime" => "1544006375", "MsgType" => "event", "Event" => "subscribe", "EventKey" => null]; $appId = 'wx7610e3344bdea6f6'; $messageObj = (new EventObject())->bind($message); $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId); $adminConfigObject = (new AdminConfigObject())->bind($adminConfig); $refreshToken = OpenPlatformService::instance()->getRefreshToken($adminConfigObject->admin_id); $this->initPlatform($appId, $refreshToken); $userId = OfficialAccountsService::instance()->getOpenidModel()->getUserId($adminConfigObject->admin_id, $messageObj->FromUserName); $result = OfficialAccountsEventService::instance()->eventSubscribe($adminConfigObject, $messageObj, $userId); if ($result) { $this->assertInstanceOf(Message::class, $result); } $user = UserService::instance()->getUserModel()->getUserInfo($userId); $this->assertTrue($user['is_subscribe'] == UserConstants::USER_IS_SUBSCRIBE_YES); } /** * 取消关注 * @throws Exception */ public function testEventUnSubscribe() { $message = ["ToUserName" => "gh_a663b5b13cc0", "FromUserName" => "oKWvT0zCJH40HMT4ie4HlF7MoARQ", "CreateTime" => "1544006375", "MsgType" => "event", "Event" => "subscribe", "EventKey" => null]; $appId = 'wx7610e3344bdea6f6'; $messageObj = (new EventObject())->bind($message); $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId); $adminConfigObject = (new AdminConfigObject())->bind($adminConfig); $userId = OfficialAccountsService::instance()->getOpenidModel()->getUserId($adminConfigObject->admin_id, $messageObj->FromUserName); OfficialAccountsEventService::instance()->eventUnSubscribe($userId, $adminConfigObject->admin_id); $user = UserService::instance()->getUserModel()->getUserInfo($userId); $this->assertTrue($user['is_subscribe'] == UserConstants::USER_IS_SUBSCRIBE_NO); } public function testTplCallback() { $appid = 'adfsdf'; var_dump(Redis::instance()->get("TPL_CALLBACK_" . $appid)); for ($i = 0;$i < 101; $i++) { OfficialAccountsEventService::instance()->updateTplCallback($appid); } } }