BasicWeWork.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat\Contracts;
  16. /**
  17. * 企业微信基础类
  18. * Class BasicWeWork
  19. * @package WeChat\Contracts
  20. */
  21. class BasicWeWork extends BasicWeChat
  22. {
  23. /**
  24. * 获取访问 AccessToken
  25. * @return string
  26. * @throws \WeChat\Exceptions\InvalidResponseException
  27. * @throws \WeChat\Exceptions\LocalCacheException
  28. */
  29. public function getAccessToken()
  30. {
  31. if ($this->access_token) return $this->access_token;
  32. $ckey = $this->config->get('appid') . '_access_token';
  33. if ($this->access_token = Tools::getCache($ckey)) return $this->access_token;
  34. list($appid, $secret) = [$this->config->get('appid'), $this->config->get('appsecret')];
  35. $result = Tools::json2arr(Tools::get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$appid}&corpsecret={$secret}"));
  36. if (isset($result['access_token']) && $result['access_token']) Tools::setCache($ckey, $result['access_token'], 7000);
  37. return $this->access_token = $result['access_token'];
  38. }
  39. }