OfficialAccountsEventTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/12/5
  6. * Time: 下午6:37
  7. */
  8. use app\main\constants\UserConstants;
  9. use app\main\model\object\AdminConfigObject;
  10. use app\main\model\object\EventObject;
  11. use app\main\service\AdminService;
  12. use app\main\service\BookService;
  13. use app\main\service\OfficialAccountsEventService;
  14. use app\main\service\OfficialAccountsService;
  15. use app\main\service\OpenPlatformService;
  16. use app\main\service\UserService;
  17. use EasyWeChat\Kernel\Messages\Message;
  18. use think\Config;
  19. use \app\common\library\Redis;
  20. class OfficialAccountsEventTest extends TestInit
  21. {
  22. /**
  23. * 初始化平台信息
  24. * @param $appId
  25. * @param $token
  26. * @throws Exception
  27. * @throws \think\Exception
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. */
  32. public function initPlatform($appId, $token)
  33. {
  34. $ophost = OfficialAccountsService::instance()->getOphostModel()->getInfoByHost('dev.kpread.com');
  35. $platformData = OpenPlatformService::instance()->getPlatformModel()->getInfo($ophost['platform_id']);
  36. Config::set('wechat.app_id', $platformData['appid']);
  37. Config::set('wechat.secret', $platformData['secret']);
  38. Config::set('wechat.token', $platformData['token']);
  39. Config::set('wechat.aes_key', $platformData['aes_key']);
  40. OpenPlatformService::instance()->getOpenPlatform();
  41. OpenPlatformService::instance()->getOfficialAccount($appId, $token);
  42. }
  43. /**
  44. * 文本
  45. * @throws Exception
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public function testEventText()
  51. {
  52. $message = ["ToUserName" => "gh_a663b5b13cc0", "FromUserName" => "oKWvT0zCJH40HMT4ie4HlF7MoARQ", "CreateTime" => "1544006375", "MsgType" => "text", "Content" => "特工狂妃", "MsgId" => "6631456885899741567"];
  53. $appId = 'wx7610e3344bdea6f6';
  54. $messageObj = (new EventObject())->bind($message);
  55. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId);
  56. $adminConfigObject = (new AdminConfigObject())->bind($adminConfig);
  57. $text = OfficialAccountsEventService::instance()->eventText($messageObj, $adminConfigObject);
  58. if (is_string($text)) {
  59. $this->assertTrue($text == BookService::instance()->getEventTextNoResult($adminConfigObject->admin_id));
  60. } else {
  61. $this->assertInstanceOf(Message::class, $text);
  62. }
  63. }
  64. /**
  65. * 关注
  66. * @throws Exception
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. */
  71. public function testEventSubscribe()
  72. {
  73. $message = ["ToUserName" => "gh_a663b5b13cc0", "FromUserName" => "oKWvT0zCJH40HMT4ie4HlF7MoARQ", "CreateTime" => "1544006375", "MsgType" => "event", "Event" => "subscribe", "EventKey" => null];
  74. $appId = 'wx7610e3344bdea6f6';
  75. $messageObj = (new EventObject())->bind($message);
  76. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId);
  77. $adminConfigObject = (new AdminConfigObject())->bind($adminConfig);
  78. $refreshToken = OpenPlatformService::instance()->getRefreshToken($adminConfigObject->admin_id);
  79. $this->initPlatform($appId, $refreshToken);
  80. $userId = OfficialAccountsService::instance()->getOpenidModel()->getUserId($adminConfigObject->admin_id, $messageObj->FromUserName);
  81. $result = OfficialAccountsEventService::instance()->eventSubscribe($adminConfigObject, $messageObj, $userId);
  82. if ($result) {
  83. $this->assertInstanceOf(Message::class, $result);
  84. }
  85. $user = UserService::instance()->getUserModel()->getUserInfo($userId);
  86. $this->assertTrue($user['is_subscribe'] == UserConstants::USER_IS_SUBSCRIBE_YES);
  87. }
  88. /**
  89. * 取消关注
  90. * @throws Exception
  91. */
  92. public function testEventUnSubscribe()
  93. {
  94. $message = ["ToUserName" => "gh_a663b5b13cc0", "FromUserName" => "oKWvT0zCJH40HMT4ie4HlF7MoARQ", "CreateTime" => "1544006375", "MsgType" => "event", "Event" => "subscribe", "EventKey" => null];
  95. $appId = 'wx7610e3344bdea6f6';
  96. $messageObj = (new EventObject())->bind($message);
  97. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId);
  98. $adminConfigObject = (new AdminConfigObject())->bind($adminConfig);
  99. $userId = OfficialAccountsService::instance()->getOpenidModel()->getUserId($adminConfigObject->admin_id, $messageObj->FromUserName);
  100. OfficialAccountsEventService::instance()->eventUnSubscribe($userId, $adminConfigObject->admin_id);
  101. $user = UserService::instance()->getUserModel()->getUserInfo($userId);
  102. $this->assertTrue($user['is_subscribe'] == UserConstants::USER_IS_SUBSCRIBE_NO);
  103. }
  104. public function testTplCallback()
  105. {
  106. $appid = 'adfsdf';
  107. var_dump(Redis::instance()->get("TPL_CALLBACK_" . $appid));
  108. for ($i = 0;$i < 101; $i++) {
  109. OfficialAccountsEventService::instance()->updateTplCallback($appid);
  110. }
  111. }
  112. }