WechatApp.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/9/7
  12. */
  13. namespace lemo\service;
  14. use app\common\model\WxAccount;
  15. use EasyWeChat\Factory;
  16. use think\facade\Request;
  17. class WechatApp
  18. {
  19. protected $config = [
  20. 'log' => [
  21. 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  22. 'channels' => [
  23. // 测试环境
  24. 'dev' => [
  25. 'driver' => 'single',
  26. 'path' => '../runtime/easywechat.log',
  27. 'level' => 'debug',
  28. ],
  29. // 生产环境
  30. 'prod' => [
  31. 'driver' => 'daily',
  32. 'path' => '../runtime/easywechat.log',
  33. 'level' => 'info',
  34. ],
  35. ],
  36. ],
  37. 'http' => [
  38. 'max_retries' => 1,
  39. 'retry_delay' => 500,
  40. 'timeout' => 5.0,
  41. // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
  42. ],
  43. /**
  44. * OAuth 配置
  45. *
  46. * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
  47. * callback:OAuth授权完成后的回调页地址
  48. */
  49. 'oauth' => [
  50. 'scopes' => ['snsapi_userinfo'],
  51. 'callback' => '/examples/oauth_callback.php', //回掉地址
  52. ],
  53. ];
  54. public $wechat;
  55. public $store_id;
  56. public $wechatApp;
  57. public function __construct($store_id=1)
  58. {
  59. // 微信配置
  60. $this->store_id = $store_id;
  61. $this->wechat = WxAccount::where('status', 1)
  62. ->where('store_id', $this->store_id)
  63. ->find();
  64. if ($this->wechat) {
  65. $this->config = ['app_id' => $this->wechat->app_id,
  66. 'secret' => $this->wechat->app_secret,
  67. 'token' => $this->wechat->w_token,
  68. 'response_type' => 'array',
  69. ];
  70. $this->wechatApp = cache('wechatapp_'.$this->wechat->id);
  71. if(!$this->wechatApp){
  72. $this->wechatApp = Factory::officialAccount($this->config);
  73. }
  74. } else {
  75. return false;
  76. }
  77. }
  78. }