Server.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Controllers\Wechat;
  3. use App\Exceptions\WeChatException;
  4. use App\Http\Controllers\Controller;
  5. class Server extends Controller
  6. {
  7. /**
  8. * @return string
  9. */
  10. public function index()
  11. {
  12. try {
  13. if (isset($_GET['echostr'])) {
  14. return $_GET['echostr'];
  15. } else {
  16. $xml = file_get_contents('php://input');
  17. libxml_disable_entity_loader(true);
  18. $object = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  19. $messageObj = new Message();
  20. if (
  21. \DB::table('kddx_user_openid')->where([
  22. 'public_id' => $object->ToUserName,
  23. 'openid' => $object->FromUserName
  24. ])->first()
  25. ) {
  26. \DB::table('kddx_user_openid')->where([
  27. 'public_id' => $object->ToUserName,
  28. 'openid' => $object->FromUserName
  29. ])->update(['logintime' => $object->CreateTime, 'subscribe' => 1]);
  30. } else {
  31. \DB::table('kddx_user_openid')->insert([
  32. 'public_id' => $object->ToUserName,
  33. 'openid' => $object->FromUserName,
  34. 'logintime' => $object->CreateTime,
  35. 'subscribe' => 1
  36. ]);
  37. }
  38. switch ($object->MsgType) {
  39. case 'text':
  40. return $messageObj->text($object);
  41. break;
  42. case 'image':
  43. return $messageObj->image($object);
  44. break;
  45. case 'voice':
  46. return $messageObj->voice($object);
  47. break;
  48. case 'video':
  49. return $messageObj->video($object);
  50. break;
  51. case 'shortvideo':
  52. return $messageObj->shortvideo($object);
  53. break;
  54. case 'event':
  55. switch ($object->Event) {
  56. case 'subscribe':
  57. return Event::subscribe($object);
  58. break;
  59. case 'unsubscribe':
  60. return Event::unsubscribe($object);
  61. break;
  62. case 'SCAN':
  63. return Event::scan($object);
  64. break;
  65. case "CLICK":
  66. return Event::click($object);
  67. }
  68. }
  69. }
  70. return 'Success';
  71. } catch (\Exception $e) {
  72. throw new WeChatException();
  73. }
  74. }
  75. }