WechatAccountUtil.php 922 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Http\Utils;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Enum\ErrorEnum;
  5. use App\Models\WechatAccountModel;
  6. use EasyWeChat\Factory;
  7. class WechatAccountUtil extends BaseUtil
  8. {
  9. /**
  10. * @param $code 公众号code码
  11. * @return \EasyWeChat\OfficialAccount\Application
  12. * @throws CommonException
  13. */
  14. public static function getApp($code)
  15. {
  16. $wechatAccount = WechatAccountModel::findByWechatAppCode($code);
  17. if(!$wechatAccount){
  18. throw new CommonException(ErrorEnum::ERROR_ACCOUNT_EXIST);
  19. }
  20. $config = [
  21. "app_id"=>$wechatAccount->wechat_app_id,
  22. "secret"=>$wechatAccount->wechat_app_secret,
  23. "response_type"=>"array",
  24. "token"=>$wechatAccount->wechat_app_token,
  25. "aes_key"=>$wechatAccount->wechat_app_aes_key
  26. ];
  27. return Factory::officialAccount($config);
  28. }
  29. }