WechatOpenPlatformService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Modules\Channel\Services\WechatOpenPlatform;
  3. use EasyWeChat\OpenPlatform\Application;
  4. use Illuminate\Support\Facades\DB;
  5. use Modules\Common\Errors\Errors;
  6. use Modules\Common\Exceptions\CommonBusinessException;
  7. class WechatOpenPlatformService
  8. {
  9. public static function getComponentInfoByCompanyUid($companyUid) {
  10. $componentInfo = DB::table('wechat_open_platform_infos')
  11. ->where([
  12. 'company_uid' => $companyUid,
  13. 'is_enabled' => 1,
  14. ])->orderBy('id', 'desc')
  15. ->first();
  16. if(!$componentInfo) {
  17. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  18. }
  19. return $componentInfo;
  20. }
  21. public static function getComponentInfoByAppid($componentAppid) {
  22. $componentInfo = DB::table('wechat_open_platform_infos')
  23. ->where([
  24. 'app_id' => $componentAppid,
  25. 'is_enabled' => 1,
  26. ])->orderBy('id', 'desc')
  27. ->first();
  28. if(!$componentInfo) {
  29. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  30. }
  31. return $componentInfo;
  32. }
  33. /**
  34. * 构造app
  35. * @param $componentInfo
  36. * @return Application
  37. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  38. */
  39. public static function buildApplication($componentInfo) {
  40. $config = [
  41. 'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
  42. 'secret' => $componentInfo->secret, // 开放平台账号的 secret
  43. 'token' => $componentInfo->token, // 开放平台账号的 token
  44. 'aes_key' => $componentInfo->aes_key, // 明文模式请勿填写 EncodingAESKey
  45. 'http' => [
  46. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  47. 'timeout' => 5.0,
  48. 'retry' => true, // 使用默认重试配置
  49. ],
  50. ];
  51. return new Application($config);
  52. }
  53. }