Index.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace addons\wechat\controller;
  3. use addons\wechat\library\Config;
  4. use addons\wechat\model\WechatAutoreply;
  5. use addons\wechat\model\WechatCaptcha;
  6. use addons\wechat\model\WechatContext;
  7. use addons\wechat\model\WechatResponse;
  8. use addons\wechat\model\WechatConfig;
  9. use EasyWeChat\Factory;
  10. use addons\wechat\library\Wechat as WechatService;
  11. use addons\wechat\library\Config as ConfigService;
  12. use think\Log;
  13. /**
  14. * 微信接口
  15. */
  16. class Index extends \think\addons\Controller
  17. {
  18. public $app = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->app = Factory::officialAccount(Config::load());
  23. }
  24. /**
  25. *
  26. */
  27. public function index()
  28. {
  29. $this->error("当前插件暂无前台页面");
  30. }
  31. /**
  32. * 微信API对接接口
  33. */
  34. public function api()
  35. {
  36. $this->app->server->push(function ($message) {
  37. $wechatService = new WechatService;
  38. $matches = null;
  39. $openid = $message['FromUserName'];
  40. $to_openid = $message['ToUserName'];
  41. $unknownMessage = WechatConfig::getValue('default.unknown.message');
  42. $unknownMessage = $unknownMessage ? $unknownMessage : "";
  43. switch ($message['MsgType']) {
  44. case 'event': //事件消息
  45. $event = $message['Event'];
  46. $eventkey = $message['EventKey'] ? $message['EventKey'] : $message['Event'];
  47. //验证码消息
  48. if (in_array($event, ['subscribe', 'SCAN']) && preg_match("/^captcha_([a-zA-Z0-9]+)_([0-9\.]+)/", $eventkey, $matches)) {
  49. return WechatCaptcha::send($openid, $matches[1], $matches[2]);
  50. }
  51. switch ($event) {
  52. case 'subscribe'://添加关注
  53. $subscribeMessage = WechatConfig::getValue('default.subscribe.message');
  54. $subscribeMessage = $subscribeMessage ? $subscribeMessage : "欢迎关注我们!";
  55. return $subscribeMessage;
  56. case 'unsubscribe'://取消关注
  57. return '';
  58. case 'LOCATION'://获取地理位置
  59. return '';
  60. case 'VIEW': //跳转链接,eventkey为链接
  61. return '';
  62. case 'SCAN': //扫码
  63. return '';
  64. default:
  65. break;
  66. }
  67. $wechatResponse = WechatResponse::where(["eventkey" => $eventkey, 'status' => 'normal'])->find();
  68. if ($wechatResponse) {
  69. $responseContent = (array)json_decode($wechatResponse['content'], true);
  70. $wechatContext = WechatContext::where(['openid' => $openid])->order('id', 'desc')->find();
  71. $data = ['eventkey' => $eventkey, 'command' => '', 'refreshtime' => time(), 'openid' => $openid];
  72. if ($wechatContext) {
  73. $wechatContext->save($data);
  74. } else {
  75. $wechatContext = WechatContext::create($data, true);
  76. }
  77. $result = $wechatService->response($this, $openid, '', $responseContent, $wechatContext);
  78. if ($result) {
  79. return $result;
  80. }
  81. }
  82. return $unknownMessage;
  83. case 'text': //文字消息
  84. case 'image': //图片消息
  85. case 'voice': //语音消息
  86. case 'video': //视频消息
  87. case 'location': //坐标消息
  88. case 'link': //链接消息
  89. default: //其它消息
  90. //自动回复处理
  91. if ($message['MsgType'] == 'text') {
  92. $autoreply = null;
  93. $autoreplyList = WechatAutoreply::where('status', 'normal')->cache(true)->order('weigh DESC,id DESC')->select();
  94. foreach ($autoreplyList as $index => $item) {
  95. //完全匹配和正则匹配
  96. if ($item['text'] == $message['Content'] || (in_array(mb_substr($item['text'], 0, 1), ['#', '~', '/']) && preg_match($item['text'], $message['Content'], $matches))) {
  97. $autoreply = $item;
  98. break;
  99. }
  100. }
  101. if ($autoreply) {
  102. $wechatResponse = WechatResponse::where(["eventkey" => $autoreply['eventkey'], 'status' => 'normal'])->find();
  103. if ($wechatResponse) {
  104. $responseContent = (array)json_decode($wechatResponse['content'], true);
  105. $wechatContext = WechatContext::where(['openid' => $openid])->order('id', 'desc')->find();
  106. $result = $wechatService->response($this, $openid, $message['Content'], $responseContent, $wechatContext, $matches);
  107. if ($result) {
  108. return $result;
  109. }
  110. }
  111. }
  112. }
  113. return $unknownMessage;
  114. }
  115. return ""; //SUCCESS
  116. });
  117. $response = $this->app->server->serve();
  118. // 将响应输出
  119. $response->send();
  120. return;
  121. }
  122. /**
  123. * 登录回调
  124. */
  125. public function callback()
  126. {
  127. }
  128. /**
  129. * 支付回调
  130. */
  131. public function notify()
  132. {
  133. Log::record(file_get_contents('php://input'), "notify");
  134. $response = $this->app->handlePaidNotify(function ($message, $fail) {
  135. // 你的逻辑
  136. return true;
  137. // 或者错误消息
  138. $fail('Order not exists.');
  139. });
  140. $response->send();
  141. return;
  142. }
  143. }