BaseApplication.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\wechat;
  12. use crmeb\services\wechat\contract\BaseApplicationInterface;
  13. use think\facade\Log;
  14. /**
  15. * Class BaseApplication
  16. * @package crmeb\services\wechat
  17. */
  18. abstract class BaseApplication implements BaseApplicationInterface
  19. {
  20. //app端
  21. const APP = 'app';
  22. //h5端、公众端
  23. const WEB = 'web';
  24. //小程序端
  25. const MINI = 'mini';
  26. //开发平台
  27. const OPEN = 'open';
  28. //pc端
  29. const PC = 'pc';
  30. /**
  31. * 访问端
  32. * @var string
  33. */
  34. protected $accessEnd;
  35. /**
  36. * @var array
  37. */
  38. protected static $property = [];
  39. /**
  40. * @var string
  41. */
  42. protected $pushMessageHandler;
  43. /**
  44. * Debug
  45. * @var bool
  46. */
  47. protected $debug = true;
  48. /**
  49. * 设置消息处理类
  50. * @param string $handler
  51. * @return $this
  52. */
  53. public function setPushMessageHandler(string $handler)
  54. {
  55. $this->pushMessageHandler = $handler;
  56. return $this;
  57. }
  58. /**
  59. * 设置访问端
  60. * @param string $accessEnd
  61. * @return $this
  62. */
  63. public function setAccessEnd(string $accessEnd)
  64. {
  65. if (in_array($accessEnd, [self::APP, self::WEB, self::MINI])) {
  66. $this->accessEnd = $accessEnd;
  67. }
  68. return $this;
  69. }
  70. /**
  71. * 自动获取访问端
  72. * @param \think\Request $request
  73. * @return string
  74. */
  75. public function getAuthAccessEnd(\think\Request $request)
  76. {
  77. if (!$this->accessEnd) {
  78. try {
  79. if ($request->isApp()) {
  80. $this->accessEnd = self::APP;
  81. } else if ($request->isPc()) {
  82. $this->accessEnd = self::PC;
  83. } else if ($request->isWechat() || $request->isH5()) {
  84. $this->accessEnd = self::WEB;
  85. } else if ($request->isRoutine()) {
  86. $this->accessEnd = self::MINI;
  87. } else {
  88. $this->accessEnd = self::WEB;
  89. }
  90. } catch (\Throwable $e) {
  91. $this->accessEnd = self::WEB;
  92. }
  93. }
  94. return $this->accessEnd;
  95. }
  96. /**
  97. * 记录错误日志
  98. * @param \Throwable $e
  99. */
  100. protected static function error(\Throwable $e)
  101. {
  102. static::instance()->debug && Log::error([
  103. 'error' => $e->getMessage(),
  104. 'line' => $e->getLine(),
  105. 'file' => $e->getFile()
  106. ]);
  107. }
  108. /**
  109. * 请求日志
  110. * @param string $message
  111. * @param $request
  112. * @param $response
  113. */
  114. protected static function logger(string $message, $request, $response)
  115. {
  116. $debug = static::instance()->debug;
  117. if ($debug) {
  118. Log::info([
  119. 'message' => $message,
  120. 'request' => json_encode($request),
  121. 'response' => json_encode($response)
  122. ]);
  123. }
  124. }
  125. /**
  126. * @param $name
  127. * @param $arguments
  128. * @return mixed
  129. */
  130. public static function __callStatic($name, $arguments)
  131. {
  132. if (in_array($name, array_keys(static::$property))) {
  133. $name = static::$property[$name];
  134. return static::instance()->application()->{$name};
  135. }
  136. throw new WechatException('方法不存在');
  137. }
  138. }