WechatOpenPlatformService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. if(!$componentInfo) {
  41. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  42. }
  43. $config = [
  44. 'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
  45. 'secret' => $componentInfo->secret, // 开放平台账号的 secret
  46. 'token' => $componentInfo->token, // 开放平台账号的 token
  47. 'aes_key' => $componentInfo->aes_key, // 明文模式请勿填写 EncodingAESKey
  48. 'http' => [
  49. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  50. 'timeout' => 5.0,
  51. 'retry' => true, // 使用默认重试配置
  52. ],
  53. ];
  54. return new Application($config);
  55. }
  56. }