WechatOpenPlatformService.php 2.1 KB

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