Wechat.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use app\common\model\WechatAutoreply;
  5. use app\common\model\WechatContext;
  6. use app\common\model\WechatResponse;
  7. use app\common\model\WechatConfig;
  8. use EasyWeChat\Foundation\Application;
  9. use EasyWeChat\Payment\Order;
  10. use fast\service\Wechat as WechatService;
  11. use think\Config;
  12. use think\Log;
  13. /**
  14. * 微信接口
  15. */
  16. class Wechat extends Frontend
  17. {
  18. public $app = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->app = new Application(Config::get('wechat'));
  23. }
  24. /**
  25. * 微信API对接接口
  26. */
  27. public function api()
  28. {
  29. $this->app->server->setMessageHandler(function ($message) {
  30. $WechatService = new WechatService;
  31. $WechatContext = new WechatContext;
  32. $WechatResponse = new WechatResponse;
  33. $openid = $message->FromUserName;
  34. $to_openid = $message->ToUserName;
  35. $event = $message->Event;
  36. $eventkey = $message->EventKey ? $message->EventKey : $message->Event;
  37. $unknownmessage = WechatConfig::value('default.unknown.message');
  38. $unknownmessage = $unknownmessage ? $unknownmessage : "对找到对应指令!";
  39. switch ($message->MsgType)
  40. {
  41. case 'event': //事件消息
  42. switch ($event)
  43. {
  44. case 'subscribe'://添加关注
  45. $subscribemessage = WechatConfig::value('default.subscribe.message');
  46. $subscribemessage = $subscribemessage ? $subscribemessage : "欢迎关注我们!";
  47. return $subscribemessage;
  48. case 'unsubscribe'://取消关注
  49. return '';
  50. case 'LOCATION'://获取地理位置
  51. return '';
  52. case 'VIEW': //跳转链接,eventkey为链接
  53. return '';
  54. default:
  55. break;
  56. }
  57. $response = $WechatResponse->where(["eventkey" => $eventkey, 'status' => 'normal'])->find();
  58. if ($response)
  59. {
  60. $content = (array) json_decode($response['content'], TRUE);
  61. $context = $WechatContext->where(['openid' => $openid])->find();
  62. $data = ['eventkey' => $eventkey, 'command' => '', 'refreshtime' => time(), 'openid' => $openid];
  63. if ($context)
  64. {
  65. $WechatContext->data($data)->where('id', $context['id'])->update();
  66. $data['id'] = $context['id'];
  67. }
  68. else
  69. {
  70. $id = $WechatContext->data($data)->save();
  71. $data['id'] = $id;
  72. }
  73. $result = $WechatService->response($this, $openid, $content, $data);
  74. if ($result)
  75. {
  76. return $result;
  77. }
  78. }
  79. return $unknownmessage;
  80. case 'text': //文字消息
  81. case 'image': //图片消息
  82. case 'voice': //语音消息
  83. case 'video': //视频消息
  84. case 'location': //坐标消息
  85. case 'link': //链接消息
  86. default: //其它消息
  87. //上下文事件处理
  88. $context = $WechatContext->where(['openid' => ['=', $openid], 'refreshtime' => ['>=', time() - 1800]])->find();
  89. if ($context && $context['eventkey'])
  90. {
  91. $response = $WechatResponse->where(['eventkey' => $context['eventkey'], 'status' => 'normal'])->find();
  92. if ($response)
  93. {
  94. $WechatContext->data(array('refreshtime' => time()))->where('id', $context['id'])->update();
  95. $content = (array) json_decode($response['content'], TRUE);
  96. $result = $WechatService->command($this, $openid, $content, $context);
  97. if ($result)
  98. {
  99. return $result;
  100. }
  101. }
  102. }
  103. //自动回复处理
  104. if ($message->MsgType == 'text')
  105. {
  106. $wechat_autoreply = new WechatAutoreply();
  107. $autoreply = $wechat_autoreply->where(['text' => $message->Content, 'status' => 'normal'])->find();
  108. if ($autoreply)
  109. {
  110. $response = $WechatResponse->where(["eventkey" => $autoreply['eventkey'], 'status' => 'normal'])->find();
  111. if ($response)
  112. {
  113. $content = (array) json_decode($response['content'], TRUE);
  114. $context = $WechatContext->where(['openid' => $openid])->find();
  115. $result = $WechatService->response($this, $openid, $content, $context);
  116. if ($result)
  117. {
  118. return $result;
  119. }
  120. }
  121. }
  122. }
  123. return $unknownmessage;
  124. }
  125. return ""; //SUCCESS
  126. });
  127. $response = $this->app->server->serve();
  128. // 将响应输出
  129. $response->send();
  130. }
  131. /**
  132. * 登录回调
  133. */
  134. public function callback()
  135. {
  136. }
  137. /**
  138. * 支付回调
  139. */
  140. public function notify()
  141. {
  142. Log::record(file_get_contents('php://input'), "notify");
  143. $response = $this->app->payment->handleNotify(function($notify, $successful) {
  144. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  145. $orderinfo = Order::findByTransactionId($notify->transaction_id);
  146. if ($orderinfo)
  147. {
  148. //订单已处理
  149. return true;
  150. }
  151. $orderinfo = Order::get($notify->out_trade_no);
  152. if (!$orderinfo)
  153. { // 如果订单不存在
  154. return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  155. }
  156. // 如果订单存在
  157. // 检查订单是否已经更新过支付状态,已经支付成功了就不再更新了
  158. if ($orderinfo['paytime'])
  159. {
  160. return true;
  161. }
  162. // 用户是否支付成功
  163. if ($successful)
  164. {
  165. // 请在这里编写处理成功的处理逻辑
  166. return true; // 返回处理完成
  167. }
  168. else
  169. { // 用户支付失败
  170. return true;
  171. }
  172. });
  173. $response->send();
  174. }
  175. }