Wechat.php 7.5 KB

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