123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace App\Http\Utils;
- use App\Exceptions\CommonException;
- use App\Http\Enum\ErrorEnum;
- use App\Models\WechatAccountModel;
- use EasyWeChat\Factory;
- class WechatAccountUtil extends BaseUtil
- {
- /**
- * @param $code 公众号code码
- * @return \EasyWeChat\OfficialAccount\Application
- * @throws CommonException
- */
- public static function getApp($code)
- {
- $wechatAccount = WechatAccountModel::findByWechatAppCode($code);
- if(!$wechatAccount){
- throw new CommonException(ErrorEnum::ERROR_ACCOUNT_EXIST);
- }
- $config = [
- "app_id"=>$wechatAccount->wechat_app_id,
- "secret"=>$wechatAccount->wechat_app_secret,
- "response_type"=>"array",
- "token"=>$wechatAccount->wechat_app_token,
- "aes_key"=>$wechatAccount->wechat_app_aes_key
- ];
- return Factory::officialAccount($config);
- }
- /**
- * 获取微信公众号对象
- * @param $id int 公众号ID
- * @return \EasyWeChat\OfficialAccount\Application
- * @throws CommonException
- */
- public static function getAppByID($id)
- {
- $wechatAccount = WechatAccountModel::findById($id);
- if(!$wechatAccount){
- throw new CommonException(ErrorEnum::ERROR_ACCOUNT_EXIST);
- }
- $config = [
- "app_id"=>$wechatAccount->wechat_app_id,
- "secret"=>$wechatAccount->wechat_app_secret,
- "response_type"=>"array",
- "token"=>$wechatAccount->wechat_app_token,
- "aes_key"=>$wechatAccount->wechat_app_aes_key
- ];
- return Factory::officialAccount($config);
- }
- }
|