IndexController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\frontend\modules\wechat\controllers;
  3. use app\common\components\BaseController;
  4. use app\common\facades\EasyWeChat;
  5. use app\common\models\AccountWechats;
  6. use app\common\models\frame\Rule;
  7. use app\common\models\frame\RuleKeyword;
  8. use app\common\models\QrCode;
  9. /**
  10. * Created by PhpStorm.
  11. * Author: 芸众商城 www.yunzshop.com
  12. * Date: 2017/3/3
  13. * Time: 22:16
  14. */
  15. class IndexController extends BaseController
  16. {
  17. public function __construct()
  18. {
  19. $this->init();
  20. }
  21. public function init()
  22. {
  23. $uniacid = request('id');
  24. //设置uniacid
  25. \YunShop::app()->uniacid = $uniacid;
  26. \Setting::$uniqueAccountId = $uniacid;
  27. //设置公众号信息
  28. AccountWechats::setConfig(AccountWechats::getAccountByUniacid($uniacid));
  29. }
  30. public function index()
  31. {
  32. \Log::debug('----------公众号消息---------',$_GET);
  33. // 接入判断
  34. if ( isset( $_GET["signature"] ) && isset( $_GET["timestamp"] ) && isset( $_GET["nonce"] ) && isset( $_GET["echostr"] ) ) {
  35. $signature = $_GET["signature"];
  36. $timestamp = $_GET["timestamp"];
  37. $nonce = $_GET["nonce"];
  38. $token = \Setting::get('plugin.wechat.token');
  39. $tmpArr = [ $token, $timestamp, $nonce ];
  40. sort( $tmpArr, SORT_STRING );
  41. $tmpStr = implode( $tmpArr );
  42. $tmpStr = sha1( $tmpStr );
  43. if ( $tmpStr == $signature ) {
  44. \Log::debug('----------公众号接入成功---------',$_GET);
  45. \Setting::set('plugin.wechat.status', 1);
  46. \Log::debug('----------公众号接入成功状态---------',\Setting::get('plugin.wechat.status'));
  47. ob_clean();
  48. return $_GET["echostr"];
  49. } else {
  50. \Log::debug('----------公众号接入失败---------',$_GET);
  51. }
  52. } else {// 不是接入,则触发事件,交给监听者处理.
  53. // 获取第三方库easyWechat的app对象
  54. $wechatApp = EasyWeChat::officialAccount();
  55. $server = $wechatApp->server;
  56. try {
  57. $message = $server->getMessage();// 异常代码
  58. $plugin = $this->checkPlugin($message);
  59. event(new \app\common\events\WechatMessage($wechatApp,$server,$message,$plugin));
  60. if (\Setting::get('plugin.wechat.is_open')) {//公众号开启,才进行事件触发
  61. // event(new \app\common\events\WechatMessage($wechatApp,$server,$message,$plugin));
  62. if($message['Event']=="subscribe" && app('plugins')->isEnabled('pet')){
  63. event(new \app\common\events\PetWeChatEvent($_GET));
  64. }
  65. }
  66. return 'success';
  67. } catch (\Exception $exception) {
  68. \Log::debug('----------公众号异常---------',$exception->getMessage());
  69. }
  70. }
  71. }
  72. protected function checkPlugin($message)
  73. {
  74. if ($message['MsgType'] == 'event') {
  75. $keyword = '';
  76. if ($message['Event'] == 'SCAN') {
  77. $eventKey = $message['EventKey'];
  78. $qrCode = QrCode::uniacid()->where('scene_str', $eventKey)->first();
  79. if (empty($qrCode)) {
  80. return '';
  81. }
  82. $keyword = $qrCode->keyword;
  83. } elseif ($message['Event'] == 'CLICK') {
  84. $keyword = $message['EventKey'];
  85. } elseif ($message['Event'] == 'TEMPLATESENDJOBFINISH') {
  86. exit('success'); //发送模板消息的回调推送事件直接返回success
  87. }
  88. } elseif ($message['MsgType'] == 'text') {
  89. $keyword = $message['Content'];
  90. } else {
  91. return '';
  92. }
  93. if (!$keyword) {
  94. return '';
  95. }
  96. $keyword = RuleKeyword::uniacid()->where(['content'=>$keyword, 'status'=>1])->first();
  97. if (!$keyword) {
  98. return '';
  99. }
  100. $rule = Rule::find($keyword->rid);
  101. if (!$rule) {
  102. return '';
  103. }
  104. $names = explode(':', $rule['name']);
  105. $plugin = isset($names[1]) ? $names[1] : '';
  106. return $plugin;
  107. }
  108. }