WechatService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\exam\library;
  3. use EasyWeChat\Factory;
  4. /**
  5. * 微信公众号、小程序服务
  6. */
  7. class WechatService
  8. {
  9. public static function getConfig(): array
  10. {
  11. $wx_config = getConfig('wx_config');
  12. switch (true) {
  13. case is_empty_in_array($wx_config, 'appid'):
  14. fail('缺少app_id配置');
  15. case is_empty_in_array($wx_config, 'secret'):
  16. fail('缺少secret配置');
  17. // case is_empty_in_array($pay_config, 'mchid'):
  18. // fail('缺少mchid配置');
  19. // case is_empty_in_array($pay_config, 'key'):
  20. // fail('缺少key配置');
  21. // case is_empty_in_array($pay_config, 'notify_url'):
  22. // fail('缺少支付回调配置');
  23. }
  24. return [
  25. 'app_id' => $wx_config['appid'],
  26. 'secret' => $wx_config['secret'],
  27. // 'mch_id' => $pay_config['mchid'],
  28. // 'key' => $pay_config['key'],
  29. // 'notify_url' => $pay_config['notify_url'],
  30. ];
  31. }
  32. /**
  33. * 公众号实例
  34. * @return \EasyWeChat\OfficialAccount\Application
  35. */
  36. public static function getApp(): \EasyWeChat\Payment\Application
  37. {
  38. return Factory::payment(self::getConfig());
  39. }
  40. /**
  41. * 小程序实例
  42. * @return \EasyWeChat\MiniProgram\Application
  43. */
  44. public static function getMiniApp(): \EasyWeChat\MiniProgram\Application
  45. {
  46. return Factory::miniProgram(self::getConfig());
  47. }
  48. /**
  49. * 支付实例
  50. * @return \EasyWeChat\Payment\Application
  51. */
  52. public static function getPayment(): \EasyWeChat\Payment\Application
  53. {
  54. $config = Factory::payment(self::getConfig());
  55. // $config['cert_path'] = APP_PATH . '/common/certs/apiclient_cert.pem';
  56. // $config['key_path'] = APP_PATH . '/common/certs/apiclient_key.pem';
  57. return $config;
  58. }
  59. /**
  60. * H5发起登录
  61. * @param array $params 回调参数
  62. * @param int $type 授权方式
  63. * @return \Symfony\Component\HttpFoundation\RedirectResponse|void
  64. */
  65. public function login(array $params = [], int $type = 0)
  66. {
  67. $scopes = $type ? 'snsapi_userinfo' : 'snsapi_base';
  68. $callback_url = self::CALLBACK_URL;
  69. $callback_url = $params ? $callback_url . '?' . http_build_query($params) : $callback_url;
  70. $this->getApp()->oauth->withRedirectUrl($callback_url)->scopes([$scopes])->redirect()->send();
  71. }
  72. /**
  73. * 小程序登录
  74. * @param string $code 前端js code
  75. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  76. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  77. */
  78. public function miniLogin(string $code)
  79. {
  80. $app = self::getMiniApp();
  81. return $app->auth->session($code);
  82. }
  83. /**
  84. * 统一支付
  85. * @param string $openid 用户open id
  86. * @param string $out_trade_no 订单编号
  87. * @param int $fee 订单金额
  88. * @param string $body 订单说明
  89. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  90. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  91. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  92. * @throws \GuzzleHttp\Exception\GuzzleException
  93. */
  94. public function unifyPay(string $openid, string $out_trade_no, int $fee, string $body)
  95. {
  96. $payment = self::getPayment();
  97. $result = $payment->order->unify([
  98. 'body' => $body,
  99. 'out_trade_no' => $out_trade_no,
  100. 'total_fee' => $fee,
  101. 'trade_type' => 'JSAPI',
  102. 'openid' => $openid,
  103. ]);
  104. if ($result['return_code'] == 'FAIL') {
  105. fail('发起支付失败:' . $result['return_msg']);
  106. }
  107. return $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  108. }
  109. /**
  110. * 微信小程序消息解密
  111. * 比如获取电话等功能,信息是加密的,需要解密
  112. * @param $session
  113. * @param $iv
  114. * @param $encryptedData
  115. * @return mixed
  116. */
  117. public function decryptedData($session, $iv, $encryptedData)
  118. {
  119. $app = self::getMiniApp();
  120. return $app->encryptor->decryptData($session, $iv, $encryptedData);
  121. }
  122. }