WechatAccountUtil.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. /**
  30. * 获取微信公众号对象
  31. * @param $id int 公众号ID
  32. * @return \EasyWeChat\OfficialAccount\Application
  33. * @throws CommonException
  34. */
  35. public static function getAppByID($id)
  36. {
  37. $wechatAccount = WechatAccountModel::findById($id);
  38. if(!$wechatAccount){
  39. throw new CommonException(ErrorEnum::ERROR_ACCOUNT_EXIST);
  40. }
  41. $config = [
  42. "app_id"=>$wechatAccount->wechat_app_id,
  43. "secret"=>$wechatAccount->wechat_app_secret,
  44. "response_type"=>"array",
  45. "token"=>$wechatAccount->wechat_app_token,
  46. "aes_key"=>$wechatAccount->wechat_app_aes_key
  47. ];
  48. return Factory::officialAccount($config);
  49. }
  50. }