RoutineServer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\routine;
  12. use service\SystemConfigService;
  13. use think\Db;
  14. use basic\AuthBasic;
  15. /**微信公众号 model
  16. * Class RoutineServer
  17. * @package app\wap\model\routine
  18. */
  19. class RoutineServer extends AuthBasic
  20. {
  21. /**
  22. * 微信公众号
  23. * @param string $routineAppId
  24. * @param string $routineAppSecret
  25. * @return mixed
  26. */
  27. public static function getAccessToken()
  28. {
  29. $routineAppId = SystemConfigService::get('wechat_appid');
  30. $routineAppSecret = SystemConfigService::get('wechat_appsecret');
  31. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $routineAppId . "&secret=" . $routineAppSecret;
  32. return json_decode(parent::curlGet($url), true);
  33. }
  34. /**
  35. * 获取access_token 数据库
  36. * @return mixed
  37. */
  38. public static function get_access_token()
  39. {
  40. $accessToken = Db::name('routine_access_token')->where('id', 1)->find();
  41. if ($accessToken['stop_time'] > time()) return $accessToken['access_token'];
  42. else {
  43. $accessToken = self::getAccessToken();
  44. if (isset($accessToken['access_token'])) {
  45. $data['access_token'] = $accessToken['access_token'];
  46. $data['stop_time'] = bcadd($accessToken['expires_in'], time(), 0);
  47. Db::name('routine_access_token')->where('id', 1)->update($data);
  48. }
  49. return $accessToken['access_token'];
  50. }
  51. }
  52. }