123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/12/5
- * Time: 下午6:37
- */
- use app\main\constants\UserConstants;
- use app\main\model\object\AdminConfigObject;
- use app\main\model\object\EventObject;
- use app\main\service\AdminService;
- use app\main\service\BookService;
- use app\main\service\OfficialAccountsEventService;
- use app\main\service\OfficialAccountsService;
- use app\main\service\OpenPlatformService;
- use app\main\service\UserService;
- use EasyWeChat\Kernel\Messages\Message;
- use think\Config;
- use \app\common\library\Redis;
- class OfficialAccountsEventTest extends TestInit
- {
- /**
- * 初始化平台信息
- * @param $appId
- * @param $token
- * @throws Exception
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function initPlatform($appId, $token)
- {
- $ophost = OfficialAccountsService::instance()->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);
- }
- }
- }
|