BasicWeChat.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\extra\weMini;
  3. use think\Exception;
  4. use WeChat\Contracts\DataArray;
  5. use WeChat\Contracts\Tools;
  6. use yzh52521\EasyHttp\Http;
  7. class BasicWeChat
  8. {
  9. /**
  10. * 当前微信配置
  11. * @var DataArray
  12. */
  13. public $config;
  14. /**
  15. * 访问AccessToken
  16. * @var string
  17. */
  18. public $access_token = '';
  19. /**
  20. * 当前请求方法参数
  21. * @var array
  22. */
  23. protected $currentMethod = [];
  24. /**
  25. * 当前模式
  26. * @var bool
  27. */
  28. protected $isTry = false;
  29. /**
  30. * 静态缓存
  31. * @var static
  32. */
  33. protected static $cache;
  34. /**
  35. * 注册代替函数
  36. * @var string
  37. */
  38. protected $GetAccessTokenCallback;
  39. public function __construct(array $options)
  40. {
  41. if (empty($options['appid'])) {
  42. throw new Exception("Missing Config -- [appid]");
  43. }
  44. if (empty($options['appsecret'])) {
  45. throw new Exception("Missing Config -- [appsecret]");
  46. }
  47. if (isset($options['GetAccessTokenCallback']) && is_callable($options['GetAccessTokenCallback'])) {
  48. $this->GetAccessTokenCallback = $options['GetAccessTokenCallback'];
  49. }
  50. $this->config = new DataArray($options);
  51. }
  52. public function getAccessToken()
  53. {
  54. if (!empty($this->access_token)) {
  55. return $this->access_token;
  56. }
  57. $cache = $this->config->get('appid') . '_access_token';
  58. $this->access_token = Tools::getCache($cache);
  59. if (!empty($this->access_token)) {
  60. return $this->access_token;
  61. }
  62. // 处理开放平台授权公众号获取AccessToken
  63. if (!empty($this->GetAccessTokenCallback) && is_callable($this->GetAccessTokenCallback)) {
  64. $this->access_token = call_user_func_array($this->GetAccessTokenCallback, [$this->config->get('appid'), $this]);
  65. if (!empty($this->access_token)) {
  66. Tools::setCache($cache, $this->access_token, 7000);
  67. }
  68. return $this->access_token;
  69. }
  70. list($appid, $secret) = [$this->config->get('appid'), $this->config->get('appsecret')];
  71. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
  72. $result = Http::get($url)->array();
  73. if (!empty($result['access_token'])) {
  74. Tools::setCache($cache, $result['access_token'], 7000);
  75. }
  76. return $this->access_token = $result['access_token'];
  77. }
  78. public function setAccessToken($accessToken)
  79. {
  80. if (!is_string($accessToken)) {
  81. throw new InvalidArgumentException("Invalid AccessToken type, need string.");
  82. }
  83. $cache = $this->config->get('appid') . '_access_token';
  84. Tools::setCache($cache, $this->access_token = $accessToken);
  85. }
  86. /**
  87. * 清理删除 AccessToken
  88. * @return bool
  89. */
  90. public function delAccessToken()
  91. {
  92. $this->access_token = '';
  93. return Tools::delCache($this->config->get('appid') . '_access_token');
  94. }
  95. protected function registerApi(&$url, $method, $arguments = [])
  96. {
  97. $this->currentMethod = ['method' => $method, 'arguments' => $arguments];
  98. if (empty($this->access_token)) $this->access_token = $this->getAccessToken();
  99. return $url = str_replace('ACCESS_TOKEN', urlencode($this->access_token), $url);
  100. }
  101. }