PublishHandler.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\service\handler;
  14. use app\service\service\WechatService;
  15. /**
  16. * 第三方平台测试上线
  17. *
  18. * @author Anyon <zoujingli@qq.com>
  19. * @date 2016/10/27 14:14
  20. */
  21. class PublishHandler
  22. {
  23. /**
  24. * 当前微信APPID
  25. * @var string
  26. */
  27. protected static $appid;
  28. /**
  29. * 事件初始化
  30. * @param string $appid
  31. * @return string
  32. * @throws \WeChat\Exceptions\InvalidDecryptException
  33. * @throws \think\Exception
  34. * @throws \think\exception\PDOException
  35. */
  36. public static function handler($appid)
  37. {
  38. try {
  39. $wechat = WechatService::WeChatReceive($appid);
  40. } catch (\Exception $e) {
  41. return "Wechat message handling failed, {$e->getMessage()}";
  42. }
  43. /* 分别执行对应类型的操作 */
  44. switch (strtolower($wechat->getMsgType())) {
  45. case 'text':
  46. $receive = $wechat->getReceive();
  47. if ($receive['Content'] === 'TESTCOMPONENT_MSG_TYPE_TEXT') {
  48. return $wechat->text('TESTCOMPONENT_MSG_TYPE_TEXT_callback')->reply([], true);
  49. }
  50. $key = str_replace("QUERY_AUTH_CODE:", '', $receive['Content']);
  51. WechatService::instance('Service')->getQueryAuthorizerInfo($key);
  52. return $wechat->text("{$key}_from_api")->reply([], true);
  53. case 'event':
  54. $receive = $wechat->getReceive();
  55. return $wechat->text("{$receive['Event']}from_callback")->reply([], true);
  56. default:
  57. return 'success';
  58. }
  59. }
  60. }