Index.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\controller\wechat;
  10. use app\api\controller\Base;
  11. use app\common\library\wechat\Factory;
  12. use think\facade\Request;
  13. use xzncit\core\message\Raw;
  14. use xzncit\core\Utils;
  15. use app\common\library\wechat\mp\Message;
  16. /**
  17. * 公众号控制器
  18. * Class Index
  19. * @package app\api\controller\wechat
  20. */
  21. class Index extends Base {
  22. /**
  23. * 对接公众号
  24. * @return string
  25. */
  26. public function index(){
  27. try{
  28. $response = Factory::wechat()->server->push(function ($data){
  29. $msgType = Utils::lowerCase($data["MsgType"] ?? "");
  30. $message = new Message($data);
  31. if(!empty($msgType) && method_exists($message,$msgType)){
  32. return $message->$msgType();
  33. }else{
  34. return new Raw("success");
  35. }
  36. });
  37. return $response->send();
  38. }catch (\Exception $ex){
  39. return $ex->getMessage();
  40. }
  41. }
  42. /**
  43. * 获取公众号配置
  44. * @return \think\response\Json
  45. */
  46. public function config(){
  47. try {
  48. $sign = Factory::wechat()->script->getJsSign(Request::param("url","","trim"));
  49. return $this->returnAjax("ok",1,$sign);
  50. }catch (\Exception $ex){
  51. return $this->returnAjax($ex->getMessage(),0);
  52. }
  53. }
  54. }